summaryrefslogtreecommitdiff
path: root/Objects/obmalloc.c
diff options
context:
space:
mode:
authorVictor Stinner <vstinner@redhat.com>2018-11-22 17:40:53 +0100
committerGitHub <noreply@github.com>2018-11-22 17:40:53 +0100
commitc9b3fc6b59b625c36c31ad437253e7140938af1a (patch)
treeb38312e30130c19f67b8874556c76e1c317e9d89 /Objects/obmalloc.c
parent0c15e508baec7e542933db2b31ea950a646cd968 (diff)
downloadcpython-git-c9b3fc6b59b625c36c31ad437253e7140938af1a.tar.gz
bpo-9263: _PyObject_Dump() detects freed memory (GH-10061) (GH-10662) (GH-10663)
* 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. (cherry picked from commit 82af0b63b07aa8d92b50098e382b458143cfc677) * bpo-9263: Fix _PyObject_Dump() for freed object (#10661) If _PyObject_Dump() detects that the object is freed, don't try to dump it (exit immediately). Enhance also _PyObject_IsFreed(): it now detects if the pointer itself looks like freed memory. (cherry picked from commit 2cf5d32fd9e61488e8b0be55a2e92a752ba8b06b) (cherry picked from commit 95036ea25d47f0081bda2ba96ea327f3375cb6a4)
Diffstat (limited to 'Objects/obmalloc.c')
-rw-r--r--Objects/obmalloc.c17
1 files changed, 17 insertions, 0 deletions
diff --git a/Objects/obmalloc.c b/Objects/obmalloc.c
index dca186801a..d46d149311 100644
--- a/Objects/obmalloc.c
+++ b/Objects/obmalloc.c
@@ -1907,6 +1907,23 @@ _PyMem_DebugRawCalloc(void *ctx, size_t nelem, size_t elsize)
return _PyMem_DebugRawAlloc(1, ctx, nbytes);
}
+
+/* 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.