Skip to content

Commit cecfd17

Browse files
eendebakptclaude
andcommitted
Address review: restore the stack spill in DICT_MERGE, copy kwargs for tp_call in PyObject_VectorcallDict
Calling _PyEval_MergeKwargs() inside an if condition hid the escaping call from the cases generator, so the stack pointer was not saved before the merge. Drop _PyEval_KwargsToDict() and bump the magic number. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
1 parent 62f89d3 commit cecfd17

10 files changed

Lines changed: 209 additions & 48 deletions

File tree

‎Include/internal/pycore_ceval.h‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,6 @@ PyAPI_FUNC(void) _PyEval_FormatExcCheckArg(PyThreadState *tstate, PyObject *exc,
299299
PyAPI_FUNC(void) _PyEval_FormatExcUnbound(PyThreadState *tstate, PyCodeObject *co, int oparg);
300300
PyAPI_FUNC(void) _PyEval_FormatKwargsError(PyThreadState *tstate, PyObject *func, PyObject *kwargs, PyObject *dupkey);
301301
PyAPI_FUNC(int) _PyEval_MergeKwargs(PyThreadState *tstate, PyObject *func, PyObject *dict, PyObject *kwargs);
302-
PyAPI_FUNC(PyObject *) _PyEval_KwargsToDict(PyThreadState *tstate, PyObject *func, PyObject *kwargs);
303302
PyAPI_FUNC(PyObject *) _PyEval_ImportFrom(PyThreadState *, PyObject *, PyObject *);
304303

305304
PyAPI_FUNC(PyObject *) _PyEval_LazyImportName(

‎Include/internal/pycore_magic_number.h‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,7 @@ Known values:
303303
Python 3.16a1 3703 (Replace DELETE_GLOBAL with PUSH_NULL; STORE_GLOBAL)
304304
Python 3.16a1 3704 (Replace DELETE_ATTR with PUSH_NULL; STORE_ATTR)
305305
Python 3.16a1 3705 (Add INTRINSIC_ADD_CONDITIONAL_ANNOTATION)
306+
Python 3.16a1 3706 (Push a lone ** call argument without DICT_MERGE)
306307
307308
Python 3.17 will start with 3750
308309
@@ -312,7 +313,7 @@ Known values:
312313
313314
*/
314315

315-
#define PYC_MAGIC_NUMBER 3705
316+
#define PYC_MAGIC_NUMBER 3706
316317
/* This is equivalent to converting PYC_MAGIC_NUMBER to 2 bytes
317318
(little-endian) and then appending b'\r\n'. */
318319
#define PYC_MAGIC_NUMBER_TOKEN \

‎Include/internal/pycore_uop_metadata.h‎

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎Lib/test/test_call.py‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,47 @@ def test_kwargs_unpacking_not_aliased_in_tp_call(self):
119119
e = {}
120120
self.assertIsNot(get_kwargs(**e), e)
121121

122+
@cpython_only
123+
@unittest.skipIf(_testcapi is None, "requires _testcapi")
124+
def test_vectorcalldict_kwargs_not_aliased_in_tp_call(self):
125+
# gh-86795: PyObject_VectorcallDict() must not hand the caller's
126+
# kwargs dict to a tp_call callee either.
127+
get_kwargs = _testcapi.get_kwargs # METH_VARARGS | METH_KEYWORDS
128+
d = {'a': 1}
129+
res = _testcapi.pyobject_fastcalldict(get_kwargs, (), d)
130+
self.assertEqual(res, d)
131+
self.assertIsNot(res, d)
132+
133+
@cpython_only
134+
def test_kwargs_merge_saves_stack_pointer(self):
135+
# gh-86199: DICT_MERGE can run arbitrary code (the keys() and
136+
# __getitem__() of the mapping), so the frame's stack pointer must
137+
# be saved first: the mapping has to be visible to the GC.
138+
visible = []
139+
140+
class Mapping:
141+
def keys(self):
142+
referents = gc.get_referents(gen)
143+
visible.append(any(x is self for x in referents))
144+
gc.collect()
145+
return ['b']
146+
147+
def __getitem__(self, key):
148+
return 2
149+
150+
def fn(**kw):
151+
return kw
152+
153+
mapping = Mapping()
154+
155+
def caller():
156+
d = {'a': 1}
157+
yield fn(**d, **mapping)
158+
159+
gen = caller()
160+
self.assertEqual(next(gen), {'a': 1, 'b': 2})
161+
self.assertEqual(visible, [True])
162+
122163
def test_frames_are_popped_after_failed_calls(self):
123164
# GH-93252: stuff blows up if we don't pop the new frame after
124165
# recovering from failed calls:

‎Modules/_testinternalcapi/test_cases.c.h‎

Lines changed: 57 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎Objects/call.c‎

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -220,8 +220,22 @@ _PyObject_MakeTpCall(PyThreadState *tstate, PyObject *callable,
220220
}
221221

222222
PyObject *kwdict;
223-
if (keywords == NULL || PyDict_Check(keywords)) {
224-
kwdict = keywords;
223+
if (keywords == NULL) {
224+
kwdict = NULL;
225+
}
226+
else if (PyDict_Check(keywords)) {
227+
/* Pass tp_call a copy of the dict so that the callee cannot
228+
mutate the caller's dict. */
229+
if (PyDict_GET_SIZE(keywords)) {
230+
kwdict = PyDict_Copy(keywords);
231+
if (kwdict == NULL) {
232+
Py_DECREF(argstuple);
233+
return NULL;
234+
}
235+
}
236+
else {
237+
keywords = kwdict = NULL;
238+
}
225239
}
226240
else {
227241
if (PyTuple_GET_SIZE(keywords)) {

‎Python/bytecodes.c‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2651,7 +2651,8 @@ dummy_func(
26512651
PyObject *dict_o = PyStackRef_AsPyObjectBorrow(dict);
26522652
PyObject *update_o = PyStackRef_AsPyObjectBorrow(update);
26532653

2654-
if (_PyEval_MergeKwargs(tstate, callable_o, dict_o, update_o) < 0) {
2654+
int err = _PyEval_MergeKwargs(tstate, callable_o, dict_o, update_o);
2655+
if (err < 0) {
26552656
ERROR_NO_POP();
26562657
}
26572658
u = update;
@@ -5626,11 +5627,16 @@ dummy_func(
56265627
}
56275628
PyObject *kwargs_o = PyStackRef_AsPyObjectBorrow(kwargs);
56285629
if (kwargs_o != NULL && !PyDict_CheckExact(kwargs_o)) {
5629-
PyObject *dict_o = _PyEval_KwargsToDict(
5630-
tstate, PyStackRef_AsPyObjectBorrow(func), kwargs_o);
5630+
PyObject *dict_o = PyDict_New();
56315631
if (dict_o == NULL) {
56325632
ERROR_NO_POP();
56335633
}
5634+
int err = _PyEval_MergeKwargs(
5635+
tstate, PyStackRef_AsPyObjectBorrow(func), dict_o, kwargs_o);
5636+
if (err < 0) {
5637+
Py_DECREF(dict_o);
5638+
ERROR_NO_POP();
5639+
}
56345640
_PyStackRef temp = kwargs;
56355641
kwargs = PyStackRef_FromPyObjectSteal(dict_o);
56365642
PyStackRef_CLOSE(temp);

‎Python/ceval.c‎

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3511,22 +3511,6 @@ _PyEval_MergeKwargs(PyThreadState *tstate, PyObject *func,
35113511
return 0;
35123512
}
35133513

3514-
/* Return a new exact dict with the items of the mapping 'kwargs',
3515-
raising the same TypeError as DICT_MERGE on failure. */
3516-
PyObject *
3517-
_PyEval_KwargsToDict(PyThreadState *tstate, PyObject *func, PyObject *kwargs)
3518-
{
3519-
PyObject *dict = PyDict_New();
3520-
if (dict == NULL) {
3521-
return NULL;
3522-
}
3523-
if (_PyEval_MergeKwargs(tstate, func, dict, kwargs) < 0) {
3524-
Py_DECREF(dict);
3525-
return NULL;
3526-
}
3527-
return dict;
3528-
}
3529-
35303514
void
35313515
_PyEval_FormatExcCheckArg(PyThreadState *tstate, PyObject *exc,
35323516
const char *format_str, PyObject *obj)

‎Python/executor_cases.c.h‎

Lines changed: 26 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)