Skip to content

Commit 960936f

Browse files
authored
gh-128627: Emscripten: Fix address calculation for wasm-gc trampoline (#128782)
Modifies the memory calculation to divide the entire memory address by 4, not just the base address.
1 parent 298dda5 commit 960936f

File tree

3 files changed

+9
-11
lines changed

3 files changed

+9
-11
lines changed

Diff for: Python/emscripten_trampoline.c

+7-9
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,9 @@ EM_JS(CountArgsFunc, _PyEM_GetCountArgsPtr, (), {
2626
// Binary module for the checks. It has to be done in web assembly because
2727
// clang/llvm have no support yet for the reference types yet. In fact, the wasm
2828
// binary toolkit doesn't yet support the ref.test instruction either. To
29-
// convert the following module to the binary, my approach is to find and
30-
// replace "ref.test $type" -> "drop i32.const n" on the source text. This
31-
// results in the bytes "0x1a, 0x41, n" where we need the bytes "0xfb, 0x14, n"
32-
// so doing a find and replace on the output from "0x1a, 0x41" -> "0xfb, 0x14"
33-
// gets us the output we need.
29+
// convert the following textual wasm to a binary, you can build wabt from this
30+
// branch: https://github.com/WebAssembly/wabt/pull/2529 and then use that
31+
// wat2wasm binary.
3432
//
3533
// (module
3634
// (type $type0 (func (param) (result i32)))
@@ -154,15 +152,15 @@ addOnPreRun(() => {
154152
let ptr = 0;
155153
try {
156154
const mod = new WebAssembly.Module(code);
157-
const inst = new WebAssembly.Instance(mod, {e: {t: wasmTable}});
155+
const inst = new WebAssembly.Instance(mod, { e: { t: wasmTable } });
158156
ptr = addFunction(inst.exports.f);
159-
} catch(e) {
157+
} catch (e) {
160158
// If something goes wrong, we'll null out _PyEM_CountFuncParams and fall
161159
// back to the JS trampoline.
162160
}
163161
Module._PyEM_CountArgsPtr = ptr;
164-
const offset = HEAP32[__PyEM_EMSCRIPTEN_COUNT_ARGS_OFFSET/4];
165-
HEAP32[__PyRuntime/4 + offset] = ptr;
162+
const offset = HEAP32[__PyEM_EMSCRIPTEN_COUNT_ARGS_OFFSET / 4];
163+
HEAP32[(__PyRuntime + offset) / 4] = ptr;
166164
});
167165
);
168166

Diff for: configure

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: configure.ac

+1-1
Original file line numberDiff line numberDiff line change
@@ -2369,7 +2369,7 @@ AS_CASE([$ac_sys_system],
23692369
dnl Include file system support
23702370
AS_VAR_APPEND([LINKFORSHARED], [" -sFORCE_FILESYSTEM -lidbfs.js -lnodefs.js -lproxyfs.js -lworkerfs.js"])
23712371
AS_VAR_APPEND([LINKFORSHARED], [" -sEXPORTED_RUNTIME_METHODS=FS,callMain,ENV"])
2372-
AS_VAR_APPEND([LINKFORSHARED], [" -sEXPORTED_FUNCTIONS=_main,_Py_Version"])
2372+
AS_VAR_APPEND([LINKFORSHARED], [" -sEXPORTED_FUNCTIONS=_main,_Py_Version,__PyRuntime,__PyEM_EMSCRIPTEN_COUNT_ARGS_OFFSET"])
23732373
AS_VAR_APPEND([LINKFORSHARED], [" -sSTACK_SIZE=5MB"])
23742374
23752375
AS_VAR_IF([enable_wasm_dynamic_linking], [yes], [

0 commit comments

Comments
 (0)