Skip to content

Commit dcd28cf

Browse files
abrownsbc100
andauthored
Fix make THREAD_MODEL=posix (#311)
* Fixes for the THREAD_MODEL=posix build * Fix expected symbols from previous commit * Enable `lock` in `random.c` when threads are enabled This uses the `_REENTRANT` definition to indicate when the `lock` should be available. * Disable `aio.h` when compiling for threads In talking to @sunfishcode about `aio.h`, this functionality is not yet a primary concern (it was already disabled in the default, single-threaded mode). Additionally, this change adds expectation lines for the new symbols/includes added and removed by `pthread.h`. This change was reached by running: ```console $ git diff --no-index expected/wasm32-wasi sysroot/share/wasm32-wasi > patch.diff # replace "sysroot/share" with "expected" in `patch.diff` $ git apply patch.diff --reject # manually fix any rejections ``` * Specify the TLS model until LLVM 15 is released The `-ftls-model` configuration can be removed once https://reviews.llvm.org/D130053 makes its way into an upstream release. * Rename `__wasi_libc_pthread_self` to `__wasilibc_pthread_self` The symbol is still undefined, though. * Add different sets of expected output based on THREAD_MODEL * Re-add trailing whitespace to `predefined-macros.txt` @sbc100 wanted to retain the whitespace trailing after certain predefined macro lines. This change restores that whitespace from upstream and re-generates the POSIX version using the following command: ```console $ git diff --no-index expected/wasm32-wasi/posix/predefined-macros.txt sysroot/share/wasm32-wasi/predefined-macros.txt | sed 's/sysroot\/share\/wasm32-wasi/expected\/wasm32-wasi\/posix/' | git apply ``` * Protect `preopens.c` against concurrent access * Only build thread-capable wasi-libc on latest version of Clang * Use `thrd_sleep` from MUSL instead of aliasing `nanosleep` * Define `pthread_setcancelstate` in `THREAD_MODEL=posix` builds There are other options here (e.g., always define the `pthread_*` symbols with stubs) but until we discuss that this is an intermediate working step. * Define a Wasm global to store `pthread_self` * Remove `g_needs_dynamic_alloc` global * Document the state of pthread support * review: de-duplicate symbols based on #314 * review: only define `__wasilibc_cwd_{un}lock` when needed * review: add #ifdefs to `__pthread_setcancelstate` * review: add additional #ifdefs to `pthread_self.c` * review: put lock definition behind #ifdef _REENTRANT * review: remove pthread_setcancelstate.c * review: re-fix indentation * review: alias __clock_nanosleep in bottom half * review: remove extra line Co-authored-by: Sam Clegg <sbc@chromium.org>
1 parent 69031b6 commit dcd28cf

26 files changed

Lines changed: 4973 additions & 46 deletions

.github/workflows/main.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,14 @@ jobs:
6666
name: ${{ format( 'sysroot-{0}.tgz', matrix.os) }}
6767
path: sysroot
6868

69+
- name: Build libc + threads
70+
# Only build the thread-capable wasi-libc in the latest supported Clang
71+
# version; the earliest version does not have all necessary builtins
72+
# (e.g., `__builtin_wasm_memory_atomic_notify`).
73+
if: matrix.clang_version != '10.0.0'
74+
shell: bash
75+
run: make -j4 THREAD_MODEL=posix
76+
6977
# Disable the headerstest job for now, while WASI transitions from the
7078
# witx snapshots to wit proposals, and we have a few manual edits to the
7179
# generated header to make life easier for folks.

Makefile

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ EXTRA_CFLAGS ?= -O2 -DNDEBUG
1313
SYSROOT ?= $(CURDIR)/sysroot
1414
# A directory to install to for "make install".
1515
INSTALL_DIR ?= /usr/local
16-
# single or posix
16+
# single or posix; note that pthread support is still a work-in-progress.
1717
THREAD_MODEL ?= single
1818
# dlmalloc or none
1919
MALLOC_IMPL ?= dlmalloc
@@ -147,6 +147,7 @@ LIBC_TOP_HALF_MUSL_SOURCES = \
147147
unistd/posix_close.c \
148148
stat/futimesat.c \
149149
legacy/getpagesize.c \
150+
thread/thrd_sleep.c \
150151
) \
151152
$(filter-out %/procfdname.c %/syscall.c %/syscall_ret.c %/vdso.c %/version.c, \
152153
$(wildcard $(LIBC_TOP_HALF_MUSL_SRC_DIR)/internal/*.c)) \
@@ -185,6 +186,7 @@ LIBC_TOP_HALF_MUSL_SOURCES = \
185186
%/cimagf.c %/cimag.c %cimagl.c, \
186187
$(wildcard $(LIBC_TOP_HALF_MUSL_SRC_DIR)/complex/*.c)) \
187188
$(wildcard $(LIBC_TOP_HALF_MUSL_SRC_DIR)/crypt/*.c)
189+
188190
MUSL_PRINTSCAN_SOURCES = \
189191
$(LIBC_TOP_HALF_MUSL_SRC_DIR)/internal/floatscan.c \
190192
$(LIBC_TOP_HALF_MUSL_SRC_DIR)/stdio/vfprintf.c \
@@ -227,7 +229,9 @@ ifeq ($(THREAD_MODEL), single)
227229
CFLAGS += -mthread-model single
228230
endif
229231
ifeq ($(THREAD_MODEL), posix)
230-
CFLAGS += -mthread-model posix -pthread
232+
# Specify the tls-model until LLVM 15 is released (which should contain
233+
# https://reviews.llvm.org/D130053).
234+
CFLAGS += -mthread-model posix -pthread -ftls-model=local-exec
231235
endif
232236

233237
# Expose the public headers to the implementation. We use `-isystem` for
@@ -356,11 +360,12 @@ MUSL_OMIT_HEADERS += \
356360
"netinet/ether.h" \
357361
"sys/timerfd.h" \
358362
"libintl.h" \
359-
"sys/sysmacros.h"
363+
"sys/sysmacros.h" \
364+
"aio.h"
360365

361366
ifeq ($(THREAD_MODEL), single)
362367
# Remove headers not supported in single-threaded mode.
363-
MUSL_OMIT_HEADERS += "aio.h" "pthread.h"
368+
MUSL_OMIT_HEADERS += "pthread.h"
364369
endif
365370

366371
default: finish
@@ -595,7 +600,7 @@ check-symbols: startup_files libc
595600

596601
# Check that the computed metadata matches the expected metadata.
597602
# This ignores whitespace because on Windows the output has CRLF line endings.
598-
diff -wur "$(CURDIR)/expected/$(MULTIARCH_TRIPLE)" "$(SYSROOT_SHARE)"
603+
diff -wur "$(CURDIR)/expected/$(MULTIARCH_TRIPLE)/$(THREAD_MODEL)" "$(SYSROOT_SHARE)"
599604

600605
install: finish
601606
mkdir -p "$(INSTALL_DIR)"

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ environment variables, program startup, and many other APIs.
77

88
WASI Libc is sufficiently stable and usable for many purposes, as most of the
99
POSIX-compatible APIs are stable, though it is continuing to evolve to better
10-
align with wasm and WASI.
10+
align with wasm and WASI. For example, pthread support is still a work in
11+
progress.
1112

1213
## Usage
1314

0 commit comments

Comments
 (0)