JSPI task pointers are truncated to u32 on wasm64
The JSPI executor in js-sys 0.3.106 (wasm-bindgen 0.2.129) narrows Rc<SpawnedTask> pointers to u32 in both scheduling paths:
The __wbg_jspi_task_poll trampoline receives a u32 and calls Rc::from_raw(task as *const SpawnedTask).
On wasm64, a task pointer at or above 0x1_0000_0000 loses its upper bits. Reconstructing the Rc then violates Rc::from_raw's safety requirements. This path handles tasks spawned within a JSPI context and is not gated to wasm32.
Reduced repro
This demonstrates the pointer truncation on native 64-bit Rust, not an end-to-end JSPI failure. It uses Rc::as_ptr to retain ownership and never dereferences the truncated pointer.
use std::rc::Rc;
fn main() {
assert_eq!(usize::BITS, 64, "run on a 64-bit target");
let task = Rc::new(42_u8);
let original = Rc::as_ptr(&task);
assert!(
original as usize > u32::MAX as usize,
"this run needs an allocation above the 32-bit address range"
);
let transported = original as u32;
let restored = transported as *const u8;
println!("original: {original:p}");
println!("restored: {restored:p}");
assert_eq!(restored, original, "task pointer was truncated");
}Run with rustc repro.rs -o repro && ./repro.
Observed on x86_64; the final assertion fails:
original: 0x5644ed517ce0
restored: 0xed517ce0
assertion `left == right` failed: task pointer was truncatedThe pointer should retain its width through the scheduling intrinsics, JS glue, and poll trampoline, or this path should reject wasm64.
Proposed fix
Use u64 consistently for the internal task token in futures/jspi.rs:
- Change both spawn intrinsic parameters from
u32tou64. - Change both
Rc::into_raw(...) as u32casts toas u64. - Change
__wbg_jspi_task_poll's parameter tou64.
The existing Rc::from_raw(task as *const SpawnedTask) can remain unchanged.
Simply replacing u32 with usize would introduce an ABI mismatch: wasm-bindgen lowers wasm64 usize imports through f64/JavaScript Number, while the raw poll export would expect i64/BigInt. Using u64 gives both sides a matching, lossless i64/BigInt ABI.
The existing JSPI glue forwards the token without additional conversion. The trade-off is that wasm32 would also use BigInt for this token.
Validation should cover existing wasm32 JSPI tests and a round-trip above u32::MAX through the generated scheduling/trampoline ABI, including initial scheduling and rescheduling.
This proposal is based on source inspection; I haven't tested the patch..
Source: wasm-bindgen/wasm-bindgen