authorgravatar for 124872+jedisct1@users.noreply.github.comFrank Denis <124872+jedisct1@users.noreply.github.com> 2023-01-09 19:17:06+01:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-01-10 17:14:27-05:00
log24d8d12caf05e52fe938d1eb01211b2470cffb41
tree85820c0a6bf542af3d0794fb029490460e439528
parent0cba60afed935b9ea0f40324cc15730414f2550e

Update wasi-libc to a1c7c2c7a4b2813c6f67bd2ef6e0f430d31cebad

Some notable changes: - `ENOENT` is returned instead of `ENOTCAPABLE` when a path has not be pre-opened (https://github.com/WebAssembly/wasi-libc/pull/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.

17 files changed, 197 insertions(+), 75 deletions(-)

lib/libc/include/wasm-wasi-musl/wasi/api.h+1-1
......@@ -2099,7 +2099,7 @@ __wasi_errno_t __wasi_sock_shutdown(
20992099 *
21002100 * @see https://github.com/WebAssembly/wasi-threads/#readme
21012101 */
2102__wasi_errno_t __wasi_thread_spawn(
2102int32_t __wasi_thread_spawn(
21032103 /**
21042104 * A pointer to an opaque struct to be passed to the module's entry
21052105 * function.
lib/libc/wasi/libc-bottom-half/cloudlibc/src/libc/dirent/scandirat.c+24-1
......@@ -11,6 +11,7 @@
1111#include <stdlib.h>
1212#include <string.h>
1313#include <unistd.h>
14#include <sys/stat.h>
1415
1516#include "dirent_impl.h"
1617
......@@ -21,6 +22,8 @@ static int sel_true(const struct dirent *de) {
2122int __wasilibc_nocwd_scandirat(int dirfd, const char *dir, struct dirent ***namelist,
2223 int (*sel)(const struct dirent *),
2324 int (*compar)(const struct dirent **, const struct dirent **)) {
25 struct stat statbuf;
26
2427 // Match all files if no select function is provided.
2528 if (sel == NULL)
2629 sel = sel_true;
......@@ -89,10 +92,30 @@ int __wasilibc_nocwd_scandirat(int dirfd, const char *dir, struct dirent ***name
8992 malloc(offsetof(struct dirent, d_name) + entry.d_namlen + 1);
9093 if (dirent == NULL)
9194 goto bad;
92 dirent->d_ino = entry.d_ino;
9395 dirent->d_type = entry.d_type;
9496 memcpy(dirent->d_name, name, entry.d_namlen);
9597 dirent->d_name[entry.d_namlen] = '\0';
98
99 // `fd_readdir` implementations may set the inode field to zero if the
100 // the inode number is unknown. In that case, do an `fstatat` to get the
101 // inode number.
102 off_t d_ino = entry.d_ino;
103 unsigned char d_type = entry.d_type;
104 if (d_ino == 0) {
105 if (fstatat(fd, dirent->d_name, &statbuf, AT_SYMLINK_NOFOLLOW) != 0) {
106 return -1;
107 }
108
109 // Fill in the inode.
110 d_ino = statbuf.st_ino;
111
112 // In case someone raced with us and replaced the object with this name
113 // with another of a different type, update the type too.
114 d_type = __wasilibc_iftodt(statbuf.st_mode & S_IFMT);
115 }
116 dirent->d_ino = d_ino;
117 dirent->d_type = d_type;
118
96119 cookie = entry.d_next;
97120
98121 if (sel(dirent)) {
lib/libc/wasi/libc-bottom-half/signal/signal.c+2-2
......@@ -138,5 +138,5 @@ void (*signal(int sig, void (*func)(int)))(int) {
138138 return old;
139139}
140140
141extern __typeof(signal) bsd_signal __attribute__((__weak__, alias("signal")));
142extern __typeof(signal) __sysv_signal __attribute__((__weak__, alias("signal")));
141extern __typeof(signal) bsd_signal __attribute__((weak, alias("signal")));
142extern __typeof(signal) __sysv_signal __attribute__((weak, alias("signal")));
lib/libc/wasi/libc-bottom-half/sources/__wasilibc_real.c+2-3
......@@ -665,8 +665,7 @@ int32_t __imported_wasi_thread_spawn(int32_t arg0) __attribute__((
665665 __import_name__("thread_spawn")
666666));
667667
668__wasi_errno_t __wasi_thread_spawn(void* start_arg) {
669 int32_t ret = __imported_wasi_thread_spawn((int32_t) start_arg);
670 return (uint16_t) ret;
668int32_t __wasi_thread_spawn(void* start_arg) {
669 return __imported_wasi_thread_spawn((int32_t) start_arg);
671670}
672671#endif
lib/libc/wasi/libc-bottom-half/sources/isatty.c+1-1
......@@ -19,4 +19,4 @@ int __isatty(int fd) {
1919
2020 return 1;
2121}
22extern __typeof(__isatty) isatty __attribute__((__weak__, alias("__isatty")));
22extern __typeof(__isatty) isatty __attribute__((weak, alias("__isatty")));
lib/libc/wasi/libc-bottom-half/sources/posix.c+50-50
......@@ -58,9 +58,9 @@ int __wasilibc_open_nomode(const char *path, int oflag) {
5858 char *relative_path;
5959 int dirfd = find_relpath(path, &relative_path);
6060
61 // If we can't find a preopen for it, indicate that we lack capabilities.
61 // If we can't find a preopen for it, fail as if we can't find the path.
6262 if (dirfd == -1) {
63 errno = ENOTCAPABLE;
63 errno = ENOENT;
6464 return -1;
6565 }
6666
......@@ -71,9 +71,9 @@ int access(const char *path, int amode) {
7171 char *relative_path;
7272 int dirfd = find_relpath(path, &relative_path);
7373
74 // If we can't find a preopen for it, indicate that we lack capabilities.
74 // If we can't find a preopen for it, fail as if we can't find the path.
7575 if (dirfd == -1) {
76 errno = ENOTCAPABLE;
76 errno = ENOENT;
7777 return -1;
7878 }
7979
......@@ -88,9 +88,9 @@ ssize_t readlink(
8888 char *relative_path;
8989 int dirfd = find_relpath(path, &relative_path);
9090
91 // If we can't find a preopen for it, indicate that we lack capabilities.
91 // If we can't find a preopen for it, fail as if we can't find the path.
9292 if (dirfd == -1) {
93 errno = ENOTCAPABLE;
93 errno = ENOENT;
9494 return -1;
9595 }
9696
......@@ -101,9 +101,9 @@ int stat(const char *restrict path, struct stat *restrict buf) {
101101 char *relative_path;
102102 int dirfd = find_relpath(path, &relative_path);
103103
104 // If we can't find a preopen for it, indicate that we lack capabilities.
104 // If we can't find a preopen for it, fail as if we can't find the path.
105105 if (dirfd == -1) {
106 errno = ENOTCAPABLE;
106 errno = ENOENT;
107107 return -1;
108108 }
109109
......@@ -114,9 +114,9 @@ int lstat(const char *restrict path, struct stat *restrict buf) {
114114 char *relative_path;
115115 int dirfd = find_relpath(path, &relative_path);
116116
117 // If we can't find a preopen for it, indicate that we lack capabilities.
117 // If we can't find a preopen for it, fail as if we can't find the path.
118118 if (dirfd == -1) {
119 errno = ENOTCAPABLE;
119 errno = ENOENT;
120120 return -1;
121121 }
122122
......@@ -127,9 +127,9 @@ int utime(const char *path, const struct utimbuf *times) {
127127 char *relative_path;
128128 int dirfd = find_relpath(path, &relative_path);
129129
130 // If we can't find a preopen for it, indicate that we lack capabilities.
130 // If we can't find a preopen for it, fail as if we can't find the path.
131131 if (dirfd == -1) {
132 errno = ENOTCAPABLE;
132 errno = ENOENT;
133133 return -1;
134134 }
135135
......@@ -147,9 +147,9 @@ int utimes(const char *path, const struct timeval times[2]) {
147147 char *relative_path;
148148 int dirfd = find_relpath(path, &relative_path);
149149
150 // If we can't find a preopen for it, indicate that we lack capabilities.
150 // If we can't find a preopen for it, fail as if we can't find the path.
151151 if (dirfd == -1) {
152 errno = ENOTCAPABLE;
152 errno = ENOENT;
153153 return -1;
154154 }
155155
......@@ -169,9 +169,9 @@ int unlink(const char *path) {
169169 char *relative_path;
170170 int dirfd = find_relpath(path, &relative_path);
171171
172 // If we can't find a preopen for it, indicate that we lack capabilities.
172 // If we can't find a preopen for it, fail as if we can't find the path.
173173 if (dirfd == -1) {
174 errno = ENOTCAPABLE;
174 errno = ENOENT;
175175 return -1;
176176 }
177177
......@@ -185,9 +185,9 @@ int rmdir(const char *path) {
185185 char *relative_path;
186186 int dirfd = find_relpath(path, &relative_path);
187187
188 // If we can't find a preopen for it, indicate that we lack capabilities.
188 // If we can't find a preopen for it, fail as if we can't find the path.
189189 if (dirfd == -1) {
190 errno = ENOTCAPABLE;
190 errno = ENOENT;
191191 return -1;
192192 }
193193
......@@ -198,21 +198,21 @@ int remove(const char *path) {
198198 char *relative_path;
199199 int dirfd = find_relpath(path, &relative_path);
200200
201 // If we can't find a preopen for it, indicate that we lack capabilities.
201 // If we can't find a preopen for it, fail as if we can't find the path.
202202 if (dirfd == -1) {
203 errno = ENOTCAPABLE;
203 errno = ENOENT;
204204 return -1;
205205 }
206206
207207 // First try to remove it as a file.
208208 int r = __wasilibc_nocwd___wasilibc_unlinkat(dirfd, relative_path);
209 if (r != 0 && (errno == EISDIR || errno == ENOTCAPABLE)) {
209 if (r != 0 && (errno == EISDIR || errno == ENOENT)) {
210210 // That failed, but it might be a directory.
211211 r = __wasilibc_nocwd___wasilibc_rmdirat(dirfd, relative_path);
212212
213213 // If it isn't a directory, we lack capabilities to remove it as a file.
214214 if (errno == ENOTDIR)
215 errno = ENOTCAPABLE;
215 errno = ENOENT;
216216 }
217217 return r;
218218}
......@@ -221,9 +221,9 @@ int mkdir(const char *path, mode_t mode) {
221221 char *relative_path;
222222 int dirfd = find_relpath(path, &relative_path);
223223
224 // If we can't find a preopen for it, indicate that we lack capabilities.
224 // If we can't find a preopen for it, fail as if we can't find the path.
225225 if (dirfd == -1) {
226 errno = ENOTCAPABLE;
226 errno = ENOENT;
227227 return -1;
228228 }
229229
......@@ -234,9 +234,9 @@ DIR *opendir(const char *dirname) {
234234 char *relative_path;
235235 int dirfd = find_relpath(dirname, &relative_path);
236236
237 // If we can't find a preopen for it, indicate that we lack capabilities.
237 // If we can't find a preopen for it, fail as if we can't find the path.
238238 if (dirfd == -1) {
239 errno = ENOTCAPABLE;
239 errno = ENOENT;
240240 return NULL;
241241 }
242242
......@@ -252,9 +252,9 @@ int scandir(
252252 char *relative_path;
253253 int dirfd = find_relpath(dir, &relative_path);
254254
255 // If we can't find a preopen for it, indicate that we lack capabilities.
255 // If we can't find a preopen for it, fail as if we can't find the path.
256256 if (dirfd == -1) {
257 errno = ENOTCAPABLE;
257 errno = ENOENT;
258258 return -1;
259259 }
260260
......@@ -265,9 +265,9 @@ int symlink(const char *target, const char *linkpath) {
265265 char *relative_path;
266266 int dirfd = find_relpath(linkpath, &relative_path);
267267
268 // If we can't find a preopen for it, indicate that we lack capabilities.
268 // If we can't find a preopen for it, fail as if we can't find the path.
269269 if (dirfd == -1) {
270 errno = ENOTCAPABLE;
270 errno = ENOENT;
271271 return -1;
272272 }
273273
......@@ -287,8 +287,8 @@ int link(const char *old, const char *new) {
287287 new_dirfd, new_relative_path, 0);
288288 }
289289
290 // We couldn't find a preopen for it; indicate that we lack capabilities.
291 errno = ENOTCAPABLE;
290 // We couldn't find a preopen for it; fail as if we can't find the path.
291 errno = ENOENT;
292292 return -1;
293293}
294294
......@@ -305,8 +305,8 @@ int rename(const char *old, const char *new) {
305305 new_dirfd, new_relative_path);
306306 }
307307
308 // We couldn't find a preopen for it; indicate that we lack capabilities.
309 errno = ENOTCAPABLE;
308 // We couldn't find a preopen for it; fail as if we can't find the path.
309 errno = ENOENT;
310310 return -1;
311311}
312312
......@@ -317,9 +317,9 @@ __wasilibc_access(const char *path, int mode, int flags)
317317 char *relative_path;
318318 int dirfd = find_relpath(path, &relative_path);
319319
320 // If we can't find a preopen for it, indicate that we lack capabilities.
320 // If we can't find a preopen for it, fail as if we can't find the path.
321321 if (dirfd == -1) {
322 errno = ENOTCAPABLE;
322 errno = ENOENT;
323323 return -1;
324324 }
325325
......@@ -334,9 +334,9 @@ __wasilibc_utimens(const char *path, const struct timespec times[2], int flags)
334334 char *relative_path;
335335 int dirfd = find_relpath(path, &relative_path);
336336
337 // If we can't find a preopen for it, indicate that we lack capabilities.
337 // If we can't find a preopen for it, fail as if we can't find the path.
338338 if (dirfd == -1) {
339 errno = ENOTCAPABLE;
339 errno = ENOENT;
340340 return -1;
341341 }
342342
......@@ -351,9 +351,9 @@ __wasilibc_stat(const char *__restrict path, struct stat *__restrict st, int fla
351351 char *relative_path;
352352 int dirfd = find_relpath(path, &relative_path);
353353
354 // If we can't find a preopen for it, indicate that we lack capabilities.
354 // If we can't find a preopen for it, fail as if we can't find the path.
355355 if (dirfd == -1) {
356 errno = ENOTCAPABLE;
356 errno = ENOENT;
357357 return -1;
358358 }
359359
......@@ -369,9 +369,9 @@ __wasilibc_link(const char *oldpath, const char *newpath, int flags)
369369 int old_dirfd = find_relpath(oldpath, &old_relative_path);
370370 int new_dirfd = find_relpath(newpath, &new_relative_path);
371371
372 // If we can't find a preopen for it, indicate that we lack capabilities.
372 // If we can't find a preopen for it, fail as if we can't find the path.
373373 if (old_dirfd == -1 || new_dirfd == -1) {
374 errno = ENOTCAPABLE;
374 errno = ENOENT;
375375 return -1;
376376 }
377377
......@@ -387,9 +387,9 @@ __wasilibc_link_oldat(int olddirfd, const char *oldpath, const char *newpath, in
387387 char *new_relative_path;
388388 int new_dirfd = find_relpath(newpath, &new_relative_path);
389389
390 // If we can't find a preopen for it, indicate that we lack capabilities.
390 // If we can't find a preopen for it, fail as if we can't find the path.
391391 if (new_dirfd == -1) {
392 errno = ENOTCAPABLE;
392 errno = ENOENT;
393393 return -1;
394394 }
395395
......@@ -405,9 +405,9 @@ __wasilibc_link_newat(const char *oldpath, int newdirfd, const char *newpath, in
405405 char *old_relative_path;
406406 int old_dirfd = find_relpath(oldpath, &old_relative_path);
407407
408 // If we can't find a preopen for it, indicate that we lack capabilities.
408 // If we can't find a preopen for it, fail as if we can't find the path.
409409 if (old_dirfd == -1) {
410 errno = ENOTCAPABLE;
410 errno = ENOENT;
411411 return -1;
412412 }
413413
......@@ -423,9 +423,9 @@ __wasilibc_rename_oldat(int fromdirfd, const char *from, const char *to)
423423 char *to_relative_path;
424424 int to_dirfd = find_relpath(to, &to_relative_path);
425425
426 // If we can't find a preopen for it, indicate that we lack capabilities.
426 // If we can't find a preopen for it, fail as if we can't find the path.
427427 if (to_dirfd == -1) {
428 errno = ENOTCAPABLE;
428 errno = ENOENT;
429429 return -1;
430430 }
431431
......@@ -439,9 +439,9 @@ __wasilibc_rename_newat(const char *from, int todirfd, const char *to)
439439 char *from_relative_path;
440440 int from_dirfd = find_relpath(from, &from_relative_path);
441441
442 // If we can't find a preopen for it, indicate that we lack capabilities.
442 // If we can't find a preopen for it, fail as if we can't find the path.
443443 if (from_dirfd == -1) {
444 errno = ENOTCAPABLE;
444 errno = ENOENT;
445445 return -1;
446446 }
447447
lib/libc/wasi/libc-top-half/musl/include/pthread.h+2
......@@ -85,7 +85,9 @@ extern "C" {
8585
8686int pthread_create(pthread_t *__restrict, const pthread_attr_t *__restrict, void *(*)(void *), void *__restrict);
8787int pthread_detach(pthread_t);
88#ifdef __wasilibc_unmodified_upstream
8889_Noreturn void pthread_exit(void *);
90#endif
8991int pthread_join(pthread_t, void **);
9092
9193#ifdef __GNUC__
lib/libc/wasi/libc-top-half/musl/include/unistd.h+2
......@@ -336,7 +336,9 @@ pid_t gettid(void);
336336#endif
337337#define _POSIX_VDISABLE 0
338338
339#if defined(__wasilibc_unmodified_upstream) || defined(_REENTRANT)
339340#define _POSIX_THREADS _POSIX_VERSION
341#endif
340342#define _POSIX_THREAD_PROCESS_SHARED _POSIX_VERSION
341343#define _POSIX_THREAD_SAFE_FUNCTIONS _POSIX_VERSION
342344#define _POSIX_THREAD_ATTR_STACKADDR _POSIX_VERSION
lib/libc/wasi/libc-top-half/musl/src/env/__init_tls.c+51
......@@ -16,6 +16,43 @@
1616volatile int __thread_list_lock;
1717
1818#ifndef __wasilibc_unmodified_upstream
19
20/* These symbols are generated by wasm-ld. __stack_high/__stack_low
21 * symbols are only available in LLVM v16 and higher, therefore they're
22 * defined as weak symbols and if not available, __heap_base/__data_end
23 * is used instead.
24 *
25 * TODO: remove usage of __heap_base/__data_end for stack size calculation
26 * once we drop support for LLVM v15 and older.
27 */
28extern unsigned char __heap_base;
29extern unsigned char __data_end;
30extern unsigned char __global_base;
31extern weak unsigned char __stack_high;
32extern weak unsigned char __stack_low;
33
34static inline void setup_default_stack_size()
35{
36 ptrdiff_t stack_size;
37
38 if (&__stack_high)
39 stack_size = &__stack_high - &__stack_low;
40 else {
41 unsigned char *sp;
42 __asm__(
43 ".globaltype __stack_pointer, i32\n"
44 "global.get __stack_pointer\n"
45 "local.set %0\n"
46 : "=r"(sp));
47 stack_size = sp > &__global_base ? &__heap_base - &__data_end : (ptrdiff_t)&__global_base;
48 }
49
50 if (stack_size > __default_stacksize)
51 __default_stacksize =
52 stack_size < DEFAULT_STACK_MAX ?
53 stack_size : DEFAULT_STACK_MAX;
54}
55
1956void __wasi_init_tp() {
2057 __init_tp((void *)__get_tp());
2158}
......@@ -31,6 +68,20 @@ int __init_tp(void *p)
3168 if (!r) libc.can_do_threads = 1;
3269 td->detach_state = DT_JOINABLE;
3370 td->tid = __syscall(SYS_set_tid_address, &__thread_list_lock);
71#else
72 setup_default_stack_size();
73 td->detach_state = DT_JOINABLE;
74 /*
75 * Initialize the TID to a value which doesn't conflict with
76 * host-allocated TIDs, so that TID-based locks can work.
77 *
78 * Note:
79 * - Host-allocated TIDs range from 1 to 0x1fffffff. (inclusive)
80 * - __tl_lock and __lockfile uses TID 0 as "unlocked".
81 * - __lockfile relies on the fact the most significant two bits
82 * of TIDs are 0.
83 */
84 td->tid = 0x3fffffff;
3485#endif
3586 td->locale = &libc.global_locale;
3687 td->robust_list.head = &td->robust_list.head;
lib/libc/wasi/libc-top-half/musl/src/include/pthread.h+2
......@@ -7,7 +7,9 @@ hidden int __pthread_once(pthread_once_t *, void (*)(void));
77hidden void __pthread_testcancel(void);
88hidden int __pthread_setcancelstate(int, int *);
99hidden int __pthread_create(pthread_t *restrict, const pthread_attr_t *restrict, void *(*)(void *), void *restrict);
10#ifdef __wasilibc_unmodified_upstream
1011hidden _Noreturn void __pthread_exit(void *);
12#endif
1113hidden int __pthread_join(pthread_t, void **);
1214hidden int __pthread_mutex_lock(pthread_mutex_t *);
1315hidden int __pthread_mutex_trylock(pthread_mutex_t *);
lib/libc/wasi/libc-top-half/musl/src/internal/pthread_impl.h+5
......@@ -216,7 +216,12 @@ extern hidden unsigned __default_stacksize;
216216extern hidden unsigned __default_guardsize;
217217
218218#define DEFAULT_STACK_SIZE 131072
219#ifdef __wasilibc_unmodified_upstream
219220#define DEFAULT_GUARD_SIZE 8192
221#else
222/* guard doesn't make much sense without mprotect. */
223#define DEFAULT_GUARD_SIZE 0
224#endif
220225
221226#define DEFAULT_STACK_MAX (8<<20)
222227#define DEFAULT_GUARD_MAX (1<<20)
lib/libc/wasi/libc-top-half/musl/src/thread/__wait.c+1-1
......@@ -48,7 +48,7 @@ void __wait(volatile int *addr, volatile int *waiters, int val, int priv)
4848 __syscall(SYS_futex, addr, FUTEX_WAIT|priv, val, 0) != -ENOSYS
4949 || __syscall(SYS_futex, addr, FUTEX_WAIT, val, 0);
5050#else
51 __wasilibc_futex_wait(addr, FUTEX_WAIT, val, 0);
51 __wasilibc_futex_wait(addr, FUTEX_WAIT, val, -1);
5252#endif
5353 }
5454 if (waiters) a_dec(waiters);
lib/libc/wasi/libc-top-half/musl/src/thread/pthread_attr_get.c+4
......@@ -17,6 +17,7 @@ int pthread_attr_getinheritsched(const pthread_attr_t *restrict a, int *restrict
1717 return 0;
1818}
1919
20#ifdef __wasilibc_unmodified_upstream /* WASI has no CPU scheduling support. */
2021int pthread_attr_getschedparam(const pthread_attr_t *restrict a, struct sched_param *restrict param)
2122{
2223 param->sched_priority = a->_a_prio;
......@@ -28,6 +29,7 @@ int pthread_attr_getschedpolicy(const pthread_attr_t *restrict a, int *restrict
2829 *policy = a->_a_policy;
2930 return 0;
3031}
32#endif
3133
3234int pthread_attr_getscope(const pthread_attr_t *restrict a, int *restrict scope)
3335{
......@@ -56,11 +58,13 @@ int pthread_barrierattr_getpshared(const pthread_barrierattr_t *restrict a, int
5658 return 0;
5759}
5860
61#ifdef __wasilibc_unmodified_upstream /* Forward declaration of WASI's `__clockid` type. */
5962int pthread_condattr_getclock(const pthread_condattr_t *restrict a, clockid_t *restrict clk)
6063{
6164 *clk = a->__attr & 0x7fffffff;
6265 return 0;
6366}
67#endif
6468
6569int pthread_condattr_getpshared(const pthread_condattr_t *restrict a, int *restrict pshared)
6670{
lib/libc/wasi/libc-top-half/musl/src/thread/pthread_barrier_destroy.c+2
......@@ -9,7 +9,9 @@ int pthread_barrier_destroy(pthread_barrier_t *b)
99 while ((v = b->_b_lock) & INT_MAX)
1010 __wait(&b->_b_lock, 0, v, 0);
1111 }
12#ifdef __wasilibc_unmodified_upstream /* WASI does not understand processes or locking between them. */
1213 __vm_wait();
14#endif
1315 }
1416 return 0;
1517}
lib/libc/wasi/libc-top-half/musl/src/thread/pthread_barrier_wait.c+8
......@@ -23,7 +23,9 @@ static int pshared_barrier_wait(pthread_barrier_t *b)
2323 __wait(&b->_b_count, &b->_b_waiters2, v, 0);
2424 }
2525
26#ifdef __wasilibc_unmodified_upstream /* WASI does not understand processes or locking between them. */
2627 __vm_lock();
28#endif
2729
2830 /* Ensure all threads have a vm lock before proceeding */
2931 if (a_fetch_add(&b->_b_count, -1)==1-limit) {
......@@ -44,7 +46,9 @@ static int pshared_barrier_wait(pthread_barrier_t *b)
4446 if (v==INT_MIN+1 || (v==1 && w))
4547 __wake(&b->_b_lock, 1, 0);
4648
49#ifdef __wasilibc_unmodified_upstream /* WASI does not understand processes or locking between them. */
4750 __vm_unlock();
51#endif
4852
4953 return ret;
5054}
......@@ -84,8 +88,12 @@ int pthread_barrier_wait(pthread_barrier_t *b)
8488 a_spin();
8589 a_inc(&inst->finished);
8690 while (inst->finished == 1)
91#ifdef __wasilibc_unmodified_upstream
8792 __syscall(SYS_futex,&inst->finished,FUTEX_WAIT|FUTEX_PRIVATE,1,0) != -ENOSYS
8893 || __syscall(SYS_futex,&inst->finished,FUTEX_WAIT,1,0);
94#else
95 __futexwait(&inst->finished, 1, 0);
96#endif
8997 return PTHREAD_BARRIER_SERIAL_THREAD;
9098 }
9199
lib/libc/wasi/libc-top-half/musl/src/thread/pthread_create.c+36-16
......@@ -60,7 +60,11 @@ void __tl_sync(pthread_t td)
6060 if (tl_lock_waiters) __wake(&__thread_list_lock, 1, 0);
6161}
6262
63#ifdef __wasilibc_unmodified_upstream
6364_Noreturn void __pthread_exit(void *result)
65#else
66static void __pthread_exit(void *result)
67#endif
6468{
6569 pthread_t self = __pthread_self();
6670 sigset_t set;
......@@ -191,7 +195,7 @@ _Noreturn void __pthread_exit(void *result)
191195 __tl_unlock();
192196 free(self->map_base);
193197 // Can't use `exit()` here, because it is too high level
194 for (;;) __wasi_proc_exit(0);
198 return;
195199 }
196200#endif
197201
......@@ -212,7 +216,6 @@ _Noreturn void __pthread_exit(void *result)
212216 // do it manually here
213217 __tl_unlock();
214218 // Can't use `exit()` here, because it is too high level
215 for (;;) __wasi_proc_exit(0);
216219#endif
217220}
218221
......@@ -235,9 +238,14 @@ struct start_args {
235238 volatile int control;
236239 unsigned long sig_mask[_NSIG/8/sizeof(long)];
237240#else
241 /*
242 * Note: the offset of the "stack" and "tls_base" members
243 * in this structure is hardcoded in wasi_thread_start.
244 */
245 void *stack;
246 void *tls_base;
238247 void *(*start_func)(void *);
239248 void *start_arg;
240 void *tls_base;
241249#endif
242250};
243251
......@@ -271,32 +279,41 @@ static int start_c11(void *p)
271279 return 0;
272280}
273281#else
274__attribute__((export_name("wasi_thread_start")))
275_Noreturn void wasi_thread_start(int tid, void *p)
282
283/*
284 * We want to ensure wasi_thread_start is linked whenever
285 * pthread_create is used. The following reference is to ensure that.
286 * Otherwise, the linker doesn't notice the dependency because
287 * wasi_thread_start is used indirectly via a wasm export.
288 */
289void wasi_thread_start(int tid, void *p);
290hidden void *__dummy_reference = wasi_thread_start;
291
292hidden void __wasi_thread_start_C(int tid, void *p)
276293{
277294 struct start_args *args = p;
278 __asm__(".globaltype __tls_base, i32\n"
279 "local.get %0\n"
280 "global.set __tls_base\n"
281 :: "r"(args->tls_base));
282295 pthread_t self = __pthread_self();
283296 // Set the thread ID (TID) on the pthread structure. The TID is stored
284297 // atomically since it is also stored by the parent thread; this way,
285298 // whichever thread (parent or child) reaches this point first can proceed
286299 // without waiting.
287300 atomic_store((atomic_int *) &(self->tid), tid);
288 // Set the stack pointer.
289 __asm__(".globaltype __stack_pointer, i32\n"
290 "local.get %0\n"
291 "global.set __stack_pointer\n"
292 :: "r"(self->stack));
293301 // Execute the user's start function.
294 int (*start)(void*) = (int(*)(void*)) args->start_func;
295 __pthread_exit((void *)(uintptr_t)start(args->start_arg));
302 __pthread_exit(args->start_func(args->start_arg));
296303}
297304#endif
298305
306#ifdef __wasilibc_unmodified_upstream
299307#define ROUND(x) (((x)+PAGE_SIZE-1)&-PAGE_SIZE)
308#else
309/*
310 * As we allocate stack with malloc() instead of mmap/mprotect,
311 * there is no point to round it up to PAGE_SIZE.
312 * Instead, round up to a sane alignment.
313 * Note: PAGE_SIZE is rather big on WASM. (65536)
314 */
315#define ROUND(x) (((x)+16-1)&-16)
316#endif
300317
301318/* pthread_key_create.c overrides this */
302319static volatile size_t dummy = 0;
......@@ -484,6 +501,7 @@ int __pthread_create(pthread_t *restrict res, const pthread_attr_t *restrict att
484501 /* Correct the stack size */
485502 new->stack_size = stack - stack_limit;
486503
504 args->stack = new->stack; /* just for convenience of asm trampoline */
487505 args->start_func = entry;
488506 args->start_arg = arg;
489507 args->tls_base = (void*)new_tls_base;
......@@ -561,5 +579,7 @@ fail:
561579 return EAGAIN;
562580}
563581
582#ifdef __wasilibc_unmodified_upstream
564583weak_alias(__pthread_exit, pthread_exit);
584#endif
565585weak_alias(__pthread_create, pthread_create);
lib/libc/wasi/libc-top-half/musl/src/thread/pthread_key_create.c+4
......@@ -50,7 +50,9 @@ int __pthread_key_delete(pthread_key_t k)
5050 sigset_t set;
5151 pthread_t self = __pthread_self(), td=self;
5252
53#ifdef __wasilibc_unmodified_upstream
5354 __block_app_sigs(&set);
55#endif
5456 __pthread_rwlock_wrlock(&key_lock);
5557
5658 __tl_lock();
......@@ -61,7 +63,9 @@ int __pthread_key_delete(pthread_key_t k)
6163 keys[k] = 0;
6264
6365 __pthread_rwlock_unlock(&key_lock);
66#ifdef __wasilibc_unmodified_upstream
6467 __restore_sigs(&set);
68#endif
6569
6670 return 0;
6771}