Skip to content

Commit 9b80a8e

Browse files
committed
Make always-fail functions error if _WASI_EMULATED_PTHREAD isn't defined
1 parent 1b0706d commit 9b80a8e

File tree

4 files changed

+40
-3
lines changed

4 files changed

+40
-3
lines changed

Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -797,7 +797,8 @@ $(LIBWASI_EMULATED_PROCESS_CLOCKS_OBJS) $(LIBWASI_EMULATED_PROCESS_CLOCKS_SO_OBJ
797797
$(LIBWASI_EMULATED_PTHREAD_OBJS) $(LIBWASI_EMULATED_PTHREAD_SO_OBJS): CFLAGS += \
798798
-I$(LIBC_TOP_HALF_MUSL_SRC_DIR)/include \
799799
-I$(LIBC_TOP_HALF_MUSL_SRC_DIR)/internal \
800-
-I$(LIBC_TOP_HALF_MUSL_DIR)/arch/wasm32
800+
-I$(LIBC_TOP_HALF_MUSL_DIR)/arch/wasm32 \
801+
-D_WASI_EMULATED_PTHREAD
801802

802803
# emmalloc uses a lot of pointer type-punning, which is UB under strict aliasing,
803804
# and this was found to have real miscompilations in wasi-libc#421.

expected/wasm32-wasip1/predefined-macros.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3389,7 +3389,12 @@
33893389
#define preadv64 preadv
33903390
#define pthread_cleanup_pop(r) _pthread_cleanup_pop(&__cb, (r)); } while(0)
33913391
#define pthread_cleanup_push(f,x) do { struct __ptcb __cb; _pthread_cleanup_push(&__cb, f, x);
3392+
#define pthread_create(...) ({ _Static_assert(0, "This mode of WASI does not have threads enabled; to enable stub functions which always fail, compile with -D_WASI_EMULATED_PTHREAD and link with -lwasi-emulated-pthread"); 0;})
3393+
#define pthread_detach(...) ({ _Static_assert(0, "This mode of WASI does not have threads enabled; to enable stub functions which always fail, compile with -D_WASI_EMULATED_PTHREAD and link with -lwasi-emulated-pthread"); 0;})
33923394
#define pthread_equal(x,y) ((x)==(y))
3395+
#define pthread_join(...) ({ _Static_assert(0, "This mode of WASI does not have threads enabled; to enable stub functions which always fail, compile with -D_WASI_EMULATED_PTHREAD and link with -lwasi-emulated-pthread"); 0;})
3396+
#define pthread_timedjoin_np(...) ({ _Static_assert(0, "This mode of WASI does not have threads enabled; to enable stub functions which always fail, compile with -D_WASI_EMULATED_PTHREAD and link with -lwasi-emulated-pthread"); 0;})
3397+
#define pthread_tryjoin_np(...) ({ _Static_assert(0, "This mode of WASI does not have threads enabled; to enable stub functions which always fail, compile with -D_WASI_EMULATED_PTHREAD and link with -lwasi-emulated-pthread"); 0;})
33933398
#define pwrite64 pwrite
33943399
#define pwritev64 pwritev
33953400
#define readdir64 readdir

expected/wasm32-wasip2/predefined-macros.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3544,7 +3544,12 @@
35443544
#define preadv64 preadv
35453545
#define pthread_cleanup_pop(r) _pthread_cleanup_pop(&__cb, (r)); } while(0)
35463546
#define pthread_cleanup_push(f,x) do { struct __ptcb __cb; _pthread_cleanup_push(&__cb, f, x);
3547+
#define pthread_create(...) ({ _Static_assert(0, "This mode of WASI does not have threads enabled; to enable stub functions which always fail, compile with -D_WASI_EMULATED_PTHREAD and link with -lwasi-emulated-pthread"); 0;})
3548+
#define pthread_detach(...) ({ _Static_assert(0, "This mode of WASI does not have threads enabled; to enable stub functions which always fail, compile with -D_WASI_EMULATED_PTHREAD and link with -lwasi-emulated-pthread"); 0;})
35473549
#define pthread_equal(x,y) ((x)==(y))
3550+
#define pthread_join(...) ({ _Static_assert(0, "This mode of WASI does not have threads enabled; to enable stub functions which always fail, compile with -D_WASI_EMULATED_PTHREAD and link with -lwasi-emulated-pthread"); 0;})
3551+
#define pthread_timedjoin_np(...) ({ _Static_assert(0, "This mode of WASI does not have threads enabled; to enable stub functions which always fail, compile with -D_WASI_EMULATED_PTHREAD and link with -lwasi-emulated-pthread"); 0;})
3552+
#define pthread_tryjoin_np(...) ({ _Static_assert(0, "This mode of WASI does not have threads enabled; to enable stub functions which always fail, compile with -D_WASI_EMULATED_PTHREAD and link with -lwasi-emulated-pthread"); 0;})
35483553
#define pwrite64 pwrite
35493554
#define pwritev64 pwritev
35503555
#define readdir64 readdir

libc-top-half/musl/include/pthread.h

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,29 @@ extern "C" {
7777
#define PTHREAD_NULL ((pthread_t)0)
7878

7979

80+
#ifdef __wasilibc_unmodified_upstream
8081
int pthread_create(pthread_t *__restrict, const pthread_attr_t *__restrict, void *(*)(void *), void *__restrict);
8182
int pthread_detach(pthread_t);
82-
#ifdef __wasilibc_unmodified_upstream
8383
_Noreturn void pthread_exit(void *);
84-
#endif
8584
int pthread_join(pthread_t, void **);
85+
#else
86+
#if defined(_WASI_EMULATED_PTHREAD) || defined(_REENTRANT)
87+
int pthread_create(pthread_t *__restrict, const pthread_attr_t *__restrict, void *(*)(void *), void *__restrict);
88+
int pthread_detach(pthread_t);
89+
int pthread_join(pthread_t, void **);
90+
#else
91+
#include <assert.h>
92+
#define pthread_create(...) ({ _Static_assert(0, "This mode of WASI does not have threads enabled; \
93+
to enable stub functions which always fail, \
94+
compile with -D_WASI_EMULATED_PTHREAD and link with -lwasi-emulated-pthread"); 0;})
95+
#define pthread_detach(...) ({ _Static_assert(0, "This mode of WASI does not have threads enabled; \
96+
to enable stub functions which always fail, \
97+
compile with -D_WASI_EMULATED_PTHREAD and link with -lwasi-emulated-pthread"); 0;})
98+
#define pthread_join(...) ({ _Static_assert(0, "This mode of WASI does not have threads enabled; \
99+
to enable stub functions which always fail, \
100+
compile with -D_WASI_EMULATED_PTHREAD and link with -lwasi-emulated-pthread"); 0;})
101+
#endif
102+
#endif
86103

87104
#ifdef __GNUC__
88105
__attribute__((const))
@@ -232,8 +249,17 @@ int pthread_setname_np(pthread_t, const char *);
232249
int pthread_getname_np(pthread_t, char *, size_t);
233250
int pthread_getattr_default_np(pthread_attr_t *);
234251
int pthread_setattr_default_np(const pthread_attr_t *);
252+
#if defined(__wasilibc_unmodified_upstream) || defined(_WASI_EMULATED_PTHREAD) || defined(_REENTRANT)
235253
int pthread_tryjoin_np(pthread_t, void **);
236254
int pthread_timedjoin_np(pthread_t, void **, const struct timespec *);
255+
#else
256+
#define pthread_tryjoin_np(...) ({ _Static_assert(0, "This mode of WASI does not have threads enabled; \
257+
to enable stub functions which always fail, \
258+
compile with -D_WASI_EMULATED_PTHREAD and link with -lwasi-emulated-pthread"); 0;})
259+
#define pthread_timedjoin_np(...) ({ _Static_assert(0, "This mode of WASI does not have threads enabled; \
260+
to enable stub functions which always fail, \
261+
compile with -D_WASI_EMULATED_PTHREAD and link with -lwasi-emulated-pthread"); 0;})
262+
#endif
237263
#endif
238264

239265
#if _REDIR_TIME64

0 commit comments

Comments
 (0)