Skip to content

Commit 0725d03

Browse files
abrownsbc100
authored andcommittedMar 5, 2023
Fix make THREAD_MODEL=posix (WebAssembly#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 $ git apply patch.diff --reject ``` * 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 WebAssembly#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 <[email protected]>
1 parent b7ed07a commit 0725d03

24 files changed

+5122
-24
lines changed
 

‎.github/workflows/main.yml

+8
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,14 @@ jobs:
6464
name: ${{ format( 'sysroot-{0}.tgz', matrix.os) }}
6565
path: sysroot
6666

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

‎Makefile

+12-5
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ EXTRA_CFLAGS ?= -O2 -DNDEBUG -ftls-model=local-exec -D_WASI_EMULATED_MMAN -D_WAS
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 ?= posix
1818
# dlmalloc or none
1919
MALLOC_IMPL ?= dlmalloc
@@ -167,6 +167,8 @@ LIBC_TOP_HALF_MUSL_SOURCES = \
167167
linux/wait4.c \
168168
linux/eventfd.c \
169169
stat/futimesat.c \
170+
legacy/getpagesize.c \
171+
thread/thrd_sleep.c \
170172
) \
171173
$(filter-out %/procfdname.c %/syscall.c %/syscall_ret.c %/vdso.c %/version.c, \
172174
$(wildcard $(LIBC_TOP_HALF_MUSL_SRC_DIR)/internal/*.c)) \
@@ -212,6 +214,7 @@ LIBC_TOP_HALF_MUSL_SOURCES = \
212214
%/cimagf.c %/cimag.c %cimagl.c, \
213215
$(wildcard $(LIBC_TOP_HALF_MUSL_SRC_DIR)/complex/*.c)) \
214216
$(wildcard $(LIBC_TOP_HALF_MUSL_SRC_DIR)/crypt/*.c)
217+
215218
MUSL_PRINTSCAN_SOURCES = \
216219
$(LIBC_TOP_HALF_MUSL_SRC_DIR)/internal/floatscan.c \
217220
$(LIBC_TOP_HALF_MUSL_SRC_DIR)/stdio/vfprintf.c \
@@ -254,7 +257,9 @@ ifeq ($(THREAD_MODEL), single)
254257
CFLAGS += -mthread-model single
255258
endif
256259
ifeq ($(THREAD_MODEL), posix)
257-
CFLAGS += -mthread-model posix -pthread
260+
# Specify the tls-model until LLVM 15 is released (which should contain
261+
# https://reviews.llvm.org/D130053).
262+
CFLAGS += -mthread-model posix -pthread -ftls-model=local-exec
258263
endif
259264

260265
# Expose the public headers to the implementation. We use `-isystem` for
@@ -373,11 +378,13 @@ MUSL_OMIT_HEADERS += \
373378
"netinet/if_ether.h" \
374379
"netinet/ether.h" \
375380
"sys/timerfd.h" \
376-
"sys/sysmacros.h"
381+
"libintl.h" \
382+
"sys/sysmacros.h" \
383+
"aio.h"
377384

378385
ifeq ($(THREAD_MODEL), single)
379386
# Remove headers not supported in single-threaded mode.
380-
MUSL_OMIT_HEADERS += "aio.h" "pthread.h"
387+
MUSL_OMIT_HEADERS += "pthread.h"
381388
endif
382389

383390
default: finish
@@ -604,7 +611,7 @@ check-symbols: startup_files libc
604611

605612
# Check that the computed metadata matches the expected metadata.
606613
# This ignores whitespace because on Windows the output has CRLF line endings.
607-
diff -wur "$(CURDIR)/expected/$(MULTIARCH_TRIPLE)" "$(SYSROOT_SHARE)"
614+
diff -wur "$(CURDIR)/expected/$(MULTIARCH_TRIPLE)/$(THREAD_MODEL)" "$(SYSROOT_SHARE)"
608615

609616
install: finish
610617
mkdir -p "$(INSTALL_DIR)"

‎README.md

+6-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,12 @@
33
WASI Libc is a libc for WebAssembly programs built on top of WASI system calls.
44
It provides a wide array of POSIX-compatible C APIs, including support for
55
standard I/O, file I/O, filesystem manipulation, memory management, time, string,
6-
environment variables, program startup, multithreading, networkign, and many other
7-
APIs.
6+
environment variables, program startup, and many other APIs.
7+
8+
WASI Libc is sufficiently stable and usable for many purposes, as most of the
9+
POSIX-compatible APIs are stable, though it is continuing to evolve to better
10+
align with wasm and WASI. For example, pthread support is still a work in
11+
progress.
812

913
## Usage
1014

0 commit comments

Comments
 (0)
Please sign in to comment.