Skip to content

Commit 5b946d3

Browse files
authored
gh-101430: Update tracemalloc to handle presize properly. (gh-101745)
1 parent f1f3af7 commit 5b946d3

File tree

3 files changed

+12
-23
lines changed

3 files changed

+12
-23
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Update :mod:`tracemalloc` to handle presize of object properly. Patch by
2+
Dong-hee Na.

Modules/_tracemalloc.c

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include "pycore_fileutils.h" // _Py_write_noraise()
33
#include "pycore_gc.h" // PyGC_Head
44
#include "pycore_hashtable.h" // _Py_hashtable_t
5+
#include "pycore_object.h" // _PyType_PreHeaderSize
56
#include "pycore_pymem.h" // _Py_tracemalloc_config
67
#include "pycore_runtime.h" // _Py_ID()
78
#include "pycore_traceback.h"
@@ -1400,20 +1401,16 @@ _tracemalloc__get_object_traceback(PyObject *module, PyObject *obj)
14001401
/*[clinic end generated code: output=41ee0553a658b0aa input=29495f1b21c53212]*/
14011402
{
14021403
PyTypeObject *type;
1403-
void *ptr;
14041404
traceback_t *traceback;
14051405

14061406
type = Py_TYPE(obj);
1407-
if (PyType_IS_GC(type)) {
1408-
ptr = (void *)((char *)obj - sizeof(PyGC_Head));
1409-
}
1410-
else {
1411-
ptr = (void *)obj;
1412-
}
1407+
const size_t presize = _PyType_PreHeaderSize(type);
1408+
uintptr_t ptr = (uintptr_t)((char *)obj - presize);
14131409

1414-
traceback = tracemalloc_get_traceback(DEFAULT_DOMAIN, (uintptr_t)ptr);
1415-
if (traceback == NULL)
1410+
traceback = tracemalloc_get_traceback(DEFAULT_DOMAIN, ptr);
1411+
if (traceback == NULL) {
14161412
Py_RETURN_NONE;
1413+
}
14171414

14181415
return traceback_to_pyobject(traceback, NULL);
14191416
}
@@ -1723,14 +1720,9 @@ _PyTraceMalloc_NewReference(PyObject *op)
17231720
return -1;
17241721
}
17251722

1726-
uintptr_t ptr;
17271723
PyTypeObject *type = Py_TYPE(op);
1728-
if (PyType_IS_GC(type)) {
1729-
ptr = (uintptr_t)((char *)op - sizeof(PyGC_Head));
1730-
}
1731-
else {
1732-
ptr = (uintptr_t)op;
1733-
}
1724+
const size_t presize = _PyType_PreHeaderSize(type);
1725+
uintptr_t ptr = (uintptr_t)((char *)op - presize);
17341726

17351727
int res = -1;
17361728

Objects/object.c

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2387,14 +2387,9 @@ _PyObject_AssertFailed(PyObject *obj, const char *expr, const char *msg,
23872387
/* Display the traceback where the object has been allocated.
23882388
Do it before dumping repr(obj), since repr() is more likely
23892389
to crash than dumping the traceback. */
2390-
void *ptr;
23912390
PyTypeObject *type = Py_TYPE(obj);
2392-
if (_PyType_IS_GC(type)) {
2393-
ptr = (void *)((char *)obj - sizeof(PyGC_Head));
2394-
}
2395-
else {
2396-
ptr = (void *)obj;
2397-
}
2391+
const size_t presize = _PyType_PreHeaderSize(type);
2392+
void *ptr = (void *)((char *)obj - presize);
23982393
_PyMem_DumpTraceback(fileno(stderr), ptr);
23992394

24002395
/* This might succeed or fail, but we're about to abort, so at least

0 commit comments

Comments
 (0)