From 0eb08509f0f9e6aa353782b9d20c69714dd77034 Mon Sep 17 00:00:00 2001 From: Scott Shawcroft Date: Tue, 21 Mar 2023 16:42:49 -0700 Subject: [PATCH 1/2] Make set_stack_limit respect fixed stack Fixes #2830 --- shared-bindings/supervisor/Runtime.c | 6 +++++- supervisor/shared/stack.c | 12 ++++++++++-- supervisor/shared/stack.h | 2 +- 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/shared-bindings/supervisor/Runtime.c b/shared-bindings/supervisor/Runtime.c index db3d8d1e4d..ecdd914a8f 100644 --- a/shared-bindings/supervisor/Runtime.c +++ b/shared-bindings/supervisor/Runtime.c @@ -197,7 +197,11 @@ MP_DEFINE_CONST_FUN_OBJ_1(supervisor_runtime_get_next_stack_limit_obj, superviso STATIC mp_obj_t supervisor_runtime_set_next_stack_limit(mp_obj_t self, mp_obj_t size_obj) { mp_int_t size = mp_obj_get_int(size_obj); mp_arg_validate_int_min(size, 256, MP_QSTR_size); - set_next_stack_size(size); + if (!set_next_stack_size(size)) { + mp_raise_msg_varg(&mp_type_AttributeError, + MP_ERROR_TEXT("can't set attribute '%q'"), + MP_QSTR_next_stack_limit); + } return mp_const_none; } MP_DEFINE_CONST_FUN_OBJ_2(supervisor_runtime_set_next_stack_limit_obj, supervisor_runtime_set_next_stack_limit); diff --git a/supervisor/shared/stack.c b/supervisor/shared/stack.c index be97cee234..148bb0bbd5 100644 --- a/supervisor/shared/stack.c +++ b/supervisor/shared/stack.c @@ -35,7 +35,7 @@ extern uint32_t _estack; // Requested size. -static uint32_t next_stack_size = CIRCUITPY_DEFAULT_STACK_SIZE; +static uint32_t next_stack_size = 0; static uint32_t current_stack_size = 0; // Actual location and size, may be larger than requested. static uint32_t *stack_limit = NULL; @@ -49,11 +49,15 @@ static void allocate_stack(void) { stack_limit = port_stack_get_limit(); stack_length = (port_stack_get_top() - stack_limit) * sizeof(uint32_t); current_stack_size = stack_length; + next_stack_size = stack_length; } else { mp_uint_t regs[10]; mp_uint_t sp = cpu_get_regs_and_sp(regs); mp_uint_t c_size = (mp_uint_t)port_stack_get_top() - sp; + if (next_stack_size == 0) { + next_stack_size = CIRCUITPY_DEFAULT_STACK_SIZE; + } supervisor_allocation *stack_alloc = allocate_memory(c_size + next_stack_size + EXCEPTION_STACK_SIZE, true, false); if (stack_alloc == NULL) { stack_alloc = allocate_memory(c_size + CIRCUITPY_DEFAULT_STACK_SIZE + EXCEPTION_STACK_SIZE, true, false); @@ -103,8 +107,12 @@ size_t stack_get_length(void) { return stack_length; } -void set_next_stack_size(uint32_t size) { +bool set_next_stack_size(uint32_t size) { + if (port_has_fixed_stack()) { + return false; + } next_stack_size = size; + return true; } uint32_t get_next_stack_size(void) { diff --git a/supervisor/shared/stack.h b/supervisor/shared/stack.h index 4acda57354..4a03fcf275 100644 --- a/supervisor/shared/stack.h +++ b/supervisor/shared/stack.h @@ -37,7 +37,7 @@ void stack_resize(void); uint32_t *stack_get_bottom(void); size_t stack_get_length(void); // Next/current requested stack size. -void set_next_stack_size(uint32_t size); +bool set_next_stack_size(uint32_t size); uint32_t get_next_stack_size(void); uint32_t get_current_stack_size(void); bool stack_ok(void); From b5834886521a16419af25c512472029f97234cd6 Mon Sep 17 00:00:00 2001 From: Scott Shawcroft Date: Wed, 22 Mar 2023 09:24:03 -0700 Subject: [PATCH 2/2] Fix stub --- supervisor/stub/stack.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/supervisor/stub/stack.c b/supervisor/stub/stack.c index 2abc197878..bb6e0d4cb0 100644 --- a/supervisor/stub/stack.c +++ b/supervisor/stub/stack.c @@ -39,8 +39,9 @@ void stack_init(void) { void stack_resize(void) { } -void set_next_stack_size(uint32_t size) { +bool set_next_stack_size(uint32_t size) { (void)size; + return true; } uint32_t get_current_stack_size(void) {