Skip to content

Commit 7f3a4b9

Browse files
gh-81057: Move PyImport_Inittab to _PyRuntimeState (gh-99402)
We actually don't move PyImport_Inittab. Instead, we make a copy that we keep on _PyRuntimeState and use only that after Py_Initialize(). We also prevent folks from modifying PyImport_Inittab (the best we can) after that point. #81057
1 parent 67807cf commit 7f3a4b9

File tree

9 files changed

+80
-8
lines changed

9 files changed

+80
-8
lines changed

Include/cpython/import.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ struct _inittab {
2525
const char *name; /* ASCII encoded string */
2626
PyObject* (*initfunc)(void);
2727
};
28+
// This is not used after Py_Initialize() is called.
2829
PyAPI_DATA(struct _inittab *) PyImport_Inittab;
2930
PyAPI_FUNC(int) PyImport_ExtendInittab(struct _inittab *newtab);
3031

Include/internal/pycore_import.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ extern "C" {
77

88

99
struct _import_runtime_state {
10+
/* The builtin modules (defined in config.c). */
11+
struct _inittab *inittab;
1012
/* The most recent value assigned to a PyModuleDef.m_base.m_index.
1113
This is incremented each time PyModuleDef_Init() is called,
1214
which is just about every time an extension module is imported.

Include/internal/pycore_pylifecycle.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ PyAPI_FUNC(int) _Py_IsLocaleCoercionTarget(const char *ctype_loc);
3333

3434
/* Various one-time initializers */
3535

36+
extern PyStatus _PyImport_Init(void);
3637
extern PyStatus _PyFaulthandler_Init(int enable);
3738
extern int _PyTraceMalloc_Init(int enable);
3839
extern PyObject * _PyBuiltin_Init(PyInterpreterState *interp);
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
The docs clearly say that ``PyImport_Inittab``,
2+
:c:func:`PyImport_AppendInittab`, and :c:func:`PyImport_ExtendInittab`
3+
should not be used after :c:func:`Py_Initialize` has been called.
4+
We now enforce this for the two functions. Additionally, the runtime
5+
now uses an internal copy of ``PyImport_Inittab``,
6+
to guard against modification.

Python/import.c

Lines changed: 60 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,11 @@ static PyObject *import_add_module(PyThreadState *tstate, PyObject *name);
3030
/* This table is defined in config.c: */
3131
extern struct _inittab _PyImport_Inittab[];
3232

33+
// This is not used after Py_Initialize() is called.
34+
// (See _PyRuntimeState.imports.inittab.)
3335
struct _inittab *PyImport_Inittab = _PyImport_Inittab;
36+
// When we dynamically allocate a larger table for PyImport_ExtendInittab(),
37+
// we track the pointer here so we can deallocate it during finalization.
3438
static struct _inittab *inittab_copy = NULL;
3539

3640
/*[clinic input]
@@ -218,6 +222,38 @@ _imp_release_lock_impl(PyObject *module)
218222
Py_RETURN_NONE;
219223
}
220224

225+
PyStatus
226+
_PyImport_Init(void)
227+
{
228+
if (_PyRuntime.imports.inittab != NULL) {
229+
return _PyStatus_ERR("global import state already initialized");
230+
}
231+
PyStatus status = _PyStatus_OK();
232+
233+
size_t size;
234+
for (size = 0; PyImport_Inittab[size].name != NULL; size++)
235+
;
236+
size++;
237+
238+
/* Force default raw memory allocator to get a known allocator to be able
239+
to release the memory in _PyImport_Fini() */
240+
PyMemAllocatorEx old_alloc;
241+
_PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
242+
243+
/* Make the copy. */
244+
struct _inittab *copied = PyMem_RawMalloc(size * sizeof(struct _inittab));
245+
if (copied == NULL) {
246+
status = PyStatus_NoMemory();
247+
goto done;
248+
}
249+
memcpy(copied, PyImport_Inittab, size * sizeof(struct _inittab));
250+
_PyRuntime.imports.inittab = copied;
251+
252+
done:
253+
PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
254+
return status;
255+
}
256+
221257
static inline void _extensions_cache_clear(void);
222258

223259
void
@@ -228,6 +264,17 @@ _PyImport_Fini(void)
228264
PyThread_free_lock(import_lock);
229265
import_lock = NULL;
230266
}
267+
268+
/* Use the same memory allocator as _PyImport_Init(). */
269+
PyMemAllocatorEx old_alloc;
270+
_PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
271+
272+
/* Free memory allocated by _PyImport_Init() */
273+
struct _inittab *inittab = _PyRuntime.imports.inittab;
274+
_PyRuntime.imports.inittab = NULL;
275+
PyMem_RawFree(inittab);
276+
277+
PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
231278
}
232279

233280
void
@@ -889,9 +936,10 @@ static int
889936
is_builtin(PyObject *name)
890937
{
891938
int i;
892-
for (i = 0; PyImport_Inittab[i].name != NULL; i++) {
893-
if (_PyUnicode_EqualToASCIIString(name, PyImport_Inittab[i].name)) {
894-
if (PyImport_Inittab[i].initfunc == NULL)
939+
struct _inittab *inittab = _PyRuntime.imports.inittab;
940+
for (i = 0; inittab[i].name != NULL; i++) {
941+
if (_PyUnicode_EqualToASCIIString(name, inittab[i].name)) {
942+
if (inittab[i].initfunc == NULL)
895943
return -1;
896944
else
897945
return 1;
@@ -984,7 +1032,7 @@ create_builtin(PyThreadState *tstate, PyObject *name, PyObject *spec)
9841032
}
9851033

9861034
PyObject *modules = tstate->interp->modules;
987-
for (struct _inittab *p = PyImport_Inittab; p->name != NULL; p++) {
1035+
for (struct _inittab *p = _PyRuntime.imports.inittab; p->name != NULL; p++) {
9881036
if (_PyUnicode_EqualToASCIIString(name, p->name)) {
9891037
if (p->initfunc == NULL) {
9901038
/* Cannot re-init internal module ("sys" or "builtins") */
@@ -2592,6 +2640,10 @@ PyImport_ExtendInittab(struct _inittab *newtab)
25922640
size_t i, n;
25932641
int res = 0;
25942642

2643+
if (_PyRuntime.imports.inittab != NULL) {
2644+
Py_FatalError("PyImport_ExtendInittab() may be be called after Py_Initialize()");
2645+
}
2646+
25952647
/* Count the number of entries in both tables */
25962648
for (n = 0; newtab[n].name != NULL; n++)
25972649
;
@@ -2636,6 +2688,10 @@ PyImport_AppendInittab(const char *name, PyObject* (*initfunc)(void))
26362688
{
26372689
struct _inittab newtab[2];
26382690

2691+
if (_PyRuntime.imports.inittab != NULL) {
2692+
Py_FatalError("PyImport_AppendInittab() may be be called after Py_Initialize()");
2693+
}
2694+
26392695
memset(newtab, '\0', sizeof newtab);
26402696

26412697
newtab[0].name = name;

Python/pylifecycle.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -605,6 +605,11 @@ pycore_init_runtime(_PyRuntimeState *runtime,
605605
return status;
606606
}
607607

608+
status = _PyImport_Init();
609+
if (_PyStatus_EXCEPTION(status)) {
610+
return status;
611+
}
612+
608613
status = _PyInterpreterState_Enable(runtime);
609614
if (_PyStatus_EXCEPTION(status)) {
610615
return status;

Python/sysmodule.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2252,8 +2252,9 @@ list_builtin_module_names(void)
22522252
if (list == NULL) {
22532253
return NULL;
22542254
}
2255-
for (Py_ssize_t i = 0; PyImport_Inittab[i].name != NULL; i++) {
2256-
PyObject *name = PyUnicode_FromString(PyImport_Inittab[i].name);
2255+
struct _inittab *inittab = _PyRuntime.imports.inittab;
2256+
for (Py_ssize_t i = 0; inittab[i].name != NULL; i++) {
2257+
PyObject *name = PyUnicode_FromString(inittab[i].name);
22572258
if (name == NULL) {
22582259
goto error;
22592260
}

Tools/c-analyzer/cpython/globals-to-fix.tsv

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -392,8 +392,6 @@ Python/frozen.c - _PyImport_FrozenAliases -
392392
Python/frozen.c - _PyImport_FrozenBootstrap -
393393
Python/frozen.c - _PyImport_FrozenStdlib -
394394
Python/frozen.c - _PyImport_FrozenTest -
395-
Python/import.c - inittab_copy -
396-
Python/import.c - PyImport_Inittab -
397395
Python/preconfig.c - Py_FileSystemDefaultEncoding -
398396
Python/preconfig.c - Py_HasFileSystemDefaultEncoding -
399397
Python/preconfig.c - Py_FileSystemDefaultEncodeErrors -

Tools/c-analyzer/cpython/ignored.tsv

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ filename funcname name reason
44
##################################
55
# mutable but known to be safe
66

7+
Python/import.c - inittab_copy -
8+
Python/import.c - PyImport_Inittab -
79
Python/pylifecycle.c - _PyRuntime -
810

911
# All uses of _PyArg_Parser are handled in c-analyzr/cpython/_analyzer.py.

0 commit comments

Comments
 (0)