From 54e6fd880b6488624ea52e2c4d4e3763eda1489b Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Thu, 15 Oct 2026 14:40:25 -0700 Subject: [PATCH] Handle wasip3 task-specific information differently This commit is the dual of WebAssembly/wasi-libc#884 to handle the changes happening there. Specifically on wasip3 using TLS is going to stop working in wasi-sdk once that wasi-libc PR lands. The rationale for this is that TLS wants to be the same across all component model tasks, not unique per-task. This change detects whether wasi-sdk-35 or later is in use and invokes the wasi-libc-defined API in that situation. This means that wit-bindgen will continue to work as-is with wasi-sdk-34 and after this commit it'll additionally be compatible with wasi-sdk-35 as well. Eventually in the future wasi-sdk-35 can be unconditionally required at which point the C shims here can go away entirely. --- ci/rebuild-libwit-bindgen-cabi.sh | 11 +++++++++ crates/guest-rust/build.rs | 5 +--- crates/guest-rust/src/rt/async_support.rs | 22 ++++++------------ .../src/rt/libwit_bindgen_cabi_wasip3_tls.a | Bin 766 -> 1010 bytes .../src/rt/wit_bindgen_cabi_wasip3_tls.c | 11 +++++++++ .../src/rt/wit_bindgen_cabi_wasip3_tls.o | Bin 461 -> 706 bytes 6 files changed, 30 insertions(+), 19 deletions(-) diff --git a/ci/rebuild-libwit-bindgen-cabi.sh b/ci/rebuild-libwit-bindgen-cabi.sh index 6e2251819..45b5f2a34 100755 --- a/ci/rebuild-libwit-bindgen-cabi.sh +++ b/ci/rebuild-libwit-bindgen-cabi.sh @@ -113,11 +113,22 @@ cat >./crates/guest-rust/src/rt/wit_bindgen_cabi_wasip3_tls.c <<-EOF static _Thread_local void *task_context = NULL; +__attribute__((weak)) +extern void *__wasilibc_task_context_get(void); +__attribute__((weak)) +extern void __wasilibc_task_context_set(void*); + void *$wasip3_get(void) { + if (__wasilibc_task_context_get) + return __wasilibc_task_context_get(); return task_context; } void $wasip3_set(void *ptr) { + if (__wasilibc_task_context_set) { + __wasilibc_task_context_set(ptr); + return; + } task_context = ptr; } EOF diff --git a/crates/guest-rust/build.rs b/crates/guest-rust/build.rs index 0793c250d..96a42f529 100644 --- a/crates/guest-rust/build.rs +++ b/crates/guest-rust/build.rs @@ -18,10 +18,7 @@ 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") { + if target_env == "p3" { link_lib("cabi_wasip3_tls"); } } diff --git a/crates/guest-rust/src/rt/async_support.rs b/crates/guest-rust/src/rt/async_support.rs index 72ad6f2a8..bb7b7aee2 100644 --- a/crates/guest-rust/src/rt/async_support.rs +++ b/crates/guest-rust/src/rt/async_support.rs @@ -579,22 +579,14 @@ mod task_state { } } -#[cfg(all(target_env = "p3", feature = "std"))] -mod task_state { - std::thread_local!(static TASK_STATE: std::cell::Cell<*mut u8> = std::cell::Cell::new(std::ptr::null_mut())); - - pub fn get() -> *mut u8 { - TASK_STATE.with(|c| c.get()) - } - - pub unsafe fn set(value: *mut u8) { - TASK_STATE.with(|c| c.set(value)) - } -} - -#[cfg(all(target_env = "p3", not(feature = "std")))] +// TODO: in the future once wasi-sdk-35 is released with +// WebAssembly/wasi-libc#884 this `task_state` module can unconditionally work +// with `__wasilibc_task_context_{get,set}`. Until that time though detecting +// wasi-sdk-35 is done with with a weak symbol import which is not possible to +// write on stable Rust right now. +#[cfg(target_env = "p3")] mod wasip3_context; -#[cfg(all(target_env = "p3", not(feature = "std")))] +#[cfg(target_env = "p3")] mod task_state { pub fn get() -> *mut u8 { diff --git a/crates/guest-rust/src/rt/libwit_bindgen_cabi_wasip3_tls.a b/crates/guest-rust/src/rt/libwit_bindgen_cabi_wasip3_tls.a index d4ceedf74d52bfd179c8035448c9937e348c8b38..ad17cc33d6b1bb6f46a2435f4dd6d6d8d133860c 100644 GIT binary patch delta 421 zcmaKo&q~8U5XNVA6F0V%q=zaJDnct!(BjEn1QiV)tKe0aHm+rDx{);gJ5~DxN-pYC z_*mWqA3)sANkKZNnQwl4-!R|y7kh6aZEtSn0oJ!akWry-T+U;uL^tI_F-+2X0%&BP z^V{x7#dn6QvD%vA`<; z=dg3qGV-)II>!IFv%h!gL=94^@2?=1j>!s;-Nu712{VZ*9=vN znpy^LV)lm1UvX*v4Viw3|onyV-^CqG%w b+26&L1!SHm7ZU@6IHRa6)8r`T8H`*2qX9B+ diff --git a/crates/guest-rust/src/rt/wit_bindgen_cabi_wasip3_tls.c b/crates/guest-rust/src/rt/wit_bindgen_cabi_wasip3_tls.c index e26b0369b..b5ca51f88 100644 --- a/crates/guest-rust/src/rt/wit_bindgen_cabi_wasip3_tls.c +++ b/crates/guest-rust/src/rt/wit_bindgen_cabi_wasip3_tls.c @@ -4,10 +4,21 @@ static _Thread_local void *task_context = NULL; +__attribute__((weak)) +extern void *__wasilibc_task_context_get(void); +__attribute__((weak)) +extern void __wasilibc_task_context_set(void*); + void *wit_bindgen_0_63_0_get_task_context(void) { + if (__wasilibc_task_context_get) + return __wasilibc_task_context_get(); return task_context; } void wit_bindgen_0_63_0_set_task_context(void *ptr) { + if (__wasilibc_task_context_set) { + __wasilibc_task_context_set(ptr); + return; + } task_context = ptr; } diff --git a/crates/guest-rust/src/rt/wit_bindgen_cabi_wasip3_tls.o b/crates/guest-rust/src/rt/wit_bindgen_cabi_wasip3_tls.o index 7a38090442e746e95507dc58dfac1dd2f9840ce7..132ef77b92936c27924e7ffc5500569688cf7704 100644 GIT binary patch delta 407 zcmaKo&q~8U5XNVA6F0P#q+k#U6+ufCv^Vi0sA%w51kWyMT+7;YBT4*ss`d#QyomTT z-hBYyz?)AXxSNxLbWSth{P@0M-szh?UD*9ZMuoa@IZvb#QN~ANlxFt?(8vMjw^1$= z*^hY<HwIA3PLz@p`AJ7@xUh!fKI{TToYEUWxjW6&h_6rucXbvm_7Bx&3| e>>Y)c!{!iNghk@FD6mZ%c5K)qen_9T@%aaSe0Vbe delta 189 zcmX@adX{;Dh@=q{b8220e|&sSW?pJyQG9M{ZhlcE69ePK0BJ>LAkD+b$fe07#30ZB z1PqQ~GE0Mrfm;M9G}(huPILkzJ5Y0WW?niI$5W6Z76ul^$<>Ui5&{g2O-wu`iN)FR z$@zIDsTCy*AW5dlyBSq1Sf_#1G79OHBo!CyAuD2HVvy!4O3len)^qlEab*FSC(6ad Pz#z^jD$6uki)jV`S@|!W