summaryrefslogtreecommitdiffstats
path: root/Python
diff options
context:
space:
mode:
authorBrandt Bucher <brandtbucher@microsoft.com>2022-12-06 14:01:38 (GMT)
committerGitHub <noreply@github.com>2022-12-06 14:01:38 (GMT)
commitb72014c783e5698beb18ee1249597e510b8bcb5a (patch)
tree348e6b38b80f9ec04a12f94d99c1199ebaa75c1c /Python
parent85d5a7e8ef472a4a64e5de883cf313c111a8ec77 (diff)
downloadcpython-b72014c783e5698beb18ee1249597e510b8bcb5a.zip
cpython-b72014c783e5698beb18ee1249597e510b8bcb5a.tar.gz
cpython-b72014c783e5698beb18ee1249597e510b8bcb5a.tar.bz2
GH-99729: Unlink frames before clearing them (GH-100030)
Diffstat (limited to 'Python')
-rw-r--r--Python/bytecodes.c5
-rw-r--r--Python/ceval.c13
-rw-r--r--Python/frame.c3
-rw-r--r--Python/generated_cases.c.h5
4 files changed, 15 insertions, 11 deletions
diff --git a/Python/bytecodes.c b/Python/bytecodes.c
index 41dd1ac..d0480ac 100644
--- a/Python/bytecodes.c
+++ b/Python/bytecodes.c
@@ -619,7 +619,10 @@ dummy_func(
DTRACE_FUNCTION_EXIT();
_Py_LeaveRecursiveCallPy(tstate);
assert(frame != &entry_frame);
- frame = cframe.current_frame = pop_frame(tstate, frame);
+ // GH-99729: We need to unlink the frame *before* clearing it:
+ _PyInterpreterFrame *dying = frame;
+ frame = cframe.current_frame = dying->previous;
+ _PyEvalFrameClearAndPop(tstate, dying);
_PyFrame_StackPush(frame, retval);
goto resume_frame;
}
diff --git a/Python/ceval.c b/Python/ceval.c
index 80bfa21..9e4179e 100644
--- a/Python/ceval.c
+++ b/Python/ceval.c
@@ -1009,14 +1009,6 @@ trace_function_exit(PyThreadState *tstate, _PyInterpreterFrame *frame, PyObject
return 0;
}
-static _PyInterpreterFrame *
-pop_frame(PyThreadState *tstate, _PyInterpreterFrame *frame)
-{
- _PyInterpreterFrame *prev_frame = frame->previous;
- _PyEvalFrameClearAndPop(tstate, frame);
- return prev_frame;
-}
-
int _Py_CheckRecursiveCallPy(
PyThreadState *tstate)
@@ -1432,7 +1424,10 @@ exit_unwind:
assert(_PyErr_Occurred(tstate));
_Py_LeaveRecursiveCallPy(tstate);
assert(frame != &entry_frame);
- frame = cframe.current_frame = pop_frame(tstate, frame);
+ // GH-99729: We need to unlink the frame *before* clearing it:
+ _PyInterpreterFrame *dying = frame;
+ frame = cframe.current_frame = dying->previous;
+ _PyEvalFrameClearAndPop(tstate, dying);
if (frame == &entry_frame) {
/* Restore previous cframe and exit */
tstate->cframe = cframe.previous;
diff --git a/Python/frame.c b/Python/frame.c
index 52f6ef4..b1525cc 100644
--- a/Python/frame.c
+++ b/Python/frame.c
@@ -127,6 +127,9 @@ _PyFrame_Clear(_PyInterpreterFrame *frame)
* to have cleared the enclosing generator, if any. */
assert(frame->owner != FRAME_OWNED_BY_GENERATOR ||
_PyFrame_GetGenerator(frame)->gi_frame_state == FRAME_CLEARED);
+ // GH-99729: Clearing this frame can expose the stack (via finalizers). It's
+ // crucial that this frame has been unlinked, and is no longer visible:
+ assert(_PyThreadState_GET()->cframe->current_frame != frame);
if (frame->frame_obj) {
PyFrameObject *f = frame->frame_obj;
frame->frame_obj = NULL;
diff --git a/Python/generated_cases.c.h b/Python/generated_cases.c.h
index 3a40382..0805386 100644
--- a/Python/generated_cases.c.h
+++ b/Python/generated_cases.c.h
@@ -628,7 +628,10 @@
DTRACE_FUNCTION_EXIT();
_Py_LeaveRecursiveCallPy(tstate);
assert(frame != &entry_frame);
- frame = cframe.current_frame = pop_frame(tstate, frame);
+ // GH-99729: We need to unlink the frame *before* clearing it:
+ _PyInterpreterFrame *dying = frame;
+ frame = cframe.current_frame = dying->previous;
+ _PyEvalFrameClearAndPop(tstate, dying);
_PyFrame_StackPush(frame, retval);
goto resume_frame;
}