diff --git a/crates/spidermonkey-embedding-splicer/src/splice.rs b/crates/spidermonkey-embedding-splicer/src/splice.rs index 536f1874..2a84b544 100644 --- a/crates/spidermonkey-embedding-splicer/src/splice.rs +++ b/crates/spidermonkey-embedding-splicer/src/splice.rs @@ -475,9 +475,17 @@ fn synthesize_import_functions( // These functions retrieve the corresponding type // from a JS::HandleValue - // All except for the BigInt one are trivial and thus - // do not require regular explicit template extraction - // unless there are major ABI changes in Spidermonkey + // Extract core integers through SpiderMonkey conversion helpers + // (https://searchfox.org/mozilla-central/source/js/public/Conversions.h, + // ECMA-262 ToInt32: https://tc39.es/ecma262/#sec-toint32) rather than + // loading the raw low 32 bits of a `JS::Value` inline. SpiderMonkey stores + // `u32` values above `INT32_MAX` as IEEE-754 doubles, so an inline + // `i64.load` + `i32.wrap_i64` reads double mantissa bits and silently + // corrupts every value in `2147483648..4294967295`. + let coreabi_from_int32 = module + .exports + .get_export_id_by_name("coreabi_from_int32".to_string()) + .context("engine is missing coreabi_from_int32; rebuild the embedding")?; let coreabi_from_bigint64 = module .exports .get_export_id_by_name("coreabi_from_bigint64".to_string()) @@ -597,13 +605,7 @@ fn synthesize_import_functions( func.i32_add(); match arg { CoreTy::I32 => { - func.i64_load(MemArg { - align: 3, - max_align: 0, - offset: 0, - memory, - }); - func.i32_wrap_i64(); + func.call(get_export_fid(module, &coreabi_from_int32)); } CoreTy::I64 => { func.call(get_export_fid(module, &coreabi_from_bigint64)); @@ -855,6 +857,7 @@ fn synthesize_import_functions( // remove unnecessary exports module.exports.delete(coreabi_to_bigint64); module.exports.delete(coreabi_from_bigint64); + module.exports.delete(coreabi_from_int32); module.exports.delete(coreabi_get_import.unwrap()); for id in coreabi_sample_ids { module.exports.delete(id); diff --git a/embedding/embedding.cpp b/embedding/embedding.cpp index fc17ef11..5622b848 100644 --- a/embedding/embedding.cpp +++ b/embedding/embedding.cpp @@ -66,9 +66,27 @@ using componentize::embedding::Runtime; // These functions are used both internally and also exported for use directly // by the splicer codegen +// Convert any JavaScript Number (Int32 or double-boxed u32) to its 32-bit +// two's-complement core Wasm i32 bit pattern via JS::ToInt32 +// (ECMA-262 ToInt32: https://tc39.es/ecma262/#sec-toint32, +// SpiderMonkey: https://searchfox.org/mozilla-central/source/js/public/Conversions.h). +// Calling handle.toInt32() instead reads raw double storage bits whenever a +// u32 value above INT32_MAX is stored as a SpiderMonkey double. +__attribute__((noinline, export_name("coreabi_from_int32"))) int32_t +from_int32(JS::MutableHandleValue handle) { + return JS::ToInt32(handle.toNumber()); +} + __attribute__((noinline, export_name("coreabi_from_bigint64"))) int64_t from_bigint64(JS::MutableHandleValue handle) { JS::BigInt *arg0 = handle.toBigInt(); + // Try signed s64 before unsigned u64 because a core Wasm i64 carries untyped + // bits and canonical bindings supply either representation. Checking only + // BigIntIsUint64 aborts the component on every negative s64 value. + int64_t arg0_int64; + if (JS::detail::BigIntIsInt64(arg0, &arg0_int64)) { + return arg0_int64; + } uint64_t arg0_uint64; if (!JS::detail::BigIntIsUint64(arg0, &arg0_uint64)) { Runtime.engine->abort( @@ -94,7 +112,7 @@ to_bigint64(JSContext *cx, int64_t val) { __attribute__((export_name("coreabi_sample_i32"))) bool CoreAbiSampleI32(JSContext *cx, unsigned argc, JS::Value *vp) { JS::CallArgs args = JS::CallArgsFromVp(argc, vp); - int32_t arg0 = static_cast(args[0].toInt32()); + int32_t arg0 = from_int32(args[0]); args.rval().setInt32(arg0 * 32771); return true; } @@ -321,12 +339,10 @@ __attribute__((export_name("call"))) uint32_t call(uint32_t fn_idx, Runtime.free_list.push_back(retptr); switch (fn->ret.value()) { case CoreVal::I32: - *((uint32_t *)retptr) = ret.toInt32(); + *((uint32_t *)retptr) = from_int32(&ret); break; case CoreVal::I64: - if (!JS::detail::BigIntIsUint64(ret.toBigInt(), (uint64_t *)retptr)) { - abort(); - } + *((int64_t *)retptr) = from_bigint64(&ret); break; case CoreVal::F32: *((float *)retptr) = ret.isInt32() ? static_cast(ret.toInt32()) diff --git a/test/cases/integer-boundaries/integers.js b/test/cases/integer-boundaries/integers.js new file mode 100644 index 00000000..2ae57dae --- /dev/null +++ b/test/cases/integer-boundaries/integers.js @@ -0,0 +1,7 @@ +export const u32 = x => x; +export const s32 = x => x; +export const u64 = x => x; +export const s64 = x => x; +export const pair = (x, y) => [x, y]; +export const directU32 = () => 4294967295; +export const directS64 = () => -9223372036854775808n; diff --git a/test/cases/integer-boundaries/source.js b/test/cases/integer-boundaries/source.js new file mode 100644 index 00000000..1f92f4d1 --- /dev/null +++ b/test/cases/integer-boundaries/source.js @@ -0,0 +1,10 @@ +import * as host from 'local:integer-boundaries/integers'; +export const integers = { + u32: x => host.u32(x), + s32: x => host.s32(x), + u64: x => host.u64(x), + s64: x => host.s64(x), + pair: (x, y) => host.pair(x, y), + directU32: () => 4294967295, + directS64: () => -9223372036854775808n, +}; diff --git a/test/cases/integer-boundaries/test.js b/test/cases/integer-boundaries/test.js new file mode 100644 index 00000000..7b2bfddf --- /dev/null +++ b/test/cases/integer-boundaries/test.js @@ -0,0 +1,27 @@ +import { strictEqual, deepStrictEqual } from 'node:assert'; +export function test({ integers }) { + for (const x of [0, 1, 2147483647, 2147483648, 4294967295]) { + strictEqual(integers.u32(x), x); + } + for (const x of [-2147483648, -1, 0, 2147483647]) { + strictEqual(integers.s32(x), x); + } + for (const x of [ + 0n, + 1n, + 9223372036854775807n, + 9223372036854775808n, + 18446744073709551615n, + ]) { + strictEqual(integers.u64(x), x); + } + for (const x of [-9223372036854775808n, -1n, 0n, 9223372036854775807n]) { + strictEqual(integers.s64(x), x); + } + strictEqual(integers.directU32(), 4294967295); + strictEqual(integers.directS64(), -9223372036854775808n); + deepStrictEqual(integers.pair(-9223372036854775808n, 4294967295), [ + -9223372036854775808n, + 4294967295, + ]); +} diff --git a/test/cases/integer-boundaries/world.wit b/test/cases/integer-boundaries/world.wit new file mode 100644 index 00000000..1e753350 --- /dev/null +++ b/test/cases/integer-boundaries/world.wit @@ -0,0 +1,14 @@ +package local:integer-boundaries; +interface integers { + %u32: func(x: u32) -> u32; + %s32: func(x: s32) -> s32; + %u64: func(x: u64) -> u64; + %s64: func(x: s64) -> s64; + pair: func(x: s64, y: u32) -> tuple; + direct-u32: func() -> u32; + direct-s64: func() -> s64; +} +world the-world { + import integers; + export integers; +}