Repository navigation
gh-161407: Allocate audit hook entries with malloc() #161409
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| Fix a crash at exit when an audit hook was added with :c:func:`PySys_AddAuditHook` | ||
| before :c:func:`Py_Initialize` and the raw memory allocator changed during startup, for | ||
| example with ``PYTHONMALLOC=mimalloc`` or in the :ref:`Python Development Mode <devmode>`. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -32,6 +32,7 @@ Data members: | |
| #include "pycore_pylifecycle.h" // _PyErr_WriteUnraisableDefaultHook() | ||
| #include "pycore_pymath.h" // _PY_SHORT_FLOAT_REPR | ||
| #include "pycore_pymem.h" // _PyMem_DefaultRawFree() | ||
| #include "pycore_pymem_init.h" // _PyMem_RawMalloc() | ||
| #include "pycore_pystate.h" // _PyThreadState_GET() | ||
| #include "pycore_pystats.h" // _Py_PrintSpecializationStats() | ||
| #include "pycore_runtime.h" // _PyRuntimeState_Get*() | ||
|
|
@@ -448,7 +449,7 @@ _PySys_ClearAuditHooks(PyThreadState *ts) | |
| runtime->audit_hooks.head = NULL; | ||
| while (e) { | ||
| n = e->next; | ||
| PyMem_RawFree(e); | ||
| _PyMem_RawFree(NULL, e); | ||
| e = n; | ||
| } | ||
| } | ||
|
|
@@ -496,8 +497,12 @@ PySys_AddAuditHook(Py_AuditHookFunction hook, void *userData) | |
| } | ||
| } | ||
|
|
||
| _Py_AuditHookEntry *e = (_Py_AuditHookEntry*)PyMem_RawMalloc( | ||
| sizeof(_Py_AuditHookEntry)); | ||
| /* Use malloc(), not PyMem_RawMalloc(): a hook can be added before | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Honestly, I don't think that it's worth it to bother with _PyMem_RawMalloc() wrapper (and pass NULL as context). I would prefer to use malloc() / free() directly. The "new" PyInitConfig C API also uses directly malloc() / free() to avoid troubles with changing the memory allocator. |
||
| Py_Initialize(), and the entry is freed during finalization, after | ||
| PYTHONMALLOC may have changed the raw allocator (and, in debug builds, | ||
| the allocator under the debug hooks). */ | ||
| _Py_AuditHookEntry *e = (_Py_AuditHookEntry*)_PyMem_RawMalloc( | ||
| NULL, sizeof(_Py_AuditHookEntry)); | ||
| if (!e) { | ||
| if (tstate != NULL) { | ||
| _PyErr_NoMemory(tstate); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think that adding a test is useful, since Python/sysmodule.c no longer depends on the Python memory allocator (PYTHONMALLOC) with this change. I suggest removing the test.