Skip to content

Commit

Permalink
Add emulated thread-creation-related functions
Browse files Browse the repository at this point in the history
These functions always fail
  • Loading branch information
ArcaneNibble committed Sep 25, 2024
1 parent 01a7f89 commit 87d8ee2
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 0 deletions.
19 changes: 19 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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))
Expand All @@ -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) \
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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 += \
Expand Down Expand Up @@ -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 += \
Expand All @@ -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
Expand Down
8 changes: 8 additions & 0 deletions expected/wasm32-wasip1/defined-symbols.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions expected/wasm32-wasip2/defined-symbols.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
40 changes: 40 additions & 0 deletions stub-pthreads/stub-pthreads-emulated.c
Original file line number Diff line number Diff line change
@@ -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;
}

0 comments on commit 87d8ee2

Please sign in to comment.