Skip to content

Commit a6bd8e1

Browse files
authored
wasm: check Emscripten version when using JSPI (#8627)
Tokio's JSPI parking relies on timer and immediate cleanup fixes that landed in Emscripten 6.0.10. Thus, check the Emscripten version via `uname` when JSPI is enabled and panic if it is older than 6.0.10.
1 parent 9dd749d commit a6bd8e1

2 files changed

Lines changed: 80 additions & 4 deletions

File tree

‎tokio/src/lib.rs‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -483,7 +483,8 @@
483483
//!
484484
//! When the build links WebAssembly JavaScript Promise Integration (JSPI), a
485485
//! wait that would block suspends on the host event loop rather than blocking.
486-
//! Builds without JSPI will spin on timed parks, and panic otherwise.
486+
//! This requires Emscripten 6.0.10 or later. Builds without JSPI will spin on
487+
//! timed parks, and panic otherwise.
487488
//!
488489
//! Suspension requires the current export to have been wrapped with
489490
//! `WebAssembly.promising`. A wait from any other activation throws

‎tokio/src/runtime/jspi.rs‎

Lines changed: 78 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,28 @@ type Promise = *mut c_void;
2020

2121
const EM_PROMISE_FULFILL: i32 = 0;
2222

23+
/// Minimum Emscripten version required for JSPI (`emscripten#27720` fixed
24+
/// `emscripten_clear_timeout` and `emscripten_clear_immediate` keep-alive
25+
/// handling in 6.0.10).
26+
const MIN_EMSCRIPTEN_VERSION: (u32, u32, u32) = (6, 0, 10);
27+
28+
#[repr(C)]
29+
struct UtsName {
30+
sysname: [u8; 65],
31+
nodename: [u8; 65],
32+
release: [u8; 65],
33+
version: [u8; 65],
34+
machine: [u8; 65],
35+
domainname: [u8; 65],
36+
}
37+
2338
extern "C" {
2439
/// Reports the `ASYNCIFY` build mode: 0 = none, 1 = legacy `Asyncify`,
2540
/// 2 = JSPI. Only mode 2 supports suspension.
2641
fn emscripten_has_asyncify() -> i32;
2742

43+
fn uname(buf: *mut UtsName) -> i32;
44+
2845
fn emscripten_promise_create() -> Promise;
2946
fn emscripten_promise_destroy(promise: Promise);
3047
fn emscripten_promise_resolve(promise: Promise, result: i32, value: *mut c_void);
@@ -62,11 +79,69 @@ extern "C-unwind" {
6279
fn emscripten_promise_await(promise: Promise) -> SettledResult;
6380
}
6481

82+
#[derive(Copy, Clone)]
83+
enum JspiState {
84+
Disabled,
85+
Enabled,
86+
UnsupportedVersion(u32, u32, u32),
87+
}
88+
89+
fn emscripten_version() -> Option<(u32, u32, u32)> {
90+
let mut uts = UtsName {
91+
sysname: [0; 65],
92+
nodename: [0; 65],
93+
release: [0; 65],
94+
version: [0; 65],
95+
machine: [0; 65],
96+
domainname: [0; 65],
97+
};
98+
// SAFETY: `uts` is a valid `struct utsname` buffer. Emscripten's `uname`
99+
// populates `uts.release` with `"__EMSCRIPTEN_MAJOR__.__EMSCRIPTEN_MINOR__.__EMSCRIPTEN_TINY__"`.
100+
if unsafe { uname(&mut uts) } != 0 {
101+
return None;
102+
}
103+
let len = uts
104+
.release
105+
.iter()
106+
.position(|&b| b == 0)
107+
.unwrap_or(uts.release.len());
108+
let release = std::str::from_utf8(&uts.release[..len]).ok()?;
109+
let mut parts = release.split('.');
110+
let major = parts.next()?.parse().ok()?;
111+
let minor = parts.next()?.parse().ok()?;
112+
let tiny = parts.next()?.parse().ok()?;
113+
if parts.next().is_some() {
114+
return None;
115+
}
116+
Some((major, minor, tiny))
117+
}
118+
119+
fn detect_jspi() -> JspiState {
120+
// SAFETY: an Emscripten libc query with no arguments and no side effects.
121+
if unsafe { emscripten_has_asyncify() } != 2 {
122+
return JspiState::Disabled;
123+
}
124+
match emscripten_version() {
125+
Some((major, minor, tiny)) if (major, minor, tiny) < MIN_EMSCRIPTEN_VERSION => {
126+
JspiState::UnsupportedVersion(major, minor, tiny)
127+
}
128+
// All existing versions use `major.minor.tiny`, so an unrecognised
129+
// format is assumed to be a newer version.
130+
_ => JspiState::Enabled,
131+
}
132+
}
133+
65134
/// Whether JSPI suspension is available: linked with `-sJSPI`.
66135
pub(crate) fn jspi_enabled() -> bool {
67-
static ENABLED: OnceLock<bool> = OnceLock::new();
68-
// SAFETY: an Emscripten libc query with no arguments and no side effects.
69-
*ENABLED.get_or_init(|| unsafe { emscripten_has_asyncify() == 2 })
136+
static STATE: OnceLock<JspiState> = OnceLock::new();
137+
match *STATE.get_or_init(detect_jspi) {
138+
JspiState::Disabled => false,
139+
JspiState::Enabled => true,
140+
JspiState::UnsupportedVersion(major, minor, tiny) => panic!(
141+
"cannot use JSPI on wasm32-unknown-emscripten with Emscripten \
142+
{major}.{minor}.{tiny}: Emscripten 6.0.10 or later is required"
143+
),
144+
}
70145
}
71146

72147
/// The promise a parked activation is suspended on, null while not parked.

0 commit comments

Comments
 (0)