Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 260e8ce

Browse files
committedSep 25, 2024·
Make always-fail functions error if _WASI_EMULATED_PTHREAD isn't defined
1 parent 8aee1e6 commit 260e8ce

File tree

4 files changed

+40
-3
lines changed

4 files changed

+40
-3
lines changed
 

‎Makefile

+2-1
Original file line numberDiff line numberDiff line change
@@ -787,7 +787,8 @@ $(LIBWASI_EMULATED_PROCESS_CLOCKS_OBJS) $(LIBWASI_EMULATED_PROCESS_CLOCKS_SO_OBJ
787787
$(LIBWASI_EMULATED_PTHREAD_OBJS) $(LIBWASI_EMULATED_PTHREAD_SO_OBJS): CFLAGS += \
788788
-I$(LIBC_TOP_HALF_MUSL_SRC_DIR)/include \
789789
-I$(LIBC_TOP_HALF_MUSL_SRC_DIR)/internal \
790-
-I$(LIBC_TOP_HALF_MUSL_DIR)/arch/wasm32
790+
-I$(LIBC_TOP_HALF_MUSL_DIR)/arch/wasm32 \
791+
-D_WASI_EMULATED_PTHREAD
791792

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

‎expected/wasm32-wasip1/predefined-macros.txt

+5
Original file line numberDiff line numberDiff line change
@@ -3347,7 +3347,12 @@
33473347
#define preadv64 preadv
33483348
#define pthread_cleanup_pop(r) _pthread_cleanup_pop(&__cb, (r)); } while(0)
33493349
#define pthread_cleanup_push(f,x) do { struct __ptcb __cb; _pthread_cleanup_push(&__cb, f, x);
3350+
#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;})
3351+
#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;})
33503352
#define pthread_equal(x,y) ((x)==(y))
3353+
#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;})
3354+
#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;})
3355+
#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;})
33513356
#define pwrite64 pwrite
33523357
#define pwritev64 pwritev
33533358
#define readdir64 readdir

‎expected/wasm32-wasip2/predefined-macros.txt

+5
Original file line numberDiff line numberDiff line change
@@ -3502,7 +3502,12 @@
35023502
#define preadv64 preadv
35033503
#define pthread_cleanup_pop(r) _pthread_cleanup_pop(&__cb, (r)); } while(0)
35043504
#define pthread_cleanup_push(f,x) do { struct __ptcb __cb; _pthread_cleanup_push(&__cb, f, x);
3505+
#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;})
3506+
#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;})
35053507
#define pthread_equal(x,y) ((x)==(y))
3508+
#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;})
3509+
#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;})
3510+
#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;})
35063511
#define pwrite64 pwrite
35073512
#define pwritev64 pwritev
35083513
#define readdir64 readdir

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

+28-2
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)
Please sign in to comment.