Skip to content

Commit 31b9dd4

Browse files
authored
Merge pull request #5245 from jepler/pool-fix-circuitpython
gc.c: Ensure a gap of one byte before the finaliser table.
2 parents 3e9daec + 955c027 commit 31b9dd4

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

py/gc.c

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@
3232
#include "py/gc.h"
3333
#include "py/runtime.h"
3434

35+
#if MICROPY_DEBUG_VALGRIND
36+
#include <valgrind/memcheck.h>
37+
#endif
38+
3539
#include "supervisor/shared/safe_mode.h"
3640

3741
#if CIRCUITPY_MEMORYMONITOR
@@ -126,7 +130,7 @@ void gc_init(void *start, void *end) {
126130
// => T = A * (1 + BLOCKS_PER_ATB / BLOCKS_PER_FTB + BLOCKS_PER_ATB * BYTES_PER_BLOCK)
127131
size_t total_byte_len = (byte *)end - (byte *)start;
128132
#if MICROPY_ENABLE_FINALISER
129-
MP_STATE_MEM(gc_alloc_table_byte_len) = total_byte_len * MP_BITS_PER_BYTE / (MP_BITS_PER_BYTE + MP_BITS_PER_BYTE * BLOCKS_PER_ATB / BLOCKS_PER_FTB + MP_BITS_PER_BYTE * BLOCKS_PER_ATB * BYTES_PER_BLOCK);
133+
MP_STATE_MEM(gc_alloc_table_byte_len) = (total_byte_len - 1) * MP_BITS_PER_BYTE / (MP_BITS_PER_BYTE + MP_BITS_PER_BYTE * BLOCKS_PER_ATB / BLOCKS_PER_FTB + MP_BITS_PER_BYTE * BLOCKS_PER_ATB * BYTES_PER_BLOCK);
130134
#else
131135
MP_STATE_MEM(gc_alloc_table_byte_len) = total_byte_len / (1 + MP_BITS_PER_BYTE / 2 * BYTES_PER_BLOCK);
132136
#endif
@@ -135,7 +139,7 @@ void gc_init(void *start, void *end) {
135139

136140
#if MICROPY_ENABLE_FINALISER
137141
size_t gc_finaliser_table_byte_len = (MP_STATE_MEM(gc_alloc_table_byte_len) * BLOCKS_PER_ATB + BLOCKS_PER_FTB - 1) / BLOCKS_PER_FTB;
138-
MP_STATE_MEM(gc_finaliser_table_start) = MP_STATE_MEM(gc_alloc_table_start) + MP_STATE_MEM(gc_alloc_table_byte_len);
142+
MP_STATE_MEM(gc_finaliser_table_start) = MP_STATE_MEM(gc_alloc_table_start) + MP_STATE_MEM(gc_alloc_table_byte_len) + 1;
139143
#endif
140144

141145
size_t gc_pool_block_len = MP_STATE_MEM(gc_alloc_table_byte_len) * BLOCKS_PER_ATB;
@@ -391,6 +395,11 @@ void gc_collect_ptr(void *ptr) {
391395
__attribute__((no_sanitize_address))
392396
#endif
393397
static void *gc_get_ptr(void **ptrs, int i) {
398+
#if MICROPY_DEBUG_VALGRIND
399+
if (!VALGRIND_CHECK_MEM_IS_ADDRESSABLE(&ptrs[i], sizeof(*ptrs))) {
400+
return NULL;
401+
}
402+
#endif
394403
return ptrs[i];
395404
}
396405

py/mpconfig.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,11 @@
486486
#define MICROPY_DEBUG_VM_STACK_OVERFLOW (0)
487487
#endif
488488

489+
// Whether to enable extra instrumentation for valgrind
490+
#ifndef MICROPY_DEBUG_VALGRIND
491+
#define MICROPY_DEBUG_VALGRIND (0)
492+
#endif
493+
489494
/*****************************************************************************/
490495
/* Optimisations */
491496

0 commit comments

Comments
 (0)