From 03be42ab847f04a89d207d98fdb28048cfedf72d Mon Sep 17 00:00:00 2001 From: Scott Shawcroft Date: Fri, 8 Mar 2019 15:29:14 -0800 Subject: Enter safe mode when an allocation is attempted on an uninitialized heap. --- py/gc.c | 13 +++++++++++++ py/gc.h | 1 + 2 files changed, 14 insertions(+) (limited to 'py') diff --git a/py/gc.c b/py/gc.c index 1f5cd8d01..e0b77c4cf 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 64f4b0d17..c05e006b4 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. -- cgit v1.2.3