Enter safe mode when an allocation is attempted on an uninitialized heap.

This commit is contained in:
Scott Shawcroft 2019-03-08 15:29:14 -08:00
parent ef4a74e201
commit 03be42ab84
No known key found for this signature in database
GPG Key ID: FD0EDC4B6C53CA59
15 changed files with 70 additions and 14 deletions

View File

@ -9,7 +9,7 @@ msgid ""
msgstr "" msgstr ""
"Project-Id-Version: PACKAGE VERSION\n" "Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \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" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n" "Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n" "Language-Team: LANGUAGE <LL@li.org>\n"
@ -289,6 +289,10 @@ msgstr ""
msgid "Array values should be single bytes." msgid "Array values should be single bytes."
msgstr "" msgstr ""
#: supervisor/shared/safe_mode.c
msgid "Attempted heap allocation when MicroPython VM not running.\n"
msgstr ""
#: main.c #: main.c
msgid "Auto-reload is off.\n" msgid "Auto-reload is off.\n"
msgstr "Auto-reload tidak aktif.\n" msgstr "Auto-reload tidak aktif.\n"

View File

@ -9,7 +9,7 @@ msgid ""
msgstr "" msgstr ""
"Project-Id-Version: PACKAGE VERSION\n" "Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \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" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n" "Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n" "Language-Team: LANGUAGE <LL@li.org>\n"
@ -287,6 +287,10 @@ msgstr ""
msgid "Array values should be single bytes." msgid "Array values should be single bytes."
msgstr "" msgstr ""
#: supervisor/shared/safe_mode.c
msgid "Attempted heap allocation when MicroPython VM not running.\n"
msgstr ""
#: main.c #: main.c
msgid "Auto-reload is off.\n" msgid "Auto-reload is off.\n"
msgstr "" msgstr ""

View File

@ -8,7 +8,7 @@ msgid ""
msgstr "" msgstr ""
"Project-Id-Version: \n" "Project-Id-Version: \n"
"Report-Msgid-Bugs-To: \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" "PO-Revision-Date: 2018-07-27 11:55-0700\n"
"Last-Translator: Pascal Deneaux\n" "Last-Translator: Pascal Deneaux\n"
"Language-Team: Sebastian Plamauer, 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." msgid "Array values should be single bytes."
msgstr "Array-Werte sollten aus Einzelbytes bestehen." 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 #: main.c
msgid "Auto-reload is off.\n" msgid "Auto-reload is off.\n"
msgstr "Automatisches Neuladen ist deaktiviert.\n" msgstr "Automatisches Neuladen ist deaktiviert.\n"

View File

@ -8,7 +8,7 @@ msgid ""
msgstr "" msgstr ""
"Project-Id-Version: \n" "Project-Id-Version: \n"
"Report-Msgid-Bugs-To: \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" "PO-Revision-Date: 2018-07-27 11:55-0700\n"
"Last-Translator: \n" "Last-Translator: \n"
"Language-Team: \n" "Language-Team: \n"
@ -287,6 +287,10 @@ msgstr ""
msgid "Array values should be single bytes." msgid "Array values should be single bytes."
msgstr "" msgstr ""
#: supervisor/shared/safe_mode.c
msgid "Attempted heap allocation when MicroPython VM not running.\n"
msgstr ""
#: main.c #: main.c
msgid "Auto-reload is off.\n" msgid "Auto-reload is off.\n"
msgstr "" msgstr ""

View File

@ -8,7 +8,7 @@ msgid ""
msgstr "" msgstr ""
"Project-Id-Version: \n" "Project-Id-Version: \n"
"Report-Msgid-Bugs-To: \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" "PO-Revision-Date: 2018-07-27 11:55-0700\n"
"Last-Translator: \n" "Last-Translator: \n"
"Language-Team: @sommersoft, @MrCertainly\n" "Language-Team: @sommersoft, @MrCertainly\n"
@ -289,6 +289,10 @@ msgstr ""
msgid "Array values should be single bytes." msgid "Array values should be single bytes."
msgstr "" msgstr ""
#: supervisor/shared/safe_mode.c
msgid "Attempted heap allocation when MicroPython VM not running.\n"
msgstr ""
#: main.c #: main.c
msgid "Auto-reload is off.\n" msgid "Auto-reload is off.\n"
msgstr "Auto-reload be off.\n" msgstr "Auto-reload be off.\n"

View File

@ -9,7 +9,7 @@ msgid ""
msgstr "" msgstr ""
"Project-Id-Version: \n" "Project-Id-Version: \n"
"Report-Msgid-Bugs-To: \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" "PO-Revision-Date: 2018-08-24 22:56-0500\n"
"Last-Translator: \n" "Last-Translator: \n"
"Language-Team: \n" "Language-Team: \n"
@ -294,6 +294,10 @@ msgstr ""
msgid "Array values should be single bytes." msgid "Array values should be single bytes."
msgstr "Valores del array deben ser bytes individuales." 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 #: main.c
msgid "Auto-reload is off.\n" msgid "Auto-reload is off.\n"
msgstr "Auto-recarga deshabilitada.\n" msgstr "Auto-recarga deshabilitada.\n"

View File

@ -8,7 +8,7 @@ msgid ""
msgstr "" msgstr ""
"Project-Id-Version: \n" "Project-Id-Version: \n"
"Report-Msgid-Bugs-To: \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" "PO-Revision-Date: 2018-12-20 22:15-0800\n"
"Last-Translator: Timothy <me@timothygarcia.ca>\n" "Last-Translator: Timothy <me@timothygarcia.ca>\n"
"Language-Team: fil\n" "Language-Team: fil\n"
@ -291,6 +291,10 @@ msgstr "May halfwords (type 'H') dapat ang array"
msgid "Array values should be single bytes." msgid "Array values should be single bytes."
msgstr "Array values ay dapat 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 #: main.c
msgid "Auto-reload is off.\n" msgid "Auto-reload is off.\n"
msgstr "Awtomatikong pag re-reload ay OFF.\n" msgstr "Awtomatikong pag re-reload ay OFF.\n"

View File

@ -8,7 +8,7 @@ msgid ""
msgstr "" msgstr ""
"Project-Id-Version: 0.1\n" "Project-Id-Version: 0.1\n"
"Report-Msgid-Bugs-To: \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" "PO-Revision-Date: 2018-12-23 20:05+0100\n"
"Last-Translator: Pierrick Couturier <arofarn@arofarn.info>\n" "Last-Translator: Pierrick Couturier <arofarn@arofarn.info>\n"
"Language-Team: fr\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." msgid "Array values should be single bytes."
msgstr "Les valeurs du tableau doivent être des octets simples '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 #: main.c
msgid "Auto-reload is off.\n" msgid "Auto-reload is off.\n"
msgstr "Auto-rechargement désactivé.\n" msgstr "Auto-rechargement désactivé.\n"

View File

@ -9,7 +9,7 @@ msgid ""
msgstr "" msgstr ""
"Project-Id-Version: PACKAGE VERSION\n" "Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \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" "PO-Revision-Date: 2018-10-02 16:27+0200\n"
"Last-Translator: Enrico Paganin <enrico.paganin@mail.com>\n" "Last-Translator: Enrico Paganin <enrico.paganin@mail.com>\n"
"Language-Team: \n" "Language-Team: \n"
@ -290,6 +290,10 @@ msgstr ""
msgid "Array values should be single bytes." msgid "Array values should be single bytes."
msgstr "" msgstr ""
#: supervisor/shared/safe_mode.c
msgid "Attempted heap allocation when MicroPython VM not running.\n"
msgstr ""
#: main.c #: main.c
msgid "Auto-reload is off.\n" msgid "Auto-reload is off.\n"
msgstr "Auto-reload disattivato.\n" msgstr "Auto-reload disattivato.\n"

View File

@ -9,7 +9,7 @@ msgid ""
msgstr "" msgstr ""
"Project-Id-Version: PACKAGE VERSION\n" "Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \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" "PO-Revision-Date: 2018-10-02 21:14-0000\n"
"Last-Translator: \n" "Last-Translator: \n"
"Language-Team: \n" "Language-Team: \n"
@ -290,6 +290,10 @@ msgstr "Array deve conter meias palavras (tipo 'H')"
msgid "Array values should be single bytes." msgid "Array values should be single bytes."
msgstr "" msgstr ""
#: supervisor/shared/safe_mode.c
msgid "Attempted heap allocation when MicroPython VM not running.\n"
msgstr ""
#: main.c #: main.c
msgid "Auto-reload is off.\n" msgid "Auto-reload is off.\n"
msgstr "A atualização automática está desligada.\n" msgstr "A atualização automática está desligada.\n"

3
main.c
View File

@ -139,8 +139,7 @@ void stop_mp(void) {
MP_STATE_VM(vfs_cur) = vfs; MP_STATE_VM(vfs_cur) = vfs;
#endif #endif
// Run any finalizers before we stop using the heap. gc_deinit();
gc_sweep_all();
} }
#define STRING_LIST(...) {__VA_ARGS__, ""} #define STRING_LIST(...) {__VA_ARGS__, ""}

13
py/gc.c
View File

@ -31,6 +31,8 @@
#include "py/gc.h" #include "py/gc.h"
#include "py/runtime.h" #include "py/runtime.h"
#include "supervisor/shared/safe_mode.h"
#if MICROPY_ENABLE_GC #if MICROPY_ENABLE_GC
#if MICROPY_DEBUG_VERBOSE // print debugging info #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); 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) { void gc_lock(void) {
GC_ENTER(); GC_ENTER();
MP_STATE_MEM(gc_lock_depth)++; 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; return NULL;
} }
if (MP_STATE_MEM(gc_pool_start) == 0) {
reset_into_safe_mode(GC_ALLOC_OUTSIDE_VM);
}
GC_ENTER(); GC_ENTER();
// check if GC is locked // check if GC is locked

View File

@ -32,6 +32,7 @@
#include "py/misc.h" #include "py/misc.h"
void gc_init(void *start, void *end); void gc_init(void *start, void *end);
void gc_deinit(void);
// These lock/unlock functions can be nested. // These lock/unlock functions can be nested.
// They can be used to prevent the GC from allocating/freeing. // They can be used to prevent the GC from allocating/freeing.

View File

@ -105,7 +105,7 @@ void print_safe_mode_message(safe_mode_t reason) {
if (reason != NO_SAFE_MODE) { if (reason != NO_SAFE_MODE) {
serial_write("\r\n"); serial_write("\r\n");
serial_write_compressed(translate("You are running in safe mode which means something unanticipated happened.\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")); 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) { if (reason == HARD_CRASH) {
serial_write_compressed(translate("Crash into the HardFault_Handler.\n")); 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")); serial_write_compressed(translate("MicroPython NLR jump failed. Likely memory corruption.\n"));
} else if (reason == MICROPY_FATAL_ERROR) { } else if (reason == MICROPY_FATAL_ERROR) {
serial_write_compressed(translate("MicroPython fatal error.\n")); 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) { } 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")); 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"));

View File

@ -35,7 +35,8 @@ typedef enum {
HEAP_OVERWRITTEN, HEAP_OVERWRITTEN,
MANUAL_SAFE_MODE, MANUAL_SAFE_MODE,
MICROPY_NLR_JUMP_FAIL, MICROPY_NLR_JUMP_FAIL,
MICROPY_FATAL_ERROR MICROPY_FATAL_ERROR,
GC_ALLOC_OUTSIDE_VM
} safe_mode_t; } safe_mode_t;
safe_mode_t wait_for_safe_mode_reset(void); safe_mode_t wait_for_safe_mode_reset(void);