File tree Expand file tree Collapse file tree 4 files changed +26
-6
lines changed Expand file tree Collapse file tree 4 files changed +26
-6
lines changed Original file line number Diff line number Diff 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 uniniatlized 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,11 @@ 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 calling `__set_stack_limits`
657+ // until we start running a thread. We also need to call this again for each
658+ // new thread that the runs on a worker.
650659 $setDylinkStackLimits: function ( stackTop , stackMax ) {
651660 for ( var name in LDSO . loadedLibsByName ) {
652661#if DYLINK_DEBUG
Original file line number Diff line number Diff 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
Original file line number Diff line number Diff line change @@ -375,9 +375,6 @@ function preRun() {
375375}
376376
377377function initRuntime ( ) {
378- #if STACK_OVERFLOW_CHECK
379- checkStackCookie ( ) ;
380- #endif
381378#if ASSERTIONS
382379 assert ( ! runtimeInitialized ) ;
383380#endif
@@ -391,6 +388,10 @@ function initRuntime() {
391388 if ( ENVIRONMENT_IS_PTHREAD ) return ;
392389#endif
393390
391+ #if STACK_OVERFLOW_CHECK
392+ checkStackCookie ( ) ;
393+ #endif
394+
394395#if STACK_OVERFLOW_CHECK >= 2
395396#if RUNTIME_LOGGING
396397 err ( '__set_stack_limits: ' + _emscripten_stack_get_base ( ) + ', ' + _emscripten_stack_get_end ( ) ) ;
Original file line number Diff line number Diff line change 88// Initializes the stack cookie. Called at the startup of main and at the startup of each thread in pthreads mode.
99function 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
You can’t perform that action at this time.
0 commit comments