• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Cyclic garbage collector implementation for free-threaded build.
2 #include "Python.h"
3 #include "pycore_brc.h"           // struct _brc_thread_state
4 #include "pycore_ceval.h"         // _Py_set_eval_breaker_bit()
5 #include "pycore_context.h"
6 #include "pycore_dict.h"          // _PyDict_MaybeUntrack()
7 #include "pycore_initconfig.h"
8 #include "pycore_interp.h"        // PyInterpreterState.gc
9 #include "pycore_object.h"
10 #include "pycore_object_alloc.h"  // _PyObject_MallocWithType()
11 #include "pycore_object_stack.h"
12 #include "pycore_pyerrors.h"
13 #include "pycore_pystate.h"       // _PyThreadState_GET()
14 #include "pycore_tstate.h"        // _PyThreadStateImpl
15 #include "pycore_weakref.h"       // _PyWeakref_ClearRef()
16 #include "pydtrace.h"
17 
18 #ifdef Py_GIL_DISABLED
19 
20 typedef struct _gc_runtime_state GCState;
21 
22 #ifdef Py_DEBUG
23 #  define GC_DEBUG
24 #endif
25 
26 // Each thread buffers the count of allocated objects in a thread-local
27 // variable up to +/- this amount to reduce the overhead of updating
28 // the global count.
29 #define LOCAL_ALLOC_COUNT_THRESHOLD 512
30 
31 // Automatically choose the generation that needs collecting.
32 #define GENERATION_AUTO (-1)
33 
34 // A linked list of objects using the `ob_tid` field as the next pointer.
35 // The linked list pointers are distinct from any real thread ids, because the
36 // thread ids returned by _Py_ThreadId() are also pointers to distinct objects.
37 // No thread will confuse its own id with a linked list pointer.
38 struct worklist {
39     uintptr_t head;
40 };
41 
42 struct worklist_iter {
43     uintptr_t *ptr;   // pointer to current object
44     uintptr_t *next;  // next value of ptr
45 };
46 
47 struct visitor_args {
48     size_t offset;  // offset of PyObject from start of block
49 };
50 
51 // Per-collection state
52 struct collection_state {
53     struct visitor_args base;
54     PyInterpreterState *interp;
55     GCState *gcstate;
56     Py_ssize_t collected;
57     Py_ssize_t uncollectable;
58     Py_ssize_t long_lived_total;
59     struct worklist unreachable;
60     struct worklist legacy_finalizers;
61     struct worklist wrcb_to_call;
62     struct worklist objs_to_decref;
63 };
64 
65 // iterate over a worklist
66 #define WORKSTACK_FOR_EACH(stack, op) \
67     for ((op) = (PyObject *)(stack)->head; (op) != NULL; (op) = (PyObject *)(op)->ob_tid)
68 
69 // iterate over a worklist with support for removing the current object
70 #define WORKSTACK_FOR_EACH_ITER(stack, iter, op) \
71     for (worklist_iter_init((iter), &(stack)->head), (op) = (PyObject *)(*(iter)->ptr); \
72          (op) != NULL; \
73          worklist_iter_init((iter), (iter)->next), (op) = (PyObject *)(*(iter)->ptr))
74 
75 static void
worklist_push(struct worklist * worklist,PyObject * op)76 worklist_push(struct worklist *worklist, PyObject *op)
77 {
78     assert(op->ob_tid == 0);
79     op->ob_tid = worklist->head;
80     worklist->head = (uintptr_t)op;
81 }
82 
83 static PyObject *
worklist_pop(struct worklist * worklist)84 worklist_pop(struct worklist *worklist)
85 {
86     PyObject *op = (PyObject *)worklist->head;
87     if (op != NULL) {
88         worklist->head = op->ob_tid;
89         _Py_atomic_store_uintptr_relaxed(&op->ob_tid, 0);
90     }
91     return op;
92 }
93 
94 static void
worklist_iter_init(struct worklist_iter * iter,uintptr_t * next)95 worklist_iter_init(struct worklist_iter *iter, uintptr_t *next)
96 {
97     iter->ptr = next;
98     PyObject *op = (PyObject *)*(iter->ptr);
99     if (op) {
100         iter->next = &op->ob_tid;
101     }
102 }
103 
104 static void
worklist_remove(struct worklist_iter * iter)105 worklist_remove(struct worklist_iter *iter)
106 {
107     PyObject *op = (PyObject *)*(iter->ptr);
108     *(iter->ptr) = op->ob_tid;
109     op->ob_tid = 0;
110     iter->next = iter->ptr;
111 }
112 
113 static inline int
gc_is_frozen(PyObject * op)114 gc_is_frozen(PyObject *op)
115 {
116     return (op->ob_gc_bits & _PyGC_BITS_FROZEN) != 0;
117 }
118 
119 static inline int
gc_is_unreachable(PyObject * op)120 gc_is_unreachable(PyObject *op)
121 {
122     return (op->ob_gc_bits & _PyGC_BITS_UNREACHABLE) != 0;
123 }
124 
125 static void
gc_set_unreachable(PyObject * op)126 gc_set_unreachable(PyObject *op)
127 {
128     op->ob_gc_bits |= _PyGC_BITS_UNREACHABLE;
129 }
130 
131 static void
gc_clear_unreachable(PyObject * op)132 gc_clear_unreachable(PyObject *op)
133 {
134     op->ob_gc_bits &= ~_PyGC_BITS_UNREACHABLE;
135 }
136 
137 // Initialize the `ob_tid` field to zero if the object is not already
138 // initialized as unreachable.
139 static void
gc_maybe_init_refs(PyObject * op)140 gc_maybe_init_refs(PyObject *op)
141 {
142     if (!gc_is_unreachable(op)) {
143         gc_set_unreachable(op);
144         op->ob_tid = 0;
145     }
146 }
147 
148 static inline Py_ssize_t
gc_get_refs(PyObject * op)149 gc_get_refs(PyObject *op)
150 {
151     return (Py_ssize_t)op->ob_tid;
152 }
153 
154 static inline void
gc_add_refs(PyObject * op,Py_ssize_t refs)155 gc_add_refs(PyObject *op, Py_ssize_t refs)
156 {
157     assert(_PyObject_GC_IS_TRACKED(op));
158     op->ob_tid += refs;
159 }
160 
161 static inline void
gc_decref(PyObject * op)162 gc_decref(PyObject *op)
163 {
164     op->ob_tid -= 1;
165 }
166 
167 static void
disable_deferred_refcounting(PyObject * op)168 disable_deferred_refcounting(PyObject *op)
169 {
170     if (_PyObject_HasDeferredRefcount(op)) {
171         op->ob_gc_bits &= ~_PyGC_BITS_DEFERRED;
172         op->ob_ref_shared -= (1 << _Py_REF_SHARED_SHIFT);
173     }
174 }
175 
176 static Py_ssize_t
merge_refcount(PyObject * op,Py_ssize_t extra)177 merge_refcount(PyObject *op, Py_ssize_t extra)
178 {
179     assert(_PyInterpreterState_GET()->stoptheworld.world_stopped);
180 
181     Py_ssize_t refcount = Py_REFCNT(op);
182     refcount += extra;
183 
184 #ifdef Py_REF_DEBUG
185     _Py_AddRefTotal(_PyThreadState_GET(), extra);
186 #endif
187 
188     // No atomics necessary; all other threads in this interpreter are paused.
189     op->ob_tid = 0;
190     op->ob_ref_local = 0;
191     op->ob_ref_shared = _Py_REF_SHARED(refcount, _Py_REF_MERGED);
192     return refcount;
193 }
194 
195 static void
gc_restore_tid(PyObject * op)196 gc_restore_tid(PyObject *op)
197 {
198     assert(_PyInterpreterState_GET()->stoptheworld.world_stopped);
199     mi_segment_t *segment = _mi_ptr_segment(op);
200     if (_Py_REF_IS_MERGED(op->ob_ref_shared)) {
201         op->ob_tid = 0;
202     }
203     else {
204         // NOTE: may change ob_tid if the object was re-initialized by
205         // a different thread or its segment was abandoned and reclaimed.
206         // The segment thread id might be zero, in which case we should
207         // ensure the refcounts are now merged.
208         op->ob_tid = segment->thread_id;
209         if (op->ob_tid == 0) {
210             merge_refcount(op, 0);
211         }
212     }
213 }
214 
215 static void
gc_restore_refs(PyObject * op)216 gc_restore_refs(PyObject *op)
217 {
218     if (gc_is_unreachable(op)) {
219         gc_restore_tid(op);
220         gc_clear_unreachable(op);
221     }
222 }
223 
224 // Given a mimalloc memory block return the PyObject stored in it or NULL if
225 // the block is not allocated or the object is not tracked or is immortal.
226 static PyObject *
op_from_block(void * block,void * arg,bool include_frozen)227 op_from_block(void *block, void *arg, bool include_frozen)
228 {
229     struct visitor_args *a = arg;
230     if (block == NULL) {
231         return NULL;
232     }
233     PyObject *op = (PyObject *)((char*)block + a->offset);
234     assert(PyObject_IS_GC(op));
235     if (!_PyObject_GC_IS_TRACKED(op)) {
236         return NULL;
237     }
238     if (!include_frozen && gc_is_frozen(op)) {
239         return NULL;
240     }
241     return op;
242 }
243 
244 static int
gc_visit_heaps_lock_held(PyInterpreterState * interp,mi_block_visit_fun * visitor,struct visitor_args * arg)245 gc_visit_heaps_lock_held(PyInterpreterState *interp, mi_block_visit_fun *visitor,
246                          struct visitor_args *arg)
247 {
248     // Offset of PyObject header from start of memory block.
249     Py_ssize_t offset_base = 0;
250     if (_PyMem_DebugEnabled()) {
251         // The debug allocator adds two words at the beginning of each block.
252         offset_base += 2 * sizeof(size_t);
253     }
254 
255     // Objects with Py_TPFLAGS_PREHEADER have two extra fields
256     Py_ssize_t offset_pre = offset_base + 2 * sizeof(PyObject*);
257 
258     // visit each thread's heaps for GC objects
259     for (PyThreadState *p = interp->threads.head; p != NULL; p = p->next) {
260         struct _mimalloc_thread_state *m = &((_PyThreadStateImpl *)p)->mimalloc;
261         if (!_Py_atomic_load_int(&m->initialized)) {
262             // The thread may not have called tstate_mimalloc_bind() yet.
263             continue;
264         }
265 
266         arg->offset = offset_base;
267         if (!mi_heap_visit_blocks(&m->heaps[_Py_MIMALLOC_HEAP_GC], true,
268                                   visitor, arg)) {
269             return -1;
270         }
271         arg->offset = offset_pre;
272         if (!mi_heap_visit_blocks(&m->heaps[_Py_MIMALLOC_HEAP_GC_PRE], true,
273                                   visitor, arg)) {
274             return -1;
275         }
276     }
277 
278     // visit blocks in the per-interpreter abandoned pool (from dead threads)
279     mi_abandoned_pool_t *pool = &interp->mimalloc.abandoned_pool;
280     arg->offset = offset_base;
281     if (!_mi_abandoned_pool_visit_blocks(pool, _Py_MIMALLOC_HEAP_GC, true,
282                                          visitor, arg)) {
283         return -1;
284     }
285     arg->offset = offset_pre;
286     if (!_mi_abandoned_pool_visit_blocks(pool, _Py_MIMALLOC_HEAP_GC_PRE, true,
287                                          visitor, arg)) {
288         return -1;
289     }
290     return 0;
291 }
292 
293 // Visits all GC objects in the interpreter's heaps.
294 // NOTE: It is not safe to allocate or free any mimalloc managed memory while
295 // this function is running.
296 static int
gc_visit_heaps(PyInterpreterState * interp,mi_block_visit_fun * visitor,struct visitor_args * arg)297 gc_visit_heaps(PyInterpreterState *interp, mi_block_visit_fun *visitor,
298                struct visitor_args *arg)
299 {
300     // Other threads in the interpreter must be paused so that we can safely
301     // traverse their heaps.
302     assert(interp->stoptheworld.world_stopped);
303 
304     int err;
305     HEAD_LOCK(&_PyRuntime);
306     err = gc_visit_heaps_lock_held(interp, visitor, arg);
307     HEAD_UNLOCK(&_PyRuntime);
308     return err;
309 }
310 
311 static void
merge_queued_objects(_PyThreadStateImpl * tstate,struct collection_state * state)312 merge_queued_objects(_PyThreadStateImpl *tstate, struct collection_state *state)
313 {
314     struct _brc_thread_state *brc = &tstate->brc;
315     _PyObjectStack_Merge(&brc->local_objects_to_merge, &brc->objects_to_merge);
316 
317     PyObject *op;
318     while ((op = _PyObjectStack_Pop(&brc->local_objects_to_merge)) != NULL) {
319         // Subtract one when merging because the queue had a reference.
320         Py_ssize_t refcount = merge_refcount(op, -1);
321 
322         if (!_PyObject_GC_IS_TRACKED(op) && refcount == 0) {
323             // GC objects with zero refcount are handled subsequently by the
324             // GC as if they were cyclic trash, but we have to handle dead
325             // non-GC objects here. Add one to the refcount so that we can
326             // decref and deallocate the object once we start the world again.
327             op->ob_ref_shared += (1 << _Py_REF_SHARED_SHIFT);
328 #ifdef Py_REF_DEBUG
329             _Py_IncRefTotal(_PyThreadState_GET());
330 #endif
331             worklist_push(&state->objs_to_decref, op);
332         }
333     }
334 }
335 
336 static void
merge_all_queued_objects(PyInterpreterState * interp,struct collection_state * state)337 merge_all_queued_objects(PyInterpreterState *interp, struct collection_state *state)
338 {
339     HEAD_LOCK(&_PyRuntime);
340     for (PyThreadState *p = interp->threads.head; p != NULL; p = p->next) {
341         merge_queued_objects((_PyThreadStateImpl *)p, state);
342     }
343     HEAD_UNLOCK(&_PyRuntime);
344 }
345 
346 static void
process_delayed_frees(PyInterpreterState * interp)347 process_delayed_frees(PyInterpreterState *interp)
348 {
349     // While we are in a "stop the world" pause, we can observe the latest
350     // write sequence by advancing the write sequence immediately.
351     _Py_qsbr_advance(&interp->qsbr);
352     _PyThreadStateImpl *current_tstate = (_PyThreadStateImpl *)_PyThreadState_GET();
353     _Py_qsbr_quiescent_state(current_tstate->qsbr);
354 
355     // Merge the queues from other threads into our own queue so that we can
356     // process all of the pending delayed free requests at once.
357     HEAD_LOCK(&_PyRuntime);
358     for (PyThreadState *p = interp->threads.head; p != NULL; p = p->next) {
359         _PyThreadStateImpl *other = (_PyThreadStateImpl *)p;
360         if (other != current_tstate) {
361             llist_concat(&current_tstate->mem_free_queue, &other->mem_free_queue);
362         }
363     }
364     HEAD_UNLOCK(&_PyRuntime);
365 
366     _PyMem_ProcessDelayed((PyThreadState *)current_tstate);
367 }
368 
369 // Subtract an incoming reference from the computed "gc_refs" refcount.
370 static int
visit_decref(PyObject * op,void * arg)371 visit_decref(PyObject *op, void *arg)
372 {
373     if (_PyObject_GC_IS_TRACKED(op)
374         && !_Py_IsImmortal(op)
375         && !gc_is_frozen(op))
376     {
377         // If update_refs hasn't reached this object yet, mark it
378         // as (tentatively) unreachable and initialize ob_tid to zero.
379         gc_maybe_init_refs(op);
380         gc_decref(op);
381     }
382     return 0;
383 }
384 
385 // Compute the number of external references to objects in the heap
386 // by subtracting internal references from the refcount. The difference is
387 // computed in the ob_tid field (we restore it later).
388 static bool
update_refs(const mi_heap_t * heap,const mi_heap_area_t * area,void * block,size_t block_size,void * args)389 update_refs(const mi_heap_t *heap, const mi_heap_area_t *area,
390             void *block, size_t block_size, void *args)
391 {
392     PyObject *op = op_from_block(block, args, false);
393     if (op == NULL) {
394         return true;
395     }
396 
397     // Exclude immortal objects from garbage collection
398     if (_Py_IsImmortal(op)) {
399         op->ob_tid = 0;
400         _PyObject_GC_UNTRACK(op);
401         gc_clear_unreachable(op);
402         return true;
403     }
404 
405     Py_ssize_t refcount = Py_REFCNT(op);
406     refcount -= _PyObject_HasDeferredRefcount(op);
407     _PyObject_ASSERT(op, refcount >= 0);
408 
409     if (refcount > 0 && !_PyObject_HasDeferredRefcount(op)) {
410         // Untrack tuples and dicts as necessary in this pass, but not objects
411         // with zero refcount, which we will want to collect.
412         if (PyTuple_CheckExact(op)) {
413             _PyTuple_MaybeUntrack(op);
414             if (!_PyObject_GC_IS_TRACKED(op)) {
415                 gc_restore_refs(op);
416                 return true;
417             }
418         }
419         else if (PyDict_CheckExact(op)) {
420             _PyDict_MaybeUntrack(op);
421             if (!_PyObject_GC_IS_TRACKED(op)) {
422                 gc_restore_refs(op);
423                 return true;
424             }
425         }
426     }
427 
428     // We repurpose ob_tid to compute "gc_refs", the number of external
429     // references to the object (i.e., from outside the GC heaps). This means
430     // that ob_tid is no longer a valid thread id until it is restored by
431     // scan_heap_visitor(). Until then, we cannot use the standard reference
432     // counting functions or allow other threads to run Python code.
433     gc_maybe_init_refs(op);
434 
435     // Add the actual refcount to ob_tid.
436     gc_add_refs(op, refcount);
437 
438     // Subtract internal references from ob_tid. Objects with ob_tid > 0
439     // are directly reachable from outside containers, and so can't be
440     // collected.
441     Py_TYPE(op)->tp_traverse(op, visit_decref, NULL);
442     return true;
443 }
444 
445 static int
visit_clear_unreachable(PyObject * op,_PyObjectStack * stack)446 visit_clear_unreachable(PyObject *op, _PyObjectStack *stack)
447 {
448     if (gc_is_unreachable(op)) {
449         _PyObject_ASSERT(op, _PyObject_GC_IS_TRACKED(op));
450         gc_clear_unreachable(op);
451         return _PyObjectStack_Push(stack, op);
452     }
453     return 0;
454 }
455 
456 // Transitively clear the unreachable bit on all objects reachable from op.
457 static int
mark_reachable(PyObject * op)458 mark_reachable(PyObject *op)
459 {
460     _PyObjectStack stack = { NULL };
461     do {
462         traverseproc traverse = Py_TYPE(op)->tp_traverse;
463         if (traverse(op, (visitproc)&visit_clear_unreachable, &stack) < 0) {
464             _PyObjectStack_Clear(&stack);
465             return -1;
466         }
467         op = _PyObjectStack_Pop(&stack);
468     } while (op != NULL);
469     return 0;
470 }
471 
472 #ifdef GC_DEBUG
473 static bool
validate_refcounts(const mi_heap_t * heap,const mi_heap_area_t * area,void * block,size_t block_size,void * args)474 validate_refcounts(const mi_heap_t *heap, const mi_heap_area_t *area,
475                    void *block, size_t block_size, void *args)
476 {
477     PyObject *op = op_from_block(block, args, false);
478     if (op == NULL) {
479         return true;
480     }
481 
482     _PyObject_ASSERT_WITH_MSG(op, !gc_is_unreachable(op),
483                               "object should not be marked as unreachable yet");
484 
485     if (_Py_REF_IS_MERGED(op->ob_ref_shared)) {
486         _PyObject_ASSERT_WITH_MSG(op, op->ob_tid == 0,
487                                   "merged objects should have ob_tid == 0");
488     }
489     else if (!_Py_IsImmortal(op)) {
490         _PyObject_ASSERT_WITH_MSG(op, op->ob_tid != 0,
491                                   "unmerged objects should have ob_tid != 0");
492     }
493 
494     return true;
495 }
496 
497 static bool
validate_gc_objects(const mi_heap_t * heap,const mi_heap_area_t * area,void * block,size_t block_size,void * args)498 validate_gc_objects(const mi_heap_t *heap, const mi_heap_area_t *area,
499                     void *block, size_t block_size, void *args)
500 {
501     PyObject *op = op_from_block(block, args, false);
502     if (op == NULL) {
503         return true;
504     }
505 
506     _PyObject_ASSERT(op, gc_is_unreachable(op));
507     _PyObject_ASSERT_WITH_MSG(op, gc_get_refs(op) >= 0,
508                                   "refcount is too small");
509     return true;
510 }
511 #endif
512 
513 static bool
mark_heap_visitor(const mi_heap_t * heap,const mi_heap_area_t * area,void * block,size_t block_size,void * args)514 mark_heap_visitor(const mi_heap_t *heap, const mi_heap_area_t *area,
515                   void *block, size_t block_size, void *args)
516 {
517     PyObject *op = op_from_block(block, args, false);
518     if (op == NULL) {
519         return true;
520     }
521 
522     _PyObject_ASSERT_WITH_MSG(op, gc_get_refs(op) >= 0,
523                                   "refcount is too small");
524 
525     if (gc_is_unreachable(op) && gc_get_refs(op) != 0) {
526         // Object is reachable but currently marked as unreachable.
527         // Mark it as reachable and traverse its pointers to find
528         // any other object that may be directly reachable from it.
529         gc_clear_unreachable(op);
530 
531         // Transitively mark reachable objects by clearing the unreachable flag.
532         if (mark_reachable(op) < 0) {
533             return false;
534         }
535     }
536 
537     return true;
538 }
539 
540 static bool
restore_refs(const mi_heap_t * heap,const mi_heap_area_t * area,void * block,size_t block_size,void * args)541 restore_refs(const mi_heap_t *heap, const mi_heap_area_t *area,
542              void *block, size_t block_size, void *args)
543 {
544     PyObject *op = op_from_block(block, args, false);
545     if (op == NULL) {
546         return true;
547     }
548     gc_restore_tid(op);
549     gc_clear_unreachable(op);
550     return true;
551 }
552 
553 /* Return true if object has a pre-PEP 442 finalization method. */
554 static int
has_legacy_finalizer(PyObject * op)555 has_legacy_finalizer(PyObject *op)
556 {
557     return Py_TYPE(op)->tp_del != NULL;
558 }
559 
560 static bool
scan_heap_visitor(const mi_heap_t * heap,const mi_heap_area_t * area,void * block,size_t block_size,void * args)561 scan_heap_visitor(const mi_heap_t *heap, const mi_heap_area_t *area,
562                   void *block, size_t block_size, void *args)
563 {
564     PyObject *op = op_from_block(block, args, false);
565     if (op == NULL) {
566         return true;
567     }
568 
569     struct collection_state *state = (struct collection_state *)args;
570     if (gc_is_unreachable(op)) {
571         // Disable deferred refcounting for unreachable objects so that they
572         // are collected immediately after finalization.
573         disable_deferred_refcounting(op);
574 
575         // Merge and add one to the refcount to prevent deallocation while we
576         // are holding on to it in a worklist.
577         merge_refcount(op, 1);
578 
579         if (has_legacy_finalizer(op)) {
580             // would be unreachable, but has legacy finalizer
581             gc_clear_unreachable(op);
582             worklist_push(&state->legacy_finalizers, op);
583         }
584         else {
585             worklist_push(&state->unreachable, op);
586         }
587     }
588     else {
589         // object is reachable, restore `ob_tid`; we're done with these objects
590         gc_restore_tid(op);
591         state->long_lived_total++;
592     }
593 
594     return true;
595 }
596 
597 static int
598 move_legacy_finalizer_reachable(struct collection_state *state);
599 
600 static int
deduce_unreachable_heap(PyInterpreterState * interp,struct collection_state * state)601 deduce_unreachable_heap(PyInterpreterState *interp,
602                         struct collection_state *state)
603 {
604 
605 #ifdef GC_DEBUG
606     // Check that all objects are marked as unreachable and that the computed
607     // reference count difference (stored in `ob_tid`) is non-negative.
608     gc_visit_heaps(interp, &validate_refcounts, &state->base);
609 #endif
610 
611     // Identify objects that are directly reachable from outside the GC heap
612     // by computing the difference between the refcount and the number of
613     // incoming references.
614     gc_visit_heaps(interp, &update_refs, &state->base);
615 
616 #ifdef GC_DEBUG
617     // Check that all objects are marked as unreachable and that the computed
618     // reference count difference (stored in `ob_tid`) is non-negative.
619     gc_visit_heaps(interp, &validate_gc_objects, &state->base);
620 #endif
621 
622     // Transitively mark reachable objects by clearing the
623     // _PyGC_BITS_UNREACHABLE flag.
624     if (gc_visit_heaps(interp, &mark_heap_visitor, &state->base) < 0) {
625         // On out-of-memory, restore the refcounts and bail out.
626         gc_visit_heaps(interp, &restore_refs, &state->base);
627         return -1;
628     }
629 
630     // Identify remaining unreachable objects and push them onto a stack.
631     // Restores ob_tid for reachable objects.
632     gc_visit_heaps(interp, &scan_heap_visitor, &state->base);
633 
634     if (state->legacy_finalizers.head) {
635         // There may be objects reachable from legacy finalizers that are in
636         // the unreachable set. We need to mark them as reachable.
637         if (move_legacy_finalizer_reachable(state) < 0) {
638             return -1;
639         }
640     }
641 
642     return 0;
643 }
644 
645 static int
move_legacy_finalizer_reachable(struct collection_state * state)646 move_legacy_finalizer_reachable(struct collection_state *state)
647 {
648     // Clear the reachable bit on all objects transitively reachable
649     // from the objects with legacy finalizers.
650     PyObject *op;
651     WORKSTACK_FOR_EACH(&state->legacy_finalizers, op) {
652         if (mark_reachable(op) < 0) {
653             return -1;
654         }
655     }
656 
657     // Move the reachable objects from the unreachable worklist to the legacy
658     // finalizer worklist.
659     struct worklist_iter iter;
660     WORKSTACK_FOR_EACH_ITER(&state->unreachable, &iter, op) {
661         if (!gc_is_unreachable(op)) {
662             worklist_remove(&iter);
663             worklist_push(&state->legacy_finalizers, op);
664         }
665     }
666 
667     return 0;
668 }
669 
670 // Clear all weakrefs to unreachable objects. Weakrefs with callbacks are
671 // enqueued in `wrcb_to_call`, but not invoked yet.
672 static void
clear_weakrefs(struct collection_state * state)673 clear_weakrefs(struct collection_state *state)
674 {
675     PyObject *op;
676     WORKSTACK_FOR_EACH(&state->unreachable, op) {
677         if (PyWeakref_Check(op)) {
678             // Clear weakrefs that are themselves unreachable to ensure their
679             // callbacks will not be executed later from a `tp_clear()`
680             // inside delete_garbage(). That would be unsafe: it could
681             // resurrect a dead object or access a an already cleared object.
682             // See bpo-38006 for one example.
683             _PyWeakref_ClearRef((PyWeakReference *)op);
684         }
685 
686         if (!_PyType_SUPPORTS_WEAKREFS(Py_TYPE(op))) {
687             continue;
688         }
689 
690         // NOTE: This is never triggered for static types so we can avoid the
691         // (slightly) more costly _PyObject_GET_WEAKREFS_LISTPTR().
692         PyWeakReference **wrlist = _PyObject_GET_WEAKREFS_LISTPTR_FROM_OFFSET(op);
693 
694         // `op` may have some weakrefs.  March over the list, clear
695         // all the weakrefs, and enqueue the weakrefs with callbacks
696         // that must be called into wrcb_to_call.
697         for (PyWeakReference *wr = *wrlist; wr != NULL; wr = *wrlist) {
698             // _PyWeakref_ClearRef clears the weakref but leaves
699             // the callback pointer intact.  Obscure: it also
700             // changes *wrlist.
701             _PyObject_ASSERT((PyObject *)wr, wr->wr_object == op);
702             _PyWeakref_ClearRef(wr);
703             _PyObject_ASSERT((PyObject *)wr, wr->wr_object == Py_None);
704 
705             // We do not invoke callbacks for weakrefs that are themselves
706             // unreachable. This is partly for historical reasons: weakrefs
707             // predate safe object finalization, and a weakref that is itself
708             // unreachable may have a callback that resurrects other
709             // unreachable objects.
710             if (wr->wr_callback == NULL || gc_is_unreachable((PyObject *)wr)) {
711                 continue;
712             }
713 
714             // Create a new reference so that wr can't go away before we can
715             // process it again.
716             merge_refcount((PyObject *)wr, 1);
717 
718             // Enqueue weakref to be called later.
719             worklist_push(&state->wrcb_to_call, (PyObject *)wr);
720         }
721     }
722 }
723 
724 static void
call_weakref_callbacks(struct collection_state * state)725 call_weakref_callbacks(struct collection_state *state)
726 {
727     // Invoke the callbacks we decided to honor.
728     PyObject *op;
729     while ((op = worklist_pop(&state->wrcb_to_call)) != NULL) {
730         _PyObject_ASSERT(op, PyWeakref_Check(op));
731 
732         PyWeakReference *wr = (PyWeakReference *)op;
733         PyObject *callback = wr->wr_callback;
734         _PyObject_ASSERT(op, callback != NULL);
735 
736         /* copy-paste of weakrefobject.c's handle_callback() */
737         PyObject *temp = PyObject_CallOneArg(callback, (PyObject *)wr);
738         if (temp == NULL) {
739             PyErr_WriteUnraisable(callback);
740         }
741         else {
742             Py_DECREF(temp);
743         }
744 
745         Py_DECREF(op);  // drop worklist reference
746     }
747 }
748 
749 
750 static GCState *
get_gc_state(void)751 get_gc_state(void)
752 {
753     PyInterpreterState *interp = _PyInterpreterState_GET();
754     return &interp->gc;
755 }
756 
757 
758 void
_PyGC_InitState(GCState * gcstate)759 _PyGC_InitState(GCState *gcstate)
760 {
761     // TODO: move to pycore_runtime_init.h once the incremental GC lands.
762     gcstate->generations[0].threshold = 2000;
763 }
764 
765 
766 PyStatus
_PyGC_Init(PyInterpreterState * interp)767 _PyGC_Init(PyInterpreterState *interp)
768 {
769     GCState *gcstate = &interp->gc;
770 
771     // gh-117783: immortalize objects that would use deferred refcounting
772     // once the first non-main thread is created (but not in subinterpreters).
773     gcstate->immortalize = _Py_IsMainInterpreter(interp) ? 0 : -1;
774 
775     gcstate->garbage = PyList_New(0);
776     if (gcstate->garbage == NULL) {
777         return _PyStatus_NO_MEMORY();
778     }
779 
780     gcstate->callbacks = PyList_New(0);
781     if (gcstate->callbacks == NULL) {
782         return _PyStatus_NO_MEMORY();
783     }
784 
785     return _PyStatus_OK();
786 }
787 
788 static void
debug_cycle(const char * msg,PyObject * op)789 debug_cycle(const char *msg, PyObject *op)
790 {
791     PySys_FormatStderr("gc: %s <%s %p>\n",
792                        msg, Py_TYPE(op)->tp_name, op);
793 }
794 
795 /* Run first-time finalizers (if any) on all the objects in collectable.
796  * Note that this may remove some (or even all) of the objects from the
797  * list, due to refcounts falling to 0.
798  */
799 static void
finalize_garbage(struct collection_state * state)800 finalize_garbage(struct collection_state *state)
801 {
802     // NOTE: the unreachable worklist holds a strong reference to the object
803     // to prevent it from being deallocated while we are holding on to it.
804     PyObject *op;
805     WORKSTACK_FOR_EACH(&state->unreachable, op) {
806         if (!_PyGC_FINALIZED(op)) {
807             destructor finalize = Py_TYPE(op)->tp_finalize;
808             if (finalize != NULL) {
809                 _PyGC_SET_FINALIZED(op);
810                 finalize(op);
811                 assert(!_PyErr_Occurred(_PyThreadState_GET()));
812             }
813         }
814     }
815 }
816 
817 // Break reference cycles by clearing the containers involved.
818 static void
delete_garbage(struct collection_state * state)819 delete_garbage(struct collection_state *state)
820 {
821     PyThreadState *tstate = _PyThreadState_GET();
822     GCState *gcstate = state->gcstate;
823 
824     assert(!_PyErr_Occurred(tstate));
825 
826     PyObject *op;
827     while ((op = worklist_pop(&state->objs_to_decref)) != NULL) {
828         Py_DECREF(op);
829     }
830 
831     while ((op = worklist_pop(&state->unreachable)) != NULL) {
832         _PyObject_ASSERT(op, gc_is_unreachable(op));
833 
834         // Clear the unreachable flag.
835         gc_clear_unreachable(op);
836 
837         if (!_PyObject_GC_IS_TRACKED(op)) {
838             // Object might have been untracked by some other tp_clear() call.
839             Py_DECREF(op);  // drop the reference from the worklist
840             continue;
841         }
842 
843         state->collected++;
844 
845         if (gcstate->debug & _PyGC_DEBUG_SAVEALL) {
846             assert(gcstate->garbage != NULL);
847             if (PyList_Append(gcstate->garbage, op) < 0) {
848                 _PyErr_Clear(tstate);
849             }
850         }
851         else {
852             inquiry clear = Py_TYPE(op)->tp_clear;
853             if (clear != NULL) {
854                 (void) clear(op);
855                 if (_PyErr_Occurred(tstate)) {
856                     PyErr_FormatUnraisable("Exception ignored in tp_clear of %s",
857                                            Py_TYPE(op)->tp_name);
858                 }
859             }
860         }
861 
862         Py_DECREF(op);  // drop the reference from the worklist
863     }
864 }
865 
866 static void
handle_legacy_finalizers(struct collection_state * state)867 handle_legacy_finalizers(struct collection_state *state)
868 {
869     GCState *gcstate = state->gcstate;
870     assert(gcstate->garbage != NULL);
871 
872     PyObject *op;
873     while ((op = worklist_pop(&state->legacy_finalizers)) != NULL) {
874         state->uncollectable++;
875 
876         if (gcstate->debug & _PyGC_DEBUG_UNCOLLECTABLE) {
877             debug_cycle("uncollectable", op);
878         }
879 
880         if ((gcstate->debug & _PyGC_DEBUG_SAVEALL) || has_legacy_finalizer(op)) {
881             if (PyList_Append(gcstate->garbage, op) < 0) {
882                 PyErr_Clear();
883             }
884         }
885         Py_DECREF(op);  // drop worklist reference
886     }
887 }
888 
889 // Show stats for objects in each generations
890 static void
show_stats_each_generations(GCState * gcstate)891 show_stats_each_generations(GCState *gcstate)
892 {
893     // TODO
894 }
895 
896 // Traversal callback for handle_resurrected_objects.
897 static int
visit_decref_unreachable(PyObject * op,void * data)898 visit_decref_unreachable(PyObject *op, void *data)
899 {
900     if (gc_is_unreachable(op) && _PyObject_GC_IS_TRACKED(op)) {
901         op->ob_ref_local -= 1;
902     }
903     return 0;
904 }
905 
906 // Handle objects that may have resurrected after a call to 'finalize_garbage'.
907 static int
handle_resurrected_objects(struct collection_state * state)908 handle_resurrected_objects(struct collection_state *state)
909 {
910     // First, find externally reachable objects by computing the reference
911     // count difference in ob_ref_local. We can't use ob_tid here because
912     // that's already used to store the unreachable worklist.
913     PyObject *op;
914     struct worklist_iter iter;
915     WORKSTACK_FOR_EACH_ITER(&state->unreachable, &iter, op) {
916         assert(gc_is_unreachable(op));
917         assert(_Py_REF_IS_MERGED(op->ob_ref_shared));
918 
919         if (!_PyObject_GC_IS_TRACKED(op)) {
920             // Object was untracked by a finalizer. Schedule it for a Py_DECREF
921             // after we finish with the stop-the-world pause.
922             gc_clear_unreachable(op);
923             worklist_remove(&iter);
924             worklist_push(&state->objs_to_decref, op);
925             continue;
926         }
927 
928         Py_ssize_t refcount = (op->ob_ref_shared >> _Py_REF_SHARED_SHIFT);
929         if (refcount > INT32_MAX) {
930             // The refcount is too big to fit in `ob_ref_local`. Mark the
931             // object as immortal and bail out.
932             gc_clear_unreachable(op);
933             worklist_remove(&iter);
934             _Py_SetImmortal(op);
935             continue;
936         }
937 
938         op->ob_ref_local += (uint32_t)refcount;
939 
940         // Subtract one to account for the reference from the worklist.
941         op->ob_ref_local -= 1;
942 
943         traverseproc traverse = Py_TYPE(op)->tp_traverse;
944         (void) traverse(op,
945             (visitproc)visit_decref_unreachable,
946             NULL);
947     }
948 
949     // Find resurrected objects
950     bool any_resurrected = false;
951     WORKSTACK_FOR_EACH(&state->unreachable, op) {
952         int32_t gc_refs = (int32_t)op->ob_ref_local;
953         op->ob_ref_local = 0;  // restore ob_ref_local
954 
955         _PyObject_ASSERT(op, gc_refs >= 0);
956 
957         if (gc_is_unreachable(op) && gc_refs > 0) {
958             // Clear the unreachable flag on any transitively reachable objects
959             // from this one.
960             any_resurrected = true;
961             gc_clear_unreachable(op);
962             if (mark_reachable(op) < 0) {
963                 return -1;
964             }
965         }
966     }
967 
968     if (any_resurrected) {
969         // Remove resurrected objects from the unreachable list.
970         WORKSTACK_FOR_EACH_ITER(&state->unreachable, &iter, op) {
971             if (!gc_is_unreachable(op)) {
972                 _PyObject_ASSERT(op, Py_REFCNT(op) > 1);
973                 worklist_remove(&iter);
974                 merge_refcount(op, -1);  // remove worklist reference
975             }
976         }
977     }
978 
979 #ifdef GC_DEBUG
980     WORKSTACK_FOR_EACH(&state->unreachable, op) {
981         _PyObject_ASSERT(op, gc_is_unreachable(op));
982         _PyObject_ASSERT(op, _PyObject_GC_IS_TRACKED(op));
983         _PyObject_ASSERT(op, op->ob_ref_local == 0);
984         _PyObject_ASSERT(op, _Py_REF_IS_MERGED(op->ob_ref_shared));
985     }
986 #endif
987 
988     return 0;
989 }
990 
991 
992 /* Invoke progress callbacks to notify clients that garbage collection
993  * is starting or stopping
994  */
995 static void
invoke_gc_callback(PyThreadState * tstate,const char * phase,int generation,Py_ssize_t collected,Py_ssize_t uncollectable)996 invoke_gc_callback(PyThreadState *tstate, const char *phase,
997                    int generation, Py_ssize_t collected,
998                    Py_ssize_t uncollectable)
999 {
1000     assert(!_PyErr_Occurred(tstate));
1001 
1002     /* we may get called very early */
1003     GCState *gcstate = &tstate->interp->gc;
1004     if (gcstate->callbacks == NULL) {
1005         return;
1006     }
1007 
1008     /* The local variable cannot be rebound, check it for sanity */
1009     assert(PyList_CheckExact(gcstate->callbacks));
1010     PyObject *info = NULL;
1011     if (PyList_GET_SIZE(gcstate->callbacks) != 0) {
1012         info = Py_BuildValue("{sisnsn}",
1013             "generation", generation,
1014             "collected", collected,
1015             "uncollectable", uncollectable);
1016         if (info == NULL) {
1017             PyErr_FormatUnraisable("Exception ignored on invoking gc callbacks");
1018             return;
1019         }
1020     }
1021 
1022     PyObject *phase_obj = PyUnicode_FromString(phase);
1023     if (phase_obj == NULL) {
1024         Py_XDECREF(info);
1025         PyErr_FormatUnraisable("Exception ignored on invoking gc callbacks");
1026         return;
1027     }
1028 
1029     PyObject *stack[] = {phase_obj, info};
1030     for (Py_ssize_t i=0; i<PyList_GET_SIZE(gcstate->callbacks); i++) {
1031         PyObject *r, *cb = PyList_GET_ITEM(gcstate->callbacks, i);
1032         Py_INCREF(cb); /* make sure cb doesn't go away */
1033         r = PyObject_Vectorcall(cb, stack, 2, NULL);
1034         if (r == NULL) {
1035             PyErr_WriteUnraisable(cb);
1036         }
1037         else {
1038             Py_DECREF(r);
1039         }
1040         Py_DECREF(cb);
1041     }
1042     Py_DECREF(phase_obj);
1043     Py_XDECREF(info);
1044     assert(!_PyErr_Occurred(tstate));
1045 }
1046 
1047 static void
cleanup_worklist(struct worklist * worklist)1048 cleanup_worklist(struct worklist *worklist)
1049 {
1050     PyObject *op;
1051     while ((op = worklist_pop(worklist)) != NULL) {
1052         gc_clear_unreachable(op);
1053         Py_DECREF(op);
1054     }
1055 }
1056 
1057 static bool
gc_should_collect(GCState * gcstate)1058 gc_should_collect(GCState *gcstate)
1059 {
1060     int count = _Py_atomic_load_int_relaxed(&gcstate->generations[0].count);
1061     int threshold = gcstate->generations[0].threshold;
1062     if (count <= threshold || threshold == 0 || !gcstate->enabled) {
1063         return false;
1064     }
1065     // Avoid quadratic behavior by scaling threshold to the number of live
1066     // objects. A few tests rely on immediate scheduling of the GC so we ignore
1067     // the scaled threshold if generations[1].threshold is set to zero.
1068     return (count > gcstate->long_lived_total / 4 ||
1069             gcstate->generations[1].threshold == 0);
1070 }
1071 
1072 static void
record_allocation(PyThreadState * tstate)1073 record_allocation(PyThreadState *tstate)
1074 {
1075     struct _gc_thread_state *gc = &((_PyThreadStateImpl *)tstate)->gc;
1076 
1077     // We buffer the allocation count to avoid the overhead of atomic
1078     // operations for every allocation.
1079     gc->alloc_count++;
1080     if (gc->alloc_count >= LOCAL_ALLOC_COUNT_THRESHOLD) {
1081         // TODO: Use Py_ssize_t for the generation count.
1082         GCState *gcstate = &tstate->interp->gc;
1083         _Py_atomic_add_int(&gcstate->generations[0].count, (int)gc->alloc_count);
1084         gc->alloc_count = 0;
1085 
1086         if (gc_should_collect(gcstate) &&
1087             !_Py_atomic_load_int_relaxed(&gcstate->collecting))
1088         {
1089             _Py_ScheduleGC(tstate);
1090         }
1091     }
1092 }
1093 
1094 static void
record_deallocation(PyThreadState * tstate)1095 record_deallocation(PyThreadState *tstate)
1096 {
1097     struct _gc_thread_state *gc = &((_PyThreadStateImpl *)tstate)->gc;
1098 
1099     gc->alloc_count--;
1100     if (gc->alloc_count <= -LOCAL_ALLOC_COUNT_THRESHOLD) {
1101         GCState *gcstate = &tstate->interp->gc;
1102         _Py_atomic_add_int(&gcstate->generations[0].count, (int)gc->alloc_count);
1103         gc->alloc_count = 0;
1104     }
1105 }
1106 
1107 static void
gc_collect_internal(PyInterpreterState * interp,struct collection_state * state,int generation)1108 gc_collect_internal(PyInterpreterState *interp, struct collection_state *state, int generation)
1109 {
1110     _PyEval_StopTheWorld(interp);
1111 
1112     // update collection and allocation counters
1113     if (generation+1 < NUM_GENERATIONS) {
1114         state->gcstate->generations[generation+1].count += 1;
1115     }
1116     for (int i = 0; i <= generation; i++) {
1117         state->gcstate->generations[i].count = 0;
1118     }
1119 
1120     // merge refcounts for all queued objects
1121     merge_all_queued_objects(interp, state);
1122     process_delayed_frees(interp);
1123 
1124     // Find unreachable objects
1125     int err = deduce_unreachable_heap(interp, state);
1126     if (err < 0) {
1127         _PyEval_StartTheWorld(interp);
1128         PyErr_NoMemory();
1129         return;
1130     }
1131 
1132     // Print debugging information.
1133     if (interp->gc.debug & _PyGC_DEBUG_COLLECTABLE) {
1134         PyObject *op;
1135         WORKSTACK_FOR_EACH(&state->unreachable, op) {
1136             debug_cycle("collectable", op);
1137         }
1138     }
1139 
1140     // Record the number of live GC objects
1141     interp->gc.long_lived_total = state->long_lived_total;
1142 
1143     // Clear weakrefs and enqueue callbacks (but do not call them).
1144     clear_weakrefs(state);
1145     _PyEval_StartTheWorld(interp);
1146 
1147     // Deallocate any object from the refcount merge step
1148     cleanup_worklist(&state->objs_to_decref);
1149 
1150     // Call weakref callbacks and finalizers after unpausing other threads to
1151     // avoid potential deadlocks.
1152     call_weakref_callbacks(state);
1153     finalize_garbage(state);
1154 
1155     // Handle any objects that may have resurrected after the finalization.
1156     _PyEval_StopTheWorld(interp);
1157     err = handle_resurrected_objects(state);
1158     // Clear free lists in all threads
1159     _PyGC_ClearAllFreeLists(interp);
1160     _PyEval_StartTheWorld(interp);
1161 
1162     if (err < 0) {
1163         cleanup_worklist(&state->unreachable);
1164         cleanup_worklist(&state->legacy_finalizers);
1165         cleanup_worklist(&state->wrcb_to_call);
1166         cleanup_worklist(&state->objs_to_decref);
1167         PyErr_NoMemory();
1168         return;
1169     }
1170 
1171     // Call tp_clear on objects in the unreachable set. This will cause
1172     // the reference cycles to be broken. It may also cause some objects
1173     // to be freed.
1174     delete_garbage(state);
1175 
1176     // Append objects with legacy finalizers to the "gc.garbage" list.
1177     handle_legacy_finalizers(state);
1178 }
1179 
1180 /* This is the main function.  Read this to understand how the
1181  * collection process works. */
1182 static Py_ssize_t
gc_collect_main(PyThreadState * tstate,int generation,_PyGC_Reason reason)1183 gc_collect_main(PyThreadState *tstate, int generation, _PyGC_Reason reason)
1184 {
1185     Py_ssize_t m = 0; /* # objects collected */
1186     Py_ssize_t n = 0; /* # unreachable objects that couldn't be collected */
1187     PyTime_t t1 = 0;   /* initialize to prevent a compiler warning */
1188     GCState *gcstate = &tstate->interp->gc;
1189 
1190     // gc_collect_main() must not be called before _PyGC_Init
1191     // or after _PyGC_Fini()
1192     assert(gcstate->garbage != NULL);
1193     assert(!_PyErr_Occurred(tstate));
1194 
1195     int expected = 0;
1196     if (!_Py_atomic_compare_exchange_int(&gcstate->collecting, &expected, 1)) {
1197         // Don't start a garbage collection if one is already in progress.
1198         return 0;
1199     }
1200 
1201     if (reason == _Py_GC_REASON_HEAP && !gc_should_collect(gcstate)) {
1202         // Don't collect if the threshold is not exceeded.
1203         _Py_atomic_store_int(&gcstate->collecting, 0);
1204         return 0;
1205     }
1206 
1207     assert(generation >= 0 && generation < NUM_GENERATIONS);
1208 
1209 #ifdef Py_STATS
1210     if (_Py_stats) {
1211         _Py_stats->object_stats.object_visits = 0;
1212     }
1213 #endif
1214     GC_STAT_ADD(generation, collections, 1);
1215 
1216     if (reason != _Py_GC_REASON_SHUTDOWN) {
1217         invoke_gc_callback(tstate, "start", generation, 0, 0);
1218     }
1219 
1220     if (gcstate->debug & _PyGC_DEBUG_STATS) {
1221         PySys_WriteStderr("gc: collecting generation %d...\n", generation);
1222         show_stats_each_generations(gcstate);
1223         // ignore error: don't interrupt the GC if reading the clock fails
1224         (void)PyTime_PerfCounterRaw(&t1);
1225     }
1226 
1227     if (PyDTrace_GC_START_ENABLED()) {
1228         PyDTrace_GC_START(generation);
1229     }
1230 
1231     PyInterpreterState *interp = tstate->interp;
1232 
1233     struct collection_state state = {
1234         .interp = interp,
1235         .gcstate = gcstate,
1236     };
1237 
1238     gc_collect_internal(interp, &state, generation);
1239 
1240     m = state.collected;
1241     n = state.uncollectable;
1242 
1243     if (gcstate->debug & _PyGC_DEBUG_STATS) {
1244         PyTime_t t2;
1245         (void)PyTime_PerfCounterRaw(&t2);
1246         double d = PyTime_AsSecondsDouble(t2 - t1);
1247         PySys_WriteStderr(
1248             "gc: done, %zd unreachable, %zd uncollectable, %.4fs elapsed\n",
1249             n+m, n, d);
1250     }
1251 
1252     // Clear the current thread's free-list again.
1253     _PyThreadStateImpl *tstate_impl = (_PyThreadStateImpl *)tstate;
1254     _PyObject_ClearFreeLists(&tstate_impl->freelists, 0);
1255 
1256     if (_PyErr_Occurred(tstate)) {
1257         if (reason == _Py_GC_REASON_SHUTDOWN) {
1258             _PyErr_Clear(tstate);
1259         }
1260         else {
1261             PyErr_FormatUnraisable("Exception ignored in garbage collection");
1262         }
1263     }
1264 
1265     /* Update stats */
1266     struct gc_generation_stats *stats = &gcstate->generation_stats[generation];
1267     stats->collections++;
1268     stats->collected += m;
1269     stats->uncollectable += n;
1270 
1271     GC_STAT_ADD(generation, objects_collected, m);
1272 #ifdef Py_STATS
1273     if (_Py_stats) {
1274         GC_STAT_ADD(generation, object_visits,
1275             _Py_stats->object_stats.object_visits);
1276         _Py_stats->object_stats.object_visits = 0;
1277     }
1278 #endif
1279 
1280     if (PyDTrace_GC_DONE_ENABLED()) {
1281         PyDTrace_GC_DONE(n + m);
1282     }
1283 
1284     if (reason != _Py_GC_REASON_SHUTDOWN) {
1285         invoke_gc_callback(tstate, "stop", generation, m, n);
1286     }
1287 
1288     assert(!_PyErr_Occurred(tstate));
1289     _Py_atomic_store_int(&gcstate->collecting, 0);
1290     return n + m;
1291 }
1292 
1293 static PyObject *
list_from_object_stack(_PyObjectStack * stack)1294 list_from_object_stack(_PyObjectStack *stack)
1295 {
1296     PyObject *list = PyList_New(_PyObjectStack_Size(stack));
1297     if (list == NULL) {
1298         PyObject *op;
1299         while ((op = _PyObjectStack_Pop(stack)) != NULL) {
1300             Py_DECREF(op);
1301         }
1302         return NULL;
1303     }
1304 
1305     PyObject *op;
1306     Py_ssize_t idx = 0;
1307     while ((op = _PyObjectStack_Pop(stack)) != NULL) {
1308         assert(idx < PyList_GET_SIZE(list));
1309         PyList_SET_ITEM(list, idx++, op);
1310     }
1311     assert(idx == PyList_GET_SIZE(list));
1312     return list;
1313 }
1314 
1315 struct get_referrers_args {
1316     struct visitor_args base;
1317     PyObject *objs;
1318     _PyObjectStack results;
1319 };
1320 
1321 static int
referrersvisit(PyObject * obj,void * arg)1322 referrersvisit(PyObject* obj, void *arg)
1323 {
1324     PyObject *objs = arg;
1325     Py_ssize_t i;
1326     for (i = 0; i < PyTuple_GET_SIZE(objs); i++) {
1327         if (PyTuple_GET_ITEM(objs, i) == obj) {
1328             return 1;
1329         }
1330     }
1331     return 0;
1332 }
1333 
1334 static bool
visit_get_referrers(const mi_heap_t * heap,const mi_heap_area_t * area,void * block,size_t block_size,void * args)1335 visit_get_referrers(const mi_heap_t *heap, const mi_heap_area_t *area,
1336                     void *block, size_t block_size, void *args)
1337 {
1338     PyObject *op = op_from_block(block, args, true);
1339     if (op == NULL) {
1340         return true;
1341     }
1342     if (op->ob_gc_bits & (_PyGC_BITS_UNREACHABLE | _PyGC_BITS_FROZEN)) {
1343         // Exclude unreachable objects (in-progress GC) and frozen
1344         // objects from gc.get_objects() to match the default build.
1345         return true;
1346     }
1347 
1348     struct get_referrers_args *arg = (struct get_referrers_args *)args;
1349     if (op == arg->objs) {
1350         // Don't include the tuple itself in the referrers list.
1351         return true;
1352     }
1353     if (Py_TYPE(op)->tp_traverse(op, referrersvisit, arg->objs)) {
1354         if (_PyObjectStack_Push(&arg->results, Py_NewRef(op)) < 0) {
1355             return false;
1356         }
1357     }
1358 
1359     return true;
1360 }
1361 
1362 PyObject *
_PyGC_GetReferrers(PyInterpreterState * interp,PyObject * objs)1363 _PyGC_GetReferrers(PyInterpreterState *interp, PyObject *objs)
1364 {
1365     // NOTE: We can't append to the PyListObject during gc_visit_heaps()
1366     // because PyList_Append() may reclaim an abandoned mimalloc segments
1367     // while we are traversing them.
1368     struct get_referrers_args args = { .objs = objs };
1369     _PyEval_StopTheWorld(interp);
1370     int err = gc_visit_heaps(interp, &visit_get_referrers, &args.base);
1371     _PyEval_StartTheWorld(interp);
1372 
1373     PyObject *list = list_from_object_stack(&args.results);
1374     if (err < 0) {
1375         PyErr_NoMemory();
1376         Py_CLEAR(list);
1377     }
1378     return list;
1379 }
1380 
1381 struct get_objects_args {
1382     struct visitor_args base;
1383     _PyObjectStack objects;
1384 };
1385 
1386 static bool
visit_get_objects(const mi_heap_t * heap,const mi_heap_area_t * area,void * block,size_t block_size,void * args)1387 visit_get_objects(const mi_heap_t *heap, const mi_heap_area_t *area,
1388                   void *block, size_t block_size, void *args)
1389 {
1390     PyObject *op = op_from_block(block, args, true);
1391     if (op == NULL) {
1392         return true;
1393     }
1394     if (op->ob_gc_bits & (_PyGC_BITS_UNREACHABLE | _PyGC_BITS_FROZEN)) {
1395         // Exclude unreachable objects (in-progress GC) and frozen
1396         // objects from gc.get_objects() to match the default build.
1397         return true;
1398     }
1399 
1400     struct get_objects_args *arg = (struct get_objects_args *)args;
1401     if (_PyObjectStack_Push(&arg->objects, Py_NewRef(op)) < 0) {
1402         return false;
1403     }
1404     return true;
1405 }
1406 
1407 PyObject *
_PyGC_GetObjects(PyInterpreterState * interp,int generation)1408 _PyGC_GetObjects(PyInterpreterState *interp, int generation)
1409 {
1410     // NOTE: We can't append to the PyListObject during gc_visit_heaps()
1411     // because PyList_Append() may reclaim an abandoned mimalloc segments
1412     // while we are traversing them.
1413     struct get_objects_args args = { 0 };
1414     _PyEval_StopTheWorld(interp);
1415     int err = gc_visit_heaps(interp, &visit_get_objects, &args.base);
1416     _PyEval_StartTheWorld(interp);
1417 
1418     PyObject *list = list_from_object_stack(&args.objects);
1419     if (err < 0) {
1420         PyErr_NoMemory();
1421         Py_CLEAR(list);
1422     }
1423     return list;
1424 }
1425 
1426 static bool
visit_freeze(const mi_heap_t * heap,const mi_heap_area_t * area,void * block,size_t block_size,void * args)1427 visit_freeze(const mi_heap_t *heap, const mi_heap_area_t *area,
1428              void *block, size_t block_size, void *args)
1429 {
1430     PyObject *op = op_from_block(block, args, true);
1431     if (op != NULL && !gc_is_unreachable(op)) {
1432         op->ob_gc_bits |= _PyGC_BITS_FROZEN;
1433     }
1434     return true;
1435 }
1436 
1437 void
_PyGC_Freeze(PyInterpreterState * interp)1438 _PyGC_Freeze(PyInterpreterState *interp)
1439 {
1440     struct visitor_args args;
1441     _PyEval_StopTheWorld(interp);
1442     gc_visit_heaps(interp, &visit_freeze, &args);
1443     _PyEval_StartTheWorld(interp);
1444 }
1445 
1446 static bool
visit_unfreeze(const mi_heap_t * heap,const mi_heap_area_t * area,void * block,size_t block_size,void * args)1447 visit_unfreeze(const mi_heap_t *heap, const mi_heap_area_t *area,
1448                void *block, size_t block_size, void *args)
1449 {
1450     PyObject *op = op_from_block(block, args, true);
1451     if (op != NULL) {
1452         op->ob_gc_bits &= ~_PyGC_BITS_FROZEN;
1453     }
1454     return true;
1455 }
1456 
1457 void
_PyGC_Unfreeze(PyInterpreterState * interp)1458 _PyGC_Unfreeze(PyInterpreterState *interp)
1459 {
1460     struct visitor_args args;
1461     _PyEval_StopTheWorld(interp);
1462     gc_visit_heaps(interp, &visit_unfreeze, &args);
1463     _PyEval_StartTheWorld(interp);
1464 }
1465 
1466 struct count_frozen_args {
1467     struct visitor_args base;
1468     Py_ssize_t count;
1469 };
1470 
1471 static bool
visit_count_frozen(const mi_heap_t * heap,const mi_heap_area_t * area,void * block,size_t block_size,void * args)1472 visit_count_frozen(const mi_heap_t *heap, const mi_heap_area_t *area,
1473                    void *block, size_t block_size, void *args)
1474 {
1475     PyObject *op = op_from_block(block, args, true);
1476     if (op != NULL && gc_is_frozen(op)) {
1477         struct count_frozen_args *arg = (struct count_frozen_args *)args;
1478         arg->count++;
1479     }
1480     return true;
1481 }
1482 
1483 Py_ssize_t
_PyGC_GetFreezeCount(PyInterpreterState * interp)1484 _PyGC_GetFreezeCount(PyInterpreterState *interp)
1485 {
1486     struct count_frozen_args args = { .count = 0 };
1487     _PyEval_StopTheWorld(interp);
1488     gc_visit_heaps(interp, &visit_count_frozen, &args.base);
1489     _PyEval_StartTheWorld(interp);
1490     return args.count;
1491 }
1492 
1493 /* C API for controlling the state of the garbage collector */
1494 int
PyGC_Enable(void)1495 PyGC_Enable(void)
1496 {
1497     GCState *gcstate = get_gc_state();
1498     int old_state = gcstate->enabled;
1499     gcstate->enabled = 1;
1500     return old_state;
1501 }
1502 
1503 int
PyGC_Disable(void)1504 PyGC_Disable(void)
1505 {
1506     GCState *gcstate = get_gc_state();
1507     int old_state = gcstate->enabled;
1508     gcstate->enabled = 0;
1509     return old_state;
1510 }
1511 
1512 int
PyGC_IsEnabled(void)1513 PyGC_IsEnabled(void)
1514 {
1515     GCState *gcstate = get_gc_state();
1516     return gcstate->enabled;
1517 }
1518 
1519 /* Public API to invoke gc.collect() from C */
1520 Py_ssize_t
PyGC_Collect(void)1521 PyGC_Collect(void)
1522 {
1523     PyThreadState *tstate = _PyThreadState_GET();
1524     GCState *gcstate = &tstate->interp->gc;
1525 
1526     if (!gcstate->enabled) {
1527         return 0;
1528     }
1529 
1530     Py_ssize_t n;
1531     PyObject *exc = _PyErr_GetRaisedException(tstate);
1532     n = gc_collect_main(tstate, NUM_GENERATIONS - 1, _Py_GC_REASON_MANUAL);
1533     _PyErr_SetRaisedException(tstate, exc);
1534 
1535     return n;
1536 }
1537 
1538 Py_ssize_t
_PyGC_Collect(PyThreadState * tstate,int generation,_PyGC_Reason reason)1539 _PyGC_Collect(PyThreadState *tstate, int generation, _PyGC_Reason reason)
1540 {
1541     return gc_collect_main(tstate, generation, reason);
1542 }
1543 
1544 void
_PyGC_CollectNoFail(PyThreadState * tstate)1545 _PyGC_CollectNoFail(PyThreadState *tstate)
1546 {
1547     /* Ideally, this function is only called on interpreter shutdown,
1548        and therefore not recursively.  Unfortunately, when there are daemon
1549        threads, a daemon thread can start a cyclic garbage collection
1550        during interpreter shutdown (and then never finish it).
1551        See http://bugs.python.org/issue8713#msg195178 for an example.
1552        */
1553     gc_collect_main(tstate, NUM_GENERATIONS - 1, _Py_GC_REASON_SHUTDOWN);
1554 }
1555 
1556 void
_PyGC_DumpShutdownStats(PyInterpreterState * interp)1557 _PyGC_DumpShutdownStats(PyInterpreterState *interp)
1558 {
1559     GCState *gcstate = &interp->gc;
1560     if (!(gcstate->debug & _PyGC_DEBUG_SAVEALL)
1561         && gcstate->garbage != NULL && PyList_GET_SIZE(gcstate->garbage) > 0) {
1562         const char *message;
1563         if (gcstate->debug & _PyGC_DEBUG_UNCOLLECTABLE) {
1564             message = "gc: %zd uncollectable objects at shutdown";
1565         }
1566         else {
1567             message = "gc: %zd uncollectable objects at shutdown; " \
1568                 "use gc.set_debug(gc.DEBUG_UNCOLLECTABLE) to list them";
1569         }
1570         /* PyErr_WarnFormat does too many things and we are at shutdown,
1571            the warnings module's dependencies (e.g. linecache) may be gone
1572            already. */
1573         if (PyErr_WarnExplicitFormat(PyExc_ResourceWarning, "gc", 0,
1574                                      "gc", NULL, message,
1575                                      PyList_GET_SIZE(gcstate->garbage)))
1576         {
1577             PyErr_WriteUnraisable(NULL);
1578         }
1579         if (gcstate->debug & _PyGC_DEBUG_UNCOLLECTABLE) {
1580             PyObject *repr = NULL, *bytes = NULL;
1581             repr = PyObject_Repr(gcstate->garbage);
1582             if (!repr || !(bytes = PyUnicode_EncodeFSDefault(repr))) {
1583                 PyErr_WriteUnraisable(gcstate->garbage);
1584             }
1585             else {
1586                 PySys_WriteStderr(
1587                     "      %s\n",
1588                     PyBytes_AS_STRING(bytes)
1589                     );
1590             }
1591             Py_XDECREF(repr);
1592             Py_XDECREF(bytes);
1593         }
1594     }
1595 }
1596 
1597 
1598 void
_PyGC_Fini(PyInterpreterState * interp)1599 _PyGC_Fini(PyInterpreterState *interp)
1600 {
1601     GCState *gcstate = &interp->gc;
1602     Py_CLEAR(gcstate->garbage);
1603     Py_CLEAR(gcstate->callbacks);
1604 
1605     /* We expect that none of this interpreters objects are shared
1606        with other interpreters.
1607        See https://github.com/python/cpython/issues/90228. */
1608 }
1609 
1610 /* for debugging */
1611 
1612 #ifdef Py_DEBUG
1613 static int
visit_validate(PyObject * op,void * parent_raw)1614 visit_validate(PyObject *op, void *parent_raw)
1615 {
1616     PyObject *parent = _PyObject_CAST(parent_raw);
1617     if (_PyObject_IsFreed(op)) {
1618         _PyObject_ASSERT_FAILED_MSG(parent,
1619                                     "PyObject_GC_Track() object is not valid");
1620     }
1621     return 0;
1622 }
1623 #endif
1624 
1625 
1626 /* extension modules might be compiled with GC support so these
1627    functions must always be available */
1628 
1629 void
PyObject_GC_Track(void * op_raw)1630 PyObject_GC_Track(void *op_raw)
1631 {
1632     PyObject *op = _PyObject_CAST(op_raw);
1633     if (_PyObject_GC_IS_TRACKED(op)) {
1634         _PyObject_ASSERT_FAILED_MSG(op,
1635                                     "object already tracked "
1636                                     "by the garbage collector");
1637     }
1638     _PyObject_GC_TRACK(op);
1639 
1640 #ifdef Py_DEBUG
1641     /* Check that the object is valid: validate objects traversed
1642        by tp_traverse() */
1643     traverseproc traverse = Py_TYPE(op)->tp_traverse;
1644     (void)traverse(op, visit_validate, op);
1645 #endif
1646 }
1647 
1648 void
PyObject_GC_UnTrack(void * op_raw)1649 PyObject_GC_UnTrack(void *op_raw)
1650 {
1651     PyObject *op = _PyObject_CAST(op_raw);
1652     /* Obscure:  the Py_TRASHCAN mechanism requires that we be able to
1653      * call PyObject_GC_UnTrack twice on an object.
1654      */
1655     if (_PyObject_GC_IS_TRACKED(op)) {
1656         _PyObject_GC_UNTRACK(op);
1657     }
1658 }
1659 
1660 int
PyObject_IS_GC(PyObject * obj)1661 PyObject_IS_GC(PyObject *obj)
1662 {
1663     return _PyObject_IS_GC(obj);
1664 }
1665 
1666 void
_Py_ScheduleGC(PyThreadState * tstate)1667 _Py_ScheduleGC(PyThreadState *tstate)
1668 {
1669     if (!_Py_eval_breaker_bit_is_set(tstate, _PY_GC_SCHEDULED_BIT))
1670     {
1671         _Py_set_eval_breaker_bit(tstate, _PY_GC_SCHEDULED_BIT);
1672     }
1673 }
1674 
1675 void
_PyObject_GC_Link(PyObject * op)1676 _PyObject_GC_Link(PyObject *op)
1677 {
1678     record_allocation(_PyThreadState_GET());
1679 }
1680 
1681 void
_Py_RunGC(PyThreadState * tstate)1682 _Py_RunGC(PyThreadState *tstate)
1683 {
1684     GCState *gcstate = get_gc_state();
1685     if (!gcstate->enabled) {
1686         return;
1687     }
1688     gc_collect_main(tstate, 0, _Py_GC_REASON_HEAP);
1689 }
1690 
1691 static PyObject *
gc_alloc(PyTypeObject * tp,size_t basicsize,size_t presize)1692 gc_alloc(PyTypeObject *tp, size_t basicsize, size_t presize)
1693 {
1694     PyThreadState *tstate = _PyThreadState_GET();
1695     if (basicsize > PY_SSIZE_T_MAX - presize) {
1696         return _PyErr_NoMemory(tstate);
1697     }
1698     size_t size = presize + basicsize;
1699     char *mem = _PyObject_MallocWithType(tp, size);
1700     if (mem == NULL) {
1701         return _PyErr_NoMemory(tstate);
1702     }
1703     if (presize) {
1704         ((PyObject **)mem)[0] = NULL;
1705         ((PyObject **)mem)[1] = NULL;
1706     }
1707     PyObject *op = (PyObject *)(mem + presize);
1708     record_allocation(tstate);
1709     return op;
1710 }
1711 
1712 PyObject *
_PyObject_GC_New(PyTypeObject * tp)1713 _PyObject_GC_New(PyTypeObject *tp)
1714 {
1715     size_t presize = _PyType_PreHeaderSize(tp);
1716     size_t size = _PyObject_SIZE(tp);
1717     if (_PyType_HasFeature(tp, Py_TPFLAGS_INLINE_VALUES)) {
1718         size += _PyInlineValuesSize(tp);
1719     }
1720     PyObject *op = gc_alloc(tp, size, presize);
1721     if (op == NULL) {
1722         return NULL;
1723     }
1724     _PyObject_Init(op, tp);
1725     return op;
1726 }
1727 
1728 PyVarObject *
_PyObject_GC_NewVar(PyTypeObject * tp,Py_ssize_t nitems)1729 _PyObject_GC_NewVar(PyTypeObject *tp, Py_ssize_t nitems)
1730 {
1731     PyVarObject *op;
1732 
1733     if (nitems < 0) {
1734         PyErr_BadInternalCall();
1735         return NULL;
1736     }
1737     size_t presize = _PyType_PreHeaderSize(tp);
1738     size_t size = _PyObject_VAR_SIZE(tp, nitems);
1739     op = (PyVarObject *)gc_alloc(tp, size, presize);
1740     if (op == NULL) {
1741         return NULL;
1742     }
1743     _PyObject_InitVar(op, tp, nitems);
1744     return op;
1745 }
1746 
1747 PyObject *
PyUnstable_Object_GC_NewWithExtraData(PyTypeObject * tp,size_t extra_size)1748 PyUnstable_Object_GC_NewWithExtraData(PyTypeObject *tp, size_t extra_size)
1749 {
1750     size_t presize = _PyType_PreHeaderSize(tp);
1751     PyObject *op = gc_alloc(tp, _PyObject_SIZE(tp) + extra_size, presize);
1752     if (op == NULL) {
1753         return NULL;
1754     }
1755     memset(op, 0, _PyObject_SIZE(tp) + extra_size);
1756     _PyObject_Init(op, tp);
1757     return op;
1758 }
1759 
1760 PyVarObject *
_PyObject_GC_Resize(PyVarObject * op,Py_ssize_t nitems)1761 _PyObject_GC_Resize(PyVarObject *op, Py_ssize_t nitems)
1762 {
1763     const size_t basicsize = _PyObject_VAR_SIZE(Py_TYPE(op), nitems);
1764     const size_t presize = _PyType_PreHeaderSize(((PyObject *)op)->ob_type);
1765     _PyObject_ASSERT((PyObject *)op, !_PyObject_GC_IS_TRACKED(op));
1766     if (basicsize > (size_t)PY_SSIZE_T_MAX - presize) {
1767         return (PyVarObject *)PyErr_NoMemory();
1768     }
1769     char *mem = (char *)op - presize;
1770     mem = (char *)_PyObject_ReallocWithType(Py_TYPE(op), mem,  presize + basicsize);
1771     if (mem == NULL) {
1772         return (PyVarObject *)PyErr_NoMemory();
1773     }
1774     op = (PyVarObject *) (mem + presize);
1775     Py_SET_SIZE(op, nitems);
1776     return op;
1777 }
1778 
1779 void
PyObject_GC_Del(void * op)1780 PyObject_GC_Del(void *op)
1781 {
1782     size_t presize = _PyType_PreHeaderSize(((PyObject *)op)->ob_type);
1783     if (_PyObject_GC_IS_TRACKED(op)) {
1784         _PyObject_GC_UNTRACK(op);
1785 #ifdef Py_DEBUG
1786         PyObject *exc = PyErr_GetRaisedException();
1787         if (PyErr_WarnExplicitFormat(PyExc_ResourceWarning, "gc", 0,
1788                                      "gc", NULL, "Object of type %s is not untracked before destruction",
1789                                      ((PyObject*)op)->ob_type->tp_name)) {
1790             PyErr_WriteUnraisable(NULL);
1791         }
1792         PyErr_SetRaisedException(exc);
1793 #endif
1794     }
1795 
1796     record_deallocation(_PyThreadState_GET());
1797     PyObject *self = (PyObject *)op;
1798     if (_PyObject_GC_IS_SHARED_INLINE(self)) {
1799         _PyObject_FreeDelayed(((char *)op)-presize);
1800     }
1801     else {
1802         PyObject_Free(((char *)op)-presize);
1803     }
1804 }
1805 
1806 int
PyObject_GC_IsTracked(PyObject * obj)1807 PyObject_GC_IsTracked(PyObject* obj)
1808 {
1809     return _PyObject_GC_IS_TRACKED(obj);
1810 }
1811 
1812 int
PyObject_GC_IsFinalized(PyObject * obj)1813 PyObject_GC_IsFinalized(PyObject *obj)
1814 {
1815     return _PyGC_FINALIZED(obj);
1816 }
1817 
1818 struct custom_visitor_args {
1819     struct visitor_args base;
1820     gcvisitobjects_t callback;
1821     void *arg;
1822 };
1823 
1824 static bool
custom_visitor_wrapper(const mi_heap_t * heap,const mi_heap_area_t * area,void * block,size_t block_size,void * args)1825 custom_visitor_wrapper(const mi_heap_t *heap, const mi_heap_area_t *area,
1826                        void *block, size_t block_size, void *args)
1827 {
1828     PyObject *op = op_from_block(block, args, false);
1829     if (op == NULL) {
1830         return true;
1831     }
1832 
1833     struct custom_visitor_args *wrapper = (struct custom_visitor_args *)args;
1834     if (!wrapper->callback(op, wrapper->arg)) {
1835         return false;
1836     }
1837 
1838     return true;
1839 }
1840 
1841 // gh-117783: Immortalize objects that use deferred reference counting to
1842 // temporarily work around scaling bottlenecks.
1843 static bool
immortalize_visitor(const mi_heap_t * heap,const mi_heap_area_t * area,void * block,size_t block_size,void * args)1844 immortalize_visitor(const mi_heap_t *heap, const mi_heap_area_t *area,
1845                     void *block, size_t block_size, void *args)
1846 {
1847     PyObject *op = op_from_block(block, args, false);
1848     if (op != NULL && _PyObject_HasDeferredRefcount(op)) {
1849         _Py_SetImmortal(op);
1850         op->ob_gc_bits &= ~_PyGC_BITS_DEFERRED;
1851     }
1852     return true;
1853 }
1854 
1855 void
_PyGC_ImmortalizeDeferredObjects(PyInterpreterState * interp)1856 _PyGC_ImmortalizeDeferredObjects(PyInterpreterState *interp)
1857 {
1858     struct visitor_args args;
1859     _PyEval_StopTheWorld(interp);
1860     if (interp->gc.immortalize == 0) {
1861         gc_visit_heaps(interp, &immortalize_visitor, &args);
1862         interp->gc.immortalize = 1;
1863     }
1864     _PyEval_StartTheWorld(interp);
1865 }
1866 
1867 void
PyUnstable_GC_VisitObjects(gcvisitobjects_t callback,void * arg)1868 PyUnstable_GC_VisitObjects(gcvisitobjects_t callback, void *arg)
1869 {
1870     PyInterpreterState *interp = _PyInterpreterState_GET();
1871     struct custom_visitor_args wrapper = {
1872         .callback = callback,
1873         .arg = arg,
1874     };
1875 
1876     _PyEval_StopTheWorld(interp);
1877     gc_visit_heaps(interp, &custom_visitor_wrapper, &wrapper.base);
1878     _PyEval_StartTheWorld(interp);
1879 }
1880 
1881 /* Clear all free lists
1882  * All free lists are cleared during the collection of the highest generation.
1883  * Allocated items in the free list may keep a pymalloc arena occupied.
1884  * Clearing the free lists may give back memory to the OS earlier.
1885  * Free-threading version: Since freelists are managed per thread,
1886  * GC should clear all freelists by traversing all threads.
1887  */
1888 void
_PyGC_ClearAllFreeLists(PyInterpreterState * interp)1889 _PyGC_ClearAllFreeLists(PyInterpreterState *interp)
1890 {
1891     HEAD_LOCK(&_PyRuntime);
1892     _PyThreadStateImpl *tstate = (_PyThreadStateImpl *)interp->threads.head;
1893     while (tstate != NULL) {
1894         _PyObject_ClearFreeLists(&tstate->freelists, 0);
1895         tstate = (_PyThreadStateImpl *)tstate->base.next;
1896     }
1897     HEAD_UNLOCK(&_PyRuntime);
1898 }
1899 
1900 #endif  // Py_GIL_DISABLED
1901