From 8c0d524fcbdab525192703fe120df76f28151957 Mon Sep 17 00:00:00 2001 From: Sam Clegg Date: Mon, 8 Sep 2025 12:35:53 -0700 Subject: [PATCH] Remove global `currentModuleWeakSymbols` from dynamic linking code Using a global has led to at least one bug: #25214 --- src/lib/libdylink.js | 20 +++++++++---------- src/preamble.js | 7 +++++-- test/codesize/test_codesize_hello_dylink.json | 8 ++++---- .../test_codesize_hello_dylink_all.json | 4 ++-- 4 files changed, 21 insertions(+), 18 deletions(-) diff --git a/src/lib/libdylink.js b/src/lib/libdylink.js index 50020af24a7fc..87f8ea2ee1371 100644 --- a/src/lib/libdylink.js +++ b/src/lib/libdylink.js @@ -164,14 +164,15 @@ var LibraryDylink = { }, $GOT: {}, - $currentModuleWeakSymbols: '=new Set({{{ JSON.stringify(Array.from(WEAK_IMPORTS)) }}})', - // Create globals to each imported symbol. These are all initialized to zero - // and get assigned later in `updateGOT` + // Proxy handler used for GOT.mem and GOT.func imports. Each of these + // imports is fullfilled dynamically via the `get` method of this proxy + // handler. We abuse the `target` of the Proxy in order to pass the set of + // weak imports to the handler. $GOTHandler__internal: true, - $GOTHandler__deps: ['$GOT', '$currentModuleWeakSymbols'], + $GOTHandler__deps: ['$GOT'], $GOTHandler: { - get(obj, symName) { + get(weakImports, symName) { var rtn = GOT[symName]; if (!rtn) { #if DYLINK_DEBUG == 2 @@ -179,7 +180,7 @@ var LibraryDylink = { #endif rtn = GOT[symName] = new WebAssembly.Global({'value': '{{{ POINTER_WASM_TYPE }}}', 'mutable': true}, {{{ UNDEFINED_ADDR }}}); } - if (!currentModuleWeakSymbols.has(symName)) { + if (!weakImports.has(symName)) { // Any non-weak reference to a symbol marks it as `required`, which // enabled `reportUndefinedSymbols` to report undefined symbol errors // correctly. @@ -660,7 +661,6 @@ var LibraryDylink = { '$loadDynamicLibrary', '$getMemory', '$updateGOT', '$relocateExports', '$resolveGlobalSymbol', '$GOTHandler', '$getDylinkMetadata', '$alignMemory', - '$currentModuleWeakSymbols', '$updateTableMap', '$wasmTable', '$addOnPostCtor', @@ -799,10 +799,10 @@ var LibraryDylink = { } }; var proxy = new Proxy({}, proxyHandler); - currentModuleWeakSymbols = metadata.weakImports; + var GOTProxy = new Proxy(metadata.weakImports, GOTHandler); var info = { - 'GOT.mem': new Proxy({}, GOTHandler), - 'GOT.func': new Proxy({}, GOTHandler), + 'GOT.mem': GOTProxy, + 'GOT.func': GOTProxy, 'env': proxy, '{{{ WASI_MODULE_NAME }}}': proxy, }; diff --git a/src/preamble.js b/src/preamble.js index 2b09f06f615b9..c54ec203849bc 100644 --- a/src/preamble.js +++ b/src/preamble.js @@ -684,6 +684,9 @@ function getWasmImports() { #endif #endif // prepare imports +#if MAIN_MODULE || RELOCATABLE + var GOTProxyHandler = new Proxy(new Set({{{ JSON.stringify(Array.from(WEAK_IMPORTS)) }}}), GOTHandler); +#endif var imports = { #if MINIFY_WASM_IMPORTED_MODULES 'a': wasmImports, @@ -692,8 +695,8 @@ function getWasmImports() { '{{{ WASI_MODULE_NAME }}}': wasmImports, #endif // MINIFY_WASM_IMPORTED_MODULES #if MAIN_MODULE || RELOCATABLE - 'GOT.mem': new Proxy(wasmImports, GOTHandler), - 'GOT.func': new Proxy(wasmImports, GOTHandler), + 'GOT.mem': GOTProxyHandler, + 'GOT.func': GOTProxyHandler, #endif }; #if SPLIT_MODULE diff --git a/test/codesize/test_codesize_hello_dylink.json b/test/codesize/test_codesize_hello_dylink.json index fc8649404fd3b..565c180431299 100644 --- a/test/codesize/test_codesize_hello_dylink.json +++ b/test/codesize/test_codesize_hello_dylink.json @@ -1,10 +1,10 @@ { - "a.out.js": 26552, - "a.out.js.gz": 11345, + "a.out.js": 26546, + "a.out.js.gz": 11349, "a.out.nodebug.wasm": 17761, "a.out.nodebug.wasm.gz": 9003, - "total": 44313, - "total_gz": 20348, + "total": 44307, + "total_gz": 20352, "sent": [ "__syscall_stat64", "emscripten_resize_heap", diff --git a/test/codesize/test_codesize_hello_dylink_all.json b/test/codesize/test_codesize_hello_dylink_all.json index 09de86abc7a59..0c2e7110427d1 100644 --- a/test/codesize/test_codesize_hello_dylink_all.json +++ b/test/codesize/test_codesize_hello_dylink_all.json @@ -1,7 +1,7 @@ { - "a.out.js": 245483, + "a.out.js": 245475, "a.out.nodebug.wasm": 573783, - "total": 819266, + "total": 819258, "sent": [ "IMG_Init", "IMG_Load",