Commit 4c409beb authored by Victor Stinner's avatar Victor Stinner Committed by GitHub

bpo-36389: Change PyMem_SetupDebugHooks() constants (GH-12782)

Modify CLEANBYTE, DEADDYTE and FORBIDDENBYTE constants: use 0xCD,
0xDD and 0xFD, rather than 0xCB, 0xBB and 0xFB, to use the same byte
patterns than Windows CRT debug malloc() and free().
parent 536a35b3
...@@ -440,8 +440,9 @@ Customize Memory Allocators ...@@ -440,8 +440,9 @@ Customize Memory Allocators
Setup hooks to detect bugs in the Python memory allocator functions. Setup hooks to detect bugs in the Python memory allocator functions.
Newly allocated memory is filled with the byte ``0xCB``, freed memory is Newly allocated memory is filled with the byte ``0xCD`` (``CLEANBYTE``),
filled with the byte ``0xDB``. freed memory is filled with the byte ``0xDD`` (``DEADBYTE``). Memory blocks
are surrounded by "forbidden bytes" (``FORBIDDENBYTE``: byte ``0xFD``).
Runtime checks: Runtime checks:
...@@ -471,6 +472,12 @@ Customize Memory Allocators ...@@ -471,6 +472,12 @@ Customize Memory Allocators
if the GIL is held when functions of :c:data:`PYMEM_DOMAIN_OBJ` and if the GIL is held when functions of :c:data:`PYMEM_DOMAIN_OBJ` and
:c:data:`PYMEM_DOMAIN_MEM` domains are called. :c:data:`PYMEM_DOMAIN_MEM` domains are called.
.. versionchanged:: 3.8.0
Byte patterns ``0xCB`` (``CLEANBYTE``), ``0xDB`` (``DEADBYTE``) and
``0xFB`` (``FORBIDDENBYTE``) have been replaced with ``0xCD``, ``0xDD``
and ``0xFD`` to use the same values than Windows CRT debug ``malloc()``
and ``free()``.
.. _pymalloc: .. _pymalloc:
......
...@@ -160,21 +160,20 @@ PyAPI_FUNC(int) _PyMem_SetDefaultAllocator( ...@@ -160,21 +160,20 @@ PyAPI_FUNC(int) _PyMem_SetDefaultAllocator(
pointer value is checked. pointer value is checked.
The heuristic relies on the debug hooks on Python memory allocators which The heuristic relies on the debug hooks on Python memory allocators which
fills newly allocated memory with CLEANBYTE (0xCB) and newly freed memory fills newly allocated memory with CLEANBYTE (0xCD) and newly freed memory
with DEADBYTE (0xDB). Detect also "untouchable bytes" marked with DEADBYTE (0xDD). Detect also "untouchable bytes" marked
with FORBIDDENBYTE (0xFB). */ with FORBIDDENBYTE (0xFD). */
static inline int _PyMem_IsPtrFreed(void *ptr) static inline int _PyMem_IsPtrFreed(void *ptr)
{ {
uintptr_t value = (uintptr_t)ptr; uintptr_t value = (uintptr_t)ptr;
#if SIZEOF_VOID_P == 8 #if SIZEOF_VOID_P == 8
return (value == (uintptr_t)0xCBCBCBCBCBCBCBCB return (value == (uintptr_t)0xCDCDCDCDCDCDCDCD
|| value == (uintptr_t)0xDBDBDBDBDBDBDBDB || value == (uintptr_t)0xDDDDDDDDDDDDDDDD
|| value == (uintptr_t)0xFBFBFBFBFBFBFBFB || value == (uintptr_t)0xFDFDFDFDFDFDFDFD);
);
#elif SIZEOF_VOID_P == 4 #elif SIZEOF_VOID_P == 4
return (value == (uintptr_t)0xCBCBCBCB return (value == (uintptr_t)0xCDCDCDCD
|| value == (uintptr_t)0xDBDBDBDB || value == (uintptr_t)0xDDDDDDDD
|| value == (uintptr_t)0xFBFBFBFB); || value == (uintptr_t)0xFDFDFDFD);
#else #else
# error "unknown pointer size" # error "unknown pointer size"
#endif #endif
......
...@@ -480,11 +480,11 @@ class PyMemDebugTests(unittest.TestCase): ...@@ -480,11 +480,11 @@ class PyMemDebugTests(unittest.TestCase):
r" The [0-9] pad bytes at p-[0-9] are FORBIDDENBYTE, as expected.\n" r" The [0-9] pad bytes at p-[0-9] are FORBIDDENBYTE, as expected.\n"
r" The [0-9] pad bytes at tail={ptr} are not all FORBIDDENBYTE \(0x[0-9a-f]{{2}}\):\n" r" The [0-9] pad bytes at tail={ptr} are not all FORBIDDENBYTE \(0x[0-9a-f]{{2}}\):\n"
r" at tail\+0: 0x78 \*\*\* OUCH\n" r" at tail\+0: 0x78 \*\*\* OUCH\n"
r" at tail\+1: 0xfb\n" r" at tail\+1: 0xfd\n"
r" at tail\+2: 0xfb\n" r" at tail\+2: 0xfd\n"
r" .*\n" r" .*\n"
r" The block was made by call #[0-9]+ to debug malloc/realloc.\n" r" The block was made by call #[0-9]+ to debug malloc/realloc.\n"
r" Data at p: cb cb cb .*\n" r" Data at p: cd cd cd .*\n"
r"\n" r"\n"
r"Enable tracemalloc to get the memory block allocation traceback\n" r"Enable tracemalloc to get the memory block allocation traceback\n"
r"\n" r"\n"
...@@ -500,7 +500,7 @@ class PyMemDebugTests(unittest.TestCase): ...@@ -500,7 +500,7 @@ class PyMemDebugTests(unittest.TestCase):
r" The [0-9] pad bytes at p-[0-9] are FORBIDDENBYTE, as expected.\n" r" The [0-9] pad bytes at p-[0-9] are FORBIDDENBYTE, as expected.\n"
r" The [0-9] pad bytes at tail={ptr} are FORBIDDENBYTE, as expected.\n" r" The [0-9] pad bytes at tail={ptr} are FORBIDDENBYTE, as expected.\n"
r" The block was made by call #[0-9]+ to debug malloc/realloc.\n" r" The block was made by call #[0-9]+ to debug malloc/realloc.\n"
r" Data at p: cb cb cb .*\n" r" Data at p: cd cd cd .*\n"
r"\n" r"\n"
r"Enable tracemalloc to get the memory block allocation traceback\n" r"Enable tracemalloc to get the memory block allocation traceback\n"
r"\n" r"\n"
......
Change the value of ``CLEANBYTE``, ``DEADDYTE`` and ``FORBIDDENBYTE`` internal
constants used by debug hooks on Python memory allocators
(:c:func:`PyMem_SetupDebugHooks` function). 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()``.
...@@ -415,13 +415,12 @@ _Py_BreakPoint(void) ...@@ -415,13 +415,12 @@ _Py_BreakPoint(void)
} }
/* Heuristic checking if the object memory has been deallocated. /* Heuristic checking if the object memory is uninitialized or deallocated.
Rely on the debug hooks on Python memory allocators which fills the memory Rely on the debug hooks on Python memory allocators:
with DEADBYTE (0xDB) when memory is deallocated. see _PyMem_IsPtrFreed().
The function can be used to prevent segmentation fault on dereferencing The function can be used to prevent segmentation fault on dereferencing
pointers like 0xdbdbdbdbdbdbdbdb. Such pointer is very unlikely to be mapped pointers like 0xDDDDDDDDDDDDDDDD. */
in memory. */
int int
_PyObject_IsFreed(PyObject *op) _PyObject_IsFreed(PyObject *op)
{ {
......
...@@ -1915,13 +1915,16 @@ _Py_GetAllocatedBlocks(void) ...@@ -1915,13 +1915,16 @@ _Py_GetAllocatedBlocks(void)
/* Special bytes broadcast into debug memory blocks at appropriate times. /* Special bytes broadcast into debug memory blocks at appropriate times.
* Strings of these are unlikely to be valid addresses, floats, ints or * Strings of these are unlikely to be valid addresses, floats, ints or
* 7-bit ASCII. If modified, _PyMem_IsPtrFreed() should be updated as well. * 7-bit ASCII. If modified, _PyMem_IsPtrFreed() should be updated as well.
*
* Byte patterns 0xCB, 0xBB and 0xFB have been replaced with 0xCD, 0xDD and
* 0xFD to use the same values than Windows CRT debug malloc() and free().
*/ */
#undef CLEANBYTE #undef CLEANBYTE
#undef DEADBYTE #undef DEADBYTE
#undef FORBIDDENBYTE #undef FORBIDDENBYTE
#define CLEANBYTE 0xCB /* clean (newly allocated) memory */ #define CLEANBYTE 0xCD /* clean (newly allocated) memory */
#define DEADBYTE 0xDB /* dead (newly freed) memory */ #define DEADBYTE 0xDD /* dead (newly freed) memory */
#define FORBIDDENBYTE 0xFB /* untouchable bytes at each end of a block */ #define FORBIDDENBYTE 0xFD /* untouchable bytes at each end of a block */
static size_t serialno = 0; /* incremented on each debug {m,re}alloc */ static size_t serialno = 0; /* incremented on each debug {m,re}alloc */
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment