Skip to content

Commit dabfa55

Browse files
Keep the current task per thread on WASIp3 (#1792)
`wasip3_task_set` stored the current task in a plain global. With cooperative threads, a thread that runs while another thread is suspended inside a poll reads and overwrites that thread's current task. On WASIp3, `wasip3_task_set` is now defined in the WASIp3 TLS helper, with `WASIP3_TASK` as `_Thread_local`, so each thread with its own TLS block has its own current task. Other targets keep the MVP build with a plain global, since compiling `_Thread_local` as MVP marks the object as incompatible with shared memory. `wasip3_task_set` is a weak symbol, so a component that also links an older version of `wit-bindgen` can still end up with that version's definition.
1 parent 064cb07 commit dabfa55

5 files changed

Lines changed: 25 additions & 1 deletion

File tree

‎ci/rebuild-libwit-bindgen-cabi.sh‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,11 @@ EOF
106106
# On WASIp3, wasi-libc switches TLS blocks between tasks. Compile this helper
107107
# for that target so `_Thread_local` remains TLS; compiling it as MVP would
108108
# silently turn `task_context` into a global shared by all tasks.
109+
#
110+
# This helper also defines `wasip3_task_set` for WASIp3, with `WASIP3_TASK` in
111+
# TLS so that each thread with its own TLS block has its own current task.
112+
# `wit_bindgen_cabi_wasip3.c` can't use `_Thread_local`, since compiling it as
113+
# MVP marks the object as incompatible with shared memory.
109114
cat >./crates/guest-rust/src/rt/wit_bindgen_cabi_wasip3_tls.c <<-EOF
110115
// This file is generated by $0
111116
@@ -131,6 +136,15 @@ void $wasip3_set(void *ptr) {
131136
}
132137
task_context = ptr;
133138
}
139+
140+
static _Thread_local void *WASIP3_TASK = NULL;
141+
142+
__attribute__((__weak__))
143+
void *wasip3_task_set(void *ptr) {
144+
void *ret = WASIP3_TASK;
145+
WASIP3_TASK = ptr;
146+
return ret;
147+
}
134148
EOF
135149

136150
build() {

‎crates/guest-rust/build.rs‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,10 @@ fn main() {
2727
if target_env == "p1" || target_env == "" {
2828
link_lib("cabi_realloc");
2929
}
30-
link_lib("cabi_wasip3");
3130
if target_env == "p3" {
3231
link_lib("cabi_wasip3_tls");
32+
} else {
33+
link_lib("cabi_wasip3");
3334
}
3435
}
3536

134 Bytes
Binary file not shown.

‎crates/guest-rust/src/rt/wit_bindgen_cabi_wasip3_tls.c‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,12 @@ void wit_bindgen_0_63_0_set_task_context(void *ptr) {
2222
}
2323
task_context = ptr;
2424
}
25+
26+
static _Thread_local void *WASIP3_TASK = NULL;
27+
28+
__attribute__((__weak__))
29+
void *wasip3_task_set(void *ptr) {
30+
void *ret = WASIP3_TASK;
31+
WASIP3_TASK = ptr;
32+
return ret;
33+
}
113 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)