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.
This commit is contained in:
parent
d29ca28288
commit
5ffe1d8dc0
10
py/gc.c
10
py/gc.c
|
@ -480,12 +480,17 @@ found:
|
||||||
|
|
||||||
GC_EXIT();
|
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
|
// zero out the additional bytes of the newly allocated blocks
|
||||||
// This is needed because the blocks may have previously held pointers
|
// 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
|
// 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
|
// 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.
|
// 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);
|
memset((byte*)ret_ptr + n_bytes, 0, (end_block - start_block + 1) * BYTES_PER_BLOCK - n_bytes);
|
||||||
|
#endif
|
||||||
|
|
||||||
#if MICROPY_ENABLE_FINALISER
|
#if MICROPY_ENABLE_FINALISER
|
||||||
if (has_finaliser) {
|
if (has_finaliser) {
|
||||||
|
@ -713,8 +718,13 @@ void *gc_realloc(void *ptr_in, size_t n_bytes, bool allow_move) {
|
||||||
|
|
||||||
GC_EXIT();
|
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)
|
// 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);
|
memset((byte*)ptr_in + n_bytes, 0, new_blocks * BYTES_PER_BLOCK - n_bytes);
|
||||||
|
#endif
|
||||||
|
|
||||||
#if EXTENSIVE_HEAP_PROFILING
|
#if EXTENSIVE_HEAP_PROFILING
|
||||||
gc_dump_alloc_table();
|
gc_dump_alloc_table();
|
||||||
|
|
|
@ -117,7 +117,10 @@ void *m_malloc0(size_t num_bytes) {
|
||||||
if (ptr == NULL && num_bytes != 0) {
|
if (ptr == NULL && num_bytes != 0) {
|
||||||
return m_malloc_fail(num_bytes);
|
return m_malloc_fail(num_bytes);
|
||||||
}
|
}
|
||||||
|
// If this config is set then the GC clears all memory, so we don't need to.
|
||||||
|
#if !MICROPY_GC_CONSERVATIVE_CLEAR
|
||||||
memset(ptr, 0, num_bytes);
|
memset(ptr, 0, num_bytes);
|
||||||
|
#endif
|
||||||
return ptr;
|
return ptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -107,6 +107,15 @@
|
||||||
#define MICROPY_ALLOC_GC_STACK_SIZE (64)
|
#define MICROPY_ALLOC_GC_STACK_SIZE (64)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
// Be conservative and always clear to zero newly (re)allocated memory in the GC.
|
||||||
|
// This helps eliminate stray pointers that hold on to memory that's no longer
|
||||||
|
// used. It decreases performance due to unnecessary memory clearing.
|
||||||
|
// TODO Do analysis to understand why some memory is not properly cleared and
|
||||||
|
// find a more efficient way to clear it.
|
||||||
|
#ifndef MICROPY_GC_CONSERVATIVE_CLEAR
|
||||||
|
#define MICROPY_GC_CONSERVATIVE_CLEAR (1)
|
||||||
|
#endif
|
||||||
|
|
||||||
// Support automatic GC when reaching allocation threshold,
|
// Support automatic GC when reaching allocation threshold,
|
||||||
// configurable by gc.threshold().
|
// configurable by gc.threshold().
|
||||||
#ifndef MICROPY_GC_ALLOC_THRESHOLD
|
#ifndef MICROPY_GC_ALLOC_THRESHOLD
|
||||||
|
|
Loading…
Reference in New Issue