Skip to content

Commit

Permalink
Update wasi-libc to a1c7c2c7a4b2813c6f67bd2ef6e0f430d31cebad
Browse files Browse the repository at this point in the history
Some notable changes:
- `ENOENT` is returned instead of `ENOTCAPABLE` when a path has not
be pre-opened (WebAssembly/wasi-libc#370)
- `fd_readdir()`: some implementations may not set the inode number,
so an additional call to `fstatat()` is now done in order to get it
when that happens.
  • Loading branch information
jedisct1 committed Jan 9, 2023
1 parent 1f8f79c commit f5bb893
Show file tree
Hide file tree
Showing 17 changed files with 197 additions and 75 deletions.
2 changes: 1 addition & 1 deletion lib/libc/include/wasm-wasi-musl/wasi/api.h
Original file line number Diff line number Diff line change
Expand Up @@ -2099,7 +2099,7 @@ __wasi_errno_t __wasi_sock_shutdown(
*
* @see https://github.com/WebAssembly/wasi-threads/#readme
*/
__wasi_errno_t __wasi_thread_spawn(
int32_t __wasi_thread_spawn(
/**
* A pointer to an opaque struct to be passed to the module's entry
* function.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>

#include "dirent_impl.h"

Expand All @@ -21,6 +22,8 @@ static int sel_true(const struct dirent *de) {
int __wasilibc_nocwd_scandirat(int dirfd, const char *dir, struct dirent ***namelist,
int (*sel)(const struct dirent *),
int (*compar)(const struct dirent **, const struct dirent **)) {
struct stat statbuf;

// Match all files if no select function is provided.
if (sel == NULL)
sel = sel_true;
Expand Down Expand Up @@ -89,10 +92,30 @@ int __wasilibc_nocwd_scandirat(int dirfd, const char *dir, struct dirent ***name
malloc(offsetof(struct dirent, d_name) + entry.d_namlen + 1);
if (dirent == NULL)
goto bad;
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) {
if (fstatat(fd, dirent->d_name, &statbuf, AT_SYMLINK_NOFOLLOW) != 0) {
return -1;
}

// 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;

cookie = entry.d_next;

if (sel(dirent)) {
Expand Down
4 changes: 2 additions & 2 deletions lib/libc/wasi/libc-bottom-half/signal/signal.c
Original file line number Diff line number Diff line change
Expand Up @@ -138,5 +138,5 @@ void (*signal(int sig, void (*func)(int)))(int) {
return old;
}

extern __typeof(signal) bsd_signal __attribute__((__weak__, alias("signal")));
extern __typeof(signal) __sysv_signal __attribute__((__weak__, alias("signal")));
extern __typeof(signal) bsd_signal __attribute__((weak, alias("signal")));
extern __typeof(signal) __sysv_signal __attribute__((weak, alias("signal")));
5 changes: 2 additions & 3 deletions lib/libc/wasi/libc-bottom-half/sources/__wasilibc_real.c
Original file line number Diff line number Diff line change
Expand Up @@ -665,8 +665,7 @@ int32_t __imported_wasi_thread_spawn(int32_t arg0) __attribute__((
__import_name__("thread_spawn")
));

__wasi_errno_t __wasi_thread_spawn(void* start_arg) {
int32_t ret = __imported_wasi_thread_spawn((int32_t) start_arg);
return (uint16_t) ret;
int32_t __wasi_thread_spawn(void* start_arg) {
return __imported_wasi_thread_spawn((int32_t) start_arg);
}
#endif
2 changes: 1 addition & 1 deletion lib/libc/wasi/libc-bottom-half/sources/isatty.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ int __isatty(int fd) {

return 1;
}
extern __typeof(__isatty) isatty __attribute__((__weak__, alias("__isatty")));
extern __typeof(__isatty) isatty __attribute__((weak, alias("__isatty")));
100 changes: 50 additions & 50 deletions lib/libc/wasi/libc-bottom-half/sources/posix.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ int __wasilibc_open_nomode(const char *path, int oflag) {
char *relative_path;
int dirfd = find_relpath(path, &relative_path);

// If we can't find a preopen for it, indicate that we lack capabilities.
// If we can't find a preopen for it, fail as if we can't find the path.
if (dirfd == -1) {
errno = ENOTCAPABLE;
errno = ENOENT;
return -1;
}

Expand All @@ -71,9 +71,9 @@ int access(const char *path, int amode) {
char *relative_path;
int dirfd = find_relpath(path, &relative_path);

// If we can't find a preopen for it, indicate that we lack capabilities.
// If we can't find a preopen for it, fail as if we can't find the path.
if (dirfd == -1) {
errno = ENOTCAPABLE;
errno = ENOENT;
return -1;
}

Expand All @@ -88,9 +88,9 @@ ssize_t readlink(
char *relative_path;
int dirfd = find_relpath(path, &relative_path);

// If we can't find a preopen for it, indicate that we lack capabilities.
// If we can't find a preopen for it, fail as if we can't find the path.
if (dirfd == -1) {
errno = ENOTCAPABLE;
errno = ENOENT;
return -1;
}

Expand All @@ -101,9 +101,9 @@ int stat(const char *restrict path, struct stat *restrict buf) {
char *relative_path;
int dirfd = find_relpath(path, &relative_path);

// If we can't find a preopen for it, indicate that we lack capabilities.
// If we can't find a preopen for it, fail as if we can't find the path.
if (dirfd == -1) {
errno = ENOTCAPABLE;
errno = ENOENT;
return -1;
}

Expand All @@ -114,9 +114,9 @@ int lstat(const char *restrict path, struct stat *restrict buf) {
char *relative_path;
int dirfd = find_relpath(path, &relative_path);

// If we can't find a preopen for it, indicate that we lack capabilities.
// If we can't find a preopen for it, fail as if we can't find the path.
if (dirfd == -1) {
errno = ENOTCAPABLE;
errno = ENOENT;
return -1;
}

Expand All @@ -127,9 +127,9 @@ int utime(const char *path, const struct utimbuf *times) {
char *relative_path;
int dirfd = find_relpath(path, &relative_path);

// If we can't find a preopen for it, indicate that we lack capabilities.
// If we can't find a preopen for it, fail as if we can't find the path.
if (dirfd == -1) {
errno = ENOTCAPABLE;
errno = ENOENT;
return -1;
}

Expand All @@ -147,9 +147,9 @@ int utimes(const char *path, const struct timeval times[2]) {
char *relative_path;
int dirfd = find_relpath(path, &relative_path);

// If we can't find a preopen for it, indicate that we lack capabilities.
// If we can't find a preopen for it, fail as if we can't find the path.
if (dirfd == -1) {
errno = ENOTCAPABLE;
errno = ENOENT;
return -1;
}

Expand All @@ -169,9 +169,9 @@ int unlink(const char *path) {
char *relative_path;
int dirfd = find_relpath(path, &relative_path);

// If we can't find a preopen for it, indicate that we lack capabilities.
// If we can't find a preopen for it, fail as if we can't find the path.
if (dirfd == -1) {
errno = ENOTCAPABLE;
errno = ENOENT;
return -1;
}

Expand All @@ -185,9 +185,9 @@ int rmdir(const char *path) {
char *relative_path;
int dirfd = find_relpath(path, &relative_path);

// If we can't find a preopen for it, indicate that we lack capabilities.
// If we can't find a preopen for it, fail as if we can't find the path.
if (dirfd == -1) {
errno = ENOTCAPABLE;
errno = ENOENT;
return -1;
}

Expand All @@ -198,21 +198,21 @@ int remove(const char *path) {
char *relative_path;
int dirfd = find_relpath(path, &relative_path);

// If we can't find a preopen for it, indicate that we lack capabilities.
// If we can't find a preopen for it, fail as if we can't find the path.
if (dirfd == -1) {
errno = ENOTCAPABLE;
errno = ENOENT;
return -1;
}

// First try to remove it as a file.
int r = __wasilibc_nocwd___wasilibc_unlinkat(dirfd, relative_path);
if (r != 0 && (errno == EISDIR || errno == ENOTCAPABLE)) {
if (r != 0 && (errno == EISDIR || errno == ENOENT)) {
// That failed, but it might be a directory.
r = __wasilibc_nocwd___wasilibc_rmdirat(dirfd, relative_path);

// If it isn't a directory, we lack capabilities to remove it as a file.
if (errno == ENOTDIR)
errno = ENOTCAPABLE;
errno = ENOENT;
}
return r;
}
Expand All @@ -221,9 +221,9 @@ int mkdir(const char *path, mode_t mode) {
char *relative_path;
int dirfd = find_relpath(path, &relative_path);

// If we can't find a preopen for it, indicate that we lack capabilities.
// If we can't find a preopen for it, fail as if we can't find the path.
if (dirfd == -1) {
errno = ENOTCAPABLE;
errno = ENOENT;
return -1;
}

Expand All @@ -234,9 +234,9 @@ DIR *opendir(const char *dirname) {
char *relative_path;
int dirfd = find_relpath(dirname, &relative_path);

// If we can't find a preopen for it, indicate that we lack capabilities.
// If we can't find a preopen for it, fail as if we can't find the path.
if (dirfd == -1) {
errno = ENOTCAPABLE;
errno = ENOENT;
return NULL;
}

Expand All @@ -252,9 +252,9 @@ int scandir(
char *relative_path;
int dirfd = find_relpath(dir, &relative_path);

// If we can't find a preopen for it, indicate that we lack capabilities.
// If we can't find a preopen for it, fail as if we can't find the path.
if (dirfd == -1) {
errno = ENOTCAPABLE;
errno = ENOENT;
return -1;
}

Expand All @@ -265,9 +265,9 @@ int symlink(const char *target, const char *linkpath) {
char *relative_path;
int dirfd = find_relpath(linkpath, &relative_path);

// If we can't find a preopen for it, indicate that we lack capabilities.
// If we can't find a preopen for it, fail as if we can't find the path.
if (dirfd == -1) {
errno = ENOTCAPABLE;
errno = ENOENT;
return -1;
}

Expand All @@ -287,8 +287,8 @@ int link(const char *old, const char *new) {
new_dirfd, new_relative_path, 0);
}

// We couldn't find a preopen for it; indicate that we lack capabilities.
errno = ENOTCAPABLE;
// We couldn't find a preopen for it; fail as if we can't find the path.
errno = ENOENT;
return -1;
}

Expand All @@ -305,8 +305,8 @@ int rename(const char *old, const char *new) {
new_dirfd, new_relative_path);
}

// We couldn't find a preopen for it; indicate that we lack capabilities.
errno = ENOTCAPABLE;
// We couldn't find a preopen for it; fail as if we can't find the path.
errno = ENOENT;
return -1;
}

Expand All @@ -317,9 +317,9 @@ __wasilibc_access(const char *path, int mode, int flags)
char *relative_path;
int dirfd = find_relpath(path, &relative_path);

// If we can't find a preopen for it, indicate that we lack capabilities.
// If we can't find a preopen for it, fail as if we can't find the path.
if (dirfd == -1) {
errno = ENOTCAPABLE;
errno = ENOENT;
return -1;
}

Expand All @@ -334,9 +334,9 @@ __wasilibc_utimens(const char *path, const struct timespec times[2], int flags)
char *relative_path;
int dirfd = find_relpath(path, &relative_path);

// If we can't find a preopen for it, indicate that we lack capabilities.
// If we can't find a preopen for it, fail as if we can't find the path.
if (dirfd == -1) {
errno = ENOTCAPABLE;
errno = ENOENT;
return -1;
}

Expand All @@ -351,9 +351,9 @@ __wasilibc_stat(const char *__restrict path, struct stat *__restrict st, int fla
char *relative_path;
int dirfd = find_relpath(path, &relative_path);

// If we can't find a preopen for it, indicate that we lack capabilities.
// If we can't find a preopen for it, fail as if we can't find the path.
if (dirfd == -1) {
errno = ENOTCAPABLE;
errno = ENOENT;
return -1;
}

Expand All @@ -369,9 +369,9 @@ __wasilibc_link(const char *oldpath, const char *newpath, int flags)
int old_dirfd = find_relpath(oldpath, &old_relative_path);
int new_dirfd = find_relpath(newpath, &new_relative_path);

// If we can't find a preopen for it, indicate that we lack capabilities.
// If we can't find a preopen for it, fail as if we can't find the path.
if (old_dirfd == -1 || new_dirfd == -1) {
errno = ENOTCAPABLE;
errno = ENOENT;
return -1;
}

Expand All @@ -387,9 +387,9 @@ __wasilibc_link_oldat(int olddirfd, const char *oldpath, const char *newpath, in
char *new_relative_path;
int new_dirfd = find_relpath(newpath, &new_relative_path);

// If we can't find a preopen for it, indicate that we lack capabilities.
// If we can't find a preopen for it, fail as if we can't find the path.
if (new_dirfd == -1) {
errno = ENOTCAPABLE;
errno = ENOENT;
return -1;
}

Expand All @@ -405,9 +405,9 @@ __wasilibc_link_newat(const char *oldpath, int newdirfd, const char *newpath, in
char *old_relative_path;
int old_dirfd = find_relpath(oldpath, &old_relative_path);

// If we can't find a preopen for it, indicate that we lack capabilities.
// If we can't find a preopen for it, fail as if we can't find the path.
if (old_dirfd == -1) {
errno = ENOTCAPABLE;
errno = ENOENT;
return -1;
}

Expand All @@ -423,9 +423,9 @@ __wasilibc_rename_oldat(int fromdirfd, const char *from, const char *to)
char *to_relative_path;
int to_dirfd = find_relpath(to, &to_relative_path);

// If we can't find a preopen for it, indicate that we lack capabilities.
// If we can't find a preopen for it, fail as if we can't find the path.
if (to_dirfd == -1) {
errno = ENOTCAPABLE;
errno = ENOENT;
return -1;
}

Expand All @@ -439,9 +439,9 @@ __wasilibc_rename_newat(const char *from, int todirfd, const char *to)
char *from_relative_path;
int from_dirfd = find_relpath(from, &from_relative_path);

// If we can't find a preopen for it, indicate that we lack capabilities.
// If we can't find a preopen for it, fail as if we can't find the path.
if (from_dirfd == -1) {
errno = ENOTCAPABLE;
errno = ENOENT;
return -1;
}

Expand Down
2 changes: 2 additions & 0 deletions lib/libc/wasi/libc-top-half/musl/include/pthread.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,9 @@ extern "C" {

int pthread_create(pthread_t *__restrict, const pthread_attr_t *__restrict, void *(*)(void *), void *__restrict);
int pthread_detach(pthread_t);
#ifdef __wasilibc_unmodified_upstream
_Noreturn void pthread_exit(void *);
#endif
int pthread_join(pthread_t, void **);

#ifdef __GNUC__
Expand Down
Loading

0 comments on commit f5bb893

Please sign in to comment.