gh-111924: Fix data races when swapping allocators by colesbury · Pull Request #130287 · python/cpython · GitHub
Skip to content
Merged
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
15 changes: 7 additions & 8 deletions Include/internal/pycore_pymem.h
73 changes: 62 additions & 11 deletions Objects/obmalloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,68 @@ void _PyMem_DebugFree(void *ctx, void *p);
#define PYDBGOBJ_ALLOC \
{&_PyRuntime.allocators.debug.obj, _PyMem_DebugMalloc, _PyMem_DebugCalloc, _PyMem_DebugRealloc, _PyMem_DebugFree}

/* default raw allocator (not swappable) */

void *
_PyMem_DefaultRawMalloc(size_t size)
{
#ifdef Py_DEBUG
return _PyMem_DebugRawMalloc(&_PyRuntime.allocators.debug.raw, size);
#else
return _PyMem_RawMalloc(NULL, size);
#endif
}

void *
_PyMem_DefaultRawCalloc(size_t nelem, size_t elsize)
{
#ifdef Py_DEBUG
return _PyMem_DebugRawCalloc(&_PyRuntime.allocators.debug.raw, nelem, elsize);
#else
return _PyMem_RawCalloc(NULL, nelem, elsize);
#endif
}

void *
_PyMem_DefaultRawRealloc(void *ptr, size_t size)
{
#ifdef Py_DEBUG
return _PyMem_DebugRawRealloc(&_PyRuntime.allocators.debug.raw, ptr, size);
#else
return _PyMem_RawRealloc(NULL, ptr, size);
#endif
}

void
_PyMem_DefaultRawFree(void *ptr)
{
#ifdef Py_DEBUG
_PyMem_DebugRawFree(&_PyRuntime.allocators.debug.raw, ptr);
#else
_PyMem_RawFree(NULL, ptr);
#endif
}

wchar_t*
_PyMem_DefaultRawWcsdup(const wchar_t *str)
{
assert(str != NULL);

size_t len = wcslen(str);
if (len > (size_t)PY_SSIZE_T_MAX / sizeof(wchar_t) - 1) {
return NULL;
}

size_t size = (len + 1) * sizeof(wchar_t);
wchar_t *str2 = _PyMem_DefaultRawMalloc(size);
if (str2 == NULL) {
return NULL;
}

memcpy(str2, str, size);
return str2;
}

/* the low-level virtual memory allocator */

#ifdef WITH_PYMALLOC
Expand Down Expand Up @@ -492,17 +554,6 @@ static const int pydebug = 1;
static const int pydebug = 0;
#endif

int
_PyMem_SetDefaultAllocator(PyMemAllocatorDomain domain,
PyMemAllocatorEx *old_alloc)
{
PyMutex_Lock(&ALLOCATORS_MUTEX);
int res = set_default_allocator_unlocked(domain, pydebug, old_alloc);
PyMutex_Unlock(&ALLOCATORS_MUTEX);
return res;
}


int
_PyMem_GetAllocatorName(const char *name, PyMemAllocatorName *allocator)
{
Expand Down
43 changes: 7 additions & 36 deletions Python/import.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#include "pycore_pyerrors.h" // _PyErr_SetString()
#include "pycore_pyhash.h" // _Py_KeyedHash()
#include "pycore_pylifecycle.h"
#include "pycore_pymem.h" // _PyMem_SetDefaultAllocator()
#include "pycore_pymem.h" // _PyMem_DefaultRawFree()
#include "pycore_pystate.h" // _PyInterpreterState_GET()
#include "pycore_sysmodule.h" // _PySys_ClearAttrString()
#include "pycore_time.h" // _PyTime_AsMicroseconds()
Expand Down Expand Up @@ -2387,14 +2387,11 @@ PyImport_ExtendInittab(struct _inittab *newtab)

/* Force default raw memory allocator to get a known allocator to be able
to release the memory in _PyImport_Fini2() */
PyMemAllocatorEx old_alloc;
_PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);

/* Allocate new memory for the combined table */
p = NULL;
if (i + n <= SIZE_MAX / sizeof(struct _inittab) - 1) {
size_t size = sizeof(struct _inittab) * (i + n + 1);
p = PyMem_RawRealloc(inittab_copy, size);
p = _PyMem_DefaultRawRealloc(inittab_copy, size);
}
if (p == NULL) {
res = -1;
Expand All @@ -2408,9 +2405,7 @@ PyImport_ExtendInittab(struct _inittab *newtab)
}
memcpy(p + i, newtab, (n + 1) * sizeof(struct _inittab));
PyImport_Inittab = inittab_copy = p;

done:
PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
return res;
}

Expand Down Expand Up @@ -2445,7 +2440,7 @@ init_builtin_modules_table(void)
size++;

/* Make the copy. */
struct _inittab *copied = PyMem_RawMalloc(size * sizeof(struct _inittab));
struct _inittab *copied = _PyMem_DefaultRawMalloc(size * sizeof(struct _inittab));
if (copied == NULL) {
return -1;
}
Expand All @@ -2459,7 +2454,7 @@ fini_builtin_modules_table(void)
{
struct _inittab *inittab = INITTAB;
INITTAB = NULL;
PyMem_RawFree(inittab);
_PyMem_DefaultRawFree(inittab);
}

PyObject *
Expand Down Expand Up @@ -3977,22 +3972,10 @@ _PyImport_Init(void)
if (INITTAB != NULL) {
return _PyStatus_ERR("global import state already initialized");
}

PyStatus status = _PyStatus_OK();

/* Force default raw memory allocator to get a known allocator to be able
to release the memory in _PyImport_Fini() */
PyMemAllocatorEx old_alloc;
_PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);

if (init_builtin_modules_table() != 0) {
status = PyStatus_NoMemory();
goto done;
return PyStatus_NoMemory();
}

done:
PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
return status;
return _PyStatus_OK();
}

void
Expand All @@ -4003,31 +3986,19 @@ _PyImport_Fini(void)
// ever dlclose() the module files?
_extensions_cache_clear_all();

/* Use the same memory allocator as _PyImport_Init(). */
PyMemAllocatorEx old_alloc;
_PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);

/* Free memory allocated by _PyImport_Init() */
fini_builtin_modules_table();

PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
}

void
_PyImport_Fini2(void)
{
/* Use the same memory allocator than PyImport_ExtendInittab(). */
PyMemAllocatorEx old_alloc;
_PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);

// Reset PyImport_Inittab
PyImport_Inittab = _PyImport_Inittab;

/* Free memory allocated by PyImport_ExtendInittab() */
PyMem_RawFree(inittab_copy);
_PyMem_DefaultRawFree(inittab_copy);
inittab_copy = NULL;

PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
}


Expand Down
74 changes: 48 additions & 26 deletions Python/initconfig.c
Loading