@@ -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