Skip to content

Commit ce2f157

Browse files
authored
Update thread id validation returned by __wasi_thread_spawn (#435)
According to the documentation: https://github.com/WebAssembly/wasi-threads#design-choice-thread-ids, TID should be in the range <1, 0x1FFFFFFF>
1 parent ec4566b commit ce2f157

1 file changed

Lines changed: 11 additions & 6 deletions

File tree

libc-top-half/musl/src/thread/pthread_create.c

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#endif
1414

1515
#include <stdalign.h>
16+
#include <assert.h>
1617

1718
static void dummy_0()
1819
{
@@ -558,13 +559,17 @@ int __pthread_create(pthread_t *restrict res, const pthread_attr_t *restrict att
558559
__wait(&args->control, 0, 3, 0);
559560
}
560561
#else
562+
#define WASI_THREADS_MAX_TID 0x1FFFFFFF
561563
/* `wasi_thread_spawn` will either return a host-provided thread ID (TID)
562-
* (`>= 0`) or an error code (`< 0`). As in the unmodified version, all
563-
* spawn failures translate to EAGAIN; unlike the modified version, there is
564-
* no need to "start up" the child thread--the host does this. If the spawn
565-
* did succeed, then we store the TID atomically, since this parent thread
566-
* is racing with the child thread to set this field; this way, whichever
567-
* thread reaches this point first can continue without waiting. */
564+
* (`<1, 0x1FFFFFFF>`) or an error code (`< 0`). Please note that `0` is
565+
* reserved for compatibility reasons and must not be returned by the runtime.
566+
* As in the unmodified version, all spawn failures translate to EAGAIN;
567+
* unlike the modified version, there is no need to "start up" the child
568+
* thread--the host does this. If the spawn did succeed, then we store the
569+
* TID atomically, since this parent thread is racing with the child thread
570+
* to set this field; this way, whichever thread reaches this point first
571+
* can continue without waiting. */
572+
assert(ret != 0 && ret <= WASI_THREADS_MAX_TID);
568573
if (ret < 0) {
569574
ret = -EAGAIN;
570575
} else {

0 commit comments

Comments
 (0)