Skip to content

Commit

Permalink
Update thread id validation returned by __wasi_thread_spawn
Browse files Browse the repository at this point in the history
According to the documentation: https://github.com/WebAssembly/wasi-threads#design-choice-thread-ids, TID should be in the range <1, 0x1FFFFFFF>
  • Loading branch information
loganek committed Aug 30, 2023
1 parent ec4566b commit 0103e28
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions libc-top-half/musl/src/thread/pthread_create.c
Original file line number Diff line number Diff line change
Expand Up @@ -558,14 +558,18 @@ int __pthread_create(pthread_t *restrict res, const pthread_attr_t *restrict att
__wait(&args->control, 0, 3, 0);
}
#else
#define WASI_THREADS_MIN_TID 1
#define WASI_THREADS_MAX_TID 0x1FFFFFFF
/* `wasi_thread_spawn` will either return a host-provided thread ID (TID)
* (`>= 0`) or an error code (`< 0`). As in the unmodified version, all
* (`<1, 0x1FFFFFFF>`) or an error code (`< 0`). Please note that `0` is
* reserved for compatibility reasons and must not be returned by the runtime
* (if that happens, EAGAIN is returned). As in the unmodified version, all
* spawn failures translate to EAGAIN; unlike the modified version, there is
* no need to "start up" the child thread--the host does this. If the spawn
* did succeed, then we store the TID atomically, since this parent thread
* is racing with the child thread to set this field; this way, whichever
* thread reaches this point first can continue without waiting. */
if (ret < 0) {
if (ret < WASI_THREADS_MIN_TID || ret > WASI_THREADS_MAX_TID) {
ret = -EAGAIN;
} else {
atomic_store((atomic_int *) &(new->tid), ret);
Expand Down

0 comments on commit 0103e28

Please sign in to comment.