Skip to content

Commit b0f7582

Browse files
authored
Remove global currentModuleWeakSymbols from dynamic linking code (#25786)
Using a global has led to at least one bug: #25214
1 parent 7bd4abc commit b0f7582

File tree

4 files changed

+21
-18
lines changed

4 files changed

+21
-18
lines changed

src/lib/libdylink.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -164,22 +164,23 @@ var LibraryDylink = {
164164
},
165165

166166
$GOT: {},
167-
$currentModuleWeakSymbols: '=new Set({{{ JSON.stringify(Array.from(WEAK_IMPORTS)) }}})',
168167

169-
// Create globals to each imported symbol. These are all initialized to zero
170-
// and get assigned later in `updateGOT`
168+
// Proxy handler used for GOT.mem and GOT.func imports. Each of these
169+
// imports is fullfilled dynamically via the `get` method of this proxy
170+
// handler. We abuse the `target` of the Proxy in order to pass the set of
171+
// weak imports to the handler.
171172
$GOTHandler__internal: true,
172-
$GOTHandler__deps: ['$GOT', '$currentModuleWeakSymbols'],
173+
$GOTHandler__deps: ['$GOT'],
173174
$GOTHandler: {
174-
get(obj, symName) {
175+
get(weakImports, symName) {
175176
var rtn = GOT[symName];
176177
if (!rtn) {
177178
#if DYLINK_DEBUG == 2
178179
dbg(`new GOT entry: ${symName}`);
179180
#endif
180181
rtn = GOT[symName] = new WebAssembly.Global({'value': '{{{ POINTER_WASM_TYPE }}}', 'mutable': true}, {{{ UNDEFINED_ADDR }}});
181182
}
182-
if (!currentModuleWeakSymbols.has(symName)) {
183+
if (!weakImports.has(symName)) {
183184
// Any non-weak reference to a symbol marks it as `required`, which
184185
// enabled `reportUndefinedSymbols` to report undefined symbol errors
185186
// correctly.
@@ -660,7 +661,6 @@ var LibraryDylink = {
660661
'$loadDynamicLibrary', '$getMemory', '$updateGOT',
661662
'$relocateExports', '$resolveGlobalSymbol', '$GOTHandler',
662663
'$getDylinkMetadata', '$alignMemory',
663-
'$currentModuleWeakSymbols',
664664
'$updateTableMap',
665665
'$wasmTable',
666666
'$addOnPostCtor',
@@ -799,10 +799,10 @@ var LibraryDylink = {
799799
}
800800
};
801801
var proxy = new Proxy({}, proxyHandler);
802-
currentModuleWeakSymbols = metadata.weakImports;
802+
var GOTProxy = new Proxy(metadata.weakImports, GOTHandler);
803803
var info = {
804-
'GOT.mem': new Proxy({}, GOTHandler),
805-
'GOT.func': new Proxy({}, GOTHandler),
804+
'GOT.mem': GOTProxy,
805+
'GOT.func': GOTProxy,
806806
'env': proxy,
807807
'{{{ WASI_MODULE_NAME }}}': proxy,
808808
};

src/preamble.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -684,6 +684,9 @@ function getWasmImports() {
684684
#endif
685685
#endif
686686
// prepare imports
687+
#if MAIN_MODULE || RELOCATABLE
688+
var GOTProxyHandler = new Proxy(new Set({{{ JSON.stringify(Array.from(WEAK_IMPORTS)) }}}), GOTHandler);
689+
#endif
687690
var imports = {
688691
#if MINIFY_WASM_IMPORTED_MODULES
689692
'a': wasmImports,
@@ -692,8 +695,8 @@ function getWasmImports() {
692695
'{{{ WASI_MODULE_NAME }}}': wasmImports,
693696
#endif // MINIFY_WASM_IMPORTED_MODULES
694697
#if MAIN_MODULE || RELOCATABLE
695-
'GOT.mem': new Proxy(wasmImports, GOTHandler),
696-
'GOT.func': new Proxy(wasmImports, GOTHandler),
698+
'GOT.mem': GOTProxyHandler,
699+
'GOT.func': GOTProxyHandler,
697700
#endif
698701
};
699702
#if SPLIT_MODULE

test/codesize/test_codesize_hello_dylink.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{
2-
"a.out.js": 26552,
3-
"a.out.js.gz": 11345,
2+
"a.out.js": 26546,
3+
"a.out.js.gz": 11349,
44
"a.out.nodebug.wasm": 17761,
55
"a.out.nodebug.wasm.gz": 9003,
6-
"total": 44313,
7-
"total_gz": 20348,
6+
"total": 44307,
7+
"total_gz": 20352,
88
"sent": [
99
"__syscall_stat64",
1010
"emscripten_resize_heap",

test/codesize/test_codesize_hello_dylink_all.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
2-
"a.out.js": 245483,
2+
"a.out.js": 245475,
33
"a.out.nodebug.wasm": 573783,
4-
"total": 819266,
4+
"total": 819258,
55
"sent": [
66
"IMG_Init",
77
"IMG_Load",

0 commit comments

Comments
 (0)