Skip to content

Commit b2dc6cf

Browse files
authored
Avoid calling writeStackCookie to early in worker.js (#17063)
When running a worker we don't want to write the stack cookie until the worker is actually running a pthread. This is done in `establishStackSpace`. For this reason we don't want to run `writeStackCookie` during `run` (which is used only to initialize the worker, before its running a thread).
1 parent f813687 commit b2dc6cf

File tree

4 files changed

+27
-6
lines changed

4 files changed

+27
-6
lines changed

src/library_dylink.js

+11-1
Original file line numberDiff line numberDiff line change
@@ -588,6 +588,11 @@ var LibraryDylink = {
588588
}
589589
#if STACK_OVERFLOW_CHECK >= 2
590590
if (moduleExports['__set_stack_limits']) {
591+
#if USE_PTHREADS
592+
// When we are on an uninitialized pthread we delay calling
593+
// __set_stack_limits until $setDylinkStackLimits.
594+
if (!ENVIRONMENT_IS_PTHREAD || runtimeInitialized)
595+
#endif
591596
moduleExports['__set_stack_limits'](_emscripten_stack_get_base(), _emscripten_stack_get_end())
592597
}
593598
#endif
@@ -646,7 +651,12 @@ var LibraryDylink = {
646651
return loadModule();
647652
},
648653

649-
#if STACK_OVERFLOW_CHECK >= 2
654+
#if STACK_OVERFLOW_CHECK >= 2 && USE_PTHREADS
655+
// With USE_PTHREADS we load libraries before we are running a pthread and
656+
// therefore before we have a stack. Instead we delay calling
657+
// `__set_stack_limits` until we start running a thread. We also need to call
658+
// this again for each new thread that the runs on a worker (since each thread
659+
// has its own separate stack region).
650660
$setDylinkStackLimits: function(stackTop, stackMax) {
651661
for (var name in LDSO.loadedLibsByName) {
652662
#if DYLINK_DEBUG

src/postamble.js

+9-2
Original file line numberDiff line numberDiff line change
@@ -214,12 +214,16 @@ function stackCheckInit() {
214214
// This is normally called automatically during __wasm_call_ctors but need to
215215
// get these values before even running any of the ctors so we call it redundantly
216216
// here.
217-
// TODO(sbc): Move writeStackCookie to native to to avoid this.
217+
#if ASSERTIONS && USE_PTHREADS
218+
// See $establishStackSpace for the equivelent code that runs on a thread
219+
assert(!ENVIRONMENT_IS_PTHREAD);
220+
#endif
218221
#if RELOCATABLE
219222
_emscripten_stack_set_limits({{{ STACK_BASE }}} , {{{ STACK_MAX }}});
220223
#else
221224
_emscripten_stack_init();
222225
#endif
226+
// TODO(sbc): Move writeStackCookie to native to to avoid this.
223227
writeStackCookie();
224228
}
225229
#endif
@@ -240,7 +244,10 @@ function run(args) {
240244
}
241245

242246
#if STACK_OVERFLOW_CHECK
243-
stackCheckInit();
247+
#if USE_PTHREADS
248+
if (!ENVIRONMENT_IS_PTHREAD)
249+
#endif
250+
stackCheckInit();
244251
#endif
245252

246253
#if RELOCATABLE

src/preamble.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -371,9 +371,6 @@ function preRun() {
371371
}
372372

373373
function initRuntime() {
374-
#if STACK_OVERFLOW_CHECK
375-
checkStackCookie();
376-
#endif
377374
#if ASSERTIONS
378375
assert(!runtimeInitialized);
379376
#endif
@@ -387,6 +384,10 @@ function initRuntime() {
387384
if (ENVIRONMENT_IS_PTHREAD) return;
388385
#endif
389386

387+
#if STACK_OVERFLOW_CHECK
388+
checkStackCookie();
389+
#endif
390+
390391
#if STACK_OVERFLOW_CHECK >= 2
391392
#if RUNTIME_LOGGING
392393
err('__set_stack_limits: ' + _emscripten_stack_get_base() + ', ' + _emscripten_stack_get_end());

src/runtime_stack_check.js

+3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
// Initializes the stack cookie. Called at the startup of main and at the startup of each thread in pthreads mode.
99
function writeStackCookie() {
1010
var max = _emscripten_stack_get_end();
11+
#if RUNTIME_DEBUG
12+
err('writeStackCookie: ' + max.toString(16));
13+
#endif
1114
#if ASSERTIONS
1215
assert((max & 3) == 0);
1316
#endif

0 commit comments

Comments
 (0)