Skip to content

Commit 2b9d22d

Browse files
authored
Fix STACK_OVERFLOW_CHECK (ASSERTIONS=2) + dynamic linking (#13062)
Because the stack bounds are stored per-module wasm globals we need to call `__set_stack_limits` for each module. See: #13040
1 parent aeaf110 commit 2b9d22d

File tree

3 files changed

+25
-11
lines changed

3 files changed

+25
-11
lines changed

src/library_dylink.js

+18-4
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,23 @@ var LibraryDylink = {
6161
}
6262
},
6363

64-
$updateGOT__deps: ['$GOT'],
64+
$isInternalSym: function(symName) {
65+
// TODO: find a way to mark these in the binary or avoid exporting them.
66+
return [
67+
'__cpp_exception',
68+
'__wasm_apply_data_relocs',
69+
'__dso_handle',
70+
'__set_stack_limits'
71+
].indexOf(symName) != -1;
72+
},
73+
74+
$updateGOT__deps: ['$GOT', '$isInternalSym'],
6575
$updateGOT: function(exports) {
6676
#if DYLINK_DEBUG
6777
err("updateGOT: " + Object.keys(exports).length);
6878
#endif
6979
for (var symName in exports) {
70-
if (symName == '__cpp_exception' || symName == '__dso_handle' || symName == '__wasm_apply_relocs') {
80+
if (isInternalSym(symName)) {
7181
continue;
7282
}
7383

@@ -410,6 +420,10 @@ var LibraryDylink = {
410420
if (!flags.allowUndefined) {
411421
reportUndefinedSymbols();
412422
}
423+
#if STACK_OVERFLOW_CHECK >= 2
424+
425+
moduleExports['__set_stack_limits'](_emscripten_stack_get_base(), _emscripten_stack_get_end());
426+
#endif
413427
// initialize the module
414428
var init = moduleExports['__post_instantiate'];
415429
if (init) {
@@ -468,7 +482,7 @@ var LibraryDylink = {
468482
// If a library was already loaded, it is not loaded a second time. However
469483
// flags.global and flags.nodelete are handled every time a load request is made.
470484
// Once a library becomes "global" or "nodelete", it cannot be removed or unloaded.
471-
$loadDynamicLibrary__deps: ['$LDSO', '$loadWebAssemblyModule', '$asmjsMangle', '$fetchBinary'],
485+
$loadDynamicLibrary__deps: ['$LDSO', '$loadWebAssemblyModule', '$asmjsMangle', '$fetchBinary', '$isInternalSym'],
472486
$loadDynamicLibrary: function(lib, flags) {
473487
if (lib == '__main__' && !LDSO.loadedLibNames[lib]) {
474488
LDSO.loadedLibs[-1] = {
@@ -577,7 +591,7 @@ var LibraryDylink = {
577591
else {
578592
var curr = Module[sym], next = libModule[sym];
579593
// don't warn on functions - might be odr, linkonce_odr, etc.
580-
if (!(typeof curr === 'function' && typeof next === 'function')) {
594+
if (!(typeof curr === 'function' && typeof next === 'function') && !isInternalSym(sym)) {
581595
err("warning: symbol '" + sym + "' from '" + lib + "' already exists (duplicate symbol? or weak linking, which isn't supported yet?)");
582596
}
583597
}

src/preamble.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,7 @@ function initRuntime() {
381381
#endif
382382
runtimeInitialized = true;
383383
#if STACK_OVERFLOW_CHECK >= 2
384-
Module['___set_stack_limits'](_emscripten_stack_get_base(), _emscripten_stack_get_end());
384+
___set_stack_limits(_emscripten_stack_get_base(), _emscripten_stack_get_end());
385385
#endif
386386
{{{ getQuoted('ATINITS') }}}
387387
callRuntimeCallbacks(__ATINIT__);

tests/test_core.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -3809,13 +3809,13 @@ def test():
38093809
''', expected=['new main\nnew side\n', 'new side\nnew main\n'])
38103810
test()
38113811

3812+
print('check warnings')
3813+
self.set_setting('ASSERTIONS', 2)
3814+
test()
3815+
self.run_js('src.js')
38123816
# TODO: this in wasm
3813-
if self.get_setting('ASSERTIONS') == 1 and not self.is_wasm():
3814-
print('check warnings')
3815-
self.set_setting('ASSERTIONS', 2)
3816-
test()
3817-
full = self.run_js('src.js')
3818-
self.assertNotContained("trying to dynamically load symbol '__ZN5ClassC2EPKc' (from 'liblib.so') that already exists", full)
3817+
# full = self.run_js('src.js')
3818+
# self.assertNotContained('already exists', full)
38193819

38203820
@needs_dlfcn
38213821
def test_dylink_i64(self):

0 commit comments

Comments
 (0)