Skip to content

Commit 2c12613

Browse files
committed
test: add tests for pthread API
This change runs the pthread tests available in `libc-test` when `THREAD_MODEL=posix`. This is currently a work-in-progress, since there are custom build steps necessary to get this all to (mostly) work. Currently this might look like: ```console $ cd test $ make run THREAD_MODEL=posix \ ENGINE=~/Code/wasmtime/target/debug/wasmtime \ CC=~/Code/llvm-project/build/bin/clang ``` What are the current issues? - this requires a special build of Wasmtime that includes the `wasi-threads` implementation; this is shown above as built from source - under `THREAD_MODEL=posix`, this requires Clang to understand the `--export-memory` flag which, though merged in https://reviews.llvm.org/D135898, is not available for download yet (but can be built from latest source as shown above) - not all of the tests yet pass and some do not build: this is to be expected in several cases (PI? robust?) but there is perhaps more that can be done here to enable more of the pthread API. Only the working tests are included in this commit.
1 parent 957c711 commit 2c12613

File tree

1 file changed

+50
-6
lines changed

1 file changed

+50
-6
lines changed

test/Makefile

+50-6
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ test: run
1515
# directory (keeping with the `wasi-libc` conventions).
1616
OBJDIR ?= $(CURDIR)/build
1717
DOWNDIR ?= $(CURDIR)/download
18+
# Like the top-level `wasi-libc` Makefile, here we can decide whether to test
19+
# with threads (`posix`) or without (`single`). IMPORTANT: the top-level include
20+
# directory must be built with the same value.
21+
THREAD_MODEL ?= single
1822

1923
##### DOWNLOAD #################################################################
2024

@@ -90,6 +94,14 @@ TESTS := \
9094
$(LIBC_TEST)/src/functional/wcsstr.c \
9195
$(LIBC_TEST)/src/functional/wcstol.c
9296

97+
ifeq ($(THREAD_MODEL), posix)
98+
TESTS += \
99+
$(LIBC_TEST)/src/functional/pthread_cond.c \
100+
$(LIBC_TEST)/src/functional/pthread_tsd.c \
101+
$(LIBC_TEST)/src/functional/tls_init.c \
102+
$(LIBC_TEST)/src/functional/tls_local_exec.c
103+
endif
104+
93105
# Part of the problem including more tests is that the `libc-test`
94106
# infrastructure code is not all Wasm-compilable. As we include more tests
95107
# above, this list will also likely need to grow.
@@ -116,23 +128,54 @@ ifeq ($(origin CC), default)
116128
CC := clang
117129
endif
118130
LDFLAGS ?=
119-
CFLAGS ?= --target=wasm32-wasi --sysroot=../sysroot
131+
CFLAGS ?= --sysroot=../sysroot
120132
# Always include the `libc-test` infrastructure headers.
121133
CFLAGS += -I$(LIBC_TEST)/src/common
122134

135+
# Configure support for threads.
136+
ifeq ($(THREAD_MODEL), single)
137+
CFLAGS += --target=wasm32-wasi -mthread-model single
138+
endif
139+
ifeq ($(THREAD_MODEL), posix)
140+
# Specify the tls-model until LLVM 15 is released (which should contain
141+
# https://reviews.llvm.org/D130053).
142+
CFLAGS += --target=wasm32-wasi-pthread -mthread-model posix -pthread -ftls-model=local-exec
143+
# NOTE: `--export-memory` is invalid until https://reviews.llvm.org/D135898 is
144+
# available in a released version of `wasm-ld`; this has not happened yet so
145+
# a built-from-latest-source Clang is necessary.
146+
LDFLAGS += -Wl,--import-memory,--export-memory,--max-memory=1048576
147+
endif
148+
149+
# We want to be careful to rebuild the Wasm files if any important variables
150+
# change. This block calculates a hash from key variables and writes a file
151+
# (ENV_HASH = `build/env-<hash>`); any targets dependent on `ENV_HASH` will thus
152+
# be rebuilt whenever `ENV_HASH` is modified, which is whenever `ENV` changes.
153+
# The phony `env` target can be used to debug this.
154+
ENV = "CC=$(CC);CFLAGS=$(CFLAGS);LDFLAGS=$(LDFLAGS);THREAD_MODEL=$(THREAD_MODEL)"
155+
export ENV
156+
ENV_HASH = $(OBJDIR)/env-$(shell echo $(ENV) | md5sum | cut -d ' ' -f 1)
157+
$(ENV_HASH): | $(OBJDIRS)
158+
rm -f $(OBJDIR)/env-*
159+
echo $(ENV) > $@
160+
env: $(ENV_HASH)
161+
@echo ENV = "$$ENV"
162+
@echo ENV_HASH = $(ENV_HASH)
163+
cat $^
164+
.PHONY: env
165+
123166
# Compile each selected test using Clang. Note that failures here are likely
124167
# due to a missing `libclang_rt.builtins-wasm32.a` in the Clang lib directory.
125168
# This location is system-dependent, but could be fixed by something like:
126169
# $ sudo mkdir /usr/lib64/clang/14.0.5/lib/wasi
127170
# $ sudo cp download/lib/wasi/libclang_rt.builtins-wasm32.a /usr/lib64/clang/14.0.5/lib/wasi/
128-
build: download $(WASMS)
171+
build: download $(WASMS) $(ENV_HASH)
129172

130173
$(WASMS): | $(OBJDIRS)
131-
$(OBJDIR)/%.wasm: $(OBJDIR)/%.wasm.o $(INFRA_WASM_OBJS)
132-
$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^
174+
$(OBJDIR)/%.wasm: $(OBJDIR)/%.wasm.o $(INFRA_WASM_OBJS) $(ENV_HASH)
175+
$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $(filter-out $(ENV_HASH), $^)
133176

134177
$(WASM_OBJS): $(LIBC_TEST)/src/common/test.h | $(OBJDIRS)
135-
$(OBJDIR)/%.wasm.o: $(LIBC_TEST)/src/%.c
178+
$(OBJDIR)/%.wasm.o: $(LIBC_TEST)/src/%.c $(ENV_HASH)
136179
$(CC) $(CFLAGS) -c -o $@ $<
137180

138181
$(OBJDIRS):
@@ -144,6 +187,7 @@ clean::
144187
##### RUN ######################################################################
145188

146189
ENGINE ?= $(WASMTIME) run
190+
ENGINE_FLAGS ?= --wasm-features threads --wasi-modules experimental-wasi-threads
147191
ERRS:=$(WASMS:%.wasm=%.wasm.err)
148192

149193
# Use the provided Wasm engine to execute each test, emitting its output into
@@ -154,7 +198,7 @@ run: build $(ERRS)
154198
$(ERRS): | $(OBJDIRS)
155199

156200
%.wasm.err: %.wasm
157-
$(ENGINE) $< >$@
201+
$(ENGINE) $(ENGINE_FLAGS) $<
158202

159203
clean::
160204
rm -rf $(OBJDIR)/*/*.err

0 commit comments

Comments
 (0)