summaryrefslogtreecommitdiff
path: root/py/gc.c
diff options
context:
space:
mode:
authorDamien George <damien.p.george@gmail.com>2016-08-26 15:35:26 +1000
committerDamien George <damien.p.george@gmail.com>2016-08-26 15:35:26 +1000
commit5ffe1d8dc07930818bbac6a88bec2aa5bd973402 (patch)
tree8832549d203085db88cb72d8abb6fea1c6d57d59 /py/gc.c
parentd29ca28288581ca788e468528ea1680b99eb49e5 (diff)
py/gc: Add MICROPY_GC_CONSERVATIVE_CLEAR option to always zero memory.
There can be stray pointers in memory blocks that are not properly zero'd after allocation. This patch adds a new config option to always zero all allocated memory (via gc_alloc and gc_realloc) and hence help to eliminate stray pointers. See issue #2195.
Diffstat (limited to 'py/gc.c')
-rw-r--r--py/gc.c10
1 files changed, 10 insertions, 0 deletions
diff --git a/py/gc.c b/py/gc.c
index 97868c07f..7ed53cfc7 100644
--- a/py/gc.c
+++ b/py/gc.c
@@ -480,12 +480,17 @@ found:
GC_EXIT();
+ #if MICROPY_GC_CONSERVATIVE_CLEAR
+ // be conservative and zero out all the newly allocated blocks
+ memset((byte*)ret_ptr, 0, (end_block - start_block + 1) * BYTES_PER_BLOCK);
+ #else
// zero out the additional bytes of the newly allocated blocks
// This is needed because the blocks may have previously held pointers
// to the heap and will not be set to something else if the caller
// doesn't actually use the entire block. As such they will continue
// to point to the heap and may prevent other blocks from being reclaimed.
memset((byte*)ret_ptr + n_bytes, 0, (end_block - start_block + 1) * BYTES_PER_BLOCK - n_bytes);
+ #endif
#if MICROPY_ENABLE_FINALISER
if (has_finaliser) {
@@ -713,8 +718,13 @@ void *gc_realloc(void *ptr_in, size_t n_bytes, bool allow_move) {
GC_EXIT();
+ #if MICROPY_GC_CONSERVATIVE_CLEAR
+ // be conservative and zero out all the newly allocated blocks
+ memset((byte*)ptr_in + n_blocks * BYTES_PER_BLOCK, 0, (new_blocks - n_blocks) * BYTES_PER_BLOCK);
+ #else
// zero out the additional bytes of the newly allocated blocks (see comment above in gc_alloc)
memset((byte*)ptr_in + n_bytes, 0, new_blocks * BYTES_PER_BLOCK - n_bytes);
+ #endif
#if EXTENSIVE_HEAP_PROFILING
gc_dump_alloc_table();