frame->prev_instr < _PyCode_CODE(frame->f_code) + frame->f_code->_co_firsttraceable;
}
+static inline _PyInterpreterFrame *
+_PyFrame_GetFirstComplete(_PyInterpreterFrame *frame)
+{
+ while (frame && _PyFrame_IsIncomplete(frame)) {
+ frame = frame->previous;
+ }
+ return frame;
+}
+
+static inline _PyInterpreterFrame *
+_PyThreadState_GetFrame(PyThreadState *tstate)
+{
+ return _PyFrame_GetFirstComplete(tstate->cframe->current_frame);
+}
+
/* For use by _PyFrame_GetFrameObject
Do not call directly. */
PyFrameObject *
import gc
+import operator
import re
import sys
import textwrap
)
sneaky_frame_object = sneaky_frame_object.f_back
+ def test_entry_frames_are_invisible_during_teardown(self):
+ class C:
+ """A weakref'able class."""
+
+ def f():
+ """Try to find globals and locals as this frame is being cleared."""
+ ref = C()
+ # Ignore the fact that exec(C()) is a nonsense callback. We're only
+ # using exec here because it tries to access the current frame's
+ # globals and locals. If it's trying to get those from a shim frame,
+ # we'll crash before raising:
+ return weakref.ref(ref, exec)
+
+ with support.catch_unraisable_exception() as catcher:
+ # Call from C, so there is a shim frame directly above f:
+ weak = operator.call(f) # BOOM!
+ # Cool, we didn't crash. Check that the callback actually happened:
+ self.assertIs(catcher.unraisable.exc_type, TypeError)
+ self.assertIsNone(weak())
+
@unittest.skipIf(_testcapi is None, 'need _testcapi')
class TestCAPI(unittest.TestCase):
def getframe(self):
--- /dev/null
+Fix an issue where "incomplete" frames could be briefly visible to C code
+while other frames are being torn down, possibly resulting in corruption or
+hard crashes of the interpreter while running finalizers.
return;
}
- _PyInterpreterFrame *pyframe = tstate->cframe->current_frame;
- for (;;) {
- while (pyframe && _PyFrame_IsIncomplete(pyframe)) {
- pyframe = pyframe->previous;
- }
- if (pyframe == NULL) {
- break;
- }
+ _PyInterpreterFrame *pyframe = _PyThreadState_GetFrame(tstate);
+ while (pyframe) {
if (traceback->nframe < tracemalloc_config.max_nframe) {
tracemalloc_get_frame(pyframe, &traceback->frames[traceback->nframe]);
assert(traceback->frames[traceback->nframe].filename != NULL);
if (traceback->total_nframe < UINT16_MAX) {
traceback->total_nframe++;
}
-
- pyframe = pyframe->previous;
+ pyframe = _PyFrame_GetFirstComplete(pyframe->previous);
}
}
*/
_Py_atomic_store(&is_tripped, 0);
- _PyInterpreterFrame *frame = tstate->cframe->current_frame;
- while (frame && _PyFrame_IsIncomplete(frame)) {
- frame = frame->previous;
- }
+ _PyInterpreterFrame *frame = _PyThreadState_GetFrame(tstate);
signal_state_t *state = &signal_global_state;
for (int i = 1; i < Py_NSIG; i++) {
if (!_Py_atomic_load_relaxed(&Handlers[i].tripped)) {
PyFrameObject *back = frame->f_back;
if (back == NULL) {
_PyInterpreterFrame *prev = frame->f_frame->previous;
- while (prev && _PyFrame_IsIncomplete(prev)) {
- prev = prev->previous;
- }
+ prev = _PyFrame_GetFirstComplete(prev);
if (prev) {
back = _PyFrame_GetFrameObject(prev);
}
if (origin_depth == 0) {
((PyCoroObject *)coro)->cr_origin_or_finalizer = NULL;
} else {
- assert(_PyEval_GetFrame());
- PyObject *cr_origin = compute_cr_origin(origin_depth, _PyEval_GetFrame()->previous);
+ _PyInterpreterFrame *frame = tstate->cframe->current_frame;
+ assert(frame);
+ assert(_PyFrame_IsIncomplete(frame));
+ frame = _PyFrame_GetFirstComplete(frame->previous);
+ PyObject *cr_origin = compute_cr_origin(origin_depth, frame);
((PyCoroObject *)coro)->cr_origin_or_finalizer = cr_origin;
if (!cr_origin) {
Py_DECREF(coro);
/* First count how many frames we have */
int frame_count = 0;
for (; frame && frame_count < origin_depth; ++frame_count) {
- frame = frame->previous;
+ frame = _PyFrame_GetFirstComplete(frame->previous);
}
/* Now collect them */
return NULL;
}
PyTuple_SET_ITEM(cr_origin, i, frameinfo);
- frame = frame->previous;
+ frame = _PyFrame_GetFirstComplete(frame->previous);
}
return cr_origin;
/* Call super(), without args -- fill in from __class__
and first local variable on the stack. */
PyThreadState *tstate = _PyThreadState_GET();
- _PyInterpreterFrame *cframe = tstate->cframe->current_frame;
- if (cframe == NULL) {
+ _PyInterpreterFrame *frame = _PyThreadState_GetFrame(tstate);
+ if (frame == NULL) {
PyErr_SetString(PyExc_RuntimeError,
"super(): no current frame");
return -1;
}
- int res = super_init_without_args(cframe, cframe->f_code, &type, &obj);
+ int res = super_init_without_args(frame, frame->f_code, &type, &obj);
if (res < 0) {
return -1;
_PyEval_GetFrame(void)
{
PyThreadState *tstate = _PyThreadState_GET();
- return tstate->cframe->current_frame;
+ return _PyThreadState_GetFrame(tstate);
}
PyFrameObject *
PyEval_GetFrame(void)
{
_PyInterpreterFrame *frame = _PyEval_GetFrame();
- while (frame && _PyFrame_IsIncomplete(frame)) {
- frame = frame->previous;
- }
if (frame == NULL) {
return NULL;
}
PyObject *
_PyEval_GetBuiltins(PyThreadState *tstate)
{
- _PyInterpreterFrame *frame = tstate->cframe->current_frame;
+ _PyInterpreterFrame *frame = _PyThreadState_GetFrame(tstate);
if (frame != NULL) {
return frame->f_builtins;
}
PyEval_GetLocals(void)
{
PyThreadState *tstate = _PyThreadState_GET();
- _PyInterpreterFrame *current_frame = tstate->cframe->current_frame;
+ _PyInterpreterFrame *current_frame = _PyThreadState_GetFrame(tstate);
if (current_frame == NULL) {
_PyErr_SetString(tstate, PyExc_SystemError, "frame does not exist");
return NULL;
PyEval_GetGlobals(void)
{
PyThreadState *tstate = _PyThreadState_GET();
- _PyInterpreterFrame *current_frame = tstate->cframe->current_frame;
+ _PyInterpreterFrame *current_frame = _PyThreadState_GetFrame(tstate);
if (current_frame == NULL) {
return NULL;
}
}
assert(!_PyFrame_IsIncomplete(frame));
assert(f->f_back == NULL);
- _PyInterpreterFrame *prev = frame->previous;
- while (prev && _PyFrame_IsIncomplete(prev)) {
- prev = prev->previous;
- }
+ _PyInterpreterFrame *prev = _PyFrame_GetFirstComplete(frame->previous);
frame->previous = NULL;
if (prev) {
assert(prev->owner != FRAME_OWNED_BY_CSTACK);
PyThreadState_GetFrame(PyThreadState *tstate)
{
assert(tstate != NULL);
- _PyInterpreterFrame *f = tstate->cframe->current_frame;
- while (f && _PyFrame_IsIncomplete(f)) {
- f = f->previous;
- }
+ _PyInterpreterFrame *f = _PyThreadState_GetFrame(tstate);
if (f == NULL) {
return NULL;
}
PyThreadState *t;
for (t = i->threads.head; t != NULL; t = t->next) {
_PyInterpreterFrame *frame = t->cframe->current_frame;
- while (frame && _PyFrame_IsIncomplete(frame)) {
- frame = frame->previous;
- }
+ frame = _PyFrame_GetFirstComplete(frame);
if (frame == NULL) {
continue;
}
if (frame != NULL) {
while (depth > 0) {
- frame = frame->previous;
+ frame = _PyFrame_GetFirstComplete(frame->previous);
if (frame == NULL) {
break;
}
- if (_PyFrame_IsIncomplete(frame)) {
- continue;
- }
--depth;
}
}