mirror of
https://github.com/python/cpython.git
synced 2025-09-27 10:50:04 +00:00
[3.12] gh-119213: Be More Careful About _PyArg_Parser.kwtuple Across Interpreters (gh-119331) (gh-119425)
_PyArg_Parser holds static global data generated for modules by Argument Clinic. The _PyArg_Parser.kwtuple field is a tuple object, even though it's stored within a static global. In some cases the tuple is statically allocated and thus it's okay that it gets shared by multiple interpreters. However, in other cases the tuple is set lazily, allocated from the heap using the active interprepreter at the point the tuple is needed.
This is a problem once that interpreter is destroyed since _PyArg_Parser.kwtuple becomes at dangling pointer, leading to crashes. It isn't a problem if the tuple is allocated under the main interpreter, since its lifetime is bound to the lifetime of the runtime. The solution here is to temporarily switch to the main interpreter. The alternative would be to always statically allocate the tuple.
This change also fixes a bug where only the most recent parser was added to the global linked list.
(cherry picked from commit 81865002ae
)
This commit is contained in:
parent
7eb59cd95b
commit
0d5fe2c7b4
10 changed files with 1033 additions and 870 deletions
File diff suppressed because it is too large
Load diff
|
@ -1186,6 +1186,7 @@ _PyStaticObjects_CheckRefcnt(PyInterpreterState *interp) {
|
||||||
_PyStaticObject_CheckRefcnt((PyObject *)&_Py_ID(sound));
|
_PyStaticObject_CheckRefcnt((PyObject *)&_Py_ID(sound));
|
||||||
_PyStaticObject_CheckRefcnt((PyObject *)&_Py_ID(source));
|
_PyStaticObject_CheckRefcnt((PyObject *)&_Py_ID(source));
|
||||||
_PyStaticObject_CheckRefcnt((PyObject *)&_Py_ID(source_traceback));
|
_PyStaticObject_CheckRefcnt((PyObject *)&_Py_ID(source_traceback));
|
||||||
|
_PyStaticObject_CheckRefcnt((PyObject *)&_Py_ID(spam));
|
||||||
_PyStaticObject_CheckRefcnt((PyObject *)&_Py_ID(src));
|
_PyStaticObject_CheckRefcnt((PyObject *)&_Py_ID(src));
|
||||||
_PyStaticObject_CheckRefcnt((PyObject *)&_Py_ID(src_dir_fd));
|
_PyStaticObject_CheckRefcnt((PyObject *)&_Py_ID(src_dir_fd));
|
||||||
_PyStaticObject_CheckRefcnt((PyObject *)&_Py_ID(stacklevel));
|
_PyStaticObject_CheckRefcnt((PyObject *)&_Py_ID(stacklevel));
|
||||||
|
|
|
@ -675,6 +675,7 @@ struct _Py_global_strings {
|
||||||
STRUCT_FOR_ID(sound)
|
STRUCT_FOR_ID(sound)
|
||||||
STRUCT_FOR_ID(source)
|
STRUCT_FOR_ID(source)
|
||||||
STRUCT_FOR_ID(source_traceback)
|
STRUCT_FOR_ID(source_traceback)
|
||||||
|
STRUCT_FOR_ID(spam)
|
||||||
STRUCT_FOR_ID(src)
|
STRUCT_FOR_ID(src)
|
||||||
STRUCT_FOR_ID(src_dir_fd)
|
STRUCT_FOR_ID(src_dir_fd)
|
||||||
STRUCT_FOR_ID(stacklevel)
|
STRUCT_FOR_ID(stacklevel)
|
||||||
|
|
1
Include/internal/pycore_runtime_init_generated.h
generated
1
Include/internal/pycore_runtime_init_generated.h
generated
|
@ -1181,6 +1181,7 @@ extern "C" {
|
||||||
INIT_ID(sound), \
|
INIT_ID(sound), \
|
||||||
INIT_ID(source), \
|
INIT_ID(source), \
|
||||||
INIT_ID(source_traceback), \
|
INIT_ID(source_traceback), \
|
||||||
|
INIT_ID(spam), \
|
||||||
INIT_ID(src), \
|
INIT_ID(src), \
|
||||||
INIT_ID(src_dir_fd), \
|
INIT_ID(src_dir_fd), \
|
||||||
INIT_ID(stacklevel), \
|
INIT_ID(stacklevel), \
|
||||||
|
|
|
@ -1866,6 +1866,9 @@ _PyUnicode_InitStaticStrings(PyInterpreterState *interp) {
|
||||||
string = &_Py_ID(source_traceback);
|
string = &_Py_ID(source_traceback);
|
||||||
assert(_PyUnicode_CheckConsistency(string, 1));
|
assert(_PyUnicode_CheckConsistency(string, 1));
|
||||||
_PyUnicode_InternInPlace(interp, &string);
|
_PyUnicode_InternInPlace(interp, &string);
|
||||||
|
string = &_Py_ID(spam);
|
||||||
|
assert(_PyUnicode_CheckConsistency(string, 1));
|
||||||
|
_PyUnicode_InternInPlace(interp, &string);
|
||||||
string = &_Py_ID(src);
|
string = &_Py_ID(src);
|
||||||
assert(_PyUnicode_CheckConsistency(string, 1));
|
assert(_PyUnicode_CheckConsistency(string, 1));
|
||||||
_PyUnicode_InternInPlace(interp, &string);
|
_PyUnicode_InternInPlace(interp, &string);
|
||||||
|
|
|
@ -4,11 +4,17 @@ import string
|
||||||
import sys
|
import sys
|
||||||
from test import support
|
from test import support
|
||||||
from test.support import import_helper
|
from test.support import import_helper
|
||||||
|
from test.support import script_helper
|
||||||
from test.support import warnings_helper
|
from test.support import warnings_helper
|
||||||
# Skip this test if the _testcapi module isn't available.
|
# Skip this test if the _testcapi module isn't available.
|
||||||
_testcapi = import_helper.import_module('_testcapi')
|
_testcapi = import_helper.import_module('_testcapi')
|
||||||
from _testcapi import getargs_keywords, getargs_keyword_only
|
from _testcapi import getargs_keywords, getargs_keyword_only
|
||||||
|
|
||||||
|
try:
|
||||||
|
import _testinternalcapi
|
||||||
|
except ImportError:
|
||||||
|
_testinternalcapi = NULL
|
||||||
|
|
||||||
# > How about the following counterproposal. This also changes some of
|
# > How about the following counterproposal. This also changes some of
|
||||||
# > the other format codes to be a little more regular.
|
# > the other format codes to be a little more regular.
|
||||||
# >
|
# >
|
||||||
|
@ -1369,6 +1375,33 @@ class Test_testcapi(unittest.TestCase):
|
||||||
"argument 1 must be sequence of length 1, not 0"):
|
"argument 1 must be sequence of length 1, not 0"):
|
||||||
parse(((),), {}, '(' + f + ')', ['a'])
|
parse(((),), {}, '(' + f + ')', ['a'])
|
||||||
|
|
||||||
|
@unittest.skipIf(_testinternalcapi is None, 'needs _testinternalcapi')
|
||||||
|
def test_gh_119213(self):
|
||||||
|
rc, out, err = script_helper.assert_python_ok("-c", """if True:
|
||||||
|
from test import support
|
||||||
|
script = '''if True:
|
||||||
|
import _testinternalcapi
|
||||||
|
_testinternalcapi.gh_119213_getargs(spam='eggs')
|
||||||
|
'''
|
||||||
|
config = dict(
|
||||||
|
allow_fork=False,
|
||||||
|
allow_exec=False,
|
||||||
|
allow_threads=True,
|
||||||
|
allow_daemon_threads=False,
|
||||||
|
use_main_obmalloc=False,
|
||||||
|
gil=2,
|
||||||
|
check_multi_interp_extensions=True,
|
||||||
|
)
|
||||||
|
rc = support.run_in_subinterp_with_config(script, **config)
|
||||||
|
assert rc == 0
|
||||||
|
|
||||||
|
# The crash is different if the interpreter was not destroyed first.
|
||||||
|
#interpid = _testinternalcapi.create_interpreter()
|
||||||
|
#rc = _testinternalcapi.exec_interpreter(interpid, script)
|
||||||
|
#assert rc == 0
|
||||||
|
""")
|
||||||
|
self.assertEqual(rc, 0)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
|
|
@ -0,0 +1,3 @@
|
||||||
|
Non-builtin modules built with argument clinic were crashing if used in a
|
||||||
|
subinterpreter before the main interpreter. The objects that were causing
|
||||||
|
the problem by leaking between interpreters carelessly have been fixed.
|
|
@ -1055,6 +1055,24 @@ pending_identify(PyObject *self, PyObject *args)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*[clinic input]
|
||||||
|
gh_119213_getargs
|
||||||
|
|
||||||
|
spam: object = None
|
||||||
|
|
||||||
|
Test _PyArg_Parser.kwtuple
|
||||||
|
[clinic start generated code]*/
|
||||||
|
|
||||||
|
static PyObject *
|
||||||
|
gh_119213_getargs_impl(PyObject *module, PyObject *spam)
|
||||||
|
/*[clinic end generated code: output=d8d9c95d5b446802 input=65ef47511da80fc2]*/
|
||||||
|
{
|
||||||
|
// It must never have been called in the main interprer
|
||||||
|
assert(!_Py_IsMainInterpreter(PyInterpreterState_Get()));
|
||||||
|
return Py_NewRef(spam);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
static PyMethodDef module_functions[] = {
|
static PyMethodDef module_functions[] = {
|
||||||
{"get_configs", get_configs, METH_NOARGS},
|
{"get_configs", get_configs, METH_NOARGS},
|
||||||
{"get_recursion_depth", get_recursion_depth, METH_NOARGS},
|
{"get_recursion_depth", get_recursion_depth, METH_NOARGS},
|
||||||
|
@ -1087,6 +1105,7 @@ static PyMethodDef module_functions[] = {
|
||||||
{"pending_threadfunc", _PyCFunction_CAST(pending_threadfunc),
|
{"pending_threadfunc", _PyCFunction_CAST(pending_threadfunc),
|
||||||
METH_VARARGS | METH_KEYWORDS},
|
METH_VARARGS | METH_KEYWORDS},
|
||||||
{"pending_identify", pending_identify, METH_VARARGS, NULL},
|
{"pending_identify", pending_identify, METH_VARARGS, NULL},
|
||||||
|
GH_119213_GETARGS_METHODDEF
|
||||||
{NULL, NULL} /* sentinel */
|
{NULL, NULL} /* sentinel */
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
62
Modules/clinic/_testinternalcapi.c.h
generated
62
Modules/clinic/_testinternalcapi.c.h
generated
|
@ -206,4 +206,64 @@ _testinternalcapi_assemble_code_object(PyObject *module, PyObject *const *args,
|
||||||
exit:
|
exit:
|
||||||
return return_value;
|
return return_value;
|
||||||
}
|
}
|
||||||
/*[clinic end generated code: output=2965f1578b986218 input=a9049054013a1b77]*/
|
|
||||||
|
PyDoc_STRVAR(gh_119213_getargs__doc__,
|
||||||
|
"gh_119213_getargs($module, /, spam=None)\n"
|
||||||
|
"--\n"
|
||||||
|
"\n"
|
||||||
|
"Test _PyArg_Parser.kwtuple");
|
||||||
|
|
||||||
|
#define GH_119213_GETARGS_METHODDEF \
|
||||||
|
{"gh_119213_getargs", _PyCFunction_CAST(gh_119213_getargs), METH_FASTCALL|METH_KEYWORDS, gh_119213_getargs__doc__},
|
||||||
|
|
||||||
|
static PyObject *
|
||||||
|
gh_119213_getargs_impl(PyObject *module, PyObject *spam);
|
||||||
|
|
||||||
|
static PyObject *
|
||||||
|
gh_119213_getargs(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
|
||||||
|
{
|
||||||
|
PyObject *return_value = NULL;
|
||||||
|
#if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
|
||||||
|
|
||||||
|
#define NUM_KEYWORDS 1
|
||||||
|
static struct {
|
||||||
|
PyGC_Head _this_is_not_used;
|
||||||
|
PyObject_VAR_HEAD
|
||||||
|
PyObject *ob_item[NUM_KEYWORDS];
|
||||||
|
} _kwtuple = {
|
||||||
|
.ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
|
||||||
|
.ob_item = { &_Py_ID(spam), },
|
||||||
|
};
|
||||||
|
#undef NUM_KEYWORDS
|
||||||
|
#define KWTUPLE (&_kwtuple.ob_base.ob_base)
|
||||||
|
|
||||||
|
#else // !Py_BUILD_CORE
|
||||||
|
# define KWTUPLE NULL
|
||||||
|
#endif // !Py_BUILD_CORE
|
||||||
|
|
||||||
|
static const char * const _keywords[] = {"spam", NULL};
|
||||||
|
static _PyArg_Parser _parser = {
|
||||||
|
.keywords = _keywords,
|
||||||
|
.fname = "gh_119213_getargs",
|
||||||
|
.kwtuple = KWTUPLE,
|
||||||
|
};
|
||||||
|
#undef KWTUPLE
|
||||||
|
PyObject *argsbuf[1];
|
||||||
|
Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
|
||||||
|
PyObject *spam = Py_None;
|
||||||
|
|
||||||
|
args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 1, 0, argsbuf);
|
||||||
|
if (!args) {
|
||||||
|
goto exit;
|
||||||
|
}
|
||||||
|
if (!noptargs) {
|
||||||
|
goto skip_optional_pos;
|
||||||
|
}
|
||||||
|
spam = args[0];
|
||||||
|
skip_optional_pos:
|
||||||
|
return_value = gh_119213_getargs_impl(module, spam);
|
||||||
|
|
||||||
|
exit:
|
||||||
|
return return_value;
|
||||||
|
}
|
||||||
|
/*[clinic end generated code: output=1fa5cb831dbb391f input=a9049054013a1b77]*/
|
||||||
|
|
|
@ -4,6 +4,7 @@
|
||||||
#include "Python.h"
|
#include "Python.h"
|
||||||
#include "pycore_tuple.h" // _PyTuple_ITEMS()
|
#include "pycore_tuple.h" // _PyTuple_ITEMS()
|
||||||
#include "pycore_pylifecycle.h" // _PyArg_Fini
|
#include "pycore_pylifecycle.h" // _PyArg_Fini
|
||||||
|
#include "pycore_pystate.h" // _Py_IsMainInterpreter()
|
||||||
|
|
||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
#include <float.h>
|
#include <float.h>
|
||||||
|
@ -2002,7 +2003,23 @@ _parser_init(struct _PyArg_Parser *parser)
|
||||||
int owned;
|
int owned;
|
||||||
PyObject *kwtuple = parser->kwtuple;
|
PyObject *kwtuple = parser->kwtuple;
|
||||||
if (kwtuple == NULL) {
|
if (kwtuple == NULL) {
|
||||||
|
/* We may temporarily switch to the main interpreter to avoid
|
||||||
|
* creating a tuple that could outlive its owning interpreter. */
|
||||||
|
PyThreadState *save_tstate = NULL;
|
||||||
|
PyThreadState *temp_tstate = NULL;
|
||||||
|
if (!_Py_IsMainInterpreter(PyInterpreterState_Get())) {
|
||||||
|
temp_tstate = PyThreadState_New(_PyInterpreterState_Main());
|
||||||
|
if (temp_tstate == NULL) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
save_tstate = PyThreadState_Swap(temp_tstate);
|
||||||
|
}
|
||||||
kwtuple = new_kwtuple(keywords, len, pos);
|
kwtuple = new_kwtuple(keywords, len, pos);
|
||||||
|
if (temp_tstate != NULL) {
|
||||||
|
PyThreadState_Clear(temp_tstate);
|
||||||
|
(void)PyThreadState_Swap(save_tstate);
|
||||||
|
PyThreadState_Delete(temp_tstate);
|
||||||
|
}
|
||||||
if (kwtuple == NULL) {
|
if (kwtuple == NULL) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue