summaryrefslogtreecommitdiffstats
path: root/Python/pystate.c
diff options
context:
space:
mode:
Diffstat (limited to 'Python/pystate.c')
-rw-r--r--Python/pystate.c15
1 files changed, 15 insertions, 0 deletions
diff --git a/Python/pystate.c b/Python/pystate.c
index d1d66e2..1d8c096 100644
--- a/Python/pystate.c
+++ b/Python/pystate.c
@@ -2845,9 +2845,24 @@ tstate_mimalloc_bind(PyThreadState *tstate)
// pools to keep Python objects from different interpreters separate.
tld->segments.abandoned = &tstate->interp->mimalloc.abandoned_pool;
+ // Don't fill in the first N bytes up to ob_type in debug builds. We may
+ // access ob_tid and the refcount fields in the dict and list lock-less
+ // accesses, so they must remain valid for a while after deallocation.
+ size_t base_offset = offsetof(PyObject, ob_type);
+ if (_PyMem_DebugEnabled()) {
+ // The debug allocator adds two words at the beginning of each block.
+ base_offset += 2 * sizeof(size_t);
+ }
+ size_t debug_offsets[_Py_MIMALLOC_HEAP_COUNT] = {
+ [_Py_MIMALLOC_HEAP_OBJECT] = base_offset,
+ [_Py_MIMALLOC_HEAP_GC] = base_offset,
+ [_Py_MIMALLOC_HEAP_GC_PRE] = base_offset + 2 * sizeof(PyObject *),
+ };
+
// Initialize each heap
for (uint8_t i = 0; i < _Py_MIMALLOC_HEAP_COUNT; i++) {
_mi_heap_init_ex(&mts->heaps[i], tld, _mi_arena_id_none(), false, i);
+ mts->heaps[i].debug_offset = (uint8_t)debug_offsets[i];
}
// By default, object allocations use _Py_MIMALLOC_HEAP_OBJECT.