Debian Codegraph @

Include/internal/pycore_frame.h

raw
1 #ifndef Py_INTERNAL_FRAME_H
2 #define Py_INTERNAL_FRAME_H
3 #ifdef __cplusplus
4 extern "C" {
5 #endif
6
7 #ifndef Py_BUILD_CORE
8 # error "this header requires Py_BUILD_CORE define"
9 #endif
10
11 #include <stdbool.h>
12 #include <stddef.h> // offsetof()
13 #include "pycore_code.h" // STATS
14 #include "pycore_pymem.h" // _PyMem_IsPtrFreed()
15
16 /* See Objects/frame_layout.md for an explanation of the frame stack
17 * including explanation of the PyFrameObject and _PyInterpreterFrame
18 * structs. */
19
20
21 struct _frame {
22 PyObject_HEAD
23 PyFrameObject *f_back; /* previous frame, or NULL */
24 struct _PyInterpreterFrame *f_frame; /* points to the frame data */
25 PyObject *f_trace; /* Trace function */
26 int f_lineno; /* Current line number. Only valid if non-zero */
27 char f_trace_lines; /* Emit per-line trace events? */
28 char f_trace_opcodes; /* Emit per-opcode trace events? */
29 PyObject *f_extra_locals; /* Dict for locals set by users using f_locals, could be NULL */
30 /* This is purely for backwards compatibility for PyEval_GetLocals.
31 PyEval_GetLocals requires a borrowed reference so the actual reference
32 is stored here */
33 PyObject *f_locals_cache;
34 /* The frame data, if this frame object owns the frame */
35 PyObject *_f_frame_data[1];
36 };
37
38 extern PyFrameObject* _PyFrame_New_NoTrack(PyCodeObject *code);
39
40
41 /* other API */
42
43 typedef enum _framestate {
44 FRAME_CREATED = -3,
45 FRAME_SUSPENDED = -2,
46 FRAME_SUSPENDED_YIELD_FROM = -1,
47 FRAME_EXECUTING = 0,
48 FRAME_COMPLETED = 1,
49 FRAME_CLEARED = 4
50 } PyFrameState;
51
52 #define FRAME_STATE_SUSPENDED(S) ((S) == FRAME_SUSPENDED || (S) == FRAME_SUSPENDED_YIELD_FROM)
53 #define FRAME_STATE_FINISHED(S) ((S) >= FRAME_COMPLETED)
54
55 enum _frameowner {
56 FRAME_OWNED_BY_THREAD = 0,
57 FRAME_OWNED_BY_GENERATOR = 1,
58 FRAME_OWNED_BY_FRAME_OBJECT = 2,
59 FRAME_OWNED_BY_CSTACK = 3,
60 };
61
62 typedef struct _PyInterpreterFrame {
63 PyObject *f_executable; /* Strong reference (code object or None) */
64 struct _PyInterpreterFrame *previous;
65 PyObject *f_funcobj; /* Strong reference. Only valid if not on C stack */
66 PyObject *f_globals; /* Borrowed reference. Only valid if not on C stack */
67 PyObject *f_builtins; /* Borrowed reference. Only valid if not on C stack */
68 PyObject *f_locals; /* Strong reference, may be NULL. Only valid if not on C stack */
69 PyFrameObject *frame_obj; /* Strong reference, may be NULL. Only valid if not on C stack */
70 _Py_CODEUNIT *instr_ptr; /* Instruction currently executing (or about to begin) */
71 int stacktop; /* Offset of TOS from localsplus */
72 uint16_t return_offset; /* Only relevant during a function call */
73 char owner;
74 /* Locals and stack */
75 PyObject *localsplus[1];
76 } _PyInterpreterFrame;
77
78 #define _PyInterpreterFrame_LASTI(IF) \
79 ((int)((IF)->instr_ptr - _PyCode_CODE(_PyFrame_GetCode(IF))))
80
81 static inline PyCodeObject *_PyFrame_GetCode(_PyInterpreterFrame *f) {
82 assert(PyCode_Check(f->f_executable));
83 return (PyCodeObject *)f->f_executable;
84 }
85
86 // Similar to _PyFrame_GetCode(), but return NULL if the frame is invalid or
87 // freed. Used by dump_frame() in Python/traceback.c. The function uses
88 // heuristics to detect freed memory, it's not 100% reliable.
89 static inline PyCodeObject*
90 _PyFrame_SafeGetCode(_PyInterpreterFrame *f)
91 {
92 // globals and builtins may be NULL on a legit frame, but it's unlikely.
93 // It's more likely that it's a sign of an invalid frame.
94 if (f->f_globals == NULL || f->f_builtins == NULL) {
95 return NULL;
96 }
97
98 PyObject *executable = f->f_executable;
99 // Reimplement _PyObject_IsFreed() to avoid pycore_object.h dependency
100 if (_PyMem_IsPtrFreed(executable) || _PyMem_IsPtrFreed(Py_TYPE(executable))) {
101 return NULL;
102 }
103 if (!PyCode_Check(executable)) {
104 return NULL;
105 }
106 return (PyCodeObject *)executable;
107 }
108
109 static inline PyObject **_PyFrame_Stackbase(_PyInterpreterFrame *f) {
110 return f->localsplus + _PyFrame_GetCode(f)->co_nlocalsplus;
111 }
112
113 static inline PyObject *_PyFrame_StackPeek(_PyInterpreterFrame *f) {
114 assert(f->stacktop > _PyFrame_GetCode(f)->co_nlocalsplus);
115 assert(f->localsplus[f->stacktop-1] != NULL);
116 return f->localsplus[f->stacktop-1];
117 }
118
119 static inline PyObject *_PyFrame_StackPop(_PyInterpreterFrame *f) {
120 assert(f->stacktop > _PyFrame_GetCode(f)->co_nlocalsplus);
121 f->stacktop--;
122 return f->localsplus[f->stacktop];
123 }
124
125 static inline void _PyFrame_StackPush(_PyInterpreterFrame *f, PyObject *value) {
126 f->localsplus[f->stacktop] = value;
127 f->stacktop++;
128 }
129
130 #define FRAME_SPECIALS_SIZE ((int)((sizeof(_PyInterpreterFrame)-1)/sizeof(PyObject *)))
131
132 static inline int
133 _PyFrame_NumSlotsForCodeObject(PyCodeObject *code)
134 {
135 /* This function needs to remain in sync with the calculation of
136 * co_framesize in Tools/build/deepfreeze.py */
137 assert(code->co_framesize >= FRAME_SPECIALS_SIZE);
138 return code->co_framesize - FRAME_SPECIALS_SIZE;
139 }
140
141 static inline void _PyFrame_Copy(_PyInterpreterFrame *src, _PyInterpreterFrame *dest)
142 {
143 assert(src->stacktop >= _PyFrame_GetCode(src)->co_nlocalsplus);
144 *dest = *src;
145 for (int i = 1; i < src->stacktop; i++) {
146 dest->localsplus[i] = src->localsplus[i];
147 }
148 // Don't leave a dangling pointer to the old frame when creating generators
149 // and coroutines:
150 dest->previous = NULL;
151 }
152
153 // Similar to PyUnstable_InterpreterFrame_GetLasti(), but return NULL if the
154 // frame is invalid or freed. Used by dump_frame() in Python/traceback.c. The
155 // function uses heuristics to detect freed memory, it's not 100% reliable.
156 static inline int
157 _PyFrame_SafeGetLasti(struct _PyInterpreterFrame *f)
158 {
159 // Code based on _PyFrame_GetBytecode() but replace _PyFrame_GetCode()
160 // with _PyFrame_SafeGetCode().
161 PyCodeObject *co = _PyFrame_SafeGetCode(f);
162 if (co == NULL) {
163 return -1;
164 }
165
166 return (int)(f->instr_ptr - _PyCode_CODE(co)) * sizeof(_Py_CODEUNIT);
167 }
168
169 /* Consumes reference to func and locals.
170 Does not initialize frame->previous, which happens
171 when frame is linked into the frame stack.
172 */
173 static inline void
174 _PyFrame_Initialize(
175 _PyInterpreterFrame *frame, PyFunctionObject *func,
176 PyObject *locals, PyCodeObject *code, int null_locals_from)
177 {
178 frame->f_funcobj = (PyObject *)func;
179 frame->f_executable = Py_NewRef(code);
180 frame->f_builtins = func->func_builtins;
181 frame->f_globals = func->func_globals;
182 frame->f_locals = locals;
183 frame->stacktop = code->co_nlocalsplus;
184 frame->frame_obj = NULL;
185 frame->instr_ptr = _PyCode_CODE(code);
186 frame->return_offset = 0;
187 frame->owner = FRAME_OWNED_BY_THREAD;
188
189 for (int i = null_locals_from; i < code->co_nlocalsplus; i++) {
190 frame->localsplus[i] = NULL;
191 }
192 }
193
194 /* Gets the pointer to the locals array
195 * that precedes this frame.
196 */
197 static inline PyObject**
198 _PyFrame_GetLocalsArray(_PyInterpreterFrame *frame)
199 {
200 return frame->localsplus;
201 }
202
203 /* Fetches the stack pointer, and sets stacktop to -1.
204 Having stacktop <= 0 ensures that invalid
205 values are not visible to the cycle GC.
206 We choose -1 rather than 0 to assist debugging. */
207 static inline PyObject**
208 _PyFrame_GetStackPointer(_PyInterpreterFrame *frame)
209 {
210 PyObject **sp = frame->localsplus + frame->stacktop;
211 frame->stacktop = -1;
212 return sp;
213 }
214
215 static inline void
216 _PyFrame_SetStackPointer(_PyInterpreterFrame *frame, PyObject **stack_pointer)
217 {
218 frame->stacktop = (int)(stack_pointer - frame->localsplus);
219 }
220
221 /* Determine whether a frame is incomplete.
222 * A frame is incomplete if it is part way through
223 * creating cell objects or a generator or coroutine.
224 *
225 * Frames on the frame stack are incomplete until the
226 * first RESUME instruction.
227 * Frames owned by a generator are always complete.
228 */
229 static inline bool
230 _PyFrame_IsIncomplete(_PyInterpreterFrame *frame)
231 {
232 if (frame->owner == FRAME_OWNED_BY_CSTACK) {
233 return true;
234 }
235 return frame->owner != FRAME_OWNED_BY_GENERATOR &&
236 frame->instr_ptr < _PyCode_CODE(_PyFrame_GetCode(frame)) + _PyFrame_GetCode(frame)->_co_firsttraceable;
237 }
238
239 static inline _PyInterpreterFrame *
240 _PyFrame_GetFirstComplete(_PyInterpreterFrame *frame)
241 {
242 while (frame && _PyFrame_IsIncomplete(frame)) {
243 frame = frame->previous;
244 }
245 return frame;
246 }
247
248 static inline _PyInterpreterFrame *
249 _PyThreadState_GetFrame(PyThreadState *tstate)
250 {
251 return _PyFrame_GetFirstComplete(tstate->current_frame);
252 }
253
254 /* For use by _PyFrame_GetFrameObject
255 Do not call directly. */
256 PyFrameObject *
257 _PyFrame_MakeAndSetFrameObject(_PyInterpreterFrame *frame);
258
259 /* Gets the PyFrameObject for this frame, lazily
260 * creating it if necessary.
261 * Returns a borrowed reference */
262 static inline PyFrameObject *
263 _PyFrame_GetFrameObject(_PyInterpreterFrame *frame)
264 {
265
266 assert(!_PyFrame_IsIncomplete(frame));
267 PyFrameObject *res = frame->frame_obj;
268 if (res != NULL) {
269 return res;
270 }
271 return _PyFrame_MakeAndSetFrameObject(frame);
272 }
273
274 void
275 _PyFrame_ClearLocals(_PyInterpreterFrame *frame);
276
277 /* Clears all references in the frame.
278 * If take is non-zero, then the _PyInterpreterFrame frame
279 * may be transferred to the frame object it references
280 * instead of being cleared. Either way
281 * the caller no longer owns the references
282 * in the frame.
283 * take should be set to 1 for heap allocated
284 * frames like the ones in generators and coroutines.
285 */
286 void
287 _PyFrame_ClearExceptCode(_PyInterpreterFrame * frame);
288
289 int
290 _PyFrame_Traverse(_PyInterpreterFrame *frame, visitproc visit, void *arg);
291
292 bool
293 _PyFrame_HasHiddenLocals(_PyInterpreterFrame *frame);
294
295 PyObject *
296 _PyFrame_GetLocals(_PyInterpreterFrame *frame);
297
298 static inline bool
299 _PyThreadState_HasStackSpace(PyThreadState *tstate, int size)
300 {
301 assert(
302 (tstate->datastack_top == NULL && tstate->datastack_limit == NULL)
303 ||
304 (tstate->datastack_top != NULL && tstate->datastack_limit != NULL)
305 );
306 return tstate->datastack_top != NULL &&
307 size < tstate->datastack_limit - tstate->datastack_top;
308 }
309
310 extern _PyInterpreterFrame *
311 _PyThreadState_PushFrame(PyThreadState *tstate, size_t size);
312
313 PyAPI_FUNC(void) _PyThreadState_PopFrame(PyThreadState *tstate, _PyInterpreterFrame *frame);
314
315 /* Pushes a frame without checking for space.
316 * Must be guarded by _PyThreadState_HasStackSpace()
317 * Consumes reference to func. */
318 static inline _PyInterpreterFrame *
319 _PyFrame_PushUnchecked(PyThreadState *tstate, PyFunctionObject *func, int null_locals_from)
320 {
321 CALL_STAT_INC(frames_pushed);
322 PyCodeObject *code = (PyCodeObject *)func->func_code;
323 _PyInterpreterFrame *new_frame = (_PyInterpreterFrame *)tstate->datastack_top;
324 tstate->datastack_top += code->co_framesize;
325 assert(tstate->datastack_top < tstate->datastack_limit);
326 _PyFrame_Initialize(new_frame, func, NULL, code, null_locals_from);
327 return new_frame;
328 }
329
330 /* Pushes a trampoline frame without checking for space.
331 * Must be guarded by _PyThreadState_HasStackSpace() */
332 static inline _PyInterpreterFrame *
333 _PyFrame_PushTrampolineUnchecked(PyThreadState *tstate, PyCodeObject *code, int stackdepth)
334 {
335 CALL_STAT_INC(frames_pushed);
336 _PyInterpreterFrame *frame = (_PyInterpreterFrame *)tstate->datastack_top;
337 tstate->datastack_top += code->co_framesize;
338 assert(tstate->datastack_top < tstate->datastack_limit);
339 frame->f_funcobj = Py_None;
340 frame->f_executable = Py_NewRef(code);
341 #ifdef Py_DEBUG
342 frame->f_builtins = NULL;
343 frame->f_globals = NULL;
344 #endif
345 frame->f_locals = NULL;
346 frame->stacktop = code->co_nlocalsplus + stackdepth;
347 frame->frame_obj = NULL;
348 frame->instr_ptr = _PyCode_CODE(code);
349 frame->owner = FRAME_OWNED_BY_THREAD;
350 frame->return_offset = 0;
351 return frame;
352 }
353
354 static inline
355 PyGenObject *_PyFrame_GetGenerator(_PyInterpreterFrame *frame)
356 {
357 assert(frame->owner == FRAME_OWNED_BY_GENERATOR);
358 size_t offset_in_gen = offsetof(PyGenObject, gi_iframe);
359 return (PyGenObject *)(((char *)frame) - offset_in_gen);
360 }
361
362 PyAPI_FUNC(_PyInterpreterFrame *)
363 _PyEvalFramePushAndInit(PyThreadState *tstate, PyFunctionObject *func,
364 PyObject *locals, PyObject* const* args,
365 size_t argcount, PyObject *kwnames);
366
367 #ifdef __cplusplus
368 }
369 #endif
370 #endif /* !Py_INTERNAL_FRAME_H */