Skip to content

Commit 20c0c81

Browse files
committed
pythongh-115491: Keep some fields valid across allocations in obmalloc (free-threading)
1 parent 58a2d1f commit 20c0c81

2 files changed

Lines changed: 33 additions & 10 deletions

File tree

‎Objects/object.c‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -299,13 +299,13 @@ _Py_DecRef(PyObject *o)
299299

300300
#ifdef Py_GIL_DISABLED
301301
# ifdef Py_REF_DEBUG
302-
static inline int
303-
is_shared_refcnt_dead(Py_ssize_t shared)
302+
static int
303+
is_dead(PyObject *o)
304304
{
305305
# if SIZEOF_SIZE_T == 8
306-
return shared == (Py_ssize_t)0xDDDDDDDDDDDDDDDD;
306+
return (uintptr_t)o->ob_type == 0xDDDDDDDDDDDDDDDD;
307307
# else
308-
return shared == (Py_ssize_t)0xDDDDDDDD;
308+
return (uintptr_t)o->ob_type == 0xDDDDDDDD;
309309
# endif
310310
}
311311
# endif
@@ -335,8 +335,8 @@ _Py_DecRefSharedDebug(PyObject *o, const char *filename, int lineno)
335335
}
336336

337337
#ifdef Py_REF_DEBUG
338-
if ((_Py_REF_IS_MERGED(new_shared) && new_shared < 0) ||
339-
is_shared_refcnt_dead(shared))
338+
if ((new_shared < 0 && _Py_REF_IS_MERGED(new_shared)) ||
339+
(should_queue && is_dead(o)))
340340
{
341341
_Py_NegativeRefcount(filename, lineno, o);
342342
}

‎Objects/obmalloc.c‎

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2269,6 +2269,24 @@ write_size_t(void *p, size_t n)
22692269
}
22702270
}
22712271

2272+
static void
2273+
fill_mem_debug(debug_alloc_api_t *api, void *data, int c, size_t nbytes)
2274+
{
2275+
#ifdef Py_GIL_DISABLED
2276+
if (api->api_id == 'o') {
2277+
// Don't overwrite the first few bytes of a PyObject allocation in the
2278+
// free-threaded build
2279+
_PyThreadStateImpl *tstate = (_PyThreadStateImpl *)_PyThreadState_GET();
2280+
size_t debug_offset = tstate->mimalloc.current_object_heap->debug_offset;
2281+
if (debug_offset < nbytes) {
2282+
memset((char *)data + debug_offset, c, nbytes - debug_offset);
2283+
}
2284+
return;
2285+
}
2286+
#endif
2287+
memset(data, c, nbytes);
2288+
}
2289+
22722290
/* Let S = sizeof(size_t). The debug malloc asks for 4 * S extra bytes and
22732291
fills them with useful stuff, here calling the underlying malloc's result p:
22742292
@@ -2345,7 +2363,7 @@ _PyMem_DebugRawAlloc(int use_calloc, void *ctx, size_t nbytes)
23452363
memset(p + SST + 1, PYMEM_FORBIDDENBYTE, SST-1);
23462364

23472365
if (nbytes > 0 && !use_calloc) {
2348-
memset(data, PYMEM_CLEANBYTE, nbytes);
2366+
fill_mem_debug(api, data, PYMEM_CLEANBYTE, nbytes);
23492367
}
23502368

23512369
/* at tail, write pad (SST bytes) and serialno (SST bytes) */
@@ -2393,8 +2411,9 @@ _PyMem_DebugRawFree(void *ctx, void *p)
23932411

23942412
_PyMem_DebugCheckAddress(__func__, api->api_id, p);
23952413
nbytes = read_size_t(q);
2396-
nbytes += PYMEM_DEBUG_EXTRA_BYTES;
2397-
memset(q, PYMEM_DEADBYTE, nbytes);
2414+
nbytes += PYMEM_DEBUG_EXTRA_BYTES - 2*SST;
2415+
memset(q, PYMEM_DEADBYTE, 2*SST);
2416+
fill_mem_debug(api, p, PYMEM_DEADBYTE, nbytes);
23982417
api->alloc.free(api->alloc.ctx, q);
23992418
}
24002419

@@ -2414,7 +2433,6 @@ _PyMem_DebugRawRealloc(void *ctx, void *p, size_t nbytes)
24142433
size_t total; /* 2 * SST + nbytes + 2 * SST */
24152434
size_t original_nbytes;
24162435
#define ERASED_SIZE 64
2417-
uint8_t save[2*ERASED_SIZE]; /* A copy of erased bytes. */
24182436

24192437
_PyMem_DebugCheckAddress(__func__, api->api_id, p);
24202438

@@ -2431,9 +2449,11 @@ _PyMem_DebugRawRealloc(void *ctx, void *p, size_t nbytes)
24312449
#ifdef PYMEM_DEBUG_SERIALNO
24322450
size_t block_serialno = read_size_t(tail + SST);
24332451
#endif
2452+
#ifndef Py_GIL_DISABLED
24342453
/* Mark the header, the trailer, ERASED_SIZE bytes at the begin and
24352454
ERASED_SIZE bytes at the end as dead and save the copy of erased bytes.
24362455
*/
2456+
uint8_t save[2*ERASED_SIZE]; /* A copy of erased bytes. */
24372457
if (original_nbytes <= sizeof(save)) {
24382458
memcpy(save, data, original_nbytes);
24392459
memset(data - 2 * SST, PYMEM_DEADBYTE,
@@ -2446,6 +2466,7 @@ _PyMem_DebugRawRealloc(void *ctx, void *p, size_t nbytes)
24462466
memset(tail - ERASED_SIZE, PYMEM_DEADBYTE,
24472467
ERASED_SIZE + PYMEM_DEBUG_EXTRA_BYTES - 2 * SST);
24482468
}
2469+
#endif
24492470

24502471
/* Resize and add decorations. */
24512472
r = (uint8_t *)api->alloc.realloc(api->alloc.ctx, head, total);
@@ -2473,6 +2494,7 @@ _PyMem_DebugRawRealloc(void *ctx, void *p, size_t nbytes)
24732494
write_size_t(tail + SST, block_serialno);
24742495
#endif
24752496

2497+
#ifndef Py_GIL_DISABLED
24762498
/* Restore saved bytes. */
24772499
if (original_nbytes <= sizeof(save)) {
24782500
memcpy(data, save, Py_MIN(nbytes, original_nbytes));
@@ -2485,6 +2507,7 @@ _PyMem_DebugRawRealloc(void *ctx, void *p, size_t nbytes)
24852507
Py_MIN(nbytes - i, ERASED_SIZE));
24862508
}
24872509
}
2510+
#endif
24882511

24892512
if (r == NULL) {
24902513
return NULL;

0 commit comments

Comments
 (0)