Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions Lib/test/test_embed.py
Original file line number Diff line number Diff line change
Expand Up @@ -2062,6 +2062,24 @@ def test_open_code_hook(self):
def test_audit(self):
self.run_embedded_interpreter("test_audit")

def test_audit_hook_survives_allocator_change(self):

Copy link
Copy Markdown
Member

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.

# A hook added before Py_Initialize() is freed at finalization, after
# PYTHONMALLOC changed the raw allocator: the entry must not be
# allocated with one allocator and freed with another.
allocators = ['debug']
if not support.Py_GIL_DISABLED:
allocators.append('malloc_debug')
if support.with_mimalloc():
allocators.extend(['mimalloc', 'mimalloc_debug'])
for allocator in allocators:
with self.subTest(allocator=allocator):
self.run_embedded_interpreter(
"test_audit", env={'PYTHONMALLOC': allocator})
# The development mode installs the debug hooks too
with self.subTest(dev_mode=True):
self.run_embedded_interpreter(
"test_audit", env={'PYTHONDEVMODE': '1'})

def test_audit_tuple(self):
self.run_embedded_interpreter("test_audit_tuple")

Expand Down
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>`.
11 changes: 8 additions & 3 deletions Python/sysmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -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*()
Expand Down Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The 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);
Expand Down
Loading