8000 Allow max stack checking to be used with -flto build by determining top · boneskull/circuitpython@5d509ec · GitHub
[go: up one dir, main page]

Skip to content

Commit 5d509ec

Browse files
dhalberttannewt
authored andcommitted
Allow max stack checking to be used with -flto build by determining top
of stack in a different way.
1 parent 91d4cdb commit 5d509ec

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

atmel-samd/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ CFLAGS = $(INC) -Wall -Werror -std=gnu11 -nostdlib $(CFLAGS_CORTEX_M0) $(CFLAGS_
132132
ifeq ($(DEBUG), 1)
133133
# NDEBUG disables assert() statements. This reduces code size pretty dramatically, per tannewt.
134134
# Turn on Python modules useful for debugging (e.g. uheap, ustack).
135+
# -DMICROPY_DEBUG_MODULES may also be added to an -flto build, if you wish.
135136
CFLAGS += -Os -ggdb -DNDEBUG -DENABLE_MICRO_TRACE_BUFFER -DMICROPY_DEBUG_MODULES
136137
else
137138
CFLAGS += -Os -DNDEBUG -flto

py/stackctrl.c

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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
CDE9 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.
8193
void 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

Comments
 (0)
0