Skip to content

Commit a9cd908

Browse files
committed
[3.11] pythongh-101765: Fix SystemError / segmentation fault in iter __reduce__ when internal access of builtins.__dict__ exhausts the iterator (pythonGH-101769).
(cherry picked from commit 54dfa14) Co-authored-by: Ionite <[email protected]>
1 parent 3b4f8fc commit a9cd908

9 files changed

+265
-148
lines changed

Lib/test/test_iter.py

+81
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
from test.support import check_free_after_iterating, ALWAYS_EQ, NEVER_EQ
88
import pickle
99
import collections.abc
10+
import functools
11+
import contextlib
12+
import builtins
1013

1114
# Test result of triple loop (too big to inline)
1215
TRIPLETS = [(0, 0, 0), (0, 0, 1), (0, 0, 2),
@@ -91,6 +94,12 @@ def __call__(self):
9194
raise IndexError # Emergency stop
9295
return i
9396

97+
class EmptyIterClass:
98+
def __len__(self):
99+
return 0
100+
def __getitem__(self, i):
101+
raise StopIteration
102+
94103
# Main test suite
95104

96105
class TestCase(unittest.TestCase):
@@ -238,6 +247,78 @@ def test_mutating_seq_class_exhausted_iter(self):
238247
self.assertEqual(list(empit), [5, 6])
239248
self.assertEqual(list(a), [0, 1, 2, 3, 4, 5, 6])
240249

250+
def test_reduce_mutating_builtins_iter(self):
251+
# This is a reproducer of issue #101765
252+
# where iter `__reduce__` calls could lead to a segfault or SystemError
253+
# depending on the order of C argument evaluation, which is undefined
254+
255+
# Backup builtins
256+
builtins_dict = builtins.__dict__
257+
orig = {"iter": iter, "reversed": reversed}
258+
259+
def run(builtin_name, item, sentinel=None):
260+
it = iter(item) if sentinel is None else iter(item, sentinel)
261+
262+
class CustomStr:
263+
def __init__(self, name, iterator):
264+
self.name = name
265+
self.iterator = iterator
266+
def __hash__(self):
267+
return hash(self.name)
268+
def __eq__(self, other):
269+
# Here we exhaust our iterator, possibly changing
270+
# its `it_seq` pointer to NULL
271+
# The `__reduce__` call should correctly get
272+
# the pointers after this call
273+
list(self.iterator)
274+
return other == self.name
275+
276+
# del is required here
277+
# to not prematurely call __eq__ from
278+
# the hash collision with the old key
279+
del builtins_dict[builtin_name]
280+
builtins_dict[CustomStr(builtin_name, it)] = orig[builtin_name]
281+
282+
return it.__reduce__()
283+
284+
types = [
285+
(EmptyIterClass(),),
286+
(bytes(8),),
287+
(bytearray(8),),
288+
((1, 2, 3),),
289+
(lambda: 0, 0),
290+
(tuple[int],) # GenericAlias
291+
]
292+
293+
try:
294+
run_iter = functools.partial(run, "iter")
295+
# The returned value of `__reduce__` should not only be valid
296+
# but also *empty*, as `it` was exhausted during `__eq__`
297+
# i.e "xyz" returns (iter, ("",))
298+
self.assertEqual(run_iter("xyz"), (orig["iter"], ("",)))
299+
self.assertEqual(run_iter([1, 2, 3]), (orig["iter"], ([],)))
300+
301+
# _PyEval_GetBuiltin is also called for `reversed` in a branch of
302+
# listiter_reduce_general
303+
self.assertEqual(
304+
run("reversed", orig["reversed"](list(range(8)))),
305+
(iter, ([],))
306+
)
307+
308+
for case in types:
309+
self.assertEqual(run_iter(*case), (orig["iter"], ((),)))
310+
finally:
311+
# Restore original builtins
312+
for key, func in orig.items():
313+
# need to suppress KeyErrors in case
314+
# a failed test deletes the key without setting anything
315+
with contextlib.suppress(KeyError):
316+
# del is required here
317+
# to not invoke our custom __eq__ from
318+
# the hash collision with the old key
319+
del builtins_dict[key]
320+
builtins_dict[key] = func
321+
241322
# Test a new_style class with __iter__ but no next() method
242323
def test_new_style_iter_class(self):
243324
class IterClass(object):
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix SystemError / segmentation fault in iter ``__reduce__`` when internal access of ``builtins.__dict__`` keys mutates the iter object.

Objects/bytearrayobject.c

+8-3
Original file line numberDiff line numberDiff line change
@@ -2394,11 +2394,16 @@ PyDoc_STRVAR(length_hint_doc,
23942394
static PyObject *
23952395
bytearrayiter_reduce(bytesiterobject *it, PyObject *Py_UNUSED(ignored))
23962396
{
2397+
PyObject *iter = _PyEval_GetBuiltin(&_Py_ID(iter));
2398+
2399+
/* _PyEval_GetBuiltin can invoke arbitrary code,
2400+
* call must be before access of iterator pointers.
2401+
* see issue #101765 */
2402+
23972403
if (it->it_seq != NULL) {
2398-
return Py_BuildValue("N(O)n", _PyEval_GetBuiltin(&_Py_ID(iter)),
2399-
it->it_seq, it->it_index);
2404+
return Py_BuildValue("N(O)n", iter, it->it_seq, it->it_index);
24002405
} else {
2401-
return Py_BuildValue("N(())", _PyEval_GetBuiltin(&_Py_ID(iter)));
2406+
return Py_BuildValue("N(())", iter);
24022407
}
24032408
}
24042409

Objects/bytesobject.c

+8-3
Original file line numberDiff line numberDiff line change
@@ -3191,11 +3191,16 @@ PyDoc_STRVAR(length_hint_doc,
31913191
static PyObject *
31923192
striter_reduce(striterobject *it, PyObject *Py_UNUSED(ignored))
31933193
{
3194+
PyObject *iter = _PyEval_GetBuiltin(&_Py_ID(iter));
3195+
3196+
/* _PyEval_GetBuiltin can invoke arbitrary code,
3197+
* call must be before access of iterator pointers.
3198+
* see issue #101765 */
3199+
31943200
if (it->it_seq != NULL) {
3195-
return Py_BuildValue("N(O)n", _PyEval_GetBuiltin(&_Py_ID(iter)),
3196-
it->it_seq, it->it_index);
3201+
return Py_BuildValue("N(O)n", iter, it->it_seq, it->it_index);
31973202
} else {
3198-
return Py_BuildValue("N(())", _PyEval_GetBuiltin(&_Py_ID(iter)));
3203+
return Py_BuildValue("N(())", iter);
31993204
}
32003205
}
32013206

Objects/genericaliasobject.c

+10-1
Original file line numberDiff line numberDiff line change
@@ -885,8 +885,17 @@ ga_iter_clear(PyObject *self) {
885885
static PyObject *
886886
ga_iter_reduce(PyObject *self, PyObject *Py_UNUSED(ignored))
887887
{
888+
PyObject *iter = _PyEval_GetBuiltin(&_Py_ID(iter));
888889
gaiterobject *gi = (gaiterobject *)self;
889-
return Py_BuildValue("N(O)", _PyEval_GetBuiltin(&_Py_ID(iter)), gi->obj);
890+
891+
/* _PyEval_GetBuiltin can invoke arbitrary code,
892+
* call must be before access of iterator pointers.
893+
* see issue #101765 */
894+
895+
if (gi->obj)
896+
return Py_BuildValue("N(O)", iter, gi->obj);
897+
else
898+
return Py_BuildValue("N(())", iter);
890899
}
891900

892901
static PyMethodDef ga_iter_methods[] = {

Objects/iterobject.c

+16-6
Original file line numberDiff line numberDiff line change
@@ -103,11 +103,16 @@ PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(
103103
static PyObject *
104104
iter_reduce(seqiterobject *it, PyObject *Py_UNUSED(ignored))
105105
{
106+
PyObject *iter = _PyEval_GetBuiltin(&_Py_ID(iter));
107+
108+
/* _PyEval_GetBuiltin can invoke arbitrary code,
109+
* call must be before access of iterator pointers.
110+
* see issue #101765 */
111+
106112
if (it->it_seq != NULL)
107-
return Py_BuildValue("N(O)n", _PyEval_GetBuiltin(&_Py_ID(iter)),
108-
it->it_seq, it->it_index);
113+
return Py_BuildValue("N(O)n", iter, it->it_seq, it->it_index);
109114
else
110-
return Py_BuildValue("N(())", _PyEval_GetBuiltin(&_Py_ID(iter)));
115+
return Py_BuildValue("N(())", iter);
111116
}
112117

113118
PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
@@ -242,11 +247,16 @@ calliter_iternext(calliterobject *it)
242247
static PyObject *
243248
calliter_reduce(calliterobject *it, PyObject *Py_UNUSED(ignored))
244249
{
250+
PyObject *iter = _PyEval_GetBuiltin(&_Py_ID(iter));
251+
252+
/* _PyEval_GetBuiltin can invoke arbitrary code,
253+
* call must be before access of iterator pointers.
254+
* see issue #101765 */
255+
245256
if (it->it_callable != NULL && it->it_sentinel != NULL)
246-
return Py_BuildValue("N(OO)", _PyEval_GetBuiltin(&_Py_ID(iter)),
247-
it->it_callable, it->it_sentinel);
257+
return Py_BuildValue("N(OO)", iter, it->it_callable, it->it_sentinel);
248258
else
249-
return Py_BuildValue("N(())", _PyEval_GetBuiltin(&_Py_ID(iter)));
259+
return Py_BuildValue("N(())", iter);
250260
}
251261

252262
static PyMethodDef calliter_methods[] = {

0 commit comments

Comments
 (0)