diff options
author | Sam Gross <colesbury@gmail.com> | 2024-02-20 15:36:40 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-02-20 15:36:40 (GMT) |
commit | cc82e33af978df793b83cefe4e25e07223a3a09e (patch) | |
tree | d3eea26833e8ffb71fe3b19dd45e54d0e9fe33f9 /Python | |
parent | c0b0c2f2015fb27db4306109b2b3781eb2057c2b (diff) | |
download | cpython-cc82e33af978df793b83cefe4e25e07223a3a09e.zip cpython-cc82e33af978df793b83cefe4e25e07223a3a09e.tar.gz cpython-cc82e33af978df793b83cefe4e25e07223a3a09e.tar.bz2 |
gh-115491: Keep some fields valid across allocations (free-threading) (#115573)
This avoids filling the memory occupied by ob_tid, ob_ref_local, and
ob_ref_shared with debug bytes (e.g., 0xDD) in mimalloc in the
free-threaded build.
Diffstat (limited to 'Python')
-rw-r--r-- | Python/pystate.c | 15 |
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. |