summaryrefslogtreecommitdiffstats
path: root/Objects/obmalloc.c
diff options
context:
space:
mode:
authorVictor Stinner <vstinner@redhat.com>2018-10-23 15:39:40 (GMT)
committerGitHub <noreply@github.com>2018-10-23 15:39:40 (GMT)
commit82af0b63b07aa8d92b50098e382b458143cfc677 (patch)
tree77c2d497083f78ad622f75ffeb9680c6583d3317 /Objects/obmalloc.c
parent96f2c739542d48edd6bd15c26b555c7e59d14cce (diff)
downloadcpython-82af0b63b07aa8d92b50098e382b458143cfc677.zip
cpython-82af0b63b07aa8d92b50098e382b458143cfc677.tar.gz
cpython-82af0b63b07aa8d92b50098e382b458143cfc677.tar.bz2
bpo-9263: _PyObject_Dump() detects freed memory (GH-10061)
_PyObject_Dump() now uses an heuristic to check if the object memory has been freed: log "<freed object>" in that case. The heuristic rely on the debug hooks on Python memory allocators which fills the memory with DEADBYTE (0xDB) when memory is deallocated. Use PYTHONMALLOC=debug to always enable these debug hooks.
Diffstat (limited to 'Objects/obmalloc.c')
-rw-r--r--Objects/obmalloc.c16
1 files changed, 16 insertions, 0 deletions
diff --git a/Objects/obmalloc.c b/Objects/obmalloc.c
index eb7cbfc..d58da35 100644
--- a/Objects/obmalloc.c
+++ b/Objects/obmalloc.c
@@ -2033,6 +2033,22 @@ _PyMem_DebugRawCalloc(void *ctx, size_t nelem, size_t elsize)
}
+/* Heuristic checking if the memory has been freed. Rely on the debug hooks on
+ Python memory allocators which fills the memory with DEADBYTE (0xDB) when
+ memory is deallocated. */
+int
+_PyMem_IsFreed(void *ptr, size_t size)
+{
+ unsigned char *bytes = ptr;
+ for (size_t i=0; i < size; i++) {
+ if (bytes[i] != DEADBYTE) {
+ return 0;
+ }
+ }
+ return 1;
+}
+
+
/* The debug free first checks the 2*SST bytes on each end for sanity (in
particular, that the FORBIDDENBYTEs with the api ID are still intact).
Then fills the original bytes with DEADBYTE.