Skip to content

Commit

Permalink
Update wasi-libc to 8b7148f69ae241a2749b3defe4606da8143b72e0 (#13793)
Browse files Browse the repository at this point in the history
  • Loading branch information
jedisct1 committed Dec 6, 2022
1 parent d4adf44 commit 817cf6a
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 3 deletions.
Expand Up @@ -3,6 +3,9 @@
// SPDX-License-Identifier: BSD-2-Clause

#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>

#include <assert.h>
#include <wasi/api.h>
Expand Down Expand Up @@ -77,10 +80,36 @@ struct dirent *readdir(DIR *dirp) {
GROW(dirp->dirent, dirp->dirent_size,
offsetof(struct dirent, d_name) + entry.d_namlen + 1);
struct dirent *dirent = dirp->dirent;
dirent->d_ino = entry.d_ino;
dirent->d_type = entry.d_type;
memcpy(dirent->d_name, name, entry.d_namlen);
dirent->d_name[entry.d_namlen] = '\0';

// `fd_readdir` implementations may set the inode field to zero if the
// the inode number is unknown. In that case, do an `fstatat` to get the
// inode number.
off_t d_ino = entry.d_ino;
unsigned char d_type = entry.d_type;
if (d_ino == 0 && strcmp(dirent->d_name, "..") != 0) {
struct stat statbuf;
if (fstatat(dirp->fd, dirent->d_name, &statbuf, AT_SYMLINK_NOFOLLOW) != 0) {
if (errno == ENOENT) {
// The file disappeared before we could read it, so skip it.
dirp->buffer_processed += entry_size;
continue;
}
return NULL;
}

// Fill in the inode.
d_ino = statbuf.st_ino;

// In case someone raced with us and replaced the object with this name
// with another of a different type, update the type too.
d_type = __wasilibc_iftodt(statbuf.st_mode & S_IFMT);
}
dirent->d_ino = d_ino;
dirent->d_type = d_type;

dirp->cookie = entry.d_next;
dirp->buffer_processed += entry_size;
return dirent;
Expand Down
18 changes: 18 additions & 0 deletions lib/libc/wasi/libc-top-half/musl/src/env/__stack_chk_fail.c
@@ -1,6 +1,11 @@
#include <string.h>
#include <stdint.h>
#if defined(__wasilibc_unmodified_upstream) || defined(_REENTRANT)
#include "pthread_impl.h"
#else
// In non-_REENTRANT, include it for `a_crash`
# include "atomic.h"
#endif

uintptr_t __stack_chk_guard;

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

#if defined(__wasilibc_unmodified_upstream) || defined(_REENTRANT)
__pthread_self()->canary = __stack_chk_guard;
#endif
}

void __stack_chk_fail(void)
Expand All @@ -29,3 +36,14 @@ void __stack_chk_fail(void)
hidden void __stack_chk_fail_local(void);

weak_alias(__stack_chk_fail, __stack_chk_fail_local);

#ifndef __wasilibc_unmodified_upstream
# include <wasi/api.h>

__attribute__((constructor(60)))
static void __wasilibc_init_ssp(void) {
uintptr_t entropy;
int r = __wasi_random_get((uint8_t *)&entropy, sizeof(uintptr_t));
__init_ssp(r ? NULL : &entropy);
}
#endif
4 changes: 2 additions & 2 deletions src/target.zig
Expand Up @@ -328,8 +328,8 @@ pub fn supportsStackProbing(target: std.Target) bool {
}

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

pub fn libcProvidesStackProtector(target: std.Target) bool {
Expand Down
1 change: 1 addition & 0 deletions src/wasi_libc.zig
Expand Up @@ -551,6 +551,7 @@ const libc_top_half_src_files = [_][]const u8{
"wasi/libc-top-half/musl/src/fcntl/creat.c",
"wasi/libc-top-half/musl/src/dirent/alphasort.c",
"wasi/libc-top-half/musl/src/dirent/versionsort.c",
"wasi/libc-top-half/musl/src/env/__stack_chk_fail.c",
"wasi/libc-top-half/musl/src/env/clearenv.c",
"wasi/libc-top-half/musl/src/env/getenv.c",
"wasi/libc-top-half/musl/src/env/putenv.c",
Expand Down

0 comments on commit 817cf6a

Please sign in to comment.