Skip to content

Commit d7e6a78

Browse files
committed
safemode prevent dynamic stack alloc
1 parent f71b418 commit d7e6a78

File tree

1 file changed

+15
-13
lines changed

1 file changed

+15
-13
lines changed

main.c

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -133,17 +133,19 @@ static void reset_devices(void) {
133133
}
134134

135135
#if MICROPY_ENABLE_PYSTACK
136-
STATIC supervisor_allocation *allocate_pystack(void) {
136+
STATIC supervisor_allocation *allocate_pystack(safe_mode_t safe_mode) {
137137
mp_int_t pystack_size = CIRCUITPY_PYSTACK_SIZE;
138138
#if CIRCUITPY_OS_GETENV && CIRCUITPY_SETTABLE_PYSTACK
139139
// Fetch value if exists from settings.toml
140140
// Leaves size to build default on any failure
141-
(void)common_hal_os_getenv_int("CIRCUITPY_PYSTACK_SIZE", &pystack_size);
142-
// Check if value is valid
143-
pystack_size = pystack_size - pystack_size % sizeof(size_t); // Round down to multiple of 4.
144-
if (pystack_size < 384) {
145-
serial_write_compressed(translate("\nInvalid CIRCUITPY_PYSTACK_SIZE\n\n\r"));
146-
pystack_size = CIRCUITPY_PYSTACK_SIZE; // Reset
141+
if (safe_mode == SAFE_MODE_NONE || safe_mode == SAFE_MODE_USER) {
142+
(void)common_hal_os_getenv_int("CIRCUITPY_PYSTACK_SIZE", &pystack_size);
143+
// Check if value is valid
144+
pystack_size = pystack_size - pystack_size % sizeof(size_t); // Round down to multiple of 4.
145+
if (pystack_size < 384) {
146+
serial_write_compressed(translate("\nInvalid CIRCUITPY_PYSTACK_SIZE\n\n\r"));
147+
pystack_size = CIRCUITPY_PYSTACK_SIZE; // Reset
148+
}
147149
}
148150
#endif
149151
supervisor_allocation *pystack = allocate_memory(pystack_size, false, false);
@@ -427,7 +429,7 @@ STATIC bool run_code_py(safe_mode_t safe_mode, bool *simulate_reset) {
427429

428430
supervisor_allocation *pystack = NULL;
429431
#if MICROPY_ENABLE_PYSTACK
430-
pystack = allocate_pystack();
432+
pystack = allocate_pystack(safe_mode);
431433
#endif
432434
supervisor_allocation *heap = allocate_remaining_memory();
433435
start_mp(heap, pystack);
@@ -769,7 +771,7 @@ STATIC void __attribute__ ((noinline)) run_safemode_py(safe_mode_t safe_mode) {
769771

770772
supervisor_allocation *pystack = NULL;
771773
#if MICROPY_ENABLE_PYSTACK
772-
pystack = allocate_pystack();
774+
pystack = allocate_pystack(safe_mode);
773775
#endif
774776
supervisor_allocation *heap = allocate_remaining_memory();
775777
start_mp(heap, pystack);
@@ -806,7 +808,7 @@ STATIC void __attribute__ ((noinline)) run_boot_py(safe_mode_t safe_mode) {
806808

807809
supervisor_allocation *pystack = NULL;
808810
#if MICROPY_ENABLE_PYSTACK
809-
pystack = allocate_pystack();
811+
pystack = allocate_pystack(safe_mode);
810812
#endif
811813
supervisor_allocation *heap = allocate_remaining_memory();
812814
start_mp(heap, pystack);
@@ -906,13 +908,13 @@ STATIC void __attribute__ ((noinline)) run_boot_py(safe_mode_t safe_mode) {
906908
#endif
907909
}
908910

909-
STATIC int run_repl(void) {
911+
STATIC int run_repl(safe_mode_t safe_mode) {
910912
int exit_code = PYEXEC_FORCED_EXIT;
911913
stack_resize();
912914
filesystem_flush();
913915
supervisor_allocation *pystack = NULL;
914916
#if MICROPY_ENABLE_PYSTACK
915-
pystack = allocate_pystack();
917+
pystack = allocate_pystack(safe_mode);
916918
#endif
917919
supervisor_allocation *heap = allocate_remaining_memory();
918920
start_mp(heap, pystack);
@@ -1078,7 +1080,7 @@ int __attribute__((used)) main(void) {
10781080
bool simulate_reset = true;
10791081
for (;;) {
10801082
if (!skip_repl) {
1081-
exit_code = run_repl();
1083+
exit_code = run_repl(get_safe_mode());
10821084
supervisor_set_run_reason(RUN_REASON_REPL_RELOAD);
10831085
}
10841086
if (exit_code == PYEXEC_FORCED_EXIT) {

0 commit comments

Comments
 (0)