From 87d8ee2384506fddbd2680ce8e51237ed15d0e22 Mon Sep 17 00:00:00 2001 From: R Date: Wed, 25 Sep 2024 18:38:20 +0100 Subject: [PATCH] Add emulated thread-creation-related functions These functions always fail --- Makefile | 19 ++++++++++ expected/wasm32-wasip1/defined-symbols.txt | 8 +++++ expected/wasm32-wasip2/defined-symbols.txt | 8 +++++ stub-pthreads/stub-pthreads-emulated.c | 40 ++++++++++++++++++++++ 4 files changed, 75 insertions(+) create mode 100644 stub-pthreads/stub-pthreads-emulated.c diff --git a/Makefile b/Makefile index ed2a426b..bb303091 100644 --- a/Makefile +++ b/Makefile @@ -143,6 +143,8 @@ LIBWASI_EMULATED_SIGNAL_SOURCES = \ LIBWASI_EMULATED_SIGNAL_MUSL_SOURCES = \ $(LIBC_TOP_HALF_MUSL_SRC_DIR)/signal/psignal.c \ $(LIBC_TOP_HALF_MUSL_SRC_DIR)/string/strsignal.c +LIBWASI_EMULATED_PTHREAD_SOURCES = \ + $(STUB_PTHREADS_DIR)/stub-pthreads-emulated.c LIBDL_SOURCES = $(LIBC_TOP_HALF_MUSL_SRC_DIR)/misc/dl.c LIBSETJMP_SOURCES = $(LIBC_TOP_HALF_MUSL_SRC_DIR)/setjmp/wasm32/rt.c LIBC_BOTTOM_HALF_CRT_SOURCES = $(wildcard $(LIBC_BOTTOM_HALF_DIR)/crt/*.c) @@ -500,6 +502,7 @@ LIBWASI_EMULATED_PROCESS_CLOCKS_OBJS = $(call objs,$(LIBWASI_EMULATED_PROCESS_CL LIBWASI_EMULATED_GETPID_OBJS = $(call objs,$(LIBWASI_EMULATED_GETPID_SOURCES)) LIBWASI_EMULATED_SIGNAL_OBJS = $(call objs,$(LIBWASI_EMULATED_SIGNAL_SOURCES)) LIBWASI_EMULATED_SIGNAL_MUSL_OBJS = $(call objs,$(LIBWASI_EMULATED_SIGNAL_MUSL_SOURCES)) +LIBWASI_EMULATED_PTHREAD_OBJS = $(call objs,$(LIBWASI_EMULATED_PTHREAD_SOURCES)) LIBDL_OBJS = $(call objs,$(LIBDL_SOURCES)) LIBSETJMP_OBJS = $(call objs,$(LIBSETJMP_SOURCES)) LIBC_BOTTOM_HALF_CRT_OBJS = $(call objs,$(LIBC_BOTTOM_HALF_CRT_SOURCES)) @@ -603,6 +606,7 @@ LIBWASI_EMULATED_PROCESS_CLOCKS_SO_OBJS = $(patsubst %.o,%.pic.o,$(LIBWASI_EMULA LIBWASI_EMULATED_GETPID_SO_OBJS = $(patsubst %.o,%.pic.o,$(LIBWASI_EMULATED_GETPID_OBJS)) LIBWASI_EMULATED_SIGNAL_SO_OBJS = $(patsubst %.o,%.pic.o,$(LIBWASI_EMULATED_SIGNAL_OBJS)) LIBWASI_EMULATED_SIGNAL_MUSL_SO_OBJS = $(patsubst %.o,%.pic.o,$(LIBWASI_EMULATED_SIGNAL_MUSL_OBJS)) +LIBWASI_EMULATED_PTHREAD_SO_OBJS = $(patsubst %.o,%.pic.o,$(LIBWASI_EMULATED_PTHREAD_OBJS)) LIBDL_SO_OBJS = $(patsubst %.o,%.pic.o,$(LIBDL_OBJS)) LIBSETJMP_SO_OBJS = $(patsubst %.o,%.pic.o,$(LIBSETJMP_OBJS)) BULK_MEMORY_SO_OBJS = $(patsubst %.o,%.pic.o,$(BULK_MEMORY_OBJS)) @@ -618,6 +622,7 @@ PIC_OBJS = \ $(LIBWASI_EMULATED_GETPID_SO_OBJS) \ $(LIBWASI_EMULATED_SIGNAL_SO_OBJS) \ $(LIBWASI_EMULATED_SIGNAL_MUSL_SO_OBJS) \ + $(LIBWASI_EMULATED_PTHREAD_SO_OBJS) \ $(LIBDL_SO_OBJS) \ $(LIBSETJMP_SO_OBJS) \ $(BULK_MEMORY_SO_OBJS) \ @@ -659,6 +664,8 @@ $(OBJDIR)/libwasi-emulated-getpid.so.a: $(LIBWASI_EMULATED_GETPID_SO_OBJS) $(OBJDIR)/libwasi-emulated-signal.so.a: $(LIBWASI_EMULATED_SIGNAL_SO_OBJS) $(LIBWASI_EMULATED_SIGNAL_MUSL_SO_OBJS) +$(OBJDIR)/libwasi-emulated-pthread.so.a: $(LIBWASI_EMULATED_PTHREAD_SO_OBJS) + $(OBJDIR)/libdl.so.a: $(LIBDL_SO_OBJS) $(OBJDIR)/libsetjmp.so.a: $(LIBSETJMP_SO_OBJS) @@ -677,6 +684,8 @@ $(SYSROOT_LIB)/libwasi-emulated-getpid.a: $(LIBWASI_EMULATED_GETPID_OBJS) $(SYSROOT_LIB)/libwasi-emulated-signal.a: $(LIBWASI_EMULATED_SIGNAL_OBJS) $(LIBWASI_EMULATED_SIGNAL_MUSL_OBJS) +$(SYSROOT_LIB)/libwasi-emulated-pthread.a: $(LIBWASI_EMULATED_PTHREAD_OBJS) + $(SYSROOT_LIB)/libdl.a: $(LIBDL_OBJS) $(SYSROOT_LIB)/libsetjmp.a: $(LIBSETJMP_OBJS) @@ -775,6 +784,11 @@ $(LIBC_TOP_HALF_ALL_OBJS) $(LIBC_TOP_HALF_ALL_SO_OBJS) $(MUSL_PRINTSCAN_LONG_DOU $(LIBWASI_EMULATED_PROCESS_CLOCKS_OBJS) $(LIBWASI_EMULATED_PROCESS_CLOCKS_SO_OBJS): CFLAGS += \ -I$(LIBC_BOTTOM_HALF_CLOUDLIBC_SRC) +$(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 + # 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. $(EMMALLOC_OBJS): CFLAGS += \ @@ -824,6 +838,7 @@ LIBC_SO = \ $(SYSROOT_LIB)/libwasi-emulated-process-clocks.so \ $(SYSROOT_LIB)/libwasi-emulated-getpid.so \ $(SYSROOT_LIB)/libwasi-emulated-signal.so \ + $(SYSROOT_LIB)/libwasi-emulated-pthread.so \ $(SYSROOT_LIB)/libdl.so ifeq ($(BUILD_LIBSETJMP),yes) LIBC_SO += \ @@ -842,6 +857,10 @@ STATIC_LIBS = \ $(SYSROOT_LIB)/libwasi-emulated-getpid.a \ $(SYSROOT_LIB)/libwasi-emulated-signal.a \ $(SYSROOT_LIB)/libdl.a +ifneq ($(THREAD_MODEL), posix) + STATIC_LIBS += \ + $(SYSROOT_LIB)/libwasi-emulated-pthread.a +endif ifeq ($(BUILD_LIBSETJMP),yes) STATIC_LIBS += \ $(SYSROOT_LIB)/libsetjmp.a diff --git a/expected/wasm32-wasip1/defined-symbols.txt b/expected/wasm32-wasip1/defined-symbols.txt index 0e5ecc25..99bbd244 100644 --- a/expected/wasm32-wasip1/defined-symbols.txt +++ b/expected/wasm32-wasip1/defined-symbols.txt @@ -181,6 +181,9 @@ __powf_log2_data __progname __progname_full __pthread_cond_timedwait +__pthread_create +__pthread_detach +__pthread_join __pthread_key_create __pthread_key_delete __pthread_mutex_consistent @@ -980,9 +983,12 @@ pthread_condattr_getpshared pthread_condattr_init pthread_condattr_setclock pthread_condattr_setpshared +pthread_create +pthread_detach pthread_equal pthread_exit pthread_getspecific +pthread_join pthread_key_create pthread_key_delete pthread_mutex_consistent @@ -1026,6 +1032,8 @@ pthread_spin_lock pthread_spin_trylock pthread_spin_unlock pthread_testcancel +pthread_timedjoin_np +pthread_tryjoin_np putc putc_unlocked putchar diff --git a/expected/wasm32-wasip2/defined-symbols.txt b/expected/wasm32-wasip2/defined-symbols.txt index 0b14edd2..22edab2f 100644 --- a/expected/wasm32-wasip2/defined-symbols.txt +++ b/expected/wasm32-wasip2/defined-symbols.txt @@ -184,6 +184,9 @@ __powf_log2_data __progname __progname_full __pthread_cond_timedwait +__pthread_create +__pthread_detach +__pthread_join __pthread_key_create __pthread_key_delete __pthread_mutex_consistent @@ -1113,9 +1116,12 @@ pthread_condattr_getpshared pthread_condattr_init pthread_condattr_setclock pthread_condattr_setpshared +pthread_create +pthread_detach pthread_equal pthread_exit pthread_getspecific +pthread_join pthread_key_create pthread_key_delete pthread_mutex_consistent @@ -1159,6 +1165,8 @@ pthread_spin_lock pthread_spin_trylock pthread_spin_unlock pthread_testcancel +pthread_timedjoin_np +pthread_tryjoin_np putc putc_unlocked putchar diff --git a/stub-pthreads/stub-pthreads-emulated.c b/stub-pthreads/stub-pthreads-emulated.c new file mode 100644 index 00000000..d7e04774 --- /dev/null +++ b/stub-pthreads/stub-pthreads-emulated.c @@ -0,0 +1,40 @@ +// This file is linked into wasi-emulated-pthread + +#include "pthread_impl.h" + +int __pthread_create(pthread_t *restrict res, const pthread_attr_t *restrict attrp, void *(*entry)(void *), void *restrict arg) +{ + /* + "The system lacked the necessary resources to create another thread, + or the system-imposed limit on the total number of threads in a process + {PTHREAD_THREADS_MAX} would be exceeded." + */ + return EAGAIN; +} +weak_alias(__pthread_create, pthread_create); +int __pthread_detach(pthread_t t) +{ + /* + If we are the only thread, when we exit the whole process exits. + So the storage will be reclaimed no matter what. + */ + return 0; +} +weak_alias(__pthread_detach, pthread_detach); +int __pthread_join(pthread_t t, void **res) +{ + /* + "The behavior is undefined if the value specified by the thread argument + to pthread_join() refers to the calling thread." + */ + return 0; +} +weak_alias(__pthread_join, pthread_join); +int pthread_tryjoin_np(pthread_t t, void **res) +{ + return 0; +} +int pthread_timedjoin_np(pthread_t t, void **res, const struct timespec *at) +{ + return 0; +}