From 260e8ce4ab1b7f78f50eecba81d26ad0be2c193b Mon Sep 17 00:00:00 2001 From: R Date: Wed, 25 Sep 2024 20:57:04 +0100 Subject: [PATCH] Make always-fail functions error if _WASI_EMULATED_PTHREAD isn't defined --- Makefile | 3 +- expected/wasm32-wasip1/predefined-macros.txt | 5 ++++ expected/wasm32-wasip2/predefined-macros.txt | 5 ++++ libc-top-half/musl/include/pthread.h | 30 ++++++++++++++++++-- 4 files changed, 40 insertions(+), 3 deletions(-) diff --git a/Makefile b/Makefile index bb303091b..a44f110e3 100644 --- a/Makefile +++ b/Makefile @@ -787,7 +787,8 @@ $(LIBWASI_EMULATED_PROCESS_CLOCKS_OBJS) $(LIBWASI_EMULATED_PROCESS_CLOCKS_SO_OBJ $(LIBWASI_EMULATED_PTHREAD_OBJS) $(LIBWASI_EMULATED_PTHREAD_SO_OBJS): CFLAGS += \ -I$(LIBC_TOP_HALF_MUSL_SRC_DIR)/include \ -I$(LIBC_TOP_HALF_MUSL_SRC_DIR)/internal \ - -I$(LIBC_TOP_HALF_MUSL_DIR)/arch/wasm32 + -I$(LIBC_TOP_HALF_MUSL_DIR)/arch/wasm32 \ + -D_WASI_EMULATED_PTHREAD # emmalloc uses a lot of pointer type-punning, which is UB under strict aliasing, # and this was found to have real miscompilations in wasi-libc#421. diff --git a/expected/wasm32-wasip1/predefined-macros.txt b/expected/wasm32-wasip1/predefined-macros.txt index 5d56b9964..d80a436fd 100644 --- a/expected/wasm32-wasip1/predefined-macros.txt +++ b/expected/wasm32-wasip1/predefined-macros.txt @@ -3347,7 +3347,12 @@ #define preadv64 preadv #define pthread_cleanup_pop(r) _pthread_cleanup_pop(&__cb, (r)); } while(0) #define pthread_cleanup_push(f,x) do { struct __ptcb __cb; _pthread_cleanup_push(&__cb, f, x); +#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;}) +#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;}) #define pthread_equal(x,y) ((x)==(y)) +#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;}) +#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;}) +#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;}) #define pwrite64 pwrite #define pwritev64 pwritev #define readdir64 readdir diff --git a/expected/wasm32-wasip2/predefined-macros.txt b/expected/wasm32-wasip2/predefined-macros.txt index bb766d777..728df6147 100644 --- a/expected/wasm32-wasip2/predefined-macros.txt +++ b/expected/wasm32-wasip2/predefined-macros.txt @@ -3502,7 +3502,12 @@ #define preadv64 preadv #define pthread_cleanup_pop(r) _pthread_cleanup_pop(&__cb, (r)); } while(0) #define pthread_cleanup_push(f,x) do { struct __ptcb __cb; _pthread_cleanup_push(&__cb, f, x); +#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;}) +#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;}) #define pthread_equal(x,y) ((x)==(y)) +#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;}) +#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;}) +#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;}) #define pwrite64 pwrite #define pwritev64 pwritev #define readdir64 readdir diff --git a/libc-top-half/musl/include/pthread.h b/libc-top-half/musl/include/pthread.h index 2c35d0b51..fa9f66ae8 100644 --- a/libc-top-half/musl/include/pthread.h +++ b/libc-top-half/musl/include/pthread.h @@ -77,12 +77,29 @@ extern "C" { #define PTHREAD_NULL ((pthread_t)0) +#ifdef __wasilibc_unmodified_upstream int pthread_create(pthread_t *__restrict, const pthread_attr_t *__restrict, void *(*)(void *), void *__restrict); int pthread_detach(pthread_t); -#ifdef __wasilibc_unmodified_upstream _Noreturn void pthread_exit(void *); -#endif int pthread_join(pthread_t, void **); +#else +#if defined(_WASI_EMULATED_PTHREAD) || defined(_REENTRANT) +int pthread_create(pthread_t *__restrict, const pthread_attr_t *__restrict, void *(*)(void *), void *__restrict); +int pthread_detach(pthread_t); +int pthread_join(pthread_t, void **); +#else +#include +#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;}) +#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;}) +#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;}) +#endif +#endif #ifdef __GNUC__ __attribute__((const)) @@ -232,8 +249,17 @@ int pthread_setname_np(pthread_t, const char *); int pthread_getname_np(pthread_t, char *, size_t); int pthread_getattr_default_np(pthread_attr_t *); int pthread_setattr_default_np(const pthread_attr_t *); +#if defined(__wasilibc_unmodified_upstream) || defined(_WASI_EMULATED_PTHREAD) || defined(_REENTRANT) int pthread_tryjoin_np(pthread_t, void **); int pthread_timedjoin_np(pthread_t, void **, const struct timespec *); +#else +#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;}) +#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;}) +#endif #endif #if _REDIR_TIME64