gh-116008: Detect freed thread state in faulthandler (#141988)

Add _PyMem_IsULongFreed() function.
This commit is contained in:
Victor Stinner 2025-11-27 12:35:00 +01:00 committed by GitHub
parent 83d8134c5b
commit d5d9e89dde
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 40 additions and 4 deletions

View file

@ -70,6 +70,27 @@ static inline int _PyMem_IsPtrFreed(const void *ptr)
#endif
}
// Similar to _PyMem_IsPtrFreed() but expects an 'unsigned long' instead of a
// pointer.
static inline int _PyMem_IsULongFreed(unsigned long value)
{
#if SIZEOF_LONG == 8
return (value == 0
|| value == (unsigned long)0xCDCDCDCDCDCDCDCD
|| value == (unsigned long)0xDDDDDDDDDDDDDDDD
|| value == (unsigned long)0xFDFDFDFDFDFDFDFD
|| value == (unsigned long)0xFFFFFFFFFFFFFFFF);
#elif SIZEOF_LONG == 4
return (value == 0
|| value == (unsigned long)0xCDCDCDCD
|| value == (unsigned long)0xDDDDDDDD
|| value == (unsigned long)0xFDFDFDFD
|| value == (unsigned long)0xFFFFFFFF);
#else
# error "unknown long size"
#endif
}
extern int _PyMem_GetAllocatorName(
const char *name,
PyMemAllocatorName *allocator);