aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/Python/pystate.c
diff options
context:
space:
mode:
authorBénédikt Tran <10796600+picnixz@users.noreply.github.com>2025-04-19 11:03:06 +0200
committerGitHub <noreply@github.com>2025-04-19 11:03:06 +0200
commit427e7fc099e61d9a3d1f04e2fbde8b749e5fcf0a (patch)
treec442f5544d284e79aee0d72c037d337068f92121 /Python/pystate.c
parent8a9c6c4d16a746eea1e000d6701d1c274c1f331b (diff)
downloadcpython-427e7fc099e61d9a3d1f04e2fbde8b749e5fcf0a.tar.gz
cpython-427e7fc099e61d9a3d1f04e2fbde8b749e5fcf0a.zip
gh-132399: ensure correct alignment of `PyInterpreterState` (#132428)
Diffstat (limited to 'Python/pystate.c')
-rw-r--r--Python/pystate.c17
1 files changed, 14 insertions, 3 deletions
diff --git a/Python/pystate.c b/Python/pystate.c
index ee35f0fa945..aba558279a6 100644
--- a/Python/pystate.c
+++ b/Python/pystate.c
@@ -569,11 +569,19 @@ _PyInterpreterState_Enable(_PyRuntimeState *runtime)
return _PyStatus_OK();
}
-
static PyInterpreterState *
alloc_interpreter(void)
{
- return PyMem_RawCalloc(1, sizeof(PyInterpreterState));
+ size_t alignment = _Alignof(PyInterpreterState);
+ size_t allocsize = sizeof(PyInterpreterState) + alignment - 1;
+ void *mem = PyMem_RawCalloc(1, allocsize);
+ if (mem == NULL) {
+ return NULL;
+ }
+ PyInterpreterState *interp = _Py_ALIGN_UP(mem, alignment);
+ assert(_Py_IS_ALIGNED(interp, alignment));
+ interp->_malloced = mem;
+ return interp;
}
static void
@@ -587,12 +595,15 @@ free_interpreter(PyInterpreterState *interp)
PyMem_RawFree(interp->obmalloc);
interp->obmalloc = NULL;
}
- PyMem_RawFree(interp);
+ assert(_Py_IS_ALIGNED(interp, _Alignof(PyInterpreterState)));
+ PyMem_RawFree(interp->_malloced);
}
}
+
#ifndef NDEBUG
static inline int check_interpreter_whence(long);
#endif
+
/* Get the interpreter state to a minimal consistent state.
Further init happens in pylifecycle.c before it can be used.
All fields not initialized here are expected to be zeroed out,