From 4b58efbc0a23d412d983e1f23af44200b44129c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89milie=20Feral?= Date: Mon, 2 Dec 2019 12:19:31 +0100 Subject: [PATCH] [python] Fix stack boundary on the device --- python/port/port.cpp | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/python/port/port.cpp b/python/port/port.cpp index 9e77ced31..7fa7a4044 100644 --- a/python/port/port.cpp +++ b/python/port/port.cpp @@ -99,13 +99,20 @@ extern "C" { } void MicroPython::init(void * heapStart, void * heapEnd) { + volatile int stackTop; + void * stackTopAddress = (void *)(&stackTop); + /* We delimit the stack part that will be used by Python. The stackTop is the + * address of the first object that can be allocated on Python stack. This + * boundaries are used: + * - by gc_collect to determine where to collect roots of the objects that + * must be kept on the heap + * - to check if the maximal recursion depth has been reached. */ #if MP_PORT_USE_STACK_SYMBOLS - mp_stack_set_top(&_stack_start); - size_t stackLimitInBytes = (char *)&_stack_start - (char *)&_stack_end; + mp_stack_set_top(stackTopAddress); + size_t stackLimitInBytes = (char *)stackTopAddress - (char *)&_stack_end; mp_stack_set_limit(stackLimitInBytes); #else - volatile int stackTop; - mp_stack_set_top((void *)(&stackTop)); + mp_stack_set_top(stackTopAddress); /* The stack limit is set to roughly mimic the maximal recursion depth of the * device - and actually to be slightly less to be sure not to beat the device * performance. */