gh-101430: Update tracemalloc to handle presize properly. (gh-101745)

This commit is contained in:
Dong-hee Na 2023-02-10 08:30:03 +09:00 committed by GitHub
parent f1f3af7b82
commit 5b946d3719
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 12 additions and 23 deletions

View File

@ -0,0 +1,2 @@
Update :mod:`tracemalloc` to handle presize of object properly. Patch by
Dong-hee Na.

View File

@ -2,6 +2,7 @@
#include "pycore_fileutils.h" // _Py_write_noraise() #include "pycore_fileutils.h" // _Py_write_noraise()
#include "pycore_gc.h" // PyGC_Head #include "pycore_gc.h" // PyGC_Head
#include "pycore_hashtable.h" // _Py_hashtable_t #include "pycore_hashtable.h" // _Py_hashtable_t
#include "pycore_object.h" // _PyType_PreHeaderSize
#include "pycore_pymem.h" // _Py_tracemalloc_config #include "pycore_pymem.h" // _Py_tracemalloc_config
#include "pycore_runtime.h" // _Py_ID() #include "pycore_runtime.h" // _Py_ID()
#include "pycore_traceback.h" #include "pycore_traceback.h"
@ -1400,20 +1401,16 @@ _tracemalloc__get_object_traceback(PyObject *module, PyObject *obj)
/*[clinic end generated code: output=41ee0553a658b0aa input=29495f1b21c53212]*/ /*[clinic end generated code: output=41ee0553a658b0aa input=29495f1b21c53212]*/
{ {
PyTypeObject *type; PyTypeObject *type;
void *ptr;
traceback_t *traceback; traceback_t *traceback;
type = Py_TYPE(obj); type = Py_TYPE(obj);
if (PyType_IS_GC(type)) { const size_t presize = _PyType_PreHeaderSize(type);
ptr = (void *)((char *)obj - sizeof(PyGC_Head)); uintptr_t ptr = (uintptr_t)((char *)obj - presize);
}
else {
ptr = (void *)obj;
}
traceback = tracemalloc_get_traceback(DEFAULT_DOMAIN, (uintptr_t)ptr); traceback = tracemalloc_get_traceback(DEFAULT_DOMAIN, ptr);
if (traceback == NULL) if (traceback == NULL) {
Py_RETURN_NONE; Py_RETURN_NONE;
}
return traceback_to_pyobject(traceback, NULL); return traceback_to_pyobject(traceback, NULL);
} }
@ -1723,14 +1720,9 @@ _PyTraceMalloc_NewReference(PyObject *op)
return -1; return -1;
} }
uintptr_t ptr;
PyTypeObject *type = Py_TYPE(op); PyTypeObject *type = Py_TYPE(op);
if (PyType_IS_GC(type)) { const size_t presize = _PyType_PreHeaderSize(type);
ptr = (uintptr_t)((char *)op - sizeof(PyGC_Head)); uintptr_t ptr = (uintptr_t)((char *)op - presize);
}
else {
ptr = (uintptr_t)op;
}
int res = -1; int res = -1;

View File

@ -2387,14 +2387,9 @@ _PyObject_AssertFailed(PyObject *obj, const char *expr, const char *msg,
/* Display the traceback where the object has been allocated. /* Display the traceback where the object has been allocated.
Do it before dumping repr(obj), since repr() is more likely Do it before dumping repr(obj), since repr() is more likely
to crash than dumping the traceback. */ to crash than dumping the traceback. */
void *ptr;
PyTypeObject *type = Py_TYPE(obj); PyTypeObject *type = Py_TYPE(obj);
if (_PyType_IS_GC(type)) { const size_t presize = _PyType_PreHeaderSize(type);
ptr = (void *)((char *)obj - sizeof(PyGC_Head)); void *ptr = (void *)((char *)obj - presize);
}
else {
ptr = (void *)obj;
}
_PyMem_DumpTraceback(fileno(stderr), ptr); _PyMem_DumpTraceback(fileno(stderr), ptr);
/* This might succeed or fail, but we're about to abort, so at least /* This might succeed or fail, but we're about to abort, so at least