Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 27 additions & 6 deletions ci/rebuild-libwit-bindgen-cabi.sh
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ wasip3_set=wit_bindgen_${version}_set_task_context
rm -f crates/guest-rust/src/rt/wit_bindgen_*.{rs,o,c}
rm -f crates/guest-rust/src/rt/libwit_bindgen_cabi_realloc.a
rm -f crates/guest-rust/src/rt/libwit_bindgen_cabi_wasip3.a
rm -f crates/guest-rust/src/rt/libwit_bindgen_cabi_wasip3_tls.a

cat >./crates/guest-rust/src/rt/wit_bindgen_cabi_realloc.rs <<-EOF
// This file is generated by $0
Expand Down Expand Up @@ -100,6 +101,15 @@ void *wasip3_task_set(void *ptr) {
WASIP3_TASK = ptr;
return ret;
}
EOF

# On WASIp3, wasi-libc switches TLS blocks between tasks. Compile this helper
# for that target so `_Thread_local` remains TLS; compiling it as MVP would
# silently turn `task_context` into a global shared by all tasks.
cat >./crates/guest-rust/src/rt/wit_bindgen_cabi_wasip3_tls.c <<-EOF
// This file is generated by $0

#include <stddef.h>

static _Thread_local void *task_context = NULL;

Expand All @@ -114,19 +124,30 @@ EOF

build() {
file=$1
$WASI_SDK_PATH/bin/clang crates/guest-rust/src/rt/$1.c \
-O -c -o crates/guest-rust/src/rt/$1.o -mcpu=mvp -fPIC
shift
$WASI_SDK_PATH/bin/clang crates/guest-rust/src/rt/$file.c \
-O -c -o crates/guest-rust/src/rt/$file.o -fPIC "$@"
# Remove the `producers` section. This appears to differ whether the host for
# clang is either macOS or Linux. Not needed here anyway, so discard it to help
# either host produce the same object.
wasm-tools strip -d producers ./crates/guest-rust/src/rt/$1.o \
-o ./crates/guest-rust/src/rt/$1.o
wasm-tools strip -d producers ./crates/guest-rust/src/rt/$file.o \
-o ./crates/guest-rust/src/rt/$file.o
}

build wit_bindgen_cabi_realloc
build wit_bindgen_cabi_wasip3
build wit_bindgen_cabi_realloc -mcpu=mvp
build wit_bindgen_cabi_wasip3 -mcpu=mvp
build wit_bindgen_cabi_wasip3_tls --target=wasm32-wasip3

# Catch accidentally compiling the task-local state as a plain global again.
if ! wasm-tools print crates/guest-rust/src/rt/wit_bindgen_cabi_wasip3_tls.o \
| grep -qF '(import "env" "__wasm_get_tls_base"'; then
echo "error: WASIp3 task context must use TLS" >&2
exit 1
fi

$WASI_SDK_PATH/bin/llvm-ar crus crates/guest-rust/src/rt/libwit_bindgen_cabi_realloc.a \
crates/guest-rust/src/rt/wit_bindgen_cabi_realloc.o
$WASI_SDK_PATH/bin/llvm-ar crus crates/guest-rust/src/rt/libwit_bindgen_cabi_wasip3.a \
crates/guest-rust/src/rt/wit_bindgen_cabi_wasip3.o
$WASI_SDK_PATH/bin/llvm-ar crus crates/guest-rust/src/rt/libwit_bindgen_cabi_wasip3_tls.a \
crates/guest-rust/src/rt/wit_bindgen_cabi_wasip3_tls.o
6 changes: 6 additions & 0 deletions crates/guest-rust/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ fn main() {
link_lib("cabi_realloc");
}
link_lib("cabi_wasip3");

// The no-std async runtime's task context uses WASIp3 TLS. Keep this
// separate from the MVP task helper needed by every wasm32 target.
if target_env == "p3" && !cfg!(feature = "std") {
link_lib("cabi_wasip3_tls");
}
Comment thread
macovedj marked this conversation as resolved.
}

fn link_lib(name: &str) {
Expand Down
Binary file modified crates/guest-rust/src/rt/libwit_bindgen_cabi_wasip3.a
Binary file not shown.
Binary file not shown.
10 changes: 0 additions & 10 deletions crates/guest-rust/src/rt/wit_bindgen_cabi_wasip3.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,3 @@ void *wasip3_task_set(void *ptr) {
WASIP3_TASK = ptr;
return ret;
}

static _Thread_local void *task_context = NULL;

void *wit_bindgen_0_62_0_get_task_context(void) {
return task_context;
}

void wit_bindgen_0_62_0_set_task_context(void *ptr) {
task_context = ptr;
}
Binary file modified crates/guest-rust/src/rt/wit_bindgen_cabi_wasip3.o
Binary file not shown.
13 changes: 13 additions & 0 deletions crates/guest-rust/src/rt/wit_bindgen_cabi_wasip3_tls.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// This file is generated by ./ci/rebuild-libwit-bindgen-cabi.sh

#include <stddef.h>

static _Thread_local void *task_context = NULL;

void *wit_bindgen_0_62_0_get_task_context(void) {
return task_context;
}

void wit_bindgen_0_62_0_set_task_context(void *ptr) {
task_context = ptr;
}
Binary file not shown.
Loading