• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 
2 #define _PY_INTERPRETER
3 
4 #include "Python.h"
5 #include "frameobject.h"
6 #include "pycore_code.h"          // stats
7 #include "pycore_frame.h"
8 #include "pycore_object.h"        // _PyObject_GC_UNTRACK()
9 #include "opcode.h"
10 
11 int
_PyFrame_Traverse(_PyInterpreterFrame * frame,visitproc visit,void * arg)12 _PyFrame_Traverse(_PyInterpreterFrame *frame, visitproc visit, void *arg)
13 {
14     Py_VISIT(frame->frame_obj);
15     Py_VISIT(frame->f_locals);
16     Py_VISIT(frame->f_funcobj);
17     Py_VISIT(_PyFrame_GetCode(frame));
18    /* locals */
19     PyObject **locals = _PyFrame_GetLocalsArray(frame);
20     int i = 0;
21     /* locals and stack */
22     for (; i <frame->stacktop; i++) {
23         Py_VISIT(locals[i]);
24     }
25     return 0;
26 }
27 
28 PyFrameObject *
_PyFrame_MakeAndSetFrameObject(_PyInterpreterFrame * frame)29 _PyFrame_MakeAndSetFrameObject(_PyInterpreterFrame *frame)
30 {
31     assert(frame->frame_obj == NULL);
32     PyObject *exc = PyErr_GetRaisedException();
33 
34     PyFrameObject *f = _PyFrame_New_NoTrack(_PyFrame_GetCode(frame));
35     if (f == NULL) {
36         Py_XDECREF(exc);
37         return NULL;
38     }
39     PyErr_SetRaisedException(exc);
40 
41     // GH-97002: There was a time when a frame object could be created when we
42     // are allocating the new frame object f above, so frame->frame_obj would
43     // be assigned already. That path does not exist anymore. We won't call any
44     // Python code in this function and garbage collection will not run.
45     // Notice that _PyFrame_New_NoTrack() can potentially raise a MemoryError,
46     // but it won't allocate a traceback until the frame unwinds, so we are safe
47     // here.
48     assert(frame->frame_obj == NULL);
49     assert(frame->owner != FRAME_OWNED_BY_FRAME_OBJECT);
50     assert(frame->owner != FRAME_CLEARED);
51     f->f_frame = frame;
52     frame->frame_obj = f;
53     return f;
54 }
55 
56 static void
take_ownership(PyFrameObject * f,_PyInterpreterFrame * frame)57 take_ownership(PyFrameObject *f, _PyInterpreterFrame *frame)
58 {
59     assert(frame->owner != FRAME_OWNED_BY_CSTACK);
60     assert(frame->owner != FRAME_OWNED_BY_FRAME_OBJECT);
61     assert(frame->owner != FRAME_CLEARED);
62     Py_ssize_t size = ((char*)&frame->localsplus[frame->stacktop]) - (char *)frame;
63     Py_INCREF(_PyFrame_GetCode(frame));
64     memcpy((_PyInterpreterFrame *)f->_f_frame_data, frame, size);
65     frame = (_PyInterpreterFrame *)f->_f_frame_data;
66     f->f_frame = frame;
67     frame->owner = FRAME_OWNED_BY_FRAME_OBJECT;
68     if (_PyFrame_IsIncomplete(frame)) {
69         // This may be a newly-created generator or coroutine frame. Since it's
70         // dead anyways, just pretend that the first RESUME ran:
71         PyCodeObject *code = _PyFrame_GetCode(frame);
72         frame->instr_ptr = _PyCode_CODE(code) + code->_co_firsttraceable + 1;
73     }
74     assert(!_PyFrame_IsIncomplete(frame));
75     assert(f->f_back == NULL);
76     _PyInterpreterFrame *prev = _PyFrame_GetFirstComplete(frame->previous);
77     frame->previous = NULL;
78     if (prev) {
79         assert(prev->owner != FRAME_OWNED_BY_CSTACK);
80         /* Link PyFrameObjects.f_back and remove link through _PyInterpreterFrame.previous */
81         PyFrameObject *back = _PyFrame_GetFrameObject(prev);
82         if (back == NULL) {
83             /* Memory error here. */
84             assert(PyErr_ExceptionMatches(PyExc_MemoryError));
85             /* Nothing we can do about it */
86             PyErr_Clear();
87         }
88         else {
89             f->f_back = (PyFrameObject *)Py_NewRef(back);
90         }
91     }
92     if (!_PyObject_GC_IS_TRACKED((PyObject *)f)) {
93         _PyObject_GC_TRACK((PyObject *)f);
94     }
95 }
96 
97 void
_PyFrame_ClearLocals(_PyInterpreterFrame * frame)98 _PyFrame_ClearLocals(_PyInterpreterFrame *frame)
99 {
100     assert(frame->stacktop >= 0);
101     int stacktop = frame->stacktop;
102     frame->stacktop = 0;
103     for (int i = 0; i < stacktop; i++) {
104         Py_XDECREF(frame->localsplus[i]);
105     }
106     Py_CLEAR(frame->f_locals);
107 }
108 
109 void
_PyFrame_ClearExceptCode(_PyInterpreterFrame * frame)110 _PyFrame_ClearExceptCode(_PyInterpreterFrame *frame)
111 {
112     /* It is the responsibility of the owning generator/coroutine
113      * to have cleared the enclosing generator, if any. */
114     assert(frame->owner != FRAME_OWNED_BY_GENERATOR ||
115         _PyFrame_GetGenerator(frame)->gi_frame_state == FRAME_CLEARED);
116     // GH-99729: Clearing this frame can expose the stack (via finalizers). It's
117     // crucial that this frame has been unlinked, and is no longer visible:
118     assert(_PyThreadState_GET()->current_frame != frame);
119     if (frame->frame_obj) {
120         PyFrameObject *f = frame->frame_obj;
121         frame->frame_obj = NULL;
122         if (Py_REFCNT(f) > 1) {
123             take_ownership(f, frame);
124             Py_DECREF(f);
125             return;
126         }
127         Py_DECREF(f);
128     }
129     _PyFrame_ClearLocals(frame);
130     Py_DECREF(frame->f_funcobj);
131 }
132 
133 /* Unstable API functions */
134 
135 PyObject *
PyUnstable_InterpreterFrame_GetCode(struct _PyInterpreterFrame * frame)136 PyUnstable_InterpreterFrame_GetCode(struct _PyInterpreterFrame *frame)
137 {
138     PyObject *code = frame->f_executable;
139     Py_INCREF(code);
140     return code;
141 }
142 
143 int
PyUnstable_InterpreterFrame_GetLasti(struct _PyInterpreterFrame * frame)144 PyUnstable_InterpreterFrame_GetLasti(struct _PyInterpreterFrame *frame)
145 {
146     return _PyInterpreterFrame_LASTI(frame) * sizeof(_Py_CODEUNIT);
147 }
148 
149 int
PyUnstable_InterpreterFrame_GetLine(_PyInterpreterFrame * frame)150 PyUnstable_InterpreterFrame_GetLine(_PyInterpreterFrame *frame)
151 {
152     int addr = _PyInterpreterFrame_LASTI(frame) * sizeof(_Py_CODEUNIT);
153     return PyCode_Addr2Line(_PyFrame_GetCode(frame), addr);
154 }
155 
156 const PyTypeObject *const PyUnstable_ExecutableKinds[PyUnstable_EXECUTABLE_KINDS+1] = {
157     [PyUnstable_EXECUTABLE_KIND_SKIP] = &_PyNone_Type,
158     [PyUnstable_EXECUTABLE_KIND_PY_FUNCTION] = &PyCode_Type,
159     [PyUnstable_EXECUTABLE_KIND_BUILTIN_FUNCTION] = &PyMethod_Type,
160     [PyUnstable_EXECUTABLE_KIND_METHOD_DESCRIPTOR] = &PyMethodDescr_Type,
161     [PyUnstable_EXECUTABLE_KINDS] = NULL,
162 };
163