Skip to content

Commit b674062

Browse files
authored
Fix -sWASM_WORKERS and -sSTACK_OVERFLOW_CHECK=2 and add a test (#16497)
* Fix -sWASM_WORKERS and -sSTACK_OVERFLOW_CHECK=2 and add a test. See #16496 * Update comment
1 parent 8f1ce84 commit b674062

File tree

4 files changed

+25
-12
lines changed

4 files changed

+25
-12
lines changed

src/library_wasm_worker.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,21 @@ mergeInto(LibraryManager.library, {
5555
assert(m['sz'] % 16 == 0);
5656
#endif
5757

58+
#if STACK_OVERFLOW_CHECK >= 2
59+
// _emscripten_wasm_worker_initialize() initializes the stack for this Worker,
60+
// but it cannot call to extern __set_stack_limits() function, or Binaryen breaks
61+
// with "Fatal: Module::addFunction: __set_stack_limits already exists".
62+
// So for now, invoke this function from JS side. TODO: remove this in the future.
63+
// Note that this call is not exactly correct, since this limit will include
64+
// the TLS slot, that will be part of the region between m['sb'] and m['sz'],
65+
// so we need to fix up the call below.
66+
___set_stack_limits(m['sb'] + m['sz'], m['sb']);
67+
#endif
5868
// Run the C side Worker initialization for stack and TLS.
5969
_emscripten_wasm_worker_initialize(m['sb'], m['sz']);
60-
// The above function initializes the stack for this Worker, but C code cannot
61-
// call to extern __set_stack_limits() function, or Binaryen breaks with
62-
// "Fatal: Module::addFunction: __set_stack_limits already exists".
63-
// So for now, invoke the function from JS side. TODO: remove this in the future.
6470
#if STACK_OVERFLOW_CHECK >= 2
71+
// Fix up stack base. (TLS frame is created at the bottom address end of the stack)
72+
// See https://github.com/emscripten-core/emscripten/issues/16496
6573
___set_stack_limits(_emscripten_stack_get_base(), _emscripten_stack_get_end());
6674
#endif
6775

system/lib/compiler-rt/stack_limits.S

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,14 @@ emscripten_wasm_worker_initialize:
107107
PTR.add
108108
global.set __stack_base
109109

110+
// TODO: We'd like to do this here to avoid JS side calls to __set_stack_limits.
111+
// (or even better, we'd like to avoid duplicate versions of the stack variables)
112+
// See https://github.com/emscripten-core/emscripten/issues/16496
113+
// global.get __stack_base
114+
// global.get __stack_end
115+
// .functype __set_stack_limits (PTR, PTR) -> ()
116+
// call __set_stack_limits
117+
110118
// __wasm_init_tls(stackLowestAddress);
111119
local.get 0
112120
.functype __wasm_init_tls (PTR) -> ()

tests/test_browser.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5106,7 +5106,8 @@ def test_wasm_worker_embedded(self):
51065106
# Tests Wasm Worker thread stack setup
51075107
@also_with_minimal_runtime
51085108
def test_wasm_worker_thread_stack(self):
5109-
self.btest(test_file('wasm_worker/thread_stack.c'), expected='0', args=['-sWASM_WORKERS'])
5109+
for mode in [0, 1, 2]:
5110+
self.btest(test_file('wasm_worker/thread_stack.c'), expected='0', args=['-sWASM_WORKERS', f'-sSTACK_OVERFLOW_CHECK={mode}'])
51105111

51115112
# Tests emscripten_malloc_wasm_worker() and emscripten_current_thread_is_wasm_worker() functions
51125113
@also_with_minimal_runtime

tools/system_libs.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1031,16 +1031,14 @@ def can_use(self):
10311031

10321032
class libwasm_workers(MTLibrary):
10331033
def __init__(self, **kwargs):
1034-
self.stack_check = kwargs.pop('stack_check')
10351034
self.debug = kwargs.pop('debug')
10361035
super().__init__(**kwargs)
10371036

10381037
name = 'libwasm_workers'
10391038

10401039
def get_cflags(self):
10411040
cflags = ['-pthread',
1042-
'-D_DEBUG' if self.debug else '-Oz',
1043-
'-DSTACK_OVERFLOW_CHECK=' + ('2' if self.stack_check else '0')]
1041+
'-D_DEBUG' if self.debug else '-Oz']
10441042
if not self.debug:
10451043
cflags += ['-DNDEBUG']
10461044
if self.is_ww or self.is_mt:
@@ -1055,17 +1053,15 @@ def get_base_name(self):
10551053
name += '_stub'
10561054
if self.debug:
10571055
name += '-debug'
1058-
if self.stack_check:
1059-
name += '-stackcheck'
10601056
return name
10611057

10621058
@classmethod
10631059
def vary_on(cls):
1064-
return super().vary_on() + ['debug', 'stack_check']
1060+
return super().vary_on() + ['debug']
10651061

10661062
@classmethod
10671063
def get_default_variation(cls, **kwargs):
1068-
return super().get_default_variation(debug=settings.ASSERTIONS >= 1, stack_check=settings.STACK_OVERFLOW_CHECK == 2, **kwargs)
1064+
return super().get_default_variation(debug=settings.ASSERTIONS >= 1, **kwargs)
10691065

10701066
def get_files(self):
10711067
return files_in_path(

0 commit comments

Comments
 (0)