From 03be42ab847f04a89d207d98fdb28048cfedf72d Mon Sep 17 00:00:00 2001 From: Scott Shawcroft Date: Fri, 8 Mar 2019 15:29:14 -0800 Subject: [PATCH] Enter safe mode when an allocation is attempted on an uninitialized heap. --- locale/ID.po | 6 +++++- locale/circuitpython.pot | 6 +++++- locale/de_DE.po | 6 +++++- locale/en_US.po | 6 +++++- locale/en_x_pirate.po | 6 +++++- locale/es.po | 6 +++++- locale/fil.po | 6 +++++- locale/fr.po | 6 +++++- locale/it_IT.po | 6 +++++- locale/pt_BR.po | 6 +++++- main.c | 3 +-- py/gc.c | 13 +++++++++++++ py/gc.h | 1 + supervisor/shared/safe_mode.c | 4 +++- supervisor/shared/safe_mode.h | 3 ++- 15 files changed, 70 insertions(+), 14 deletions(-) diff --git a/locale/ID.po b/locale/ID.po index 0650438901..8961872a0c 100644 --- a/locale/ID.po +++ b/locale/ID.po @@ -9,7 +9,7 @@ msgid "" msgstr "" "Project-Id-Version: PACKAGE VERSION\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2019-03-08 12:45-0800\n" +"POT-Creation-Date: 2019-03-12 11:17-0700\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: FULL NAME \n" "Language-Team: LANGUAGE \n" @@ -289,6 +289,10 @@ msgstr "" msgid "Array values should be single bytes." msgstr "" +#: supervisor/shared/safe_mode.c +msgid "Attempted heap allocation when MicroPython VM not running.\n" +msgstr "" + #: main.c msgid "Auto-reload is off.\n" msgstr "Auto-reload tidak aktif.\n" diff --git a/locale/circuitpython.pot b/locale/circuitpython.pot index c0b460c0eb..36975f9889 100644 --- a/locale/circuitpython.pot +++ b/locale/circuitpython.pot @@ -9,7 +9,7 @@ msgid "" msgstr "" "Project-Id-Version: PACKAGE VERSION\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2019-03-08 12:45-0800\n" +"POT-Creation-Date: 2019-03-12 11:17-0700\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: FULL NAME \n" "Language-Team: LANGUAGE \n" @@ -287,6 +287,10 @@ msgstr "" msgid "Array values should be single bytes." msgstr "" +#: supervisor/shared/safe_mode.c +msgid "Attempted heap allocation when MicroPython VM not running.\n" +msgstr "" + #: main.c msgid "Auto-reload is off.\n" msgstr "" diff --git a/locale/de_DE.po b/locale/de_DE.po index b50899d7cd..1a73ec7a25 100644 --- a/locale/de_DE.po +++ b/locale/de_DE.po @@ -8,7 +8,7 @@ msgid "" msgstr "" "Project-Id-Version: \n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2019-03-08 12:45-0800\n" +"POT-Creation-Date: 2019-03-12 11:17-0700\n" "PO-Revision-Date: 2018-07-27 11:55-0700\n" "Last-Translator: Pascal Deneaux\n" "Language-Team: Sebastian Plamauer, Pascal Deneaux\n" @@ -289,6 +289,10 @@ msgstr "Array muss Halbwörter enthalten (type 'H')" msgid "Array values should be single bytes." msgstr "Array-Werte sollten aus Einzelbytes bestehen." +#: supervisor/shared/safe_mode.c +msgid "Attempted heap allocation when MicroPython VM not running.\n" +msgstr "" + #: main.c msgid "Auto-reload is off.\n" msgstr "Automatisches Neuladen ist deaktiviert.\n" diff --git a/locale/en_US.po b/locale/en_US.po index f98f42eeb5..52d4b73e8b 100644 --- a/locale/en_US.po +++ b/locale/en_US.po @@ -8,7 +8,7 @@ msgid "" msgstr "" "Project-Id-Version: \n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2019-03-08 12:45-0800\n" +"POT-Creation-Date: 2019-03-12 11:17-0700\n" "PO-Revision-Date: 2018-07-27 11:55-0700\n" "Last-Translator: \n" "Language-Team: \n" @@ -287,6 +287,10 @@ msgstr "" msgid "Array values should be single bytes." msgstr "" +#: supervisor/shared/safe_mode.c +msgid "Attempted heap allocation when MicroPython VM not running.\n" +msgstr "" + #: main.c msgid "Auto-reload is off.\n" msgstr "" diff --git a/locale/en_x_pirate.po b/locale/en_x_pirate.po index 5382c93f25..6597d98bd8 100644 --- a/locale/en_x_pirate.po +++ b/locale/en_x_pirate.po @@ -8,7 +8,7 @@ msgid "" msgstr "" "Project-Id-Version: \n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2019-03-08 12:45-0800\n" +"POT-Creation-Date: 2019-03-12 11:17-0700\n" "PO-Revision-Date: 2018-07-27 11:55-0700\n" "Last-Translator: \n" "Language-Team: @sommersoft, @MrCertainly\n" @@ -289,6 +289,10 @@ msgstr "" msgid "Array values should be single bytes." msgstr "" +#: supervisor/shared/safe_mode.c +msgid "Attempted heap allocation when MicroPython VM not running.\n" +msgstr "" + #: main.c msgid "Auto-reload is off.\n" msgstr "Auto-reload be off.\n" diff --git a/locale/es.po b/locale/es.po index 8c2b31c5a9..a6749413d9 100644 --- a/locale/es.po +++ b/locale/es.po @@ -9,7 +9,7 @@ msgid "" msgstr "" "Project-Id-Version: \n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2019-03-08 12:45-0800\n" +"POT-Creation-Date: 2019-03-12 11:17-0700\n" "PO-Revision-Date: 2018-08-24 22:56-0500\n" "Last-Translator: \n" "Language-Team: \n" @@ -294,6 +294,10 @@ msgstr "" msgid "Array values should be single bytes." msgstr "Valores del array deben ser bytes individuales." +#: supervisor/shared/safe_mode.c +msgid "Attempted heap allocation when MicroPython VM not running.\n" +msgstr "" + #: main.c msgid "Auto-reload is off.\n" msgstr "Auto-recarga deshabilitada.\n" diff --git a/locale/fil.po b/locale/fil.po index cb58f2be26..2d9ce796b0 100644 --- a/locale/fil.po +++ b/locale/fil.po @@ -8,7 +8,7 @@ msgid "" msgstr "" "Project-Id-Version: \n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2019-03-08 12:45-0800\n" +"POT-Creation-Date: 2019-03-12 11:17-0700\n" "PO-Revision-Date: 2018-12-20 22:15-0800\n" "Last-Translator: Timothy \n" "Language-Team: fil\n" @@ -291,6 +291,10 @@ msgstr "May halfwords (type 'H') dapat ang array" msgid "Array values should be single bytes." msgstr "Array values ay dapat single bytes." +#: supervisor/shared/safe_mode.c +msgid "Attempted heap allocation when MicroPython VM not running.\n" +msgstr "" + #: main.c msgid "Auto-reload is off.\n" msgstr "Awtomatikong pag re-reload ay OFF.\n" diff --git a/locale/fr.po b/locale/fr.po index 11ca7a261f..eeab87b653 100644 --- a/locale/fr.po +++ b/locale/fr.po @@ -8,7 +8,7 @@ msgid "" msgstr "" "Project-Id-Version: 0.1\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2019-03-08 12:45-0800\n" +"POT-Creation-Date: 2019-03-12 11:17-0700\n" "PO-Revision-Date: 2018-12-23 20:05+0100\n" "Last-Translator: Pierrick Couturier \n" "Language-Team: fr\n" @@ -292,6 +292,10 @@ msgstr "Le tableau doit contenir des halfwords (type 'H')" msgid "Array values should be single bytes." msgstr "Les valeurs du tableau doivent être des octets simples 'bytes'." +#: supervisor/shared/safe_mode.c +msgid "Attempted heap allocation when MicroPython VM not running.\n" +msgstr "" + #: main.c msgid "Auto-reload is off.\n" msgstr "Auto-rechargement désactivé.\n" diff --git a/locale/it_IT.po b/locale/it_IT.po index b3e411cfd8..22bc8ed4be 100644 --- a/locale/it_IT.po +++ b/locale/it_IT.po @@ -9,7 +9,7 @@ msgid "" msgstr "" "Project-Id-Version: PACKAGE VERSION\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2019-03-08 12:45-0800\n" +"POT-Creation-Date: 2019-03-12 11:17-0700\n" "PO-Revision-Date: 2018-10-02 16:27+0200\n" "Last-Translator: Enrico Paganin \n" "Language-Team: \n" @@ -290,6 +290,10 @@ msgstr "" msgid "Array values should be single bytes." msgstr "" +#: supervisor/shared/safe_mode.c +msgid "Attempted heap allocation when MicroPython VM not running.\n" +msgstr "" + #: main.c msgid "Auto-reload is off.\n" msgstr "Auto-reload disattivato.\n" diff --git a/locale/pt_BR.po b/locale/pt_BR.po index 415bb0b4c3..77e1beff65 100644 --- a/locale/pt_BR.po +++ b/locale/pt_BR.po @@ -9,7 +9,7 @@ msgid "" msgstr "" "Project-Id-Version: PACKAGE VERSION\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2019-03-08 12:45-0800\n" +"POT-Creation-Date: 2019-03-12 11:17-0700\n" "PO-Revision-Date: 2018-10-02 21:14-0000\n" "Last-Translator: \n" "Language-Team: \n" @@ -290,6 +290,10 @@ msgstr "Array deve conter meias palavras (tipo 'H')" msgid "Array values should be single bytes." msgstr "" +#: supervisor/shared/safe_mode.c +msgid "Attempted heap allocation when MicroPython VM not running.\n" +msgstr "" + #: main.c msgid "Auto-reload is off.\n" msgstr "A atualização automática está desligada.\n" diff --git a/main.c b/main.c index f0913db6ee..4b58b778d2 100755 --- a/main.c +++ b/main.c @@ -139,8 +139,7 @@ void stop_mp(void) { MP_STATE_VM(vfs_cur) = vfs; #endif - // Run any finalizers before we stop using the heap. - gc_sweep_all(); + gc_deinit(); } #define STRING_LIST(...) {__VA_ARGS__, ""} diff --git a/py/gc.c b/py/gc.c index 1f5cd8d017..e0b77c4cf0 100755 --- a/py/gc.c +++ b/py/gc.c @@ -31,6 +31,8 @@ #include "py/gc.h" #include "py/runtime.h" +#include "supervisor/shared/safe_mode.h" + #if MICROPY_ENABLE_GC #if MICROPY_DEBUG_VERBOSE // print debugging info @@ -182,6 +184,13 @@ void gc_init(void *start, void *end) { DEBUG_printf(" pool at %p, length " UINT_FMT " bytes, " UINT_FMT " blocks\n", MP_STATE_MEM(gc_pool_start), gc_pool_block_len * BYTES_PER_BLOCK, gc_pool_block_len); } +void gc_deinit() { + // Run any finalizers before we stop using the heap. + gc_sweep_all(); + + MP_STATE_MEM(gc_pool_start) = 0; +} + void gc_lock(void) { GC_ENTER(); MP_STATE_MEM(gc_lock_depth)++; @@ -463,6 +472,10 @@ void *gc_alloc(size_t n_bytes, bool has_finaliser, bool long_lived) { return NULL; } + if (MP_STATE_MEM(gc_pool_start) == 0) { + reset_into_safe_mode(GC_ALLOC_OUTSIDE_VM); + } + GC_ENTER(); // check if GC is locked diff --git a/py/gc.h b/py/gc.h index 64f4b0d177..c05e006b4f 100644 --- a/py/gc.h +++ b/py/gc.h @@ -32,6 +32,7 @@ #include "py/misc.h" void gc_init(void *start, void *end); +void gc_deinit(void); // These lock/unlock functions can be nested. // They can be used to prevent the GC from allocating/freeing. diff --git a/supervisor/shared/safe_mode.c b/supervisor/shared/safe_mode.c index c1ce1fc9c5..3bb75b8973 100644 --- a/supervisor/shared/safe_mode.c +++ b/supervisor/shared/safe_mode.c @@ -105,7 +105,7 @@ void print_safe_mode_message(safe_mode_t reason) { if (reason != NO_SAFE_MODE) { serial_write("\r\n"); serial_write_compressed(translate("You are running in safe mode which means something unanticipated happened.\n")); - if (reason == HARD_CRASH || reason == MICROPY_NLR_JUMP_FAIL || reason == MICROPY_FATAL_ERROR) { + if (reason == HARD_CRASH || reason == MICROPY_NLR_JUMP_FAIL || reason == MICROPY_FATAL_ERROR || reason == GC_ALLOC_OUTSIDE_VM) { serial_write_compressed(translate("Looks like our core CircuitPython code crashed hard. Whoops!\nPlease file an issue at https://github.com/adafruit/circuitpython/issues\n with the contents of your CIRCUITPY drive and this message:\n")); if (reason == HARD_CRASH) { serial_write_compressed(translate("Crash into the HardFault_Handler.\n")); @@ -113,6 +113,8 @@ void print_safe_mode_message(safe_mode_t reason) { serial_write_compressed(translate("MicroPython NLR jump failed. Likely memory corruption.\n")); } else if (reason == MICROPY_FATAL_ERROR) { serial_write_compressed(translate("MicroPython fatal error.\n")); + } else if (reason == GC_ALLOC_OUTSIDE_VM) { + serial_write_compressed(translate("Attempted heap allocation when MicroPython VM not running.\n")); } } else if (reason == BROWNOUT) { serial_write_compressed(translate("The microcontroller's power dipped. Please make sure your power supply provides\nenough power for the whole circuit and press reset (after ejecting CIRCUITPY).\n")); diff --git a/supervisor/shared/safe_mode.h b/supervisor/shared/safe_mode.h index eba2668ac7..59480b55b8 100644 --- a/supervisor/shared/safe_mode.h +++ b/supervisor/shared/safe_mode.h @@ -35,7 +35,8 @@ typedef enum { HEAP_OVERWRITTEN, MANUAL_SAFE_MODE, MICROPY_NLR_JUMP_FAIL, - MICROPY_FATAL_ERROR + MICROPY_FATAL_ERROR, + GC_ALLOC_OUTSIDE_VM } safe_mode_t; safe_mode_t wait_for_safe_mode_reset(void);