codekingpro/portable-devtools
114k
1#ifndef GREENLET_PYTHON_STATE_CPP
2#define GREENLET_PYTHON_STATE_CPP
3
4#include <Python.h>
5#include "TGreenlet.hpp"
6
7namespace greenlet {
8
9PythonState::PythonState()
10 : _top_frame()
11#if GREENLET_USE_CFRAME
12 ,cframe(nullptr)
13 ,use_tracing(0)
14#endif
15#if GREENLET_PY314
16 ,py_recursion_depth(0)
17 ,current_executor(nullptr)
18 ,stackpointer(nullptr)
19 #ifdef Py_GIL_DISABLED
20 ,c_stack_refs(nullptr)
21 #endif
22#elif GREENLET_PY312
23 ,py_recursion_depth(0)
24 ,c_recursion_depth(0)
25#else
26 ,recursion_depth(0)
27#endif
28#if GREENLET_PY313
29 ,delete_later(nullptr)
30#else
31 ,trash_delete_nesting(0)
32#endif
33#if GREENLET_PY311
34 ,current_frame(nullptr)
35 ,datastack_chunk(nullptr)
36 ,datastack_top(nullptr)
37 ,datastack_limit(nullptr)
38#endif
39{
40#if GREENLET_USE_CFRAME
41 /*
42 The PyThreadState->cframe pointer usually points to memory on
43 the stack, alloceted in a call into PyEval_EvalFrameDefault.
44
45 Initially, before any evaluation begins, it points to the
46 initial PyThreadState object's ``root_cframe`` object, which is
47 statically allocated for the lifetime of the thread.
48
49 A greenlet can last for longer than a call to
50 PyEval_EvalFrameDefault, so we can't set its ``cframe`` pointer
51 to be the current ``PyThreadState->cframe``; nor could we use
52 one from the greenlet parent for the same reason. Yet a further
53 no: we can't allocate one scoped to the greenlet and then
54 destroy it when the greenlet is deallocated, because inside the
55 interpreter the _PyCFrame objects form a linked list, and that too
56 can result in accessing memory beyond its dynamic lifetime (if
57 the greenlet doesn't actually finish before it dies, its entry
58 could still be in the list).
59
60 Using the ``root_cframe`` is problematic, though, because its
61 members are never modified by the interpreter and are set to 0,
62 meaning that its ``use_tracing`` flag is never updated. We don't
63 want to modify that value in the ``root_cframe`` ourself: it
64 *shouldn't* matter much because we should probably never get
65 back to the point where that's the only cframe on the stack;
66 even if it did matter, the major consequence of an incorrect
67 value for ``use_tracing`` is that if its true the interpreter
68 does some extra work --- however, it's just good code hygiene.
69
70 Our solution: before a greenlet runs, after its initial
71 creation, it uses the ``root_cframe`` just to have something to
72 put there. However, once the greenlet is actually switched to
73 for the first time, ``g_initialstub`` (which doesn't actually
74 "return" while the greenlet is running) stores a new _PyCFrame on
75 its local stack, and copies the appropriate values from the
76 currently running _PyCFrame; this is then made the _PyCFrame for the
77 newly-minted greenlet. ``g_initialstub`` then proceeds to call
78 ``glet.run()``, which results in ``PyEval_...`` adding the
79 _PyCFrame to the list. Switches continue as normal. Finally, when
80 the greenlet finishes, the call to ``glet.run()`` returns and
81 the _PyCFrame is taken out of the linked list and the stack value
82 is now unused and free to expire.
83
84 XXX: I think we can do better. If we're deallocing in the same
85 thread, can't we traverse the list and unlink our frame?
86 Can we just keep a reference to the thread state in case we
87 dealloc in another thread? (Is that even possible if we're still
88 running and haven't returned from g_initialstub?)
89 */
90 this->cframe = &PyThreadState_GET()->root_cframe;
91#endif
92}
93
94
95inline void PythonState::may_switch_away() noexcept
96{
97#if GREENLET_PY311
98 // PyThreadState_GetFrame is probably going to have to allocate a
99 // new frame object. That may trigger garbage collection. Because
100 // we call this during the early phases of a switch (it doesn't
101 // matter to which greenlet, as this has a global effect), if a GC
102 // triggers a switch away, two things can happen, both bad:
103 // - We might not get switched back to, halting forward progress.
104 // this is pathological, but possible.
105 // - We might get switched back to with a different set of
106 // arguments or a throw instead of a switch. That would corrupt
107 // our state (specifically, PyErr_Occurred() and this->args()
108 // would no longer agree).
109 //
110 // Thus, when we call this API, we need to have GC disabled.
111 // This method serves as a bottleneck we call when maybe beginning
112 // a switch. In this way, it is always safe -- no risk of GC -- to
113 // use ``_GetFrame()`` whenever we need to, just as it was in
114 // <=3.10 (because subsequent calls will be cached and not
115 // allocate memory).
116
117 GCDisabledGuard no_gc;
118 Py_XDECREF(PyThreadState_GetFrame(PyThreadState_GET()));
119#endif
120}
121
122void PythonState::operator<<(const PyThreadState *const tstate) noexcept
123{
124 this->_context.steal(tstate->context);
125#if GREENLET_USE_CFRAME
126 /*
127 IMPORTANT: ``cframe`` is a pointer into the STACK. Thus, because
128 the call to ``slp_switch()`` changes the contents of the stack,
129 you cannot read from ``ts_current->cframe`` after that call and
130 necessarily get the same values you get from reading it here.
131 Anything you need to restore from now to then must be saved in a
132 global/threadlocal variable (because we can't use stack
133 variables here either). For things that need to persist across
134 the switch, use `will_switch_from`.
135 */
136 this->cframe = tstate->cframe;
137 #if !GREENLET_PY312
138 this->use_tracing = tstate->cframe->use_tracing;
139 #endif
140#endif // GREENLET_USE_CFRAME
141#if GREENLET_PY311
142 #if GREENLET_PY314
143 this->py_recursion_depth = tstate->py_recursion_limit - tstate->py_recursion_remaining;
144 this->current_executor = tstate->current_executor;
145 #ifdef Py_GIL_DISABLED
146 this->c_stack_refs = ((_PyThreadStateImpl*)tstate)->c_stack_refs;
147 #endif
148 #elif GREENLET_PY312
149 this->py_recursion_depth = tstate->py_recursion_limit - tstate->py_recursion_remaining;
150 this->c_recursion_depth = Py_C_RECURSION_LIMIT - tstate->c_recursion_remaining;
151 #else // not 312
152 this->recursion_depth = tstate->recursion_limit - tstate->recursion_remaining;
153 #endif // GREENLET_PY312
154 #if GREENLET_PY313
155 this->current_frame = tstate->current_frame;
156 #elif GREENLET_USE_CFRAME
157 this->current_frame = tstate->cframe->current_frame;
158 #endif
159 this->datastack_chunk = tstate->datastack_chunk;
160 this->datastack_top = tstate->datastack_top;
161 this->datastack_limit = tstate->datastack_limit;
162
163 PyFrameObject *frame = PyThreadState_GetFrame((PyThreadState *)tstate);
164 Py_XDECREF(frame); // PyThreadState_GetFrame gives us a new
165 // reference.
166 this->_top_frame.steal(frame);
167 #if GREENLET_PY314
168 if (this->top_frame()) {
169 this->stackpointer = this->_top_frame->f_frame->stackpointer;
170 }
171 else {
172 this->stackpointer = nullptr;
173 }
174 #endif
175 #if GREENLET_PY313
176 // By contract of _PyTrash_thread_deposit_object,
177 // the ``delete_later`` object has a refcount of 0.
178 // We take a strong reference to it.
179 //
180 // Now, ``delete_later`` is managed as a
181 // linked list whose objects are unconditionally deallocated
182 // WITHOUT calling DECREF on them, so it's not clear what that is
183 // actually accomplishing. That is, if another object is pushed on
184 // the list and then the list is deallocated, this object will
185 // still be deallocated. This strong reference serves as a form of
186 // resurrection, meaning that when operator>> DECREFs it, we might
187 // enter its ``tp_dealloc`` function again.
188 //
189 // In practice, it's quite difficult to arrange for this to be
190 // a non-null value during a greenlet switch.
191 // ``greenlet.tests.test_greenlet_trash`` tries, but under 3.14,
192 // at least, fails to do so.
193 this->delete_later = Py_XNewRef(tstate->delete_later);
194 #elif GREENLET_PY312
195 this->trash_delete_nesting = tstate->trash.delete_nesting;
196 #else // not 312 or 3.13+
197 this->trash_delete_nesting = tstate->trash_delete_nesting;
198 #endif // GREENLET_PY312
199#else // Not 311
200 this->recursion_depth = tstate->recursion_depth;
201 this->_top_frame.steal(tstate->frame);
202 this->trash_delete_nesting = tstate->trash_delete_nesting;
203#endif // GREENLET_PY311
204}
205
206#if GREENLET_PY312
207void GREENLET_NOINLINE(PythonState::unexpose_frames)()
208{
209 if (!this->top_frame()) {
210 return;
211 }
212
213 // See GreenletState::expose_frames() and the comment on frames_were_exposed
214 // for more information about this logic.
215 _PyInterpreterFrame *iframe = this->_top_frame->f_frame;
216 while (iframe != nullptr) {
217 _PyInterpreterFrame *prev_exposed = iframe->previous;
218 assert(iframe->frame_obj);
219 memcpy(&iframe->previous, &iframe->frame_obj->_f_frame_data[0],
220 sizeof(void *));
221 iframe = prev_exposed;
222 }
223}
224#else
225void PythonState::unexpose_frames()
226{}
227#endif
228
229void PythonState::operator>>(PyThreadState *const tstate) noexcept
230{
231 tstate->context = this->_context.relinquish_ownership();
232 /* Incrementing this value invalidates the contextvars cache,
233 which would otherwise remain valid across switches */
234 tstate->context_ver++;
235#if GREENLET_USE_CFRAME
236 tstate->cframe = this->cframe;
237 /*
238 If we were tracing, we need to keep tracing.
239 There should never be the possibility of hitting the
240 root_cframe here. See note above about why we can't
241 just copy this from ``origin->cframe->use_tracing``.
242 */
243 #if !GREENLET_PY312
244 tstate->cframe->use_tracing = this->use_tracing;
245 #endif
246#endif // GREENLET_USE_CFRAME
247#if GREENLET_PY311
248 #if GREENLET_PY314
249 tstate->py_recursion_remaining = tstate->py_recursion_limit - this->py_recursion_depth;
250 tstate->current_executor = this->current_executor;
251 #ifdef Py_GIL_DISABLED
252 ((_PyThreadStateImpl*)tstate)->c_stack_refs = this->c_stack_refs;
253 #endif
254 this->unexpose_frames();
255 #elif GREENLET_PY312
256 tstate->py_recursion_remaining = tstate->py_recursion_limit - this->py_recursion_depth;
257 tstate->c_recursion_remaining = Py_C_RECURSION_LIMIT - this->c_recursion_depth;
258 this->unexpose_frames();
259 #else // \/ 3.11
260 tstate->recursion_remaining = tstate->recursion_limit - this->recursion_depth;
261 #endif // GREENLET_PY312
262 #if GREENLET_PY313
263 tstate->current_frame = this->current_frame;
264 #elif GREENLET_USE_CFRAME
265 tstate->cframe->current_frame = this->current_frame;
266 #endif
267 tstate->datastack_chunk = this->datastack_chunk;
268 tstate->datastack_top = this->datastack_top;
269 tstate->datastack_limit = this->datastack_limit;
270#if GREENLET_PY314 && defined(Py_GIL_DISABLED)
271 if (this->top_frame()) {
272 this->_top_frame->f_frame->stackpointer = this->stackpointer;
273 }
274#endif
275 this->_top_frame.relinquish_ownership();
276 #if GREENLET_PY313
277 // See comments in operator<<. We own a strong reference to
278 // this->delete_later, which may or may not be the same object as
279 // tstate->delete_later (depending if something pushed an object
280 // onto the trashcan). Again, because ``delete_later`` is managed
281 // as a linked list, it's not clear that saving and restoring the
282 // value, especially without ever setting it to NULL, accomplishes
283 // much...but the code was added by a core dev, so assume correct.
284 //
285 // Recall that tstate->delete_later is supposed to have a refcount
286 // of 0, because objects are added there from their ``tp_dealloc``
287 // method. So we should only need to DECREF it if we're the ones
288 // that INCREF'd it in operator<<. (This is different than the
289 // core dev's original code which always did this.)
290 if (this->delete_later == tstate->delete_later) {
291 Py_XDECREF(tstate->delete_later);
292 tstate->delete_later = this->delete_later;
293 this->delete_later = nullptr;
294 }
295 else {
296 // it got switched behind our back. So the reference we own
297 // needs to be explicitly cleared.
298 tstate->delete_later = this->delete_later;
299 Py_CLEAR(this->delete_later);
300 }
301
302
303 #elif GREENLET_PY312
304 tstate->trash.delete_nesting = this->trash_delete_nesting;
305 #else // not 3.12
306 tstate->trash_delete_nesting = this->trash_delete_nesting;
307 #endif // GREENLET_PY312
308#else // not 3.11
309 tstate->frame = this->_top_frame.relinquish_ownership();
310 tstate->recursion_depth = this->recursion_depth;
311 tstate->trash_delete_nesting = this->trash_delete_nesting;
312#endif // GREENLET_PY311
313}
314
315inline void PythonState::will_switch_from(PyThreadState *const origin_tstate) noexcept
316{
317#if GREENLET_USE_CFRAME && !GREENLET_PY312
318 // The weird thing is, we don't actually save this for an
319 // effect on the current greenlet, it's saved for an
320 // effect on the target greenlet. That is, we want
321 // continuity of this setting across the greenlet switch.
322 this->use_tracing = origin_tstate->cframe->use_tracing;
323#endif
324}
325
326void PythonState::set_initial_state(const PyThreadState* const tstate) noexcept
327{
328 this->_top_frame = nullptr;
329#if GREENLET_PY314
330 this->py_recursion_depth = tstate->py_recursion_limit - tstate->py_recursion_remaining;
331 this->current_executor = tstate->current_executor;
332 #ifdef Py_GIL_DISABLED
333 this->c_stack_refs = ((_PyThreadStateImpl*)tstate)->c_stack_refs;
334 #endif
335 // this->stackpointer is left null because this->_top_frame is
336 // null so there is no value to copy.
337#elif GREENLET_PY312
338 this->py_recursion_depth = tstate->py_recursion_limit - tstate->py_recursion_remaining;
339#if GREENLET_314
340 this->c_recursion_depth = 0; // unused on 3.14
341#else
342 this->c_recursion_depth = Py_C_RECURSION_LIMIT - tstate->c_recursion_remaining;
343#endif
344#elif GREENLET_PY311
345 this->recursion_depth = tstate->recursion_limit - tstate->recursion_remaining;
346#else
347 this->recursion_depth = tstate->recursion_depth;
348#endif
349}
350// TODO: Better state management about when we own the top frame.
351int PythonState::tp_traverse(visitproc visit, void* arg, bool own_top_frame) noexcept
352{
353 Py_VISIT(this->_context.borrow());
354 if (own_top_frame) {
355 Py_VISIT(this->_top_frame.borrow());
356 }
357#if GREENLET_PY314
358 // TODO: Should we be visiting the c_stack_refs objects?
359 // CPython uses a specific macro to do that which takes into
360 // account boxing and null values and then calls
361 // ``_PyGC_VisitStackRef``, but we don't have access to that, and
362 // we can't duplicate it ourself (because it compares
363 // ``visitproc`` to another function we can't access).
364 // The naive way of looping over c_stack_refs->ref and visiting
365 // those crashes the process (at least with GIL disabled).
366#endif
367 // Note that we DO NOT visit ``delete_later``. Even if it's
368 // non-null and we technically own a reference to it, its
369 // reference count already went to 0 once and it was in the
370 // process of being deallocated. The trash can mechanism linked it
371 // into a list that will be cleaned at some later time, and it has
372 // become untracked by the GC.
373 return 0;
374}
375
376void PythonState::tp_clear(bool own_top_frame) noexcept
377{
378 PythonStateContext::tp_clear();
379 // If we get here owning a frame,
380 // we got dealloc'd without being finished. We may or may not be
381 // in the same thread.
382 if (own_top_frame) {
383 this->_top_frame.CLEAR();
384 }
385}
386
387#if GREENLET_USE_CFRAME
388void PythonState::set_new_cframe(_PyCFrame& frame) noexcept
389{
390 frame = *PyThreadState_GET()->cframe;
391 /* Make the target greenlet refer to the stack value. */
392 this->cframe = &frame;
393 /*
394 And restore the link to the previous frame so this one gets
395 unliked appropriately.
396 */
397 this->cframe->previous = &PyThreadState_GET()->root_cframe;
398}
399#endif
400
401const PythonState::OwnedFrame& PythonState::top_frame() const noexcept
402{
403 return this->_top_frame;
404}
405
406void PythonState::did_finish(PyThreadState* tstate) noexcept
407{
408#if GREENLET_PY311
409 // See https://github.com/gevent/gevent/issues/1924 and
410 // https://github.com/python-greenlet/greenlet/issues/328. In
411 // short, Python 3.11 allocates memory for frames as a sort of
412 // linked list that's kept as part of PyThreadState in the
413 // ``datastack_chunk`` member and friends. These are saved and
414 // restored as part of switching greenlets.
415 //
416 // When we initially switch to a greenlet, we set those to NULL.
417 // That causes the frame management code to treat this like a
418 // brand new thread and start a fresh list of chunks, beginning
419 // with a new "root" chunk. As we make calls in this greenlet,
420 // those chunks get added, and as calls return, they get popped.
421 // But the frame code (pystate.c) is careful to make sure that the
422 // root chunk never gets popped.
423 //
424 // Thus, when a greenlet exits for the last time, there will be at
425 // least a single root chunk that we must be responsible for
426 // deallocating.
427 //
428 // The complex part is that these chunks are allocated and freed
429 // using ``_PyObject_VirtualAlloc``/``Free``. Those aren't public
430 // functions, and they aren't exported for linking. It so happens
431 // that we know they are just thin wrappers around the Arena
432 // allocator, so we can use that directly to deallocate in a
433 // compatible way.
434 //
435 // CAUTION: Check this implementation detail on every major version.
436 //
437 // It might be nice to be able to do this in our destructor, but
438 // can we be sure that no one else is using that memory? Plus, as
439 // described below, our pointers may not even be valid anymore. As
440 // a special case, there is one time that we know we can do this,
441 // and that's from the destructor of the associated UserGreenlet
442 // (NOT main greenlet)
443 PyObjectArenaAllocator alloc;
444 _PyStackChunk* chunk = nullptr;
445 if (tstate) {
446 // We really did finish, we can never be switched to again.
447 chunk = tstate->datastack_chunk;
448 // Unfortunately, we can't do much sanity checking. Our
449 // this->datastack_chunk pointer is out of date (evaluation may
450 // have popped down through it already) so we can't verify that
451 // we deallocate it. I don't think we can even check datastack_top
452 // for the same reason.
453
454 PyObject_GetArenaAllocator(&alloc);
455 tstate->datastack_chunk = nullptr;
456 tstate->datastack_limit = nullptr;
457 tstate->datastack_top = nullptr;
458
459 }
460 else if (this->datastack_chunk) {
461 // The UserGreenlet (NOT the main greenlet!) is being deallocated. If we're
462 // still holding a stack chunk, it's garbage because we know
463 // we can never switch back to let cPython clean it up.
464 // Because the last time we got switched away from, and we
465 // haven't run since then, we know our chain is valid and can
466 // be dealloced.
467 chunk = this->datastack_chunk;
468 PyObject_GetArenaAllocator(&alloc);
469 }
470
471 if (alloc.free && chunk) {
472 // In case the arena mechanism has been torn down already.
473 while (chunk) {
474 _PyStackChunk *prev = chunk->previous;
475 chunk->previous = nullptr;
476 alloc.free(alloc.ctx, chunk, chunk->size);
477 chunk = prev;
478 }
479 }
480
481 this->datastack_chunk = nullptr;
482 this->datastack_limit = nullptr;
483 this->datastack_top = nullptr;
484#endif
485}
486
487
488}; // namespace greenlet
489
490#endif // GREENLET_PYTHON_STATE_CPP
491 