Skip to content

Commit 5ffe1d8

Browse files
committed
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 adafruit#2195.
1 parent d29ca28 commit 5ffe1d8

3 files changed

Lines changed: 22 additions & 0 deletions

File tree

py/gc.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -480,12 +480,17 @@ void *gc_alloc(size_t n_bytes, bool has_finaliser) {
480480

481481
GC_EXIT();
482482

483+
#if MICROPY_GC_CONSERVATIVE_CLEAR
484+
// be conservative and zero out all the newly allocated blocks
485+
memset((byte*)ret_ptr, 0, (end_block - start_block + 1) * BYTES_PER_BLOCK);
486+
#else
483487
// zero out the additional bytes of the newly allocated blocks
484488
// This is needed because the blocks may have previously held pointers
485489
// to the heap and will not be set to something else if the caller
486490
// doesn't actually use the entire block. As such they will continue
487491
// to point to the heap and may prevent other blocks from being reclaimed.
488492
memset((byte*)ret_ptr + n_bytes, 0, (end_block - start_block + 1) * BYTES_PER_BLOCK - n_bytes);
493+
#endif
489494

490495
#if MICROPY_ENABLE_FINALISER
491496
if (has_finaliser) {
@@ -713,8 +718,13 @@ void *gc_realloc(void *ptr_in, size_t n_bytes, bool allow_move) {
713718

714719
GC_EXIT();
715720

721+
#if MICROPY_GC_CONSERVATIVE_CLEAR
722+
// be conservative and zero out all the newly allocated blocks
723+
memset((byte*)ptr_in + n_blocks * BYTES_PER_BLOCK, 0, (new_blocks - n_blocks) * BYTES_PER_BLOCK);
724+
#else
716725
// zero out the additional bytes of the newly allocated blocks (see comment above in gc_alloc)
717726
memset((byte*)ptr_in + n_bytes, 0, new_blocks * BYTES_PER_BLOCK - n_bytes);
727+
#endif
718728

719729
#if EXTENSIVE_HEAP_PROFILING
720730
gc_dump_alloc_table();

py/malloc.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,10 @@ void *m_malloc0(size_t num_bytes) {
117117
if (ptr == NULL && num_bytes != 0) {
118118
return m_malloc_fail(num_bytes);
119119
}
120+
// If this config is set then the GC clears all memory, so we don't need to.
121+
#if !MICROPY_GC_CONSERVATIVE_CLEAR
120122
memset(ptr, 0, num_bytes);
123+
#endif
121124
return ptr;
122125
}
123126

py/mpconfig.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,15 @@
107107
#define MICROPY_ALLOC_GC_STACK_SIZE (64)
108108
#endif
109109

110+
// Be conservative and always clear to zero newly (re)allocated memory in the GC.
111+
// This helps eliminate stray pointers that hold on to memory that's no longer
112+
// used. It decreases performance due to unnecessary memory clearing.
113+
// TODO Do analysis to understand why some memory is not properly cleared and
114+
// find a more efficient way to clear it.
115+
#ifndef MICROPY_GC_CONSERVATIVE_CLEAR
116+
#define MICROPY_GC_CONSERVATIVE_CLEAR (1)
117+
#endif
118+
110119
// Support automatic GC when reaching allocation threshold,
111120
// configurable by gc.threshold().
112121
#ifndef MICROPY_GC_ALLOC_THRESHOLD

0 commit comments

Comments
 (0)