8000 Merge pull request #2317 from babygrimes/frozen-mpy-bytes-gc_long_liv… · adafruit/circuitpython@6e3b363 · GitHub
[go: up one dir, main page]

Skip to content

Commit 6e3b363

Browse files
authored
Merge pull request #2317 from babygrimes/frozen-mpy-bytes-gc_long_lived-crash
Only make objects long lived if they are on the GC heap
2 parents f047f3c + f13ba7e commit 6e3b363

File tree

3 files changed

+16
-10
lines changed
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,6 @@
5353
// detect untraced object still in use
5454
#define CLEAR_ON_SWEEP (0)
5555

56-
#define WORDS_PER_BLOCK ((MICROPY_BYTES_PER_GC_BLOCK) / BYTES_PER_WORD)
57-
#define BYTES_PER_BLOCK (MICROPY_BYTES_PER_GC_BLOCK)
58-
5956
// ATB = allocation table byte
6057
// 0b00 = FREE -- free block
6158
// 0b01 = HEAD -- head of a chain of blocks
@@ -209,13 +206,6 @@ bool gc_is_locked(void) {
209206
return MP_STATE_MEM(gc_lock_depth) != 0;
210207
}
211208

212-
// ptr should be of type voi 8000 d*
213-
#define VERIFY_PTR(ptr) ( \
214-
((uintptr_t)(ptr) & (BYTES_PER_BLOCK - 1)) == 0 /* must be aligned on a block */ \
215-
&& ptr >= (void*)MP_STATE_MEM(gc_pool_start) /* must be above start of pool */ \
216-
&& ptr < (void*)MP_STATE_MEM(gc_pool_end) /* must be below end of pool */ \
217-
)
218-
219209
#ifndef TRACE_MARK
220210
#if DEBUG_PRINT
221211
#define TRACE_MARK(block, ptr) DEBUG_printf("gc_mark(%p)\n", ptr)
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,19 @@
2929
#include <stdint.h>
3030

3131
#include "py/mpconfig.h"
32+
#include "py/mpstate.h"
3233
#include "py/misc.h"
3334

35+
#define WORDS_PER_BLOCK ((MICROPY_BYTES_PER_GC_BLOCK) / BYTES_PER_WORD)
36+
#define BYTES_PER_BLOCK (MICROPY_BYTES_PER_GC_BLOCK)
37+
38+
// ptr should be of type void*
39+
#define VERIFY_PTR(ptr) ( \
40+
((uintptr_t)(ptr) & (BYTES_PER_BLOCK - 1)) == 0 /* must be aligned on a block */ \
41+
&& ptr >= (void*)MP_STATE_MEM(gc_pool_start) /* must be above start of pool */ \
42+
&& ptr < (void*)MP_STATE_MEM(gc_pool_end) /* must be below end of pool */ \
43+
)
44+
3445
void gc_init(void *start, void *end);
3546
void gc_deinit(void);
3647

Original file line numberDiff line numberDiff line change
@@ -126,6 +126,11 @@ mp_obj_t make_obj_long_lived(mp_obj_t obj, uint8_t max_depth){
126126
if (obj == NULL) {
127127
return obj;
128128
}
129+
// If not in the GC pool, do nothing. This can happen (at least) when
130+
// there are frozen mp_type_bytes objects in ROM.
131+
if (!VERIFY_PTR((void < 54A6 span class=pl-c1>*)obj)) {
132+
return obj;
133+
}
129134
if (MP_OBJ_IS_TYPE(obj, &mp_type_fun_bc)) {
130135
mp_obj_fun_bc_t *fun_bc = MP_OBJ_TO_PTR(obj);
131136
return MP_OBJ_FROM_PTR(make_fun_bc_long_lived(fun_bc, max_depth));