diff options
author | Victor Stinner <vstinner@python.org> | 2019-10-15 01:06:16 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-10-15 01:06:16 (GMT) |
commit | f82ce5b1b12873b65927149a016be6a7c65e979d (patch) | |
tree | d2db47408daada0a4e49835871fda6e15d6050dc /Include | |
parent | 42308e8b27c8023e3f9d037f5e4a8892f2dcbc48 (diff) | |
download | cpython-f82ce5b1b12873b65927149a016be6a7c65e979d.zip cpython-f82ce5b1b12873b65927149a016be6a7c65e979d.tar.gz cpython-f82ce5b1b12873b65927149a016be6a7c65e979d.tar.bz2 |
[3.8] bpo-36389: Backport debug enhancements from master (GH-16796)
* bpo-36389: _PyObject_CheckConsistency() available in release mode (GH-16612)
bpo-36389, bpo-38376: The _PyObject_CheckConsistency() function is
now also available in release mode. For example, it can be used to
debug a crash in the visit_decref() function of the GC.
Modify the following functions to also work in release mode:
* _PyDict_CheckConsistency()
* _PyObject_CheckConsistency()
* _PyType_CheckConsistency()
* _PyUnicode_CheckConsistency()
Other changes:
* _PyMem_IsPtrFreed(ptr) now also returns 1 if ptr is NULL
(equals to 0).
* _PyBytesWriter_CheckConsistency() now returns 1 and is only used
with assert().
* Reorder _PyObject_Dump() to write safe fields first, and only
attempt to render repr() at the end.
(cherry picked from commit 6876257eaabdb30f27ebcbd7d2557278ce2e5705)
* bpo-36389: Fix _PyBytesWriter in release mode (GH-16624)
Fix _PyBytesWriter API when Python is built in release mode with
assertions.
(cherry picked from commit 60ec6efd96d95476fe5e38c491491add04f026e5)
* bpo-38070: Enhance visit_decref() debug trace (GH-16631)
subtract_refs() now pass the parent object to visit_decref() which
pass it to _PyObject_ASSERT(). So if the "is freed" assertion fails,
the parent is used in debug trace, rather than the freed object. The
parent object is more likely to contain useful information. Freed
objects cannot be inspected are are displayed as "<object at xxx is
freed>" with no other detail.
(cherry picked from commit 4d5f94b8cd20f804c7868c5395a15aa6032f874c)
* Fix also a typo in PYMEM_DEADBYTE macro comment
* bpo-36389: Add newline to _PyObject_AssertFailed() (GH-16629)
Add a newline between the verbose object dump and the Py_FatalError()
logs for readability.
(cherry picked from commit 7775349895088a7ae68cecf0c74cf817f15e2c74)
Diffstat (limited to 'Include')
-rw-r--r-- | Include/internal/pycore_pymem.h | 26 |
1 files changed, 22 insertions, 4 deletions
diff --git a/Include/internal/pycore_pymem.h b/Include/internal/pycore_pymem.h index 22677d3..47d092f 100644 --- a/Include/internal/pycore_pymem.h +++ b/Include/internal/pycore_pymem.h @@ -155,9 +155,25 @@ PyAPI_FUNC(int) _PyMem_SetDefaultAllocator( PyMemAllocatorDomain domain, PyMemAllocatorEx *old_alloc); +/* Special bytes broadcast into debug memory blocks at appropriate times. + Strings of these are unlikely to be valid addresses, floats, ints or + 7-bit ASCII. + + - PYMEM_CLEANBYTE: clean (newly allocated) memory + - PYMEM_DEADBYTE dead (newly freed) memory + - PYMEM_FORBIDDENBYTE: untouchable bytes at each end of a block + + Byte patterns 0xCB, 0xDB and 0xFB have been replaced with 0xCD, 0xDD and + 0xFD to use the same values than Windows CRT debug malloc() and free(). + If modified, _PyMem_IsPtrFreed() should be updated as well. */ +#define PYMEM_CLEANBYTE 0xCD +#define PYMEM_DEADBYTE 0xDD +#define PYMEM_FORBIDDENBYTE 0xFD + /* Heuristic checking if a pointer value is newly allocated - (uninitialized) or newly freed. The pointer is not dereferenced, only the - pointer value is checked. + (uninitialized), newly freed or NULL (is equal to zero). + + The pointer is not dereferenced, only the pointer value is checked. The heuristic relies on the debug hooks on Python memory allocators which fills newly allocated memory with CLEANBYTE (0xCD) and newly freed memory @@ -167,11 +183,13 @@ static inline int _PyMem_IsPtrFreed(void *ptr) { uintptr_t value = (uintptr_t)ptr; #if SIZEOF_VOID_P == 8 - return (value == (uintptr_t)0xCDCDCDCDCDCDCDCD + return (value == 0 + || value == (uintptr_t)0xCDCDCDCDCDCDCDCD || value == (uintptr_t)0xDDDDDDDDDDDDDDDD || value == (uintptr_t)0xFDFDFDFDFDFDFDFD); #elif SIZEOF_VOID_P == 4 - return (value == (uintptr_t)0xCDCDCDCD + return (value == 0 + || value == (uintptr_t)0xCDCDCDCD || value == (uintptr_t)0xDDDDDDDD || value == (uintptr_t)0xFDFDFDFD); #else |