@@ -77,14 +77,27 @@ void mp_stack_set_bottom(void* stack_bottom) {
7777 MP_STATE_THREAD (stack_bottom ) = stack_bottom ;
7878}
7979
80+ // Return the current frame pointer. This can be used as an
81+ // approximation for the stack pointer of the _calling_ function.
82+ // This routine must not be inlined. This method is
83+ // architecture-independent, as opposed to using asm("sp") or similar.
84+ //
85+ // The stack_dummy approach used elsewhere in this file is not safe in
86+ // all cases. That value may be below the actual top of the stack.
87+ static void * approx_stack_pointer (void ){
88+ __asm volatile ("" );
89+ return __builtin_frame_address (0 );
90+ }
91+
8092// Fill stack space down toward the stack limit with a known unusual value.
8193void mp_stack_fill_with_sentinel (void ) {
8294 // Force routine to not be inlined. Better guarantee than MP_NOINLINE for -flto.
8395 __asm volatile ("" );
84- volatile char * volatile p ;
85- // Start filling stack just below the last variable in the current stack frame, which is p.
86- // Continue until we've hit the bottom of the stack (lowest address, logical "ceiling" of stack).
87- p = (char * ) (& p - 1 );
96+ // Start filling stack just below the current stack frame.
97+ // Continue until we've hit the bottom of the stack (lowest address,
98+ // logical "ceiling" of stack).
99+ char * p = (char * ) approx_stack_pointer () - 1 ;
100+
88101 while (p >= MP_STATE_THREAD (stack_bottom )) {
89102 * p -- = MP_MAX_STACK_USAGE_SENTINEL_BYTE ;
90103 }
0 commit comments