Coverage Report

Created: 2026-02-26 06:53

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/cpython/Python/gc.c
Line
Count
Source
1
//  This implements the reference cycle garbage collector.
2
//  The Python module interface to the collector is in gcmodule.c.
3
//  See InternalDocs/garbage_collector.md for more infromation.
4
5
#include "Python.h"
6
#include "pycore_ceval.h"         // _Py_set_eval_breaker_bit()
7
#include "pycore_dict.h"          // _PyInlineValuesSize()
8
#include "pycore_initconfig.h"    // _PyStatus_OK()
9
#include "pycore_interp.h"        // PyInterpreterState.gc
10
#include "pycore_interpframe.h"   // _PyFrame_GetLocalsArray()
11
#include "pycore_object_alloc.h"  // _PyObject_MallocWithType()
12
#include "pycore_pystate.h"       // _PyThreadState_GET()
13
#include "pycore_tuple.h"         // _PyTuple_MaybeUntrack()
14
#include "pycore_weakref.h"       // _PyWeakref_ClearRef()
15
16
#include "pydtrace.h"
17
18
19
#ifndef Py_GIL_DISABLED
20
21
typedef struct _gc_runtime_state GCState;
22
23
#ifdef Py_DEBUG
24
#  define GC_DEBUG
25
#endif
26
27
// Define this when debugging the GC
28
// #define GC_EXTRA_DEBUG
29
30
31
805M
#define GC_NEXT _PyGCHead_NEXT
32
319M
#define GC_PREV _PyGCHead_PREV
33
34
// update_refs() set this bit for all objects in current generation.
35
// subtract_refs() and move_unreachable() uses this to distinguish
36
// visited object is in GCing or not.
37
//
38
// move_unreachable() removes this flag from reachable objects.
39
// Only unreachable objects have this flag.
40
//
41
// No objects in interpreter have this flag after GC ends.
42
323M
#define PREV_MASK_COLLECTING   _PyGC_PREV_MASK_COLLECTING
43
44
// Lowest bit of _gc_next is used for UNREACHABLE flag.
45
//
46
// This flag represents the object is in unreachable list in move_unreachable()
47
//
48
// Although this flag is used only in move_unreachable(), move_unreachable()
49
// doesn't clear this flag to skip unnecessary iteration.
50
// move_legacy_finalizers() removes this flag instead.
51
// Between them, unreachable list is not normal list and we can not use
52
// most gc_list_* functions for it.
53
44.2M
#define NEXT_MASK_UNREACHABLE  2
54
55
1.65G
#define AS_GC(op) _Py_AS_GC(op)
56
569M
#define FROM_GC(gc) _Py_FROM_GC(gc)
57
58
// Automatically choose the generation that needs collecting.
59
#define GENERATION_AUTO (-1)
60
61
static inline int
62
gc_is_collecting(PyGC_Head *g)
63
180M
{
64
180M
    return (g->_gc_prev & PREV_MASK_COLLECTING) != 0;
65
180M
}
66
67
static inline void
68
gc_clear_collecting(PyGC_Head *g)
69
70.5M
{
70
70.5M
    g->_gc_prev &= ~PREV_MASK_COLLECTING;
71
70.5M
}
72
73
static inline Py_ssize_t
74
gc_get_refs(PyGC_Head *g)
75
168M
{
76
168M
    return (Py_ssize_t)(g->_gc_prev >> _PyGC_PREV_SHIFT);
77
168M
}
78
79
static inline void
80
gc_set_refs(PyGC_Head *g, Py_ssize_t refs)
81
27.1M
{
82
27.1M
    g->_gc_prev = (g->_gc_prev & ~_PyGC_PREV_MASK)
83
27.1M
        | ((uintptr_t)(refs) << _PyGC_PREV_SHIFT);
84
27.1M
}
85
86
static inline void
87
gc_reset_refs(PyGC_Head *g, Py_ssize_t refs)
88
72.1M
{
89
72.1M
    g->_gc_prev = (g->_gc_prev & _PyGC_PREV_MASK_FINALIZED)
90
72.1M
        | PREV_MASK_COLLECTING
91
72.1M
        | ((uintptr_t)(refs) << _PyGC_PREV_SHIFT);
92
72.1M
}
93
94
static inline void
95
gc_decref(PyGC_Head *g)
96
35.0M
{
97
35.0M
    _PyObject_ASSERT_WITH_MSG(FROM_GC(g),
98
35.0M
                              gc_get_refs(g) > 0,
99
35.0M
                              "refcount is too small");
100
35.0M
    g->_gc_prev -= 1 << _PyGC_PREV_SHIFT;
101
35.0M
}
102
103
static inline int
104
gc_old_space(PyGC_Head *g)
105
273M
{
106
273M
    return g->_gc_next & _PyGC_NEXT_MASK_OLD_SPACE_1;
107
273M
}
108
109
static inline int
110
other_space(int space)
111
1.49k
{
112
1.49k
    assert(space == 0 || space == 1);
113
1.49k
    return space ^ _PyGC_NEXT_MASK_OLD_SPACE_1;
114
1.49k
}
115
116
static inline void
117
gc_flip_old_space(PyGC_Head *g)
118
145M
{
119
145M
    g->_gc_next ^= _PyGC_NEXT_MASK_OLD_SPACE_1;
120
145M
}
121
122
static inline void
123
gc_set_old_space(PyGC_Head *g, int space)
124
68.9M
{
125
68.9M
    assert(space == 0 || space == _PyGC_NEXT_MASK_OLD_SPACE_1);
126
68.9M
    g->_gc_next &= ~_PyGC_NEXT_MASK_OLD_SPACE_1;
127
68.9M
    g->_gc_next |= space;
128
68.9M
}
129
130
static PyGC_Head *
131
GEN_HEAD(GCState *gcstate, int n)
132
0
{
133
0
    assert((gcstate->visited_space & (~1)) == 0);
134
0
    switch(n) {
135
0
        case 0:
136
0
            return &gcstate->young.head;
137
0
        case 1:
138
0
            return &gcstate->old[gcstate->visited_space].head;
139
0
        case 2:
140
0
            return &gcstate->old[gcstate->visited_space^1].head;
141
0
        default:
142
0
            Py_UNREACHABLE();
143
0
    }
144
0
}
145
146
static GCState *
147
get_gc_state(void)
148
18.8k
{
149
18.8k
    PyInterpreterState *interp = _PyInterpreterState_GET();
150
18.8k
    return &interp->gc;
151
18.8k
}
152
153
154
void
155
_PyGC_InitState(GCState *gcstate)
156
32
{
157
32
#define INIT_HEAD(GEN) \
158
128
    do { \
159
128
        GEN.head._gc_next = (uintptr_t)&GEN.head; \
160
128
        GEN.head._gc_prev = (uintptr_t)&GEN.head; \
161
128
    } while (0)
162
163
32
    assert(gcstate->young.count == 0);
164
32
    assert(gcstate->old[0].count == 0);
165
32
    assert(gcstate->old[1].count == 0);
166
32
    INIT_HEAD(gcstate->young);
167
32
    INIT_HEAD(gcstate->old[0]);
168
32
    INIT_HEAD(gcstate->old[1]);
169
32
    INIT_HEAD(gcstate->permanent_generation);
170
171
32
#undef INIT_HEAD
172
32
}
173
174
175
PyStatus
176
_PyGC_Init(PyInterpreterState *interp)
177
32
{
178
32
    GCState *gcstate = &interp->gc;
179
180
32
    gcstate->garbage = PyList_New(0);
181
32
    if (gcstate->garbage == NULL) {
182
0
        return _PyStatus_NO_MEMORY();
183
0
    }
184
185
32
    gcstate->callbacks = PyList_New(0);
186
32
    if (gcstate->callbacks == NULL) {
187
0
        return _PyStatus_NO_MEMORY();
188
0
    }
189
32
    gcstate->heap_size = 0;
190
191
32
    return _PyStatus_OK();
192
32
}
193
194
195
/*
196
_gc_prev values
197
---------------
198
199
Between collections, _gc_prev is used for doubly linked list.
200
201
Lowest two bits of _gc_prev are used for flags.
202
PREV_MASK_COLLECTING is used only while collecting and cleared before GC ends
203
or _PyObject_GC_UNTRACK() is called.
204
205
During a collection, _gc_prev is temporary used for gc_refs, and the gc list
206
is singly linked until _gc_prev is restored.
207
208
gc_refs
209
    At the start of a collection, update_refs() copies the true refcount
210
    to gc_refs, for each object in the generation being collected.
211
    subtract_refs() then adjusts gc_refs so that it equals the number of
212
    times an object is referenced directly from outside the generation
213
    being collected.
214
215
PREV_MASK_COLLECTING
216
    Objects in generation being collected are marked PREV_MASK_COLLECTING in
217
    update_refs().
218
219
220
_gc_next values
221
---------------
222
223
_gc_next takes these values:
224
225
0
226
    The object is not tracked
227
228
!= 0
229
    Pointer to the next object in the GC list.
230
    Additionally, lowest bit is used temporary for
231
    NEXT_MASK_UNREACHABLE flag described below.
232
233
NEXT_MASK_UNREACHABLE
234
    move_unreachable() then moves objects not reachable (whether directly or
235
    indirectly) from outside the generation into an "unreachable" set and
236
    set this flag.
237
238
    Objects that are found to be reachable have gc_refs set to 1.
239
    When this flag is set for the reachable object, the object must be in
240
    "unreachable" set.
241
    The flag is unset and the object is moved back to "reachable" set.
242
243
    move_legacy_finalizers() will remove this flag from "unreachable" set.
244
*/
245
246
/*** list functions ***/
247
248
static inline void
249
gc_list_init(PyGC_Head *list)
250
267k
{
251
    // List header must not have flags.
252
    // We can assign pointer by simple cast.
253
267k
    list->_gc_prev = (uintptr_t)list;
254
267k
    list->_gc_next = (uintptr_t)list;
255
267k
}
256
257
static inline int
258
gc_list_is_empty(PyGC_Head *list)
259
147M
{
260
147M
    return (list->_gc_next == (uintptr_t)list);
261
147M
}
262
263
/* Append `node` to `list`. */
264
static inline void
265
gc_list_append(PyGC_Head *node, PyGC_Head *list)
266
11.2M
{
267
11.2M
    assert((list->_gc_prev & ~_PyGC_PREV_MASK) == 0);
268
11.2M
    PyGC_Head *last = (PyGC_Head *)list->_gc_prev;
269
270
    // last <-> node
271
11.2M
    _PyGCHead_SET_PREV(node, last);
272
11.2M
    _PyGCHead_SET_NEXT(last, node);
273
274
    // node <-> list
275
11.2M
    _PyGCHead_SET_NEXT(node, list);
276
11.2M
    list->_gc_prev = (uintptr_t)node;
277
11.2M
}
278
279
/* Remove `node` from the gc list it's currently in. */
280
static inline void
281
gc_list_remove(PyGC_Head *node)
282
0
{
283
0
    PyGC_Head *prev = GC_PREV(node);
284
0
    PyGC_Head *next = GC_NEXT(node);
285
286
0
    _PyGCHead_SET_NEXT(prev, next);
287
0
    _PyGCHead_SET_PREV(next, prev);
288
289
0
    node->_gc_next = 0; /* object is not currently tracked */
290
0
}
291
292
/* Move `node` from the gc list it's currently in (which is not explicitly
293
 * named here) to the end of `list`.  This is semantically the same as
294
 * gc_list_remove(node) followed by gc_list_append(node, list).
295
 */
296
static void
297
gc_list_move(PyGC_Head *node, PyGC_Head *list)
298
292M
{
299
    /* Unlink from current list. */
300
292M
    PyGC_Head *from_prev = GC_PREV(node);
301
292M
    PyGC_Head *from_next = GC_NEXT(node);
302
292M
    _PyGCHead_SET_NEXT(from_prev, from_next);
303
292M
    _PyGCHead_SET_PREV(from_next, from_prev);
304
305
    /* Relink at end of new list. */
306
    // list must not have flags.  So we can skip macros.
307
292M
    PyGC_Head *to_prev = (PyGC_Head*)list->_gc_prev;
308
292M
    _PyGCHead_SET_PREV(node, to_prev);
309
292M
    _PyGCHead_SET_NEXT(to_prev, node);
310
292M
    list->_gc_prev = (uintptr_t)node;
311
292M
    _PyGCHead_SET_NEXT(node, list);
312
292M
}
313
314
/* append list `from` onto list `to`; `from` becomes an empty list */
315
static void
316
gc_list_merge(PyGC_Head *from, PyGC_Head *to)
317
113k
{
318
113k
    assert(from != to);
319
113k
    if (!gc_list_is_empty(from)) {
320
55.4k
        PyGC_Head *to_tail = GC_PREV(to);
321
55.4k
        PyGC_Head *from_head = GC_NEXT(from);
322
55.4k
        PyGC_Head *from_tail = GC_PREV(from);
323
55.4k
        assert(from_head != from);
324
55.4k
        assert(from_tail != from);
325
55.4k
        assert(gc_list_is_empty(to) ||
326
55.4k
            gc_old_space(to_tail) == gc_old_space(from_tail));
327
328
55.4k
        _PyGCHead_SET_NEXT(to_tail, from_head);
329
55.4k
        _PyGCHead_SET_PREV(from_head, to_tail);
330
331
55.4k
        _PyGCHead_SET_NEXT(from_tail, to);
332
55.4k
        _PyGCHead_SET_PREV(to, from_tail);
333
55.4k
    }
334
113k
    gc_list_init(from);
335
113k
}
336
337
static Py_ssize_t
338
gc_list_size(PyGC_Head *list)
339
37.7k
{
340
37.7k
    PyGC_Head *gc;
341
37.7k
    Py_ssize_t n = 0;
342
70.7M
    for (gc = GC_NEXT(list); gc != list; gc = GC_NEXT(gc)) {
343
70.7M
        n++;
344
70.7M
    }
345
37.7k
    return n;
346
37.7k
}
347
348
/* Walk the list and mark all objects as non-collecting */
349
static inline void
350
gc_list_clear_collecting(PyGC_Head *collectable)
351
18.8k
{
352
18.8k
    PyGC_Head *gc;
353
2.08M
    for (gc = GC_NEXT(collectable); gc != collectable; gc = GC_NEXT(gc)) {
354
2.06M
        gc_clear_collecting(gc);
355
2.06M
    }
356
18.8k
}
357
358
/* Append objects in a GC list to a Python list.
359
 * Return 0 if all OK, < 0 if error (out of memory for list)
360
 */
361
static int
362
append_objects(PyObject *py_list, PyGC_Head *gc_list)
363
0
{
364
0
    PyGC_Head *gc;
365
0
    for (gc = GC_NEXT(gc_list); gc != gc_list; gc = GC_NEXT(gc)) {
366
0
        PyObject *op = FROM_GC(gc);
367
0
        if (op != py_list) {
368
0
            if (PyList_Append(py_list, op)) {
369
0
                return -1; /* exception */
370
0
            }
371
0
        }
372
0
    }
373
0
    return 0;
374
0
}
375
376
// Constants for validate_list's flags argument.
377
enum flagstates {collecting_clear_unreachable_clear,
378
                 collecting_clear_unreachable_set,
379
                 collecting_set_unreachable_clear,
380
                 collecting_set_unreachable_set};
381
382
#ifdef GC_DEBUG
383
// validate_list checks list consistency.  And it works as document
384
// describing when flags are expected to be set / unset.
385
// `head` must be a doubly-linked gc list, although it's fine (expected!) if
386
// the prev and next pointers are "polluted" with flags.
387
// What's checked:
388
// - The `head` pointers are not polluted.
389
// - The objects' PREV_MASK_COLLECTING and NEXT_MASK_UNREACHABLE flags are all
390
//   `set or clear, as specified by the 'flags' argument.
391
// - The prev and next pointers are mutually consistent.
392
static void
393
validate_list(PyGC_Head *head, enum flagstates flags)
394
{
395
    assert((head->_gc_prev & ~_PyGC_PREV_MASK) == 0);
396
    assert((head->_gc_next & ~_PyGC_PREV_MASK) == 0);
397
    uintptr_t prev_value = 0, next_value = 0;
398
    switch (flags) {
399
        case collecting_clear_unreachable_clear:
400
            break;
401
        case collecting_set_unreachable_clear:
402
            prev_value = PREV_MASK_COLLECTING;
403
            break;
404
        case collecting_clear_unreachable_set:
405
            next_value = NEXT_MASK_UNREACHABLE;
406
            break;
407
        case collecting_set_unreachable_set:
408
            prev_value = PREV_MASK_COLLECTING;
409
            next_value = NEXT_MASK_UNREACHABLE;
410
            break;
411
        default:
412
            assert(! "bad internal flags argument");
413
    }
414
    PyGC_Head *prev = head;
415
    PyGC_Head *gc = GC_NEXT(head);
416
    while (gc != head) {
417
        PyGC_Head *trueprev = GC_PREV(gc);
418
        PyGC_Head *truenext = GC_NEXT(gc);
419
        assert(truenext != NULL);
420
        assert(trueprev == prev);
421
        assert((gc->_gc_prev & PREV_MASK_COLLECTING) == prev_value);
422
        assert((gc->_gc_next & NEXT_MASK_UNREACHABLE) == next_value);
423
        prev = gc;
424
        gc = truenext;
425
    }
426
    assert(prev == GC_PREV(head));
427
}
428
429
#else
430
245k
#define validate_list(x, y) do{}while(0)
431
#endif
432
433
#ifdef GC_EXTRA_DEBUG
434
435
436
static void
437
gc_list_validate_space(PyGC_Head *head, int space) {
438
    PyGC_Head *gc = GC_NEXT(head);
439
    while (gc != head) {
440
        assert(gc_old_space(gc) == space);
441
        gc = GC_NEXT(gc);
442
    }
443
}
444
445
static void
446
validate_spaces(GCState *gcstate)
447
{
448
    int visited = gcstate->visited_space;
449
    int not_visited = other_space(visited);
450
    gc_list_validate_space(&gcstate->young.head, not_visited);
451
    for (int space = 0; space < 2; space++) {
452
        gc_list_validate_space(&gcstate->old[space].head, space);
453
    }
454
    gc_list_validate_space(&gcstate->permanent_generation.head, visited);
455
}
456
457
static void
458
validate_consistent_old_space(PyGC_Head *head)
459
{
460
    PyGC_Head *gc = GC_NEXT(head);
461
    if (gc == head) {
462
        return;
463
    }
464
    int old_space = gc_old_space(gc);
465
    while (gc != head) {
466
        PyGC_Head *truenext = GC_NEXT(gc);
467
        assert(truenext != NULL);
468
        assert(gc_old_space(gc) == old_space);
469
        gc = truenext;
470
    }
471
}
472
473
474
#else
475
112k
#define validate_spaces(g) do{}while(0)
476
94.4k
#define validate_consistent_old_space(l) do{}while(0)
477
97.4k
#define gc_list_validate_space(l, s) do{}while(0)
478
#endif
479
480
/*** end of list stuff ***/
481
482
483
/* Set all gc_refs = ob_refcnt.  After this, gc_refs is > 0 and
484
 * PREV_MASK_COLLECTING bit is set for all objects in containers.
485
 */
486
static Py_ssize_t
487
update_refs(PyGC_Head *containers)
488
37.7k
{
489
37.7k
    PyGC_Head *next;
490
37.7k
    PyGC_Head *gc = GC_NEXT(containers);
491
37.7k
    Py_ssize_t candidates = 0;
492
493
72.1M
    while (gc != containers) {
494
72.1M
        next = GC_NEXT(gc);
495
72.1M
        PyObject *op = FROM_GC(gc);
496
72.1M
        if (_Py_IsImmortal(op)) {
497
0
            assert(!_Py_IsStaticImmortal(op));
498
0
            _PyObject_GC_UNTRACK(op);
499
0
           gc = next;
500
0
           continue;
501
0
        }
502
72.1M
        gc_reset_refs(gc, Py_REFCNT(op));
503
        /* Python's cyclic gc should never see an incoming refcount
504
         * of 0:  if something decref'ed to 0, it should have been
505
         * deallocated immediately at that time.
506
         * Possible cause (if the assert triggers):  a tp_dealloc
507
         * routine left a gc-aware object tracked during its teardown
508
         * phase, and did something-- or allowed something to happen --
509
         * that called back into Python.  gc can trigger then, and may
510
         * see the still-tracked dying object.  Before this assert
511
         * was added, such mistakes went on to allow gc to try to
512
         * delete the object again.  In a debug build, that caused
513
         * a mysterious segfault, when _Py_ForgetReference tried
514
         * to remove the object from the doubly-linked list of all
515
         * objects a second time.  In a release build, an actual
516
         * double deallocation occurred, which leads to corruption
517
         * of the allocator's internal bookkeeping pointers.  That's
518
         * so serious that maybe this should be a release-build
519
         * check instead of an assert?
520
         */
521
72.1M
        _PyObject_ASSERT(op, gc_get_refs(gc) != 0);
522
72.1M
        gc = next;
523
72.1M
        candidates++;
524
72.1M
    }
525
37.7k
    return candidates;
526
37.7k
}
527
528
/* A traversal callback for subtract_refs. */
529
static int
530
visit_decref(PyObject *op, void *parent)
531
183M
{
532
183M
    OBJECT_STAT_INC(object_visits);
533
183M
    _PyObject_ASSERT(_PyObject_CAST(parent), !_PyObject_IsFreed(op));
534
535
183M
    if (_PyObject_IS_GC(op)) {
536
95.5M
        PyGC_Head *gc = AS_GC(op);
537
        /* We're only interested in gc_refs for objects in the
538
         * generation being collected, which can be recognized
539
         * because only they have positive gc_refs.
540
         */
541
95.5M
        if (gc_is_collecting(gc)) {
542
35.0M
            gc_decref(gc);
543
35.0M
        }
544
95.5M
    }
545
183M
    return 0;
546
183M
}
547
548
int
549
_PyGC_VisitStackRef(_PyStackRef *ref, visitproc visit, void *arg)
550
2.98M
{
551
    // This is a bit tricky! We want to ignore stackrefs with embedded
552
    // refcounts when computing the incoming references, but otherwise treat
553
    // them like normal.
554
2.98M
    assert(!PyStackRef_IsTaggedInt(*ref));
555
2.98M
    if (!PyStackRef_RefcountOnObject(*ref) && (visit == visit_decref)) {
556
7.26k
        return 0;
557
7.26k
    }
558
2.97M
    Py_VISIT(PyStackRef_AsPyObjectBorrow(*ref));
559
2.97M
    return 0;
560
2.97M
}
561
562
int
563
_PyGC_VisitFrameStack(_PyInterpreterFrame *frame, visitproc visit, void *arg)
564
561k
{
565
561k
    _PyStackRef *ref = _PyFrame_GetLocalsArray(frame);
566
    /* locals and stack */
567
3.28M
    for (; ref < frame->stackpointer; ref++) {
568
2.72M
        if (!PyStackRef_IsTaggedInt(*ref)) {
569
2.72M
            _Py_VISIT_STACKREF(*ref);
570
2.72M
        }
571
2.72M
    }
572
561k
    return 0;
573
561k
}
574
575
/* Subtract internal references from gc_refs.  After this, gc_refs is >= 0
576
 * for all objects in containers. The ones with gc_refs > 0 are directly
577
 * reachable from outside containers, and so can't be collected.
578
 */
579
static void
580
subtract_refs(PyGC_Head *containers)
581
37.7k
{
582
37.7k
    traverseproc traverse;
583
37.7k
    PyGC_Head *gc = GC_NEXT(containers);
584
72.1M
    for (; gc != containers; gc = GC_NEXT(gc)) {
585
72.1M
        PyObject *op = FROM_GC(gc);
586
72.1M
        traverse = Py_TYPE(op)->tp_traverse;
587
72.1M
        (void) traverse(op,
588
72.1M
                        visit_decref,
589
72.1M
                        op);
590
72.1M
    }
591
37.7k
}
592
593
/* A traversal callback for move_unreachable. */
594
static int
595
visit_reachable(PyObject *op, void *arg)
596
169M
{
597
169M
    PyGC_Head *reachable = arg;
598
169M
    OBJECT_STAT_INC(object_visits);
599
169M
    if (!_PyObject_IS_GC(op)) {
600
84.5M
        return 0;
601
84.5M
    }
602
603
85.4M
    PyGC_Head *gc = AS_GC(op);
604
85.4M
    const Py_ssize_t gc_refs = gc_get_refs(gc);
605
606
    // Ignore objects in other generation.
607
    // This also skips objects "to the left" of the current position in
608
    // move_unreachable's scan of the 'young' list - they've already been
609
    // traversed, and no longer have the PREV_MASK_COLLECTING flag.
610
85.4M
    if (! gc_is_collecting(gc)) {
611
56.6M
        return 0;
612
56.6M
    }
613
    // It would be a logic error elsewhere if the collecting flag were set on
614
    // an untracked object.
615
28.7M
    _PyObject_ASSERT(op, gc->_gc_next != 0);
616
617
28.7M
    if (gc->_gc_next & NEXT_MASK_UNREACHABLE) {
618
        /* This had gc_refs = 0 when move_unreachable got
619
         * to it, but turns out it's reachable after all.
620
         * Move it back to move_unreachable's 'young' list,
621
         * and move_unreachable will eventually get to it
622
         * again.
623
         */
624
        // Manually unlink gc from unreachable list because the list functions
625
        // don't work right in the presence of NEXT_MASK_UNREACHABLE flags.
626
11.2M
        PyGC_Head *prev = GC_PREV(gc);
627
11.2M
        PyGC_Head *next = GC_NEXT(gc);
628
11.2M
        _PyObject_ASSERT(FROM_GC(prev),
629
11.2M
                         prev->_gc_next & NEXT_MASK_UNREACHABLE);
630
11.2M
        _PyObject_ASSERT(FROM_GC(next),
631
11.2M
                         next->_gc_next & NEXT_MASK_UNREACHABLE);
632
11.2M
        prev->_gc_next = gc->_gc_next;  // copy flag bits
633
11.2M
        gc->_gc_next &= ~NEXT_MASK_UNREACHABLE;
634
11.2M
        _PyGCHead_SET_PREV(next, prev);
635
636
11.2M
        gc_list_append(gc, reachable);
637
11.2M
        gc_set_refs(gc, 1);
638
11.2M
    }
639
17.4M
    else if (gc_refs == 0) {
640
        /* This is in move_unreachable's 'young' list, but
641
         * the traversal hasn't yet gotten to it.  All
642
         * we need to do is tell move_unreachable that it's
643
         * reachable.
644
         */
645
15.8M
        gc_set_refs(gc, 1);
646
15.8M
    }
647
    /* Else there's nothing to do.
648
     * If gc_refs > 0, it must be in move_unreachable's 'young'
649
     * list, and move_unreachable will eventually get to it.
650
     */
651
1.65M
    else {
652
1.65M
        _PyObject_ASSERT_WITH_MSG(op, gc_refs > 0, "refcount is too small");
653
1.65M
    }
654
28.7M
    return 0;
655
85.4M
}
656
657
/* Move the unreachable objects from young to unreachable.  After this,
658
 * all objects in young don't have PREV_MASK_COLLECTING flag and
659
 * unreachable have the flag.
660
 * All objects in young after this are directly or indirectly reachable
661
 * from outside the original young; and all objects in unreachable are
662
 * not.
663
 *
664
 * This function restores _gc_prev pointer.  young and unreachable are
665
 * doubly linked list after this function.
666
 * But _gc_next in unreachable list has NEXT_MASK_UNREACHABLE flag.
667
 * So we can not gc_list_* functions for unreachable until we remove the flag.
668
 */
669
static void
670
move_unreachable(PyGC_Head *young, PyGC_Head *unreachable)
671
37.7k
{
672
    // previous elem in the young list, used for restore gc_prev.
673
37.7k
    PyGC_Head *prev = young;
674
37.7k
    PyGC_Head *gc = GC_NEXT(young);
675
676
    /* Invariants:  all objects "to the left" of us in young are reachable
677
     * (directly or indirectly) from outside the young list as it was at entry.
678
     *
679
     * All other objects from the original young "to the left" of us are in
680
     * unreachable now, and have NEXT_MASK_UNREACHABLE.  All objects to the
681
     * left of us in 'young' now have been scanned, and no objects here
682
     * or to the right have been scanned yet.
683
     */
684
685
37.7k
    validate_consistent_old_space(young);
686
    /* Record which old space we are in, and set NEXT_MASK_UNREACHABLE bit for convenience */
687
37.7k
    uintptr_t flags = NEXT_MASK_UNREACHABLE | (gc->_gc_next & _PyGC_NEXT_MASK_OLD_SPACE_1);
688
83.4M
    while (gc != young) {
689
83.4M
        if (gc_get_refs(gc)) {
690
            /* gc is definitely reachable from outside the
691
             * original 'young'.  Mark it as such, and traverse
692
             * its pointers to find any other objects that may
693
             * be directly reachable from it.  Note that the
694
             * call to tp_traverse may append objects to young,
695
             * so we have to wait until it returns to determine
696
             * the next object to visit.
697
             */
698
68.0M
            PyObject *op = FROM_GC(gc);
699
68.0M
            traverseproc traverse = Py_TYPE(op)->tp_traverse;
700
68.0M
            _PyObject_ASSERT_WITH_MSG(op, gc_get_refs(gc) > 0,
701
68.0M
                                      "refcount is too small");
702
            // NOTE: visit_reachable may change gc->_gc_next when
703
            // young->_gc_prev == gc.  Don't do gc = GC_NEXT(gc) before!
704
68.0M
            (void) traverse(op,
705
68.0M
                    visit_reachable,
706
68.0M
                    (void *)young);
707
            // relink gc_prev to prev element.
708
68.0M
            _PyGCHead_SET_PREV(gc, prev);
709
            // gc is not COLLECTING state after here.
710
68.0M
            gc_clear_collecting(gc);
711
68.0M
            prev = gc;
712
68.0M
        }
713
15.4M
        else {
714
            /* This *may* be unreachable.  To make progress,
715
             * assume it is.  gc isn't directly reachable from
716
             * any object we've already traversed, but may be
717
             * reachable from an object we haven't gotten to yet.
718
             * visit_reachable will eventually move gc back into
719
             * young if that's so, and we'll see it again.
720
             */
721
            // Move gc to unreachable.
722
            // No need to gc->next->prev = prev because it is single linked.
723
15.4M
            prev->_gc_next = gc->_gc_next;
724
725
            // We can't use gc_list_append() here because we use
726
            // NEXT_MASK_UNREACHABLE here.
727
15.4M
            PyGC_Head *last = GC_PREV(unreachable);
728
            // NOTE: Since all objects in unreachable set has
729
            // NEXT_MASK_UNREACHABLE flag, we set it unconditionally.
730
            // But this may pollute the unreachable list head's 'next' pointer
731
            // too. That's semantically senseless but expedient here - the
732
            // damage is repaired when this function ends.
733
15.4M
            last->_gc_next = flags | (uintptr_t)gc;
734
15.4M
            _PyGCHead_SET_PREV(gc, last);
735
15.4M
            gc->_gc_next = flags | (uintptr_t)unreachable;
736
15.4M
            unreachable->_gc_prev = (uintptr_t)gc;
737
15.4M
        }
738
83.4M
        gc = _PyGCHead_NEXT(prev);
739
83.4M
    }
740
    // young->_gc_prev must be last element remained in the list.
741
37.7k
    young->_gc_prev = (uintptr_t)prev;
742
37.7k
    young->_gc_next &= _PyGC_PREV_MASK;
743
    // don't let the pollution of the list head's next pointer leak
744
37.7k
    unreachable->_gc_next &= _PyGC_PREV_MASK;
745
37.7k
}
746
747
/* In theory, all tuples should be younger than the
748
* objects they refer to, as tuples are immortal.
749
* Therefore, untracking tuples in oldest-first order in the
750
* young generation before promoting them should have tracked
751
* all the tuples that can be untracked.
752
*
753
* Unfortunately, the C API allows tuples to be created
754
* and then filled in. So this won't untrack all tuples
755
* that can be untracked. It should untrack most of them
756
* and is much faster than a more complex approach that
757
* would untrack all relevant tuples.
758
*/
759
static void
760
untrack_tuples(PyGC_Head *head)
761
39.2k
{
762
39.2k
    PyGC_Head *gc = GC_NEXT(head);
763
202M
    while (gc != head) {
764
202M
        PyObject *op = FROM_GC(gc);
765
202M
        PyGC_Head *next = GC_NEXT(gc);
766
202M
        if (PyTuple_CheckExact(op)) {
767
106M
            _PyTuple_MaybeUntrack(op);
768
106M
        }
769
202M
        gc = next;
770
202M
    }
771
39.2k
}
772
773
/* Return true if object has a pre-PEP 442 finalization method. */
774
static int
775
has_legacy_finalizer(PyObject *op)
776
2.06M
{
777
2.06M
    return Py_TYPE(op)->tp_del != NULL;
778
2.06M
}
779
780
/* Move the objects in unreachable with tp_del slots into `finalizers`.
781
 *
782
 * This function also removes NEXT_MASK_UNREACHABLE flag
783
 * from _gc_next in unreachable.
784
 */
785
static void
786
move_legacy_finalizers(PyGC_Head *unreachable, PyGC_Head *finalizers)
787
18.8k
{
788
18.8k
    PyGC_Head *gc, *next;
789
18.8k
    _PyObject_ASSERT(
790
18.8k
        FROM_GC(unreachable),
791
18.8k
        (unreachable->_gc_next & NEXT_MASK_UNREACHABLE) == 0);
792
793
    /* March over unreachable.  Move objects with finalizers into
794
     * `finalizers`.
795
     */
796
2.08M
    for (gc = GC_NEXT(unreachable); gc != unreachable; gc = next) {
797
2.06M
        PyObject *op = FROM_GC(gc);
798
799
2.06M
        _PyObject_ASSERT(op, gc->_gc_next & NEXT_MASK_UNREACHABLE);
800
2.06M
        next = GC_NEXT(gc);
801
2.06M
        gc->_gc_next &= ~NEXT_MASK_UNREACHABLE;
802
803
2.06M
        if (has_legacy_finalizer(op)) {
804
0
            gc_clear_collecting(gc);
805
0
            gc_list_move(gc, finalizers);
806
0
        }
807
2.06M
    }
808
18.8k
}
809
810
static inline void
811
clear_unreachable_mask(PyGC_Head *unreachable)
812
18.8k
{
813
    /* Check that the list head does not have the unreachable bit set */
814
18.8k
    _PyObject_ASSERT(
815
18.8k
        FROM_GC(unreachable),
816
18.8k
        ((uintptr_t)unreachable & NEXT_MASK_UNREACHABLE) == 0);
817
18.8k
    _PyObject_ASSERT(
818
18.8k
        FROM_GC(unreachable),
819
18.8k
        (unreachable->_gc_next & NEXT_MASK_UNREACHABLE) == 0);
820
821
18.8k
    PyGC_Head *gc, *next;
822
2.08M
    for (gc = GC_NEXT(unreachable); gc != unreachable; gc = next) {
823
2.06M
        _PyObject_ASSERT((PyObject*)FROM_GC(gc), gc->_gc_next & NEXT_MASK_UNREACHABLE);
824
2.06M
        next = GC_NEXT(gc);
825
2.06M
        gc->_gc_next &= ~NEXT_MASK_UNREACHABLE;
826
2.06M
    }
827
18.8k
    validate_list(unreachable, collecting_set_unreachable_clear);
828
18.8k
}
829
830
/* A traversal callback for move_legacy_finalizer_reachable. */
831
static int
832
visit_move(PyObject *op, void *arg)
833
0
{
834
0
    PyGC_Head *tolist = arg;
835
0
    OBJECT_STAT_INC(object_visits);
836
0
    if (_PyObject_IS_GC(op)) {
837
0
        PyGC_Head *gc = AS_GC(op);
838
0
        if (gc_is_collecting(gc)) {
839
0
            gc_list_move(gc, tolist);
840
0
            gc_clear_collecting(gc);
841
0
        }
842
0
    }
843
0
    return 0;
844
0
}
845
846
/* Move objects that are reachable from finalizers, from the unreachable set
847
 * into finalizers set.
848
 */
849
static void
850
move_legacy_finalizer_reachable(PyGC_Head *finalizers)
851
18.8k
{
852
18.8k
    traverseproc traverse;
853
18.8k
    PyGC_Head *gc = GC_NEXT(finalizers);
854
18.8k
    for (; gc != finalizers; gc = GC_NEXT(gc)) {
855
        /* Note that the finalizers list may grow during this. */
856
0
        traverse = Py_TYPE(FROM_GC(gc))->tp_traverse;
857
0
        (void) traverse(FROM_GC(gc),
858
0
                        visit_move,
859
0
                        (void *)finalizers);
860
0
    }
861
18.8k
}
862
863
/* Handle weakref callbacks.  Note that it's possible for such weakrefs to be
864
 * outside the unreachable set -- indeed, those are precisely the weakrefs
865
 * whose callbacks must be invoked.  See gc_weakref.txt for overview & some
866
 * details.
867
 *
868
 * The clearing of weakrefs is suble and must be done carefully, as there was
869
 * previous bugs related to this.  First, weakrefs to the unreachable set of
870
 * objects must be cleared before we start calling `tp_clear`.  If we don't,
871
 * those weakrefs can reveal unreachable objects to Python-level code and that
872
 * is not safe.  Many objects are not usable after `tp_clear` has been called
873
 * and could even cause crashes if accessed (see bpo-38006 and gh-91636 as
874
 * example bugs).
875
 *
876
 * Weakrefs with callbacks always need to be cleared before executing the
877
 * callback.  That's because sometimes a callback will call the ref object,
878
 * to check if the reference is actually dead (KeyedRef does this, for
879
 * example).  We want to indicate that it is dead, even though it is possible
880
 * a finalizer might resurrect it.  Clearing also prevents the callback from
881
 * executing more than once.
882
 *
883
 * Since Python 2.3, all weakrefs to cyclic garbage have been cleared *before*
884
 * calling finalizers.  However, since tp_subclasses started being necessary
885
 * to invalidate caches (e.g. by PyType_Modified), that clearing has created
886
 * a bug.  If the weakref to the subclass is cleared before a finalizer is
887
 * called, the cache may not be correctly invalidated.  That can lead to
888
 * segfaults since the caches can refer to deallocated objects (GH-135552
889
 * is an example).  Now, we delay the clear of weakrefs without callbacks
890
 * until *after* finalizers have been executed.  That means weakrefs without
891
 * callbacks are still usable while finalizers are being executed.
892
 *
893
 * The weakrefs that are inside the unreachable set must also be cleared.
894
 * The reason this is required is not immediately obvious.  If the weakref
895
 * refers to an object outside of the unreachable set, that object might go
896
 * away when we start clearing objects.  Normally, the object should also be
897
 * part of the unreachable set but that's not true in the case of incomplete
898
 * or missing `tp_traverse` methods.  When that object goes away, the callback
899
 * for weakref can be executed and that could reveal unreachable objects to
900
 * Python-level code.  See bpo-38006 as an example bug.
901
 */
902
static int
903
handle_weakref_callbacks(PyGC_Head *unreachable, PyGC_Head *old)
904
18.8k
{
905
18.8k
    PyGC_Head *gc;
906
18.8k
    PyGC_Head wrcb_to_call;     /* weakrefs with callbacks to call */
907
18.8k
    PyGC_Head *next;
908
18.8k
    int num_freed = 0;
909
910
18.8k
    gc_list_init(&wrcb_to_call);
911
912
    /* Find all weakrefs with callbacks and move into `wrcb_to_call` if the
913
     * callback needs to be invoked. We make another pass over wrcb_to_call,
914
     * invoking callbacks, after this pass completes.
915
     */
916
2.08M
    for (gc = GC_NEXT(unreachable); gc != unreachable; gc = next) {
917
2.06M
        PyWeakReference **wrlist;
918
919
2.06M
        PyObject *op = FROM_GC(gc);
920
2.06M
        next = GC_NEXT(gc);
921
922
2.06M
        if (! _PyType_SUPPORTS_WEAKREFS(Py_TYPE(op))) {
923
1.05M
            continue;
924
1.05M
        }
925
926
        /* It supports weakrefs.  Does it have any?
927
         *
928
         * This is never triggered for static types so we can avoid the
929
         * (slightly) more costly _PyObject_GET_WEAKREFS_LISTPTR().
930
         */
931
1.01M
        wrlist = _PyObject_GET_WEAKREFS_LISTPTR_FROM_OFFSET(op);
932
933
        /* `op` may have some weakrefs.  March over the list and move the
934
         * weakrefs with callbacks that must be called into wrcb_to_call.
935
         */
936
1.01M
        PyWeakReference *next_wr;
937
1.25M
        for (PyWeakReference *wr = *wrlist; wr != NULL; wr = next_wr) {
938
            // Get the next list element to get iterator progress if we omit
939
            // clearing of the weakref (because _PyWeakref_ClearRef changes
940
            // next pointer in the wrlist).
941
239k
            next_wr = wr->wr_next;
942
943
239k
            if (wr->wr_callback == NULL) {
944
                /* no callback */
945
239k
                continue;
946
239k
            }
947
948
            // Clear the weakref.  See the comments above this function for
949
            // reasons why we need to clear weakrefs that have callbacks.
950
            // Note that _PyWeakref_ClearRef clears the weakref but leaves the
951
            // callback pointer intact.  Obscure: it also changes *wrlist.
952
0
            _PyObject_ASSERT((PyObject *)wr, wr->wr_object == op);
953
0
            _PyWeakref_ClearRef(wr);
954
0
            _PyObject_ASSERT((PyObject *)wr, wr->wr_object == Py_None);
955
956
            /* Headache time.  `op` is going away, and is weakly referenced by
957
             * `wr`, which has a callback.  Should the callback be invoked?  If wr
958
             * is also trash, no:
959
             *
960
             * 1. There's no need to call it.  The object and the weakref are
961
             *    both going away, so it's legitimate to pretend the weakref is
962
             *    going away first.  The user has to ensure a weakref outlives its
963
             *    referent if they want a guarantee that the wr callback will get
964
             *    invoked.
965
             *
966
             * 2. It may be catastrophic to call it.  If the callback is also in
967
             *    cyclic trash (CT), then although the CT is unreachable from
968
             *    outside the current generation, CT may be reachable from the
969
             *    callback.  Then the callback could resurrect insane objects.
970
             *
971
             * Since the callback is never needed and may be unsafe in this
972
             * case, wr is simply left in the unreachable set.  Note that
973
             * clear_weakrefs() will ensure its callback will not trigger
974
             * inside delete_garbage().
975
             *
976
             * OTOH, if wr isn't part of CT, we should invoke the callback:  the
977
             * weakref outlived the trash.  Note that since wr isn't CT in this
978
             * case, its callback can't be CT either -- wr acted as an external
979
             * root to this generation, and therefore its callback did too.  So
980
             * nothing in CT is reachable from the callback either, so it's hard
981
             * to imagine how calling it later could create a problem for us.  wr
982
             * is moved to wrcb_to_call in this case.
983
             */
984
0
            if (gc_is_collecting(AS_GC((PyObject *)wr))) {
985
0
                continue;
986
0
            }
987
988
            /* Create a new reference so that wr can't go away
989
             * before we can process it again.
990
             */
991
0
            Py_INCREF(wr);
992
993
            /* Move wr to wrcb_to_call, for the next pass. */
994
0
            PyGC_Head *wrasgc = AS_GC((PyObject *)wr);
995
            // wrasgc is reachable, but next isn't, so they can't be the same
996
0
            _PyObject_ASSERT((PyObject *)wr, wrasgc != next);
997
0
            gc_list_move(wrasgc, &wrcb_to_call);
998
0
        }
999
1.01M
    }
1000
1001
    /* Invoke the callbacks we decided to honor.  It's safe to invoke them
1002
     * because they can't reference unreachable objects.
1003
     */
1004
18.8k
    int visited_space = get_gc_state()->visited_space;
1005
18.8k
    while (! gc_list_is_empty(&wrcb_to_call)) {
1006
0
        PyObject *temp;
1007
0
        PyObject *callback;
1008
1009
0
        gc = (PyGC_Head*)wrcb_to_call._gc_next;
1010
0
        PyObject *op = FROM_GC(gc);
1011
0
        _PyObject_ASSERT(op, PyWeakref_Check(op));
1012
0
        PyWeakReference *wr = (PyWeakReference *)op;
1013
0
        callback = wr->wr_callback;
1014
0
        _PyObject_ASSERT(op, callback != NULL);
1015
1016
        /* copy-paste of weakrefobject.c's handle_callback() */
1017
0
        temp = PyObject_CallOneArg(callback, (PyObject *)wr);
1018
0
        if (temp == NULL) {
1019
0
            PyErr_FormatUnraisable("Exception ignored on "
1020
0
                                   "calling weakref callback %R", callback);
1021
0
        }
1022
0
        else {
1023
0
            Py_DECREF(temp);
1024
0
        }
1025
1026
        /* Give up the reference we created in the first pass.  When
1027
         * op's refcount hits 0 (which it may or may not do right now),
1028
         * op's tp_dealloc will decref op->wr_callback too.  Note
1029
         * that the refcount probably will hit 0 now, and because this
1030
         * weakref was reachable to begin with, gc didn't already
1031
         * add it to its count of freed objects.  Example:  a reachable
1032
         * weak value dict maps some key to this reachable weakref.
1033
         * The callback removes this key->weakref mapping from the
1034
         * dict, leaving no other references to the weakref (excepting
1035
         * ours).
1036
         */
1037
0
        Py_DECREF(op);
1038
0
        if (wrcb_to_call._gc_next == (uintptr_t)gc) {
1039
            /* object is still alive -- move it */
1040
0
            gc_set_old_space(gc, visited_space);
1041
0
            gc_list_move(gc, old);
1042
0
        }
1043
0
        else {
1044
0
            ++num_freed;
1045
0
        }
1046
0
    }
1047
1048
18.8k
    return num_freed;
1049
18.8k
}
1050
1051
/* Clear all weakrefs to unreachable objects.  When this returns, no object in
1052
 * `unreachable` is weakly referenced anymore.  See the comments above
1053
 * handle_weakref_callbacks() for why these weakrefs need to be cleared.
1054
 */
1055
static void
1056
clear_weakrefs(PyGC_Head *unreachable)
1057
18.8k
{
1058
18.8k
    PyGC_Head *gc;
1059
18.8k
    PyGC_Head *next;
1060
1061
2.08M
    for (gc = GC_NEXT(unreachable); gc != unreachable; gc = next) {
1062
2.06M
        PyWeakReference **wrlist;
1063
1064
2.06M
        PyObject *op = FROM_GC(gc);
1065
2.06M
        next = GC_NEXT(gc);
1066
1067
2.06M
        if (PyWeakref_Check(op)) {
1068
            /* A weakref inside the unreachable set is always cleared. See
1069
             * the comments above handle_weakref_callbacks() for why these
1070
             * must be cleared.
1071
             */
1072
0
            _PyWeakref_ClearRef((PyWeakReference *)op);
1073
0
        }
1074
1075
2.06M
        if (! _PyType_SUPPORTS_WEAKREFS(Py_TYPE(op))) {
1076
1.05M
            continue;
1077
1.05M
        }
1078
1079
        /* It supports weakrefs.  Does it have any?
1080
         *
1081
         * This is never triggered for static types so we can avoid the
1082
         * (slightly) more costly _PyObject_GET_WEAKREFS_LISTPTR().
1083
         */
1084
1.01M
        wrlist = _PyObject_GET_WEAKREFS_LISTPTR_FROM_OFFSET(op);
1085
1086
        /* `op` may have some weakrefs.  March over the list, clear
1087
         * all the weakrefs.
1088
         */
1089
1.25M
        for (PyWeakReference *wr = *wrlist; wr != NULL; wr = *wrlist) {
1090
            /* _PyWeakref_ClearRef clears the weakref but leaves
1091
             * the callback pointer intact.  Obscure:  it also
1092
             * changes *wrlist.
1093
             */
1094
239k
            _PyObject_ASSERT((PyObject *)wr, wr->wr_object == op);
1095
239k
            _PyWeakref_ClearRef(wr);
1096
239k
            _PyObject_ASSERT((PyObject *)wr, wr->wr_object == Py_None);
1097
239k
        }
1098
1.01M
    }
1099
18.8k
}
1100
1101
static void
1102
debug_cycle(const char *msg, PyObject *op)
1103
0
{
1104
0
    PySys_FormatStderr("gc: %s <%s %p>\n",
1105
0
                       msg, Py_TYPE(op)->tp_name, op);
1106
0
}
1107
1108
/* Handle uncollectable garbage (cycles with tp_del slots, and stuff reachable
1109
 * only from such cycles).
1110
 * If _PyGC_DEBUG_SAVEALL, all objects in finalizers are appended to the module
1111
 * garbage list (a Python list), else only the objects in finalizers with
1112
 * __del__ methods are appended to garbage.  All objects in finalizers are
1113
 * merged into the old list regardless.
1114
 */
1115
static void
1116
handle_legacy_finalizers(PyThreadState *tstate,
1117
                         GCState *gcstate,
1118
                         PyGC_Head *finalizers, PyGC_Head *old)
1119
18.8k
{
1120
18.8k
    assert(!_PyErr_Occurred(tstate));
1121
18.8k
    assert(gcstate->garbage != NULL);
1122
1123
18.8k
    PyGC_Head *gc = GC_NEXT(finalizers);
1124
18.8k
    for (; gc != finalizers; gc = GC_NEXT(gc)) {
1125
0
        PyObject *op = FROM_GC(gc);
1126
1127
0
        if ((gcstate->debug & _PyGC_DEBUG_SAVEALL) || has_legacy_finalizer(op)) {
1128
0
            if (PyList_Append(gcstate->garbage, op) < 0) {
1129
0
                _PyErr_Clear(tstate);
1130
0
                break;
1131
0
            }
1132
0
        }
1133
0
    }
1134
1135
18.8k
    gc_list_merge(finalizers, old);
1136
18.8k
}
1137
1138
/* Run first-time finalizers (if any) on all the objects in collectable.
1139
 * Note that this may remove some (or even all) of the objects from the
1140
 * list, due to refcounts falling to 0.
1141
 */
1142
static void
1143
finalize_garbage(PyThreadState *tstate, PyGC_Head *collectable)
1144
18.8k
{
1145
18.8k
    destructor finalize;
1146
18.8k
    PyGC_Head seen;
1147
1148
    /* While we're going through the loop, `finalize(op)` may cause op, or
1149
     * other objects, to be reclaimed via refcounts falling to zero.  So
1150
     * there's little we can rely on about the structure of the input
1151
     * `collectable` list across iterations.  For safety, we always take the
1152
     * first object in that list and move it to a temporary `seen` list.
1153
     * If objects vanish from the `collectable` and `seen` lists we don't
1154
     * care.
1155
     */
1156
18.8k
    gc_list_init(&seen);
1157
1158
2.08M
    while (!gc_list_is_empty(collectable)) {
1159
2.06M
        PyGC_Head *gc = GC_NEXT(collectable);
1160
2.06M
        PyObject *op = FROM_GC(gc);
1161
2.06M
        gc_list_move(gc, &seen);
1162
2.06M
        if (!_PyGC_FINALIZED(op) &&
1163
2.06M
            (finalize = Py_TYPE(op)->tp_finalize) != NULL)
1164
0
        {
1165
0
            _PyGC_SET_FINALIZED(op);
1166
0
            Py_INCREF(op);
1167
0
            finalize(op);
1168
0
            assert(!_PyErr_Occurred(tstate));
1169
0
            Py_DECREF(op);
1170
0
        }
1171
2.06M
    }
1172
18.8k
    gc_list_merge(&seen, collectable);
1173
18.8k
}
1174
1175
/* Break reference cycles by clearing the containers involved.  This is
1176
 * tricky business as the lists can be changing and we don't know which
1177
 * objects may be freed.  It is possible I screwed something up here.
1178
 */
1179
static void
1180
delete_garbage(PyThreadState *tstate, GCState *gcstate,
1181
               PyGC_Head *collectable, PyGC_Head *old)
1182
18.8k
{
1183
18.8k
    assert(!_PyErr_Occurred(tstate));
1184
1185
811k
    while (!gc_list_is_empty(collectable)) {
1186
792k
        PyGC_Head *gc = GC_NEXT(collectable);
1187
792k
        PyObject *op = FROM_GC(gc);
1188
1189
792k
        _PyObject_ASSERT_WITH_MSG(op, Py_REFCNT(op) > 0,
1190
792k
                                  "refcount is too small");
1191
1192
792k
        if (gcstate->debug & _PyGC_DEBUG_SAVEALL) {
1193
0
            assert(gcstate->garbage != NULL);
1194
0
            if (PyList_Append(gcstate->garbage, op) < 0) {
1195
0
                _PyErr_Clear(tstate);
1196
0
            }
1197
0
        }
1198
792k
        else {
1199
792k
            inquiry clear;
1200
792k
            if ((clear = Py_TYPE(op)->tp_clear) != NULL) {
1201
559k
                Py_INCREF(op);
1202
559k
                (void) clear(op);
1203
559k
                if (_PyErr_Occurred(tstate)) {
1204
0
                    PyErr_FormatUnraisable("Exception ignored in tp_clear of %s",
1205
0
                                           Py_TYPE(op)->tp_name);
1206
0
                }
1207
559k
                Py_DECREF(op);
1208
559k
            }
1209
792k
        }
1210
792k
        if (GC_NEXT(collectable) == gc) {
1211
            /* object is still alive, move it, it may die later */
1212
473k
            gc_clear_collecting(gc);
1213
473k
            gc_list_move(gc, old);
1214
473k
        }
1215
792k
    }
1216
18.8k
}
1217
1218
1219
/* Deduce which objects among "base" are unreachable from outside the list
1220
   and move them to 'unreachable'. The process consist in the following steps:
1221
1222
1. Copy all reference counts to a different field (gc_prev is used to hold
1223
   this copy to save memory).
1224
2. Traverse all objects in "base" and visit all referred objects using
1225
   "tp_traverse" and for every visited object, subtract 1 to the reference
1226
   count (the one that we copied in the previous step). After this step, all
1227
   objects that can be reached directly from outside must have strictly positive
1228
   reference count, while all unreachable objects must have a count of exactly 0.
1229
3. Identify all unreachable objects (the ones with 0 reference count) and move
1230
   them to the "unreachable" list. This step also needs to move back to "base" all
1231
   objects that were initially marked as unreachable but are referred transitively
1232
   by the reachable objects (the ones with strictly positive reference count).
1233
1234
Contracts:
1235
1236
    * The "base" has to be a valid list with no mask set.
1237
1238
    * The "unreachable" list must be uninitialized (this function calls
1239
      gc_list_init over 'unreachable').
1240
1241
IMPORTANT: This function leaves 'unreachable' with the NEXT_MASK_UNREACHABLE
1242
flag set but it does not clear it to skip unnecessary iteration. Before the
1243
flag is cleared (for example, by using 'clear_unreachable_mask' function or
1244
by a call to 'move_legacy_finalizers'), the 'unreachable' list is not a normal
1245
list and we can not use most gc_list_* functions for it. */
1246
static inline Py_ssize_t
1247
37.7k
deduce_unreachable(PyGC_Head *base, PyGC_Head *unreachable) {
1248
37.7k
    validate_list(base, collecting_clear_unreachable_clear);
1249
    /* Using ob_refcnt and gc_refs, calculate which objects in the
1250
     * container set are reachable from outside the set (i.e., have a
1251
     * refcount greater than 0 when all the references within the
1252
     * set are taken into account).
1253
     */
1254
37.7k
    Py_ssize_t candidates = update_refs(base);  // gc_prev is used for gc_refs
1255
37.7k
    subtract_refs(base);
1256
1257
    /* Leave everything reachable from outside base in base, and move
1258
     * everything else (in base) to unreachable.
1259
     *
1260
     * NOTE:  This used to move the reachable objects into a reachable
1261
     * set instead.  But most things usually turn out to be reachable,
1262
     * so it's more efficient to move the unreachable things.  It "sounds slick"
1263
     * to move the unreachable objects, until you think about it - the reason it
1264
     * pays isn't actually obvious.
1265
     *
1266
     * Suppose we create objects A, B, C in that order.  They appear in the young
1267
     * generation in the same order.  If B points to A, and C to B, and C is
1268
     * reachable from outside, then the adjusted refcounts will be 0, 0, and 1
1269
     * respectively.
1270
     *
1271
     * When move_unreachable finds A, A is moved to the unreachable list.  The
1272
     * same for B when it's first encountered.  Then C is traversed, B is moved
1273
     * _back_ to the reachable list.  B is eventually traversed, and then A is
1274
     * moved back to the reachable list.
1275
     *
1276
     * So instead of not moving at all, the reachable objects B and A are moved
1277
     * twice each.  Why is this a win?  A straightforward algorithm to move the
1278
     * reachable objects instead would move A, B, and C once each.
1279
     *
1280
     * The key is that this dance leaves the objects in order C, B, A - it's
1281
     * reversed from the original order.  On all _subsequent_ scans, none of
1282
     * them will move.  Since most objects aren't in cycles, this can save an
1283
     * unbounded number of moves across an unbounded number of later collections.
1284
     * It can cost more only the first time the chain is scanned.
1285
     *
1286
     * Drawback:  move_unreachable is also used to find out what's still trash
1287
     * after finalizers may resurrect objects.  In _that_ case most unreachable
1288
     * objects will remain unreachable, so it would be more efficient to move
1289
     * the reachable objects instead.  But this is a one-time cost, probably not
1290
     * worth complicating the code to speed just a little.
1291
     */
1292
37.7k
    move_unreachable(base, unreachable);  // gc_prev is pointer again
1293
37.7k
    validate_list(base, collecting_clear_unreachable_clear);
1294
37.7k
    validate_list(unreachable, collecting_set_unreachable_set);
1295
37.7k
    return candidates;
1296
37.7k
}
1297
1298
/* Handle objects that may have resurrected after a call to 'finalize_garbage', moving
1299
   them to 'old_generation' and placing the rest on 'still_unreachable'.
1300
1301
   Contracts:
1302
       * After this function 'unreachable' must not be used anymore and 'still_unreachable'
1303
         will contain the objects that did not resurrect.
1304
1305
       * The "still_unreachable" list must be uninitialized (this function calls
1306
         gc_list_init over 'still_unreachable').
1307
1308
IMPORTANT: After a call to this function, the 'still_unreachable' set will have the
1309
PREV_MARK_COLLECTING set, but the objects in this set are going to be removed so
1310
we can skip the expense of clearing the flag to avoid extra iteration. */
1311
static inline void
1312
handle_resurrected_objects(PyGC_Head *unreachable, PyGC_Head* still_unreachable,
1313
                           PyGC_Head *old_generation)
1314
18.8k
{
1315
    // Remove the PREV_MASK_COLLECTING from unreachable
1316
    // to prepare it for a new call to 'deduce_unreachable'
1317
18.8k
    gc_list_clear_collecting(unreachable);
1318
1319
    // After the call to deduce_unreachable, the 'still_unreachable' set will
1320
    // have the PREV_MARK_COLLECTING set, but the objects are going to be
1321
    // removed so we can skip the expense of clearing the flag.
1322
18.8k
    PyGC_Head* resurrected = unreachable;
1323
18.8k
    deduce_unreachable(resurrected, still_unreachable);
1324
18.8k
    clear_unreachable_mask(still_unreachable);
1325
1326
    // Move the resurrected objects to the old generation for future collection.
1327
18.8k
    gc_list_merge(resurrected, old_generation);
1328
18.8k
}
1329
1330
static void
1331
gc_collect_region(PyThreadState *tstate,
1332
                  PyGC_Head *from,
1333
                  PyGC_Head *to,
1334
                  struct gc_collection_stats *stats);
1335
1336
static inline Py_ssize_t
1337
gc_list_set_space(PyGC_Head *list, int space)
1338
20.3k
{
1339
20.3k
    Py_ssize_t size = 0;
1340
20.3k
    PyGC_Head *gc;
1341
68.6M
    for (gc = GC_NEXT(list); gc != list; gc = GC_NEXT(gc)) {
1342
68.6M
        gc_set_old_space(gc, space);
1343
68.6M
        size++;
1344
68.6M
    }
1345
20.3k
    return size;
1346
20.3k
}
1347
1348
/* Making progress in the incremental collector
1349
 * In order to eventually collect all cycles
1350
 * the incremental collector must progress through the old
1351
 * space faster than objects are added to the old space.
1352
 *
1353
 * Each young or incremental collection adds a number of
1354
 * objects, S (for survivors) to the old space, and
1355
 * incremental collectors scan I objects from the old space.
1356
 * I > S must be true. We also want I > S * N to be where
1357
 * N > 1. Higher values of N mean that the old space is
1358
 * scanned more rapidly.
1359
 * The default incremental threshold of 10 translates to
1360
 * N == 1.4 (1 + 4/threshold)
1361
 */
1362
1363
/* Divide by 10, so that the default incremental threshold of 10
1364
 * scans objects at 1% of the heap size */
1365
90.3k
#define SCAN_RATE_DIVISOR 10
1366
1367
static void
1368
add_stats(GCState *gcstate, int gen, struct gc_collection_stats *stats)
1369
90.3k
{
1370
90.3k
    gcstate->generation_stats[gen].duration += stats->duration;
1371
90.3k
    gcstate->generation_stats[gen].collected += stats->collected;
1372
90.3k
    gcstate->generation_stats[gen].uncollectable += stats->uncollectable;
1373
90.3k
    gcstate->generation_stats[gen].candidates += stats->candidates;
1374
90.3k
    gcstate->generation_stats[gen].collections += 1;
1375
90.3k
}
1376
1377
static void
1378
gc_collect_young(PyThreadState *tstate,
1379
                 struct gc_collection_stats *stats)
1380
0
{
1381
0
    GCState *gcstate = &tstate->interp->gc;
1382
0
    validate_spaces(gcstate);
1383
0
    PyGC_Head *young = &gcstate->young.head;
1384
0
    PyGC_Head *visited = &gcstate->old[gcstate->visited_space].head;
1385
0
    untrack_tuples(young);
1386
0
    GC_STAT_ADD(0, collections, 1);
1387
1388
0
    PyGC_Head survivors;
1389
0
    gc_list_init(&survivors);
1390
0
    gc_list_set_space(young, gcstate->visited_space);
1391
0
    gc_collect_region(tstate, young, &survivors, stats);
1392
0
    gc_list_merge(&survivors, visited);
1393
0
    validate_spaces(gcstate);
1394
0
    gcstate->young.count = 0;
1395
0
    gcstate->old[gcstate->visited_space].count++;
1396
0
    validate_spaces(gcstate);
1397
0
}
1398
1399
#ifndef NDEBUG
1400
static inline int
1401
IS_IN_VISITED(PyGC_Head *gc, int visited_space)
1402
{
1403
    assert(visited_space == 0 || other_space(visited_space) == 0);
1404
    return gc_old_space(gc) == visited_space;
1405
}
1406
#endif
1407
1408
struct container_and_flag {
1409
    PyGC_Head *container;
1410
    int visited_space;
1411
    intptr_t size;
1412
};
1413
1414
/* A traversal callback for adding to container) */
1415
static int
1416
visit_add_to_container(PyObject *op, void *arg)
1417
593M
{
1418
593M
    OBJECT_STAT_INC(object_visits);
1419
593M
    struct container_and_flag *cf = (struct container_and_flag *)arg;
1420
593M
    int visited = cf->visited_space;
1421
593M
    assert(visited == get_gc_state()->visited_space);
1422
593M
    if (!_Py_IsImmortal(op) && _PyObject_IS_GC(op)) {
1423
333M
        PyGC_Head *gc = AS_GC(op);
1424
333M
        if (_PyObject_GC_IS_TRACKED(op) &&
1425
269M
            gc_old_space(gc) != visited) {
1426
143M
            gc_flip_old_space(gc);
1427
143M
            gc_list_move(gc, cf->container);
1428
143M
            cf->size++;
1429
143M
        }
1430
333M
    }
1431
593M
    return 0;
1432
593M
}
1433
1434
static intptr_t
1435
expand_region_transitively_reachable(PyGC_Head *container, PyGC_Head *gc, GCState *gcstate)
1436
327k
{
1437
327k
    struct container_and_flag arg = {
1438
327k
        .container = container,
1439
327k
        .visited_space = gcstate->visited_space,
1440
327k
        .size = 0
1441
327k
    };
1442
327k
    assert(GC_NEXT(gc) == container);
1443
1.78M
    while (gc != container) {
1444
        /* Survivors will be moved to visited space, so they should
1445
         * have been marked as visited */
1446
1.45M
        assert(IS_IN_VISITED(gc, gcstate->visited_space));
1447
1.45M
        PyObject *op = FROM_GC(gc);
1448
1.45M
        assert(_PyObject_GC_IS_TRACKED(op));
1449
1.45M
        if (_Py_IsImmortal(op)) {
1450
0
            PyGC_Head *next = GC_NEXT(gc);
1451
0
            gc_list_move(gc, &gcstate->permanent_generation.head);
1452
0
            gc = next;
1453
0
            continue;
1454
0
        }
1455
1.45M
        traverseproc traverse = Py_TYPE(op)->tp_traverse;
1456
1.45M
        (void) traverse(op,
1457
1.45M
                        visit_add_to_container,
1458
1.45M
                        &arg);
1459
1.45M
        gc = GC_NEXT(gc);
1460
1.45M
    }
1461
327k
    return arg.size;
1462
327k
}
1463
1464
/* Do bookkeeping for a completed GC cycle */
1465
static void
1466
completed_scavenge(GCState *gcstate)
1467
1.49k
{
1468
    /* We must observe two invariants:
1469
    * 1. Members of the permanent generation must be marked visited.
1470
    * 2. We cannot touch members of the permanent generation. */
1471
1.49k
    int visited;
1472
1.49k
    if (gc_list_is_empty(&gcstate->permanent_generation.head)) {
1473
        /* Permanent generation is empty so we can flip spaces bit */
1474
1.49k
        int not_visited = gcstate->visited_space;
1475
1.49k
        visited = other_space(not_visited);
1476
1.49k
        gcstate->visited_space = visited;
1477
        /* Make sure all objects have visited bit set correctly */
1478
1.49k
        gc_list_set_space(&gcstate->young.head, not_visited);
1479
1.49k
    }
1480
0
    else {
1481
         /* We must move the objects from visited to pending space. */
1482
0
        visited = gcstate->visited_space;
1483
0
        int not_visited = other_space(visited);
1484
0
        assert(gc_list_is_empty(&gcstate->old[not_visited].head));
1485
0
        gc_list_merge(&gcstate->old[visited].head, &gcstate->old[not_visited].head);
1486
0
        gc_list_set_space(&gcstate->old[not_visited].head, not_visited);
1487
0
    }
1488
1.49k
    assert(gc_list_is_empty(&gcstate->old[visited].head));
1489
1.49k
    gcstate->work_to_do = 0;
1490
1.49k
    gcstate->phase = GC_PHASE_MARK;
1491
1.49k
}
1492
1493
static intptr_t
1494
move_to_reachable(PyObject *op, PyGC_Head *reachable, int visited_space)
1495
2.08M
{
1496
2.08M
    if (op != NULL && !_Py_IsImmortal(op) && _PyObject_IS_GC(op)) {
1497
1.08M
        PyGC_Head *gc = AS_GC(op);
1498
1.08M
        if (_PyObject_GC_IS_TRACKED(op) &&
1499
1.08M
            gc_old_space(gc) != visited_space) {
1500
392k
            gc_flip_old_space(gc);
1501
392k
            gc_list_move(gc, reachable);
1502
392k
            return 1;
1503
392k
        }
1504
1.08M
    }
1505
1.69M
    return 0;
1506
2.08M
}
1507
1508
static intptr_t
1509
mark_all_reachable(PyGC_Head *reachable, PyGC_Head *visited, int visited_space)
1510
21.9k
{
1511
    // Transitively traverse all objects from reachable, until empty
1512
21.9k
    struct container_and_flag arg = {
1513
21.9k
        .container = reachable,
1514
21.9k
        .visited_space = visited_space,
1515
21.9k
        .size = 0
1516
21.9k
    };
1517
144M
    while (!gc_list_is_empty(reachable)) {
1518
144M
        PyGC_Head *gc = _PyGCHead_NEXT(reachable);
1519
144M
        assert(gc_old_space(gc) == visited_space);
1520
144M
        gc_list_move(gc, visited);
1521
144M
        PyObject *op = FROM_GC(gc);
1522
144M
        traverseproc traverse = Py_TYPE(op)->tp_traverse;
1523
144M
        (void) traverse(op,
1524
144M
                        visit_add_to_container,
1525
144M
                        &arg);
1526
144M
    }
1527
21.9k
    gc_list_validate_space(visited, visited_space);
1528
21.9k
    return arg.size;
1529
21.9k
}
1530
1531
static intptr_t
1532
mark_stacks(PyInterpreterState *interp, PyGC_Head *visited, int visited_space, bool start)
1533
20.4k
{
1534
20.4k
    PyGC_Head reachable;
1535
20.4k
    gc_list_init(&reachable);
1536
20.4k
    Py_ssize_t objects_marked = 0;
1537
    // Move all objects on stacks to reachable
1538
20.4k
    _PyRuntimeState *runtime = &_PyRuntime;
1539
20.4k
    HEAD_LOCK(runtime);
1540
20.4k
    PyThreadState* ts = PyInterpreterState_ThreadHead(interp);
1541
20.4k
    HEAD_UNLOCK(runtime);
1542
40.8k
    while (ts) {
1543
20.4k
        _PyInterpreterFrame *frame = ts->current_frame;
1544
886k
        while (frame) {
1545
881k
            if (frame->owner >= FRAME_OWNED_BY_INTERPRETER) {
1546
163k
                frame = frame->previous;
1547
163k
                continue;
1548
163k
            }
1549
718k
            _PyStackRef *locals = frame->localsplus;
1550
718k
            _PyStackRef *sp = frame->stackpointer;
1551
718k
            objects_marked += move_to_reachable(frame->f_locals, &reachable, visited_space);
1552
718k
            PyObject *func = PyStackRef_AsPyObjectBorrow(frame->f_funcobj);
1553
718k
            objects_marked += move_to_reachable(func, &reachable, visited_space);
1554
7.37M
            while (sp > locals) {
1555
6.66M
                sp--;
1556
6.66M
                if (PyStackRef_IsNullOrInt(*sp)) {
1557
2.69M
                    continue;
1558
2.69M
                }
1559
3.96M
                PyObject *op = PyStackRef_AsPyObjectBorrow(*sp);
1560
3.96M
                if (_Py_IsImmortal(op)) {
1561
973k
                    continue;
1562
973k
                }
1563
2.98M
                if (_PyObject_IS_GC(op)) {
1564
2.51M
                    PyGC_Head *gc = AS_GC(op);
1565
2.51M
                    if (_PyObject_GC_IS_TRACKED(op) &&
1566
2.50M
                        gc_old_space(gc) != visited_space) {
1567
1.21M
                        gc_flip_old_space(gc);
1568
1.21M
                        objects_marked++;
1569
1.21M
                        gc_list_move(gc, &reachable);
1570
1.21M
                    }
1571
2.51M
                }
1572
2.98M
            }
1573
718k
            if (!start && frame->visited) {
1574
                // If this frame has already been visited, then the lower frames
1575
                // will have already been visited and will not have changed
1576
16.0k
                break;
1577
16.0k
            }
1578
702k
            frame->visited = 1;
1579
702k
            frame = frame->previous;
1580
702k
        }
1581
20.4k
        HEAD_LOCK(runtime);
1582
20.4k
        ts = PyThreadState_Next(ts);
1583
20.4k
        HEAD_UNLOCK(runtime);
1584
20.4k
    }
1585
20.4k
    objects_marked += mark_all_reachable(&reachable, visited, visited_space);
1586
20.4k
    assert(gc_list_is_empty(&reachable));
1587
20.4k
    return objects_marked;
1588
20.4k
}
1589
1590
static intptr_t
1591
mark_global_roots(PyInterpreterState *interp, PyGC_Head *visited, int visited_space)
1592
1.52k
{
1593
1.52k
    PyGC_Head reachable;
1594
1.52k
    gc_list_init(&reachable);
1595
1.52k
    Py_ssize_t objects_marked = 0;
1596
1.52k
    objects_marked += move_to_reachable(interp->sysdict, &reachable, visited_space);
1597
1.52k
    objects_marked += move_to_reachable(interp->builtins, &reachable, visited_space);
1598
1.52k
    objects_marked += move_to_reachable(interp->dict, &reachable, visited_space);
1599
1.52k
    struct types_state *types = &interp->types;
1600
308k
    for (int i = 0; i < _Py_MAX_MANAGED_STATIC_BUILTIN_TYPES; i++) {
1601
306k
        objects_marked += move_to_reachable(types->builtins.initialized[i].tp_dict, &reachable, visited_space);
1602
306k
        objects_marked += move_to_reachable(types->builtins.initialized[i].tp_subclasses, &reachable, visited_space);
1603
306k
    }
1604
16.7k
    for (int i = 0; i < _Py_MAX_MANAGED_STATIC_EXT_TYPES; i++) {
1605
15.2k
        objects_marked += move_to_reachable(types->for_extensions.initialized[i].tp_dict, &reachable, visited_space);
1606
15.2k
        objects_marked += move_to_reachable(types->for_extensions.initialized[i].tp_subclasses, &reachable, visited_space);
1607
15.2k
    }
1608
1.52k
    objects_marked += mark_all_reachable(&reachable, visited, visited_space);
1609
1.52k
    assert(gc_list_is_empty(&reachable));
1610
1.52k
    return objects_marked;
1611
1.52k
}
1612
1613
static intptr_t
1614
mark_at_start(PyThreadState *tstate)
1615
1.52k
{
1616
    // TO DO -- Make this incremental
1617
1.52k
    GCState *gcstate = &tstate->interp->gc;
1618
1.52k
    PyGC_Head *visited = &gcstate->old[gcstate->visited_space].head;
1619
1.52k
    Py_ssize_t objects_marked = mark_global_roots(tstate->interp, visited, gcstate->visited_space);
1620
1.52k
    objects_marked += mark_stacks(tstate->interp, visited, gcstate->visited_space, true);
1621
1.52k
    gcstate->work_to_do -= objects_marked;
1622
1.52k
    gcstate->phase = GC_PHASE_COLLECT;
1623
1.52k
    validate_spaces(gcstate);
1624
1.52k
    return objects_marked;
1625
1.52k
}
1626
1627
static intptr_t
1628
assess_work_to_do(GCState *gcstate)
1629
90.3k
{
1630
    /* The amount of work we want to do depends on three things.
1631
     * 1. The number of new objects created
1632
     * 2. The growth in heap size since the last collection
1633
     * 3. The heap size (up to the number of new objects, to avoid quadratic effects)
1634
     *
1635
     * For a steady state heap, the amount of work to do is three times the number
1636
     * of new objects added to the heap. This ensures that we stay ahead in the
1637
     * worst case of all new objects being garbage.
1638
     *
1639
     * This could be improved by tracking survival rates, but it is still a
1640
     * large improvement on the non-marking approach.
1641
     */
1642
90.3k
    intptr_t scale_factor = gcstate->old[0].threshold;
1643
90.3k
    if (scale_factor < 2) {
1644
0
        scale_factor = 2;
1645
0
    }
1646
90.3k
    intptr_t new_objects = gcstate->young.count;
1647
90.3k
    intptr_t max_heap_fraction = new_objects*2;
1648
90.3k
    intptr_t heap_fraction = gcstate->heap_size / SCAN_RATE_DIVISOR / scale_factor;
1649
90.3k
    if (heap_fraction > max_heap_fraction) {
1650
11.0k
        heap_fraction = max_heap_fraction;
1651
11.0k
    }
1652
90.3k
    gcstate->young.count = 0;
1653
90.3k
    return new_objects + heap_fraction;
1654
90.3k
}
1655
1656
static void
1657
gc_collect_increment(PyThreadState *tstate, struct gc_collection_stats *stats)
1658
90.3k
{
1659
90.3k
    GC_STAT_ADD(1, collections, 1);
1660
90.3k
    GCState *gcstate = &tstate->interp->gc;
1661
90.3k
    gcstate->work_to_do += assess_work_to_do(gcstate);
1662
90.3k
    if (gcstate->work_to_do < 0) {
1663
69.9k
        return;
1664
69.9k
    }
1665
20.4k
    untrack_tuples(&gcstate->young.head);
1666
20.4k
    if (gcstate->phase == GC_PHASE_MARK) {
1667
1.52k
        Py_ssize_t objects_marked = mark_at_start(tstate);
1668
1.52k
        GC_STAT_ADD(1, objects_transitively_reachable, objects_marked);
1669
1.52k
        gcstate->work_to_do -= objects_marked;
1670
1.52k
        stats->candidates += objects_marked;
1671
1.52k
        validate_spaces(gcstate);
1672
1.52k
        return;
1673
1.52k
    }
1674
18.8k
    PyGC_Head *not_visited = &gcstate->old[gcstate->visited_space^1].head;
1675
18.8k
    PyGC_Head *visited = &gcstate->old[gcstate->visited_space].head;
1676
18.8k
    PyGC_Head increment;
1677
18.8k
    gc_list_init(&increment);
1678
18.8k
    int scale_factor = gcstate->old[0].threshold;
1679
18.8k
    if (scale_factor < 2) {
1680
0
        scale_factor = 2;
1681
0
    }
1682
18.8k
    intptr_t objects_marked = mark_stacks(tstate->interp, visited, gcstate->visited_space, false);
1683
18.8k
    GC_STAT_ADD(1, objects_transitively_reachable, objects_marked);
1684
18.8k
    gcstate->work_to_do -= objects_marked;
1685
18.8k
    gc_list_set_space(&gcstate->young.head, gcstate->visited_space);
1686
18.8k
    gc_list_merge(&gcstate->young.head, &increment);
1687
18.8k
    gc_list_validate_space(&increment, gcstate->visited_space);
1688
18.8k
    Py_ssize_t increment_size = gc_list_size(&increment);
1689
346k
    while (increment_size < gcstate->work_to_do) {
1690
329k
        if (gc_list_is_empty(not_visited)) {
1691
1.46k
            break;
1692
1.46k
        }
1693
327k
        PyGC_Head *gc = _PyGCHead_NEXT(not_visited);
1694
327k
        gc_list_move(gc, &increment);
1695
327k
        increment_size++;
1696
327k
        assert(!_Py_IsImmortal(FROM_GC(gc)));
1697
327k
        gc_set_old_space(gc, gcstate->visited_space);
1698
327k
        increment_size += expand_region_transitively_reachable(&increment, gc, gcstate);
1699
327k
    }
1700
18.8k
    GC_STAT_ADD(1, objects_not_transitively_reachable, increment_size);
1701
18.8k
    validate_list(&increment, collecting_clear_unreachable_clear);
1702
18.8k
    gc_list_validate_space(&increment, gcstate->visited_space);
1703
18.8k
    PyGC_Head survivors;
1704
18.8k
    gc_list_init(&survivors);
1705
18.8k
    gc_collect_region(tstate, &increment, &survivors, stats);
1706
18.8k
    gc_list_merge(&survivors, visited);
1707
18.8k
    assert(gc_list_is_empty(&increment));
1708
18.8k
    gcstate->work_to_do -= increment_size;
1709
1710
18.8k
    if (gc_list_is_empty(not_visited)) {
1711
1.49k
        completed_scavenge(gcstate);
1712
1.49k
    }
1713
18.8k
    validate_spaces(gcstate);
1714
18.8k
}
1715
1716
static void
1717
gc_collect_full(PyThreadState *tstate,
1718
                struct gc_collection_stats *stats)
1719
0
{
1720
0
    GC_STAT_ADD(2, collections, 1);
1721
0
    GCState *gcstate = &tstate->interp->gc;
1722
0
    validate_spaces(gcstate);
1723
0
    PyGC_Head *young = &gcstate->young.head;
1724
0
    PyGC_Head *pending = &gcstate->old[gcstate->visited_space^1].head;
1725
0
    PyGC_Head *visited = &gcstate->old[gcstate->visited_space].head;
1726
0
    untrack_tuples(young);
1727
    /* merge all generations into visited */
1728
0
    gc_list_merge(young, pending);
1729
0
    gc_list_validate_space(pending, 1-gcstate->visited_space);
1730
0
    gc_list_set_space(pending, gcstate->visited_space);
1731
0
    gcstate->young.count = 0;
1732
0
    gc_list_merge(pending, visited);
1733
0
    validate_spaces(gcstate);
1734
1735
0
    gc_collect_region(tstate, visited, visited,
1736
0
                      stats);
1737
0
    validate_spaces(gcstate);
1738
0
    gcstate->young.count = 0;
1739
0
    gcstate->old[0].count = 0;
1740
0
    gcstate->old[1].count = 0;
1741
0
    completed_scavenge(gcstate);
1742
0
    _PyGC_ClearAllFreeLists(tstate->interp);
1743
0
    validate_spaces(gcstate);
1744
0
}
1745
1746
/* This is the main function. Read this to understand how the
1747
 * collection process works. */
1748
static void
1749
gc_collect_region(PyThreadState *tstate,
1750
                  PyGC_Head *from,
1751
                  PyGC_Head *to,
1752
                  struct gc_collection_stats *stats)
1753
18.8k
{
1754
18.8k
    PyGC_Head unreachable; /* non-problematic unreachable trash */
1755
18.8k
    PyGC_Head finalizers;  /* objects with, & reachable from, __del__ */
1756
18.8k
    PyGC_Head *gc; /* initialize to prevent a compiler warning */
1757
18.8k
    GCState *gcstate = &tstate->interp->gc;
1758
1759
18.8k
    assert(gcstate->garbage != NULL);
1760
18.8k
    assert(!_PyErr_Occurred(tstate));
1761
1762
18.8k
    gc_list_init(&unreachable);
1763
18.8k
    stats->candidates = deduce_unreachable(from, &unreachable);
1764
18.8k
    validate_consistent_old_space(from);
1765
18.8k
    untrack_tuples(from);
1766
1767
  /* Move reachable objects to next generation. */
1768
18.8k
    validate_consistent_old_space(to);
1769
18.8k
    if (from != to) {
1770
18.8k
        gc_list_merge(from, to);
1771
18.8k
    }
1772
18.8k
    validate_consistent_old_space(to);
1773
1774
    /* All objects in unreachable are trash, but objects reachable from
1775
     * legacy finalizers (e.g. tp_del) can't safely be deleted.
1776
     */
1777
18.8k
    gc_list_init(&finalizers);
1778
    // NEXT_MASK_UNREACHABLE is cleared here.
1779
    // After move_legacy_finalizers(), unreachable is normal list.
1780
18.8k
    move_legacy_finalizers(&unreachable, &finalizers);
1781
    /* finalizers contains the unreachable objects with a legacy finalizer;
1782
     * unreachable objects reachable *from* those are also uncollectable,
1783
     * and we move those into the finalizers list too.
1784
     */
1785
18.8k
    move_legacy_finalizer_reachable(&finalizers);
1786
18.8k
    validate_list(&finalizers, collecting_clear_unreachable_clear);
1787
18.8k
    validate_list(&unreachable, collecting_set_unreachable_clear);
1788
    /* Print debugging information. */
1789
18.8k
    if (gcstate->debug & _PyGC_DEBUG_COLLECTABLE) {
1790
0
        for (gc = GC_NEXT(&unreachable); gc != &unreachable; gc = GC_NEXT(gc)) {
1791
0
            debug_cycle("collectable", FROM_GC(gc));
1792
0
        }
1793
0
    }
1794
1795
    /* Invoke weakref callbacks as necessary. */
1796
18.8k
    stats->collected += handle_weakref_callbacks(&unreachable, to);
1797
18.8k
    gc_list_validate_space(to, gcstate->visited_space);
1798
18.8k
    validate_list(to, collecting_clear_unreachable_clear);
1799
18.8k
    validate_list(&unreachable, collecting_set_unreachable_clear);
1800
1801
    /* Call tp_finalize on objects which have one. */
1802
18.8k
    finalize_garbage(tstate, &unreachable);
1803
    /* Handle any objects that may have resurrected after the call
1804
     * to 'finalize_garbage' and continue the collection with the
1805
     * objects that are still unreachable */
1806
18.8k
    PyGC_Head final_unreachable;
1807
18.8k
    gc_list_init(&final_unreachable);
1808
18.8k
    handle_resurrected_objects(&unreachable, &final_unreachable, to);
1809
1810
    /* Clear weakrefs to objects in the unreachable set.  See the comments
1811
     * above handle_weakref_callbacks() for details.
1812
     */
1813
18.8k
    clear_weakrefs(&final_unreachable);
1814
1815
    /* Call tp_clear on objects in the final_unreachable set.  This will cause
1816
    * the reference cycles to be broken.  It may also cause some objects
1817
    * in finalizers to be freed.
1818
    */
1819
18.8k
    stats->collected += gc_list_size(&final_unreachable);
1820
18.8k
    delete_garbage(tstate, gcstate, &final_unreachable, to);
1821
1822
    /* Collect statistics on uncollectable objects found and print
1823
     * debugging information. */
1824
18.8k
    Py_ssize_t n = 0;
1825
18.8k
    for (gc = GC_NEXT(&finalizers); gc != &finalizers; gc = GC_NEXT(gc)) {
1826
0
        n++;
1827
0
        if (gcstate->debug & _PyGC_DEBUG_UNCOLLECTABLE)
1828
0
            debug_cycle("uncollectable", FROM_GC(gc));
1829
0
    }
1830
18.8k
    stats->uncollectable = n;
1831
    /* Append instances in the uncollectable set to a Python
1832
     * reachable list of garbage.  The programmer has to deal with
1833
     * this if they insist on creating this type of structure.
1834
     */
1835
18.8k
    handle_legacy_finalizers(tstate, gcstate, &finalizers, to);
1836
18.8k
    gc_list_validate_space(to, gcstate->visited_space);
1837
18.8k
    validate_list(to, collecting_clear_unreachable_clear);
1838
18.8k
}
1839
1840
/* Invoke progress callbacks to notify clients that garbage collection
1841
 * is starting or stopping
1842
 */
1843
static void
1844
do_gc_callback(GCState *gcstate, const char *phase,
1845
                   int generation, struct gc_collection_stats *stats)
1846
180k
{
1847
180k
    assert(!PyErr_Occurred());
1848
1849
    /* The local variable cannot be rebound, check it for sanity */
1850
180k
    assert(PyList_CheckExact(gcstate->callbacks));
1851
180k
    PyObject *info = NULL;
1852
180k
    if (PyList_GET_SIZE(gcstate->callbacks) != 0) {
1853
0
        info = Py_BuildValue("{sisnsnsnsd}",
1854
0
            "generation", generation,
1855
0
            "collected", stats->collected,
1856
0
            "uncollectable", stats->uncollectable,
1857
0
            "candidates", stats->candidates,
1858
0
            "duration", stats->duration);
1859
0
        if (info == NULL) {
1860
0
            PyErr_FormatUnraisable("Exception ignored while invoking gc callbacks");
1861
0
            return;
1862
0
        }
1863
0
    }
1864
1865
180k
    PyObject *phase_obj = PyUnicode_FromString(phase);
1866
180k
    if (phase_obj == NULL) {
1867
0
        Py_XDECREF(info);
1868
0
        PyErr_FormatUnraisable("Exception ignored while invoking gc callbacks");
1869
0
        return;
1870
0
    }
1871
1872
180k
    PyObject *stack[] = {phase_obj, info};
1873
180k
    for (Py_ssize_t i=0; i<PyList_GET_SIZE(gcstate->callbacks); i++) {
1874
0
        PyObject *r, *cb = PyList_GET_ITEM(gcstate->callbacks, i);
1875
0
        Py_INCREF(cb); /* make sure cb doesn't go away */
1876
0
        r = PyObject_Vectorcall(cb, stack, 2, NULL);
1877
0
        if (r == NULL) {
1878
0
            PyErr_FormatUnraisable("Exception ignored while "
1879
0
                                   "calling GC callback %R", cb);
1880
0
        }
1881
0
        else {
1882
0
            Py_DECREF(r);
1883
0
        }
1884
0
        Py_DECREF(cb);
1885
0
    }
1886
180k
    Py_DECREF(phase_obj);
1887
180k
    Py_XDECREF(info);
1888
180k
    assert(!PyErr_Occurred());
1889
180k
}
1890
1891
static void
1892
invoke_gc_callback(GCState *gcstate, const char *phase,
1893
                   int generation, struct gc_collection_stats *stats)
1894
180k
{
1895
180k
    if (gcstate->callbacks == NULL) {
1896
0
        return;
1897
0
    }
1898
180k
    do_gc_callback(gcstate, phase, generation, stats);
1899
180k
}
1900
1901
static int
1902
referrersvisit(PyObject* obj, void *arg)
1903
0
{
1904
0
    PyObject *objs = arg;
1905
0
    Py_ssize_t i;
1906
0
    for (i = 0; i < PyTuple_GET_SIZE(objs); i++) {
1907
0
        if (PyTuple_GET_ITEM(objs, i) == obj) {
1908
0
            return 1;
1909
0
        }
1910
0
    }
1911
0
    return 0;
1912
0
}
1913
1914
static int
1915
gc_referrers_for(PyObject *objs, PyGC_Head *list, PyObject *resultlist)
1916
0
{
1917
0
    PyGC_Head *gc;
1918
0
    PyObject *obj;
1919
0
    traverseproc traverse;
1920
0
    for (gc = GC_NEXT(list); gc != list; gc = GC_NEXT(gc)) {
1921
0
        obj = FROM_GC(gc);
1922
0
        traverse = Py_TYPE(obj)->tp_traverse;
1923
0
        if (obj == objs || obj == resultlist) {
1924
0
            continue;
1925
0
        }
1926
0
        if (traverse(obj, referrersvisit, objs)) {
1927
0
            if (PyList_Append(resultlist, obj) < 0) {
1928
0
                return 0; /* error */
1929
0
            }
1930
0
        }
1931
0
    }
1932
0
    return 1; /* no error */
1933
0
}
1934
1935
PyObject *
1936
_PyGC_GetReferrers(PyInterpreterState *interp, PyObject *objs)
1937
0
{
1938
0
    PyObject *result = PyList_New(0);
1939
0
    if (!result) {
1940
0
        return NULL;
1941
0
    }
1942
1943
0
    GCState *gcstate = &interp->gc;
1944
0
    for (int i = 0; i < NUM_GENERATIONS; i++) {
1945
0
        if (!(gc_referrers_for(objs, GEN_HEAD(gcstate, i), result))) {
1946
0
            Py_DECREF(result);
1947
0
            return NULL;
1948
0
        }
1949
0
    }
1950
0
    return result;
1951
0
}
1952
1953
PyObject *
1954
_PyGC_GetObjects(PyInterpreterState *interp, int generation)
1955
0
{
1956
0
    assert(generation >= -1 && generation < NUM_GENERATIONS);
1957
0
    GCState *gcstate = &interp->gc;
1958
1959
0
    PyObject *result = PyList_New(0);
1960
    /* Generation:
1961
     * -1: Return all objects
1962
     * 0: All young objects
1963
     * 1: No objects
1964
     * 2: All old objects
1965
     */
1966
0
    if (result == NULL || generation == 1) {
1967
0
        return result;
1968
0
    }
1969
0
    if (generation <= 0) {
1970
0
        if (append_objects(result, &gcstate->young.head)) {
1971
0
            goto error;
1972
0
        }
1973
0
    }
1974
0
    if (generation != 0) {
1975
0
        if (append_objects(result, &gcstate->old[0].head)) {
1976
0
            goto error;
1977
0
        }
1978
0
        if (append_objects(result, &gcstate->old[1].head)) {
1979
0
            goto error;
1980
0
        }
1981
0
    }
1982
1983
0
    return result;
1984
0
error:
1985
0
    Py_DECREF(result);
1986
0
    return NULL;
1987
0
}
1988
1989
void
1990
_PyGC_Freeze(PyInterpreterState *interp)
1991
0
{
1992
0
    GCState *gcstate = &interp->gc;
1993
    /* The permanent_generation must be visited */
1994
0
    gc_list_set_space(&gcstate->young.head, gcstate->visited_space);
1995
0
    gc_list_merge(&gcstate->young.head, &gcstate->permanent_generation.head);
1996
0
    gcstate->young.count = 0;
1997
0
    PyGC_Head*old0 = &gcstate->old[0].head;
1998
0
    PyGC_Head*old1 = &gcstate->old[1].head;
1999
0
    if (gcstate->visited_space) {
2000
0
        gc_list_set_space(old0, 1);
2001
0
    }
2002
0
    else {
2003
0
        gc_list_set_space(old1, 0);
2004
0
    }
2005
0
    gc_list_merge(old0, &gcstate->permanent_generation.head);
2006
0
    gcstate->old[0].count = 0;
2007
0
    gc_list_merge(old1, &gcstate->permanent_generation.head);
2008
0
    gcstate->old[1].count = 0;
2009
0
    validate_spaces(gcstate);
2010
0
}
2011
2012
void
2013
_PyGC_Unfreeze(PyInterpreterState *interp)
2014
0
{
2015
0
    GCState *gcstate = &interp->gc;
2016
0
    gc_list_merge(&gcstate->permanent_generation.head,
2017
0
                  &gcstate->old[gcstate->visited_space].head);
2018
0
    validate_spaces(gcstate);
2019
0
}
2020
2021
Py_ssize_t
2022
_PyGC_GetFreezeCount(PyInterpreterState *interp)
2023
0
{
2024
0
    GCState *gcstate = &interp->gc;
2025
0
    return gc_list_size(&gcstate->permanent_generation.head);
2026
0
}
2027
2028
/* C API for controlling the state of the garbage collector */
2029
int
2030
PyGC_Enable(void)
2031
0
{
2032
0
    GCState *gcstate = get_gc_state();
2033
0
    int old_state = gcstate->enabled;
2034
0
    gcstate->enabled = 1;
2035
0
    return old_state;
2036
0
}
2037
2038
int
2039
PyGC_Disable(void)
2040
0
{
2041
0
    GCState *gcstate = get_gc_state();
2042
0
    int old_state = gcstate->enabled;
2043
0
    gcstate->enabled = 0;
2044
0
    return old_state;
2045
0
}
2046
2047
int
2048
PyGC_IsEnabled(void)
2049
0
{
2050
0
    GCState *gcstate = get_gc_state();
2051
0
    return gcstate->enabled;
2052
0
}
2053
2054
// Show stats for objects in each generations
2055
static void
2056
show_stats_each_generations(GCState *gcstate)
2057
0
{
2058
0
    char buf[100];
2059
0
    size_t pos = 0;
2060
2061
0
    for (int i = 0; i < NUM_GENERATIONS && pos < sizeof(buf); i++) {
2062
0
        pos += PyOS_snprintf(buf+pos, sizeof(buf)-pos,
2063
0
                             " %zd",
2064
0
                             gc_list_size(GEN_HEAD(gcstate, i)));
2065
0
    }
2066
0
    PySys_FormatStderr(
2067
0
        "gc: objects in each generation:%s\n"
2068
0
        "gc: objects in permanent generation: %zd\n",
2069
0
        buf, gc_list_size(&gcstate->permanent_generation.head));
2070
0
}
2071
2072
Py_ssize_t
2073
_PyGC_Collect(PyThreadState *tstate, int generation, _PyGC_Reason reason)
2074
90.3k
{
2075
90.3k
    GCState *gcstate = &tstate->interp->gc;
2076
90.3k
    assert(tstate->current_frame == NULL || tstate->current_frame->stackpointer != NULL);
2077
2078
90.3k
    int expected = 0;
2079
90.3k
    if (!_Py_atomic_compare_exchange_int(&gcstate->collecting, &expected, 1)) {
2080
        // Don't start a garbage collection if one is already in progress.
2081
0
        return 0;
2082
0
    }
2083
90.3k
    gcstate->frame = tstate->current_frame;
2084
2085
90.3k
    struct gc_collection_stats stats = { 0 };
2086
90.3k
    if (reason != _Py_GC_REASON_SHUTDOWN) {
2087
90.3k
        invoke_gc_callback(gcstate, "start", generation, &stats);
2088
90.3k
    }
2089
90.3k
    if (gcstate->debug & _PyGC_DEBUG_STATS) {
2090
0
        PySys_WriteStderr("gc: collecting generation %d...\n", generation);
2091
0
        show_stats_each_generations(gcstate);
2092
0
    }
2093
90.3k
    if (PyDTrace_GC_START_ENABLED()) {
2094
0
        PyDTrace_GC_START(generation);
2095
0
    }
2096
90.3k
    PyTime_t start, stop;
2097
90.3k
    (void)PyTime_PerfCounterRaw(&start);
2098
90.3k
    PyObject *exc = _PyErr_GetRaisedException(tstate);
2099
90.3k
    switch(generation) {
2100
0
        case 0:
2101
0
            gc_collect_young(tstate, &stats);
2102
0
            break;
2103
90.3k
        case 1:
2104
90.3k
            gc_collect_increment(tstate, &stats);
2105
90.3k
            break;
2106
0
        case 2:
2107
0
            gc_collect_full(tstate, &stats);
2108
0
            break;
2109
0
        default:
2110
0
            Py_UNREACHABLE();
2111
90.3k
    }
2112
90.3k
    (void)PyTime_PerfCounterRaw(&stop);
2113
90.3k
    stats.duration = PyTime_AsSecondsDouble(stop - start);
2114
90.3k
    add_stats(gcstate, generation, &stats);
2115
90.3k
    if (PyDTrace_GC_DONE_ENABLED()) {
2116
0
        PyDTrace_GC_DONE(stats.uncollectable + stats.collected);
2117
0
    }
2118
90.3k
    if (reason != _Py_GC_REASON_SHUTDOWN) {
2119
90.3k
        invoke_gc_callback(gcstate, "stop", generation, &stats);
2120
90.3k
    }
2121
90.3k
    _PyErr_SetRaisedException(tstate, exc);
2122
90.3k
    GC_STAT_ADD(generation, objects_collected, stats.collected);
2123
#ifdef Py_STATS
2124
    PyStats *s = _PyStats_GET();
2125
    if (s) {
2126
        GC_STAT_ADD(generation, object_visits,
2127
            s->object_stats.object_visits);
2128
        s->object_stats.object_visits = 0;
2129
    }
2130
#endif
2131
90.3k
    validate_spaces(gcstate);
2132
90.3k
    gcstate->frame = NULL;
2133
90.3k
    _Py_atomic_store_int(&gcstate->collecting, 0);
2134
2135
90.3k
    if (gcstate->debug & _PyGC_DEBUG_STATS) {
2136
0
        PySys_WriteStderr(
2137
0
            "gc: done, %zd unreachable, %zd uncollectable, %.4fs elapsed\n",
2138
0
            stats.collected + stats.uncollectable, stats.uncollectable, stats.duration
2139
0
        );
2140
0
    }
2141
2142
90.3k
    return stats.uncollectable + stats.collected;
2143
90.3k
}
2144
2145
/* Public API to invoke gc.collect() from C */
2146
Py_ssize_t
2147
PyGC_Collect(void)
2148
0
{
2149
0
    return _PyGC_Collect(_PyThreadState_GET(), 2, _Py_GC_REASON_MANUAL);
2150
0
}
2151
2152
void
2153
_PyGC_CollectNoFail(PyThreadState *tstate)
2154
0
{
2155
    /* Ideally, this function is only called on interpreter shutdown,
2156
       and therefore not recursively.  Unfortunately, when there are daemon
2157
       threads, a daemon thread can start a cyclic garbage collection
2158
       during interpreter shutdown (and then never finish it).
2159
       See http://bugs.python.org/issue8713#msg195178 for an example.
2160
       */
2161
0
    _PyGC_Collect(_PyThreadState_GET(), 2, _Py_GC_REASON_SHUTDOWN);
2162
0
}
2163
2164
void
2165
_PyGC_DumpShutdownStats(PyInterpreterState *interp)
2166
0
{
2167
0
    GCState *gcstate = &interp->gc;
2168
0
    if (!(gcstate->debug & _PyGC_DEBUG_SAVEALL)
2169
0
        && gcstate->garbage != NULL && PyList_GET_SIZE(gcstate->garbage) > 0) {
2170
0
        const char *message;
2171
0
        if (gcstate->debug & _PyGC_DEBUG_UNCOLLECTABLE) {
2172
0
            message = "gc: %zd uncollectable objects at shutdown";
2173
0
        }
2174
0
        else {
2175
0
            message = "gc: %zd uncollectable objects at shutdown; " \
2176
0
                "use gc.set_debug(gc.DEBUG_UNCOLLECTABLE) to list them";
2177
0
        }
2178
        /* PyErr_WarnFormat does too many things and we are at shutdown,
2179
           the warnings module's dependencies (e.g. linecache) may be gone
2180
           already. */
2181
0
        if (PyErr_WarnExplicitFormat(PyExc_ResourceWarning, "gc", 0,
2182
0
                                     "gc", NULL, message,
2183
0
                                     PyList_GET_SIZE(gcstate->garbage)))
2184
0
        {
2185
0
            PyErr_FormatUnraisable("Exception ignored in GC shutdown");
2186
0
        }
2187
0
        if (gcstate->debug & _PyGC_DEBUG_UNCOLLECTABLE) {
2188
0
            PyObject *repr = NULL, *bytes = NULL;
2189
0
            repr = PyObject_Repr(gcstate->garbage);
2190
0
            if (!repr || !(bytes = PyUnicode_EncodeFSDefault(repr))) {
2191
0
                PyErr_FormatUnraisable("Exception ignored in GC shutdown "
2192
0
                                       "while formatting garbage");
2193
0
            }
2194
0
            else {
2195
0
                PySys_WriteStderr(
2196
0
                    "      %s\n",
2197
0
                    PyBytes_AS_STRING(bytes)
2198
0
                    );
2199
0
            }
2200
0
            Py_XDECREF(repr);
2201
0
            Py_XDECREF(bytes);
2202
0
        }
2203
0
    }
2204
0
}
2205
2206
static void
2207
0
finalize_unlink_gc_head(PyGC_Head *gc) {
2208
0
    PyGC_Head *prev = GC_PREV(gc);
2209
0
    PyGC_Head *next = GC_NEXT(gc);
2210
0
    _PyGCHead_SET_NEXT(prev, next);
2211
0
    _PyGCHead_SET_PREV(next, prev);
2212
0
}
2213
2214
void
2215
_PyGC_Fini(PyInterpreterState *interp)
2216
0
{
2217
0
    GCState *gcstate = &interp->gc;
2218
0
    Py_CLEAR(gcstate->garbage);
2219
0
    Py_CLEAR(gcstate->callbacks);
2220
2221
    /* Prevent a subtle bug that affects sub-interpreters that use basic
2222
     * single-phase init extensions (m_size == -1).  Those extensions cause objects
2223
     * to be shared between interpreters, via the PyDict_Update(mdict, m_copy) call
2224
     * in import_find_extension().
2225
     *
2226
     * If they are GC objects, their GC head next or prev links could refer to
2227
     * the interpreter _gc_runtime_state PyGC_Head nodes.  Those nodes go away
2228
     * when the interpreter structure is freed and so pointers to them become
2229
     * invalid.  If those objects are still used by another interpreter and
2230
     * UNTRACK is called on them, a crash will happen.  We untrack the nodes
2231
     * here to avoid that.
2232
     *
2233
     * This bug was originally fixed when reported as gh-90228.  The bug was
2234
     * re-introduced in gh-94673.
2235
     */
2236
0
    finalize_unlink_gc_head(&gcstate->young.head);
2237
0
    finalize_unlink_gc_head(&gcstate->old[0].head);
2238
0
    finalize_unlink_gc_head(&gcstate->old[1].head);
2239
0
    finalize_unlink_gc_head(&gcstate->permanent_generation.head);
2240
0
}
2241
2242
/* for debugging */
2243
void
2244
_PyGC_Dump(PyGC_Head *g)
2245
0
{
2246
0
    PyObject_Dump(FROM_GC(g));
2247
0
}
2248
2249
2250
#ifdef Py_DEBUG
2251
static int
2252
visit_validate(PyObject *op, void *parent_raw)
2253
{
2254
    PyObject *parent = _PyObject_CAST(parent_raw);
2255
    if (_PyObject_IsFreed(op)) {
2256
        _PyObject_ASSERT_FAILED_MSG(parent,
2257
                                    "PyObject_GC_Track() object is not valid");
2258
    }
2259
    return 0;
2260
}
2261
#endif
2262
2263
2264
/* extension modules might be compiled with GC support so these
2265
   functions must always be available */
2266
2267
void
2268
PyObject_GC_Track(void *op_raw)
2269
154M
{
2270
154M
    PyObject *op = _PyObject_CAST(op_raw);
2271
154M
    if (_PyObject_GC_IS_TRACKED(op)) {
2272
0
        _PyObject_ASSERT_FAILED_MSG(op,
2273
0
                                    "object already tracked "
2274
0
                                    "by the garbage collector");
2275
0
    }
2276
154M
    _PyObject_GC_TRACK(op);
2277
2278
#ifdef Py_DEBUG
2279
    /* Check that the object is valid: validate objects traversed
2280
       by tp_traverse() */
2281
    traverseproc traverse = Py_TYPE(op)->tp_traverse;
2282
    (void)traverse(op, visit_validate, op);
2283
#endif
2284
154M
}
2285
2286
void
2287
PyObject_GC_UnTrack(void *op_raw)
2288
1.80G
{
2289
1.80G
    PyObject *op = _PyObject_CAST(op_raw);
2290
    /* The code for some objects, such as tuples, is a bit
2291
     * sloppy about when the object is tracked and untracked. */
2292
1.80G
    if (_PyObject_GC_IS_TRACKED(op)) {
2293
1.58G
        _PyObject_GC_UNTRACK(op);
2294
1.58G
    }
2295
1.80G
}
2296
2297
int
2298
PyObject_IS_GC(PyObject *obj)
2299
1.16G
{
2300
1.16G
    return _PyObject_IS_GC(obj);
2301
1.16G
}
2302
2303
void
2304
_Py_ScheduleGC(PyThreadState *tstate)
2305
18.6M
{
2306
18.6M
    if (!_Py_eval_breaker_bit_is_set(tstate, _PY_GC_SCHEDULED_BIT))
2307
90.3k
    {
2308
90.3k
        _Py_set_eval_breaker_bit(tstate, _PY_GC_SCHEDULED_BIT);
2309
90.3k
    }
2310
18.6M
}
2311
2312
void
2313
_Py_TriggerGC(struct _gc_runtime_state *gcstate)
2314
18.7M
{
2315
18.7M
    PyThreadState *tstate = _PyThreadState_GET();
2316
18.7M
    if (gcstate->enabled &&
2317
18.7M
        gcstate->young.threshold != 0 &&
2318
18.7M
        !_Py_atomic_load_int_relaxed(&gcstate->collecting) &&
2319
18.7M
        !_PyErr_Occurred(tstate))
2320
18.6M
    {
2321
18.6M
        _Py_ScheduleGC(tstate);
2322
18.6M
    }
2323
18.7M
}
2324
2325
void
2326
_PyObject_GC_Link(PyObject *op)
2327
567M
{
2328
567M
    PyGC_Head *gc = AS_GC(op);
2329
    // gc must be correctly aligned
2330
567M
    _PyObject_ASSERT(op, ((uintptr_t)gc & (sizeof(uintptr_t)-1)) == 0);
2331
567M
    gc->_gc_next = 0;
2332
567M
    gc->_gc_prev = 0;
2333
2334
567M
}
2335
2336
void
2337
_Py_RunGC(PyThreadState *tstate)
2338
90.3k
{
2339
90.3k
    if (tstate->interp->gc.enabled) {
2340
90.3k
        _PyGC_Collect(tstate, 1, _Py_GC_REASON_HEAP);
2341
90.3k
    }
2342
90.3k
}
2343
2344
static PyObject *
2345
gc_alloc(PyTypeObject *tp, size_t basicsize, size_t presize)
2346
450M
{
2347
450M
    PyThreadState *tstate = _PyThreadState_GET();
2348
450M
    if (basicsize > PY_SSIZE_T_MAX - presize) {
2349
0
        return _PyErr_NoMemory(tstate);
2350
0
    }
2351
450M
    size_t size = presize + basicsize;
2352
450M
    char *mem = _PyObject_MallocWithType(tp, size);
2353
450M
    if (mem == NULL) {
2354
0
        return _PyErr_NoMemory(tstate);
2355
0
    }
2356
450M
    ((PyObject **)mem)[0] = NULL;
2357
450M
    ((PyObject **)mem)[1] = NULL;
2358
450M
    PyObject *op = (PyObject *)(mem + presize);
2359
450M
    _PyObject_GC_Link(op);
2360
450M
    return op;
2361
450M
}
2362
2363
2364
PyObject *
2365
_PyObject_GC_New(PyTypeObject *tp)
2366
209M
{
2367
209M
    size_t presize = _PyType_PreHeaderSize(tp);
2368
209M
    size_t size = _PyObject_SIZE(tp);
2369
209M
    if (_PyType_HasFeature(tp, Py_TPFLAGS_INLINE_VALUES)) {
2370
134
        size += _PyInlineValuesSize(tp);
2371
134
    }
2372
209M
    PyObject *op = gc_alloc(tp, size, presize);
2373
209M
    if (op == NULL) {
2374
0
        return NULL;
2375
0
    }
2376
209M
    _PyObject_Init(op, tp);
2377
209M
    if (tp->tp_flags & Py_TPFLAGS_INLINE_VALUES) {
2378
134
        _PyObject_InitInlineValues(op, tp);
2379
134
    }
2380
209M
    return op;
2381
209M
}
2382
2383
PyVarObject *
2384
_PyObject_GC_NewVar(PyTypeObject *tp, Py_ssize_t nitems)
2385
240M
{
2386
240M
    PyVarObject *op;
2387
2388
240M
    if (nitems < 0) {
2389
0
        PyErr_BadInternalCall();
2390
0
        return NULL;
2391
0
    }
2392
240M
    size_t presize = _PyType_PreHeaderSize(tp);
2393
240M
    size_t size = _PyObject_VAR_SIZE(tp, nitems);
2394
240M
    op = (PyVarObject *)gc_alloc(tp, size, presize);
2395
240M
    if (op == NULL) {
2396
0
        return NULL;
2397
0
    }
2398
240M
    _PyObject_InitVar(op, tp, nitems);
2399
240M
    return op;
2400
240M
}
2401
2402
PyObject *
2403
PyUnstable_Object_GC_NewWithExtraData(PyTypeObject *tp, size_t extra_size)
2404
0
{
2405
0
    size_t presize = _PyType_PreHeaderSize(tp);
2406
0
    size_t size = _PyObject_SIZE(tp) + extra_size;
2407
0
    PyObject *op = gc_alloc(tp, size, presize);
2408
0
    if (op == NULL) {
2409
0
        return NULL;
2410
0
    }
2411
0
    memset((char *)op + sizeof(PyObject), 0, size - sizeof(PyObject));
2412
0
    _PyObject_Init(op, tp);
2413
0
    return op;
2414
0
}
2415
2416
PyVarObject *
2417
_PyObject_GC_Resize(PyVarObject *op, Py_ssize_t nitems)
2418
34
{
2419
34
    const size_t basicsize = _PyObject_VAR_SIZE(Py_TYPE(op), nitems);
2420
34
    const size_t presize = _PyType_PreHeaderSize(Py_TYPE(op));
2421
34
    _PyObject_ASSERT((PyObject *)op, !_PyObject_GC_IS_TRACKED(op));
2422
34
    if (basicsize > (size_t)PY_SSIZE_T_MAX - presize) {
2423
0
        return (PyVarObject *)PyErr_NoMemory();
2424
0
    }
2425
34
    char *mem = (char *)op - presize;
2426
34
    mem = (char *)_PyObject_ReallocWithType(Py_TYPE(op), mem, presize + basicsize);
2427
34
    if (mem == NULL) {
2428
0
        return (PyVarObject *)PyErr_NoMemory();
2429
0
    }
2430
34
    op = (PyVarObject *) (mem + presize);
2431
34
    Py_SET_SIZE(op, nitems);
2432
34
    return op;
2433
34
}
2434
2435
void
2436
PyObject_GC_Del(void *op)
2437
566M
{
2438
566M
    size_t presize = _PyType_PreHeaderSize(Py_TYPE(op));
2439
566M
    PyGC_Head *g = AS_GC(op);
2440
566M
    if (_PyObject_GC_IS_TRACKED(op)) {
2441
0
        gc_list_remove(g);
2442
0
        GCState *gcstate = get_gc_state();
2443
0
        if (gcstate->young.count > 0) {
2444
0
            gcstate->young.count--;
2445
0
        }
2446
0
        gcstate->heap_size--;
2447
#ifdef Py_DEBUG
2448
        PyObject *exc = PyErr_GetRaisedException();
2449
        if (PyErr_WarnExplicitFormat(PyExc_ResourceWarning, "gc", 0,
2450
                                     "gc", NULL,
2451
                                     "Object of type %s is not untracked "
2452
                                     "before destruction",
2453
                                     Py_TYPE(op)->tp_name))
2454
        {
2455
            PyErr_FormatUnraisable("Exception ignored on object deallocation");
2456
        }
2457
        PyErr_SetRaisedException(exc);
2458
#endif
2459
0
    }
2460
566M
    PyObject_Free(((char *)op)-presize);
2461
566M
}
2462
2463
int
2464
PyObject_GC_IsTracked(PyObject* obj)
2465
7.55k
{
2466
7.55k
    if (_PyObject_IS_GC(obj) && _PyObject_GC_IS_TRACKED(obj)) {
2467
32
        return 1;
2468
32
    }
2469
7.52k
    return 0;
2470
7.55k
}
2471
2472
int
2473
PyObject_GC_IsFinalized(PyObject *obj)
2474
0
{
2475
0
    if (_PyObject_IS_GC(obj) && _PyGC_FINALIZED(obj)) {
2476
0
         return 1;
2477
0
    }
2478
0
    return 0;
2479
0
}
2480
2481
static int
2482
visit_generation(gcvisitobjects_t callback, void *arg, struct gc_generation *gen)
2483
0
{
2484
0
    PyGC_Head *gc_list, *gc;
2485
0
    gc_list = &gen->head;
2486
0
    for (gc = GC_NEXT(gc_list); gc != gc_list; gc = GC_NEXT(gc)) {
2487
0
        PyObject *op = FROM_GC(gc);
2488
0
        Py_INCREF(op);
2489
0
        int res = callback(op, arg);
2490
0
        Py_DECREF(op);
2491
0
        if (!res) {
2492
0
            return -1;
2493
0
        }
2494
0
    }
2495
0
    return 0;
2496
0
}
2497
2498
void
2499
PyUnstable_GC_VisitObjects(gcvisitobjects_t callback, void *arg)
2500
0
{
2501
0
    GCState *gcstate = get_gc_state();
2502
0
    int original_state = gcstate->enabled;
2503
0
    gcstate->enabled = 0;
2504
0
    if (visit_generation(callback, arg, &gcstate->young) < 0) {
2505
0
        goto done;
2506
0
    }
2507
0
    if (visit_generation(callback, arg, &gcstate->old[0]) < 0) {
2508
0
        goto done;
2509
0
    }
2510
0
    if (visit_generation(callback, arg, &gcstate->old[1]) < 0) {
2511
0
        goto done;
2512
0
    }
2513
0
    visit_generation(callback, arg, &gcstate->permanent_generation);
2514
0
done:
2515
0
    gcstate->enabled = original_state;
2516
0
}
2517
2518
#endif  // Py_GIL_DISABLED