diff --git a/src/library_dylink.js b/src/library_dylink.js index 55c40d6f05e99..011b2e569fe7e 100644 --- a/src/library_dylink.js +++ b/src/library_dylink.js @@ -588,6 +588,11 @@ var LibraryDylink = { } #if STACK_OVERFLOW_CHECK >= 2 if (moduleExports['__set_stack_limits']) { +#if USE_PTHREADS + // When we are on an uninitialized pthread we delay calling + // __set_stack_limits until $setDylinkStackLimits. + if (!ENVIRONMENT_IS_PTHREAD || runtimeInitialized) +#endif moduleExports['__set_stack_limits'](_emscripten_stack_get_base(), _emscripten_stack_get_end()) } #endif @@ -646,7 +651,12 @@ var LibraryDylink = { return loadModule(); }, -#if STACK_OVERFLOW_CHECK >= 2 +#if STACK_OVERFLOW_CHECK >= 2 && USE_PTHREADS + // With USE_PTHREADS we load libraries before we are running a pthread and + // therefore before we have a stack. Instead we delay calling + // `__set_stack_limits` until we start running a thread. We also need to call + // this again for each new thread that the runs on a worker (since each thread + // has its own separate stack region). $setDylinkStackLimits: function(stackTop, stackMax) { for (var name in LDSO.loadedLibsByName) { #if DYLINK_DEBUG diff --git a/src/postamble.js b/src/postamble.js index db52bd790b9f9..b8194b95bca89 100644 --- a/src/postamble.js +++ b/src/postamble.js @@ -214,12 +214,16 @@ function stackCheckInit() { // This is normally called automatically during __wasm_call_ctors but need to // get these values before even running any of the ctors so we call it redundantly // here. - // TODO(sbc): Move writeStackCookie to native to to avoid this. +#if ASSERTIONS && USE_PTHREADS + // See $establishStackSpace for the equivelent code that runs on a thread + assert(!ENVIRONMENT_IS_PTHREAD); +#endif #if RELOCATABLE _emscripten_stack_set_limits({{{ STACK_BASE }}} , {{{ STACK_MAX }}}); #else _emscripten_stack_init(); #endif + // TODO(sbc): Move writeStackCookie to native to to avoid this. writeStackCookie(); } #endif @@ -240,7 +244,10 @@ function run(args) { } #if STACK_OVERFLOW_CHECK - stackCheckInit(); +#if USE_PTHREADS + if (!ENVIRONMENT_IS_PTHREAD) +#endif + stackCheckInit(); #endif #if RELOCATABLE diff --git a/src/preamble.js b/src/preamble.js index f85d92f4e3e8d..a549455e6f93e 100644 --- a/src/preamble.js +++ b/src/preamble.js @@ -375,9 +375,6 @@ function preRun() { } function initRuntime() { -#if STACK_OVERFLOW_CHECK - checkStackCookie(); -#endif #if ASSERTIONS assert(!runtimeInitialized); #endif @@ -391,6 +388,10 @@ function initRuntime() { if (ENVIRONMENT_IS_PTHREAD) return; #endif +#if STACK_OVERFLOW_CHECK + checkStackCookie(); +#endif + #if STACK_OVERFLOW_CHECK >= 2 #if RUNTIME_LOGGING err('__set_stack_limits: ' + _emscripten_stack_get_base() + ', ' + _emscripten_stack_get_end()); diff --git a/src/runtime_stack_check.js b/src/runtime_stack_check.js index 075ebab341e5a..61b4377edf3ad 100644 --- a/src/runtime_stack_check.js +++ b/src/runtime_stack_check.js @@ -8,6 +8,9 @@ // Initializes the stack cookie. Called at the startup of main and at the startup of each thread in pthreads mode. function writeStackCookie() { var max = _emscripten_stack_get_end(); +#if RUNTIME_DEBUG + err('writeStackCookie: ' + max.toString(16)); +#endif #if ASSERTIONS assert((max & 3) == 0); #endif