Skip to content

Commit 817cf6a

Browse files
authored
Update wasi-libc to 8b7148f69ae241a2749b3defe4606da8143b72e0 (#13793)
1 parent d4adf44 commit 817cf6a

File tree

4 files changed

+51
-3
lines changed

4 files changed

+51
-3
lines changed

lib/libc/wasi/libc-bottom-half/cloudlibc/src/libc/dirent/readdir.c

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
// SPDX-License-Identifier: BSD-2-Clause
44

55
#include <sys/stat.h>
6+
#include <sys/types.h>
7+
#include <unistd.h>
8+
#include <fcntl.h>
69

710
#include <assert.h>
811
#include <wasi/api.h>
@@ -77,10 +80,36 @@ struct dirent *readdir(DIR *dirp) {
7780
GROW(dirp->dirent, dirp->dirent_size,
7881
offsetof(struct dirent, d_name) + entry.d_namlen + 1);
7982
struct dirent *dirent = dirp->dirent;
80-
dirent->d_ino = entry.d_ino;
8183
dirent->d_type = entry.d_type;
8284
memcpy(dirent->d_name, name, entry.d_namlen);
8385
dirent->d_name[entry.d_namlen] = '\0';
86+
87+
// `fd_readdir` implementations may set the inode field to zero if the
88+
// the inode number is unknown. In that case, do an `fstatat` to get the
89+
// inode number.
90+
off_t d_ino = entry.d_ino;
91+
unsigned char d_type = entry.d_type;
92+
if (d_ino == 0 && strcmp(dirent->d_name, "..") != 0) {
93+
struct stat statbuf;
94+
if (fstatat(dirp->fd, dirent->d_name, &statbuf, AT_SYMLINK_NOFOLLOW) != 0) {
95+
if (errno == ENOENT) {
96+
// The file disappeared before we could read it, so skip it.
97+
dirp->buffer_processed += entry_size;
98+
continue;
99+
}
100+
return NULL;
101+
}
102+
103+
// Fill in the inode.
104+
d_ino = statbuf.st_ino;
105+
106+
// In case someone raced with us and replaced the object with this name
107+
// with another of a different type, update the type too.
108+
d_type = __wasilibc_iftodt(statbuf.st_mode & S_IFMT);
109+
}
110+
dirent->d_ino = d_ino;
111+
dirent->d_type = d_type;
112+
84113
dirp->cookie = entry.d_next;
85114
dirp->buffer_processed += entry_size;
86115
return dirent;

lib/libc/wasi/libc-top-half/musl/src/env/__stack_chk_fail.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
#include <string.h>
22
#include <stdint.h>
3+
#if defined(__wasilibc_unmodified_upstream) || defined(_REENTRANT)
34
#include "pthread_impl.h"
5+
#else
6+
// In non-_REENTRANT, include it for `a_crash`
7+
# include "atomic.h"
8+
#endif
49

510
uintptr_t __stack_chk_guard;
611

@@ -18,7 +23,9 @@ void __init_ssp(void *entropy)
1823
((char *)&__stack_chk_guard)[1] = 0;
1924
#endif
2025

26+
#if defined(__wasilibc_unmodified_upstream) || defined(_REENTRANT)
2127
__pthread_self()->canary = __stack_chk_guard;
28+
#endif
2229
}
2330

2431
void __stack_chk_fail(void)
@@ -29,3 +36,14 @@ void __stack_chk_fail(void)
2936
hidden void __stack_chk_fail_local(void);
3037

3138
weak_alias(__stack_chk_fail, __stack_chk_fail_local);
39+
40+
#ifndef __wasilibc_unmodified_upstream
41+
# include <wasi/api.h>
42+
43+
__attribute__((constructor(60)))
44+
static void __wasilibc_init_ssp(void) {
45+
uintptr_t entropy;
46+
int r = __wasi_random_get((uint8_t *)&entropy, sizeof(uintptr_t));
47+
__init_ssp(r ? NULL : &entropy);
48+
}
49+
#endif

src/target.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -328,8 +328,8 @@ pub fn supportsStackProbing(target: std.Target) bool {
328328
}
329329

330330
pub fn supportsStackProtector(target: std.Target) bool {
331-
// TODO: investigate whether stack-protector works on wasm
332-
return !target.isWasm();
331+
_ = target;
332+
return true;
333333
}
334334

335335
pub fn libcProvidesStackProtector(target: std.Target) bool {

src/wasi_libc.zig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -551,6 +551,7 @@ const libc_top_half_src_files = [_][]const u8{
551551
"wasi/libc-top-half/musl/src/fcntl/creat.c",
552552
"wasi/libc-top-half/musl/src/dirent/alphasort.c",
553553
"wasi/libc-top-half/musl/src/dirent/versionsort.c",
554+
"wasi/libc-top-half/musl/src/env/__stack_chk_fail.c",
554555
"wasi/libc-top-half/musl/src/env/clearenv.c",
555556
"wasi/libc-top-half/musl/src/env/getenv.c",
556557
"wasi/libc-top-half/musl/src/env/putenv.c",

0 commit comments

Comments
 (0)