Coverage Report

Created: 2025-07-04 06:49

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