Coverage Report

Created: 2026-05-16 06:46

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/cpython/Objects/codeobject.c
Line
Count
Source
1
#include "Python.h"
2
#include "opcode.h"
3
4
#include "pycore_code.h"          // _PyCodeConstructor
5
#include "pycore_function.h"      // _PyFunction_ClearCodeByVersion()
6
#include "pycore_hashtable.h"     // _Py_hashtable_t
7
#include "pycore_index_pool.h"    // _PyIndexPool_Fini()
8
#include "pycore_initconfig.h"    // _PyStatus_OK()
9
#include "pycore_interp.h"        // PyInterpreterState.co_extra_freefuncs
10
#include "pycore_interpframe.h"   // FRAME_SPECIALS_SIZE
11
#include "pycore_opcode_metadata.h" // _PyOpcode_Caches
12
#include "pycore_opcode_utils.h"  // RESUME_AT_FUNC_START
13
#include "pycore_optimizer.h"     // _Py_ExecutorDetach
14
#include "pycore_pymem.h"         // _PyMem_FreeDelayed()
15
#include "pycore_pystate.h"       // _PyInterpreterState_GET()
16
#include "pycore_setobject.h"     // _PySet_NextEntry()
17
#include "pycore_tuple.h"         // _PyTuple_ITEMS()
18
#include "pycore_unicodeobject.h" // _PyUnicode_InternImmortal()
19
#include "pycore_uniqueid.h"      // _PyObject_AssignUniqueId()
20
#include "pycore_weakref.h"       // FT_CLEAR_WEAKREFS()
21
22
#include "clinic/codeobject.c.h"
23
#include <stdbool.h>
24
25
26
#define INITIAL_SPECIALIZED_CODE_SIZE 16
27
28
static const char *
29
0
code_event_name(PyCodeEvent event) {
30
0
    switch (event) {
31
0
        #define CASE(op)                \
32
0
        case PY_CODE_EVENT_##op:         \
33
0
            return "PY_CODE_EVENT_" #op;
34
0
        PY_FOREACH_CODE_EVENT(CASE)
35
0
        #undef CASE
36
0
    }
37
0
    Py_UNREACHABLE();
38
0
}
39
40
static void
41
notify_code_watchers(PyCodeEvent event, PyCodeObject *co)
42
287k
{
43
287k
    assert(Py_REFCNT(co) > 0);
44
287k
    PyInterpreterState *interp = _PyInterpreterState_GET();
45
287k
    assert(interp->_initialized);
46
287k
    uint8_t bits = interp->active_code_watchers;
47
287k
    int i = 0;
48
287k
    while (bits) {
49
0
        assert(i < CODE_MAX_WATCHERS);
50
0
        if (bits & 1) {
51
0
            PyCode_WatchCallback cb = interp->code_watchers[i];
52
            // callback must be non-null if the watcher bit is set
53
0
            assert(cb != NULL);
54
0
            if (cb(event, co) < 0) {
55
0
                PyErr_FormatUnraisable(
56
0
                    "Exception ignored in %s watcher callback for %R",
57
0
                    code_event_name(event), co);
58
0
            }
59
0
        }
60
0
        i++;
61
0
        bits >>= 1;
62
0
    }
63
287k
}
64
65
int
66
PyCode_AddWatcher(PyCode_WatchCallback callback)
67
0
{
68
0
    PyInterpreterState *interp = _PyInterpreterState_GET();
69
0
    assert(interp->_initialized);
70
71
0
    for (int i = 0; i < CODE_MAX_WATCHERS; i++) {
72
0
        if (!interp->code_watchers[i]) {
73
0
            interp->code_watchers[i] = callback;
74
0
            interp->active_code_watchers |= (1 << i);
75
0
            return i;
76
0
        }
77
0
    }
78
79
0
    PyErr_SetString(PyExc_RuntimeError, "no more code watcher IDs available");
80
0
    return -1;
81
0
}
82
83
static inline int
84
validate_watcher_id(PyInterpreterState *interp, int watcher_id)
85
0
{
86
0
    if (watcher_id < 0 || watcher_id >= CODE_MAX_WATCHERS) {
87
0
        PyErr_Format(PyExc_ValueError, "Invalid code watcher ID %d", watcher_id);
88
0
        return -1;
89
0
    }
90
0
    if (!interp->code_watchers[watcher_id]) {
91
0
        PyErr_Format(PyExc_ValueError, "No code watcher set for ID %d", watcher_id);
92
0
        return -1;
93
0
    }
94
0
    return 0;
95
0
}
96
97
int
98
PyCode_ClearWatcher(int watcher_id)
99
0
{
100
0
    PyInterpreterState *interp = _PyInterpreterState_GET();
101
0
    assert(interp->_initialized);
102
0
    if (validate_watcher_id(interp, watcher_id) < 0) {
103
0
        return -1;
104
0
    }
105
0
    interp->code_watchers[watcher_id] = NULL;
106
0
    interp->active_code_watchers &= ~(1 << watcher_id);
107
0
    return 0;
108
0
}
109
110
/******************
111
 * generic helpers
112
 ******************/
113
114
448k
#define _PyCodeObject_CAST(op)  (assert(PyCode_Check(op)), (PyCodeObject *)(op))
115
116
static int
117
should_intern_string(PyObject *o)
118
637k
{
119
#ifdef Py_GIL_DISABLED
120
    // The free-threaded build interns (and immortalizes) all string constants
121
    return 1;
122
#else
123
    // compute if s matches [a-zA-Z0-9_]
124
637k
    const unsigned char *s, *e;
125
126
637k
    if (!PyUnicode_IS_ASCII(o))
127
3.65k
        return 0;
128
129
633k
    s = PyUnicode_1BYTE_DATA(o);
130
633k
    e = s + PyUnicode_GET_LENGTH(o);
131
6.06M
    for (; s != e; s++) {
132
5.65M
        if (!Py_ISALNUM(*s) && *s != '_')
133
227k
            return 0;
134
5.65M
    }
135
405k
    return 1;
136
633k
#endif
137
633k
}
138
139
#ifdef Py_GIL_DISABLED
140
static PyObject *intern_one_constant(PyObject *op);
141
142
// gh-130851: In the free threading build, we intern and immortalize most
143
// constants, except code objects. However, users can generate code objects
144
// with arbitrary co_consts. We don't want to immortalize or intern unexpected
145
// constants or tuples/sets containing unexpected constants.
146
static int
147
should_immortalize_constant(PyObject *v)
148
{
149
    // Only immortalize containers if we've already immortalized all their
150
    // elements.
151
    if (PyTuple_CheckExact(v)) {
152
        for (Py_ssize_t i = PyTuple_GET_SIZE(v); --i >= 0; ) {
153
            if (!_Py_IsImmortal(PyTuple_GET_ITEM(v, i))) {
154
                return 0;
155
            }
156
        }
157
        return 1;
158
    }
159
    else if (PyFrozenSet_CheckExact(v)) {
160
        PyObject *item;
161
        Py_hash_t hash;
162
        Py_ssize_t pos = 0;
163
        while (_PySet_NextEntry(v, &pos, &item, &hash)) {
164
            if (!_Py_IsImmortal(item)) {
165
                return 0;
166
            }
167
        }
168
        return 1;
169
    }
170
    else if (PySlice_Check(v)) {
171
        PySliceObject *slice = (PySliceObject *)v;
172
        return (_Py_IsImmortal(slice->start) &&
173
                _Py_IsImmortal(slice->stop) &&
174
                _Py_IsImmortal(slice->step));
175
    }
176
    return (PyLong_CheckExact(v) || PyFloat_CheckExact(v) ||
177
            PyComplex_Check(v) || PyBytes_CheckExact(v));
178
}
179
#endif
180
181
static int
182
intern_strings(PyObject *tuple)
183
375k
{
184
375k
    PyInterpreterState *interp = _PyInterpreterState_GET();
185
375k
    Py_ssize_t i;
186
187
2.32M
    for (i = PyTuple_GET_SIZE(tuple); --i >= 0; ) {
188
1.94M
        PyObject *v = PyTuple_GET_ITEM(tuple, i);
189
1.94M
        if (v == NULL || !PyUnicode_CheckExact(v)) {
190
0
            PyErr_SetString(PyExc_SystemError,
191
0
                            "non-string found in code slot");
192
0
            return -1;
193
0
        }
194
1.94M
        _PyUnicode_InternImmortal(interp, &_PyTuple_ITEMS(tuple)[i]);
195
1.94M
    }
196
375k
    return 0;
197
375k
}
198
199
/* Intern constants. In the default build, this interns selected string
200
   constants. In the free-threaded build, this also interns non-string
201
   constants. */
202
static int
203
intern_constants(PyObject *tuple, int *modified)
204
302k
{
205
302k
    PyInterpreterState *interp = _PyInterpreterState_GET();
206
1.36M
    for (Py_ssize_t i = PyTuple_GET_SIZE(tuple); --i >= 0; ) {
207
1.06M
        PyObject *v = PyTuple_GET_ITEM(tuple, i);
208
1.06M
        if (PyUnicode_CheckExact(v)) {
209
637k
            if (should_intern_string(v)) {
210
405k
                PyObject *w = v;
211
405k
                _PyUnicode_InternMortal(interp, &v);
212
405k
                if (w != v) {
213
2.18k
                    PyTuple_SET_ITEM(tuple, i, v);
214
2.18k
                    if (modified) {
215
548
                        *modified = 1;
216
548
                    }
217
2.18k
                }
218
405k
            }
219
637k
        }
220
428k
        else if (PyTuple_CheckExact(v)) {
221
114k
            if (intern_constants(v, NULL) < 0) {
222
0
                return -1;
223
0
            }
224
114k
        }
225
314k
        else if (PyFrozenSet_CheckExact(v)) {
226
644
            PyObject *w = v;
227
644
            PyObject *tmp = PySequence_Tuple(v);
228
644
            if (tmp == NULL) {
229
0
                return -1;
230
0
            }
231
644
            int tmp_modified = 0;
232
644
            if (intern_constants(tmp, &tmp_modified) < 0) {
233
0
                Py_DECREF(tmp);
234
0
                return -1;
235
0
            }
236
644
            if (tmp_modified) {
237
107
                v = PyFrozenSet_New(tmp);
238
107
                if (v == NULL) {
239
0
                    Py_DECREF(tmp);
240
0
                    return -1;
241
0
                }
242
243
107
                PyTuple_SET_ITEM(tuple, i, v);
244
107
                Py_DECREF(w);
245
107
                if (modified) {
246
0
                    *modified = 1;
247
0
                }
248
107
            }
249
644
            Py_DECREF(tmp);
250
644
        }
251
#ifdef Py_GIL_DISABLED
252
        else if (PySlice_Check(v)) {
253
            PySliceObject *slice = (PySliceObject *)v;
254
            PyObject *tmp = PyTuple_New(3);
255
            if (tmp == NULL) {
256
                return -1;
257
            }
258
            PyTuple_SET_ITEM(tmp, 0, Py_NewRef(slice->start));
259
            PyTuple_SET_ITEM(tmp, 1, Py_NewRef(slice->stop));
260
            PyTuple_SET_ITEM(tmp, 2, Py_NewRef(slice->step));
261
            int tmp_modified = 0;
262
            if (intern_constants(tmp, &tmp_modified) < 0) {
263
                Py_DECREF(tmp);
264
                return -1;
265
            }
266
            if (tmp_modified) {
267
                v = PySlice_New(PyTuple_GET_ITEM(tmp, 0),
268
                                PyTuple_GET_ITEM(tmp, 1),
269
                                PyTuple_GET_ITEM(tmp, 2));
270
                if (v == NULL) {
271
                    Py_DECREF(tmp);
272
                    return -1;
273
                }
274
                PyTuple_SET_ITEM(tuple, i, v);
275
                Py_DECREF(slice);
276
                if (modified) {
277
                    *modified = 1;
278
                }
279
            }
280
            Py_DECREF(tmp);
281
        }
282
283
        // Intern non-string constants in the free-threaded build
284
        _PyThreadStateImpl *tstate = (_PyThreadStateImpl *)_PyThreadState_GET();
285
        if (!_Py_IsImmortal(v) && !PyUnicode_CheckExact(v) &&
286
            should_immortalize_constant(v) &&
287
            !tstate->suppress_co_const_immortalization)
288
        {
289
            PyObject *interned = intern_one_constant(v);
290
            if (interned == NULL) {
291
                return -1;
292
            }
293
            else if (interned != v) {
294
                PyTuple_SET_ITEM(tuple, i, interned);
295
                Py_SETREF(v, interned);
296
                if (modified) {
297
                    *modified = 1;
298
                }
299
            }
300
        }
301
#endif
302
1.06M
    }
303
302k
    return 0;
304
302k
}
305
306
/* Return a shallow copy of a tuple that is
307
   guaranteed to contain exact strings, by converting string subclasses
308
   to exact strings and complaining if a non-string is found. */
309
static PyObject*
310
validate_and_copy_tuple(PyObject *tup)
311
0
{
312
0
    PyObject *newtuple;
313
0
    PyObject *item;
314
0
    Py_ssize_t i, len;
315
316
0
    len = PyTuple_GET_SIZE(tup);
317
0
    newtuple = PyTuple_New(len);
318
0
    if (newtuple == NULL)
319
0
        return NULL;
320
321
0
    for (i = 0; i < len; i++) {
322
0
        item = PyTuple_GET_ITEM(tup, i);
323
0
        if (PyUnicode_CheckExact(item)) {
324
0
            Py_INCREF(item);
325
0
        }
326
0
        else if (!PyUnicode_Check(item)) {
327
0
            PyErr_Format(
328
0
                PyExc_TypeError,
329
0
                "name tuples must contain only "
330
0
                "strings, not '%.500s'",
331
0
                Py_TYPE(item)->tp_name);
332
0
            Py_DECREF(newtuple);
333
0
            return NULL;
334
0
        }
335
0
        else {
336
0
            item = _PyUnicode_Copy(item);
337
0
            if (item == NULL) {
338
0
                Py_DECREF(newtuple);
339
0
                return NULL;
340
0
            }
341
0
        }
342
0
        PyTuple_SET_ITEM(newtuple, i, item);
343
0
    }
344
345
0
    return newtuple;
346
0
}
347
348
static int
349
init_co_cached(PyCodeObject *self)
350
337k
{
351
337k
    _PyCoCached *cached = FT_ATOMIC_LOAD_PTR(self->_co_cached);
352
337k
    if (cached != NULL) {
353
333k
        return 0;
354
333k
    }
355
356
4.50k
    Py_BEGIN_CRITICAL_SECTION(self);
357
4.50k
    cached = self->_co_cached;
358
4.50k
    if (cached == NULL) {
359
4.50k
        cached = PyMem_New(_PyCoCached, 1);
360
4.50k
        if (cached == NULL) {
361
0
            PyErr_NoMemory();
362
0
        }
363
4.50k
        else {
364
4.50k
            cached->_co_code = NULL;
365
4.50k
            cached->_co_cellvars = NULL;
366
4.50k
            cached->_co_freevars = NULL;
367
4.50k
            cached->_co_varnames = NULL;
368
4.50k
            FT_ATOMIC_STORE_PTR(self->_co_cached, cached);
369
4.50k
        }
370
4.50k
    }
371
4.50k
    Py_END_CRITICAL_SECTION();
372
4.50k
    return cached != NULL ? 0 : -1;
373
337k
}
374
375
/******************
376
 * _PyCode_New()
377
 ******************/
378
379
// This is also used in compile.c.
380
void
381
_Py_set_localsplus_info(int offset, PyObject *name, _PyLocals_Kind kind,
382
                        PyObject *names, PyObject *kinds)
383
29.6k
{
384
29.6k
    PyTuple_SET_ITEM(names, offset, Py_NewRef(name));
385
29.6k
    _PyLocals_SetKind(kinds, offset, kind);
386
29.6k
}
387
388
static void
389
get_localsplus_counts(PyObject *names, PyObject *kinds,
390
                      int *pnlocals, int *pncellvars,
391
                      int *pnfreevars)
392
375k
{
393
375k
    int nlocals = 0;
394
375k
    int ncellvars = 0;
395
375k
    int nfreevars = 0;
396
375k
    Py_ssize_t nlocalsplus = PyTuple_GET_SIZE(names);
397
1.42M
    for (int i = 0; i < nlocalsplus; i++) {
398
1.04M
        _PyLocals_Kind kind = _PyLocals_GetKind(kinds, i);
399
1.04M
        if (kind & CO_FAST_LOCAL) {
400
928k
            nlocals += 1;
401
928k
            if (kind & CO_FAST_CELL) {
402
8.04k
                ncellvars += 1;
403
8.04k
            }
404
928k
        }
405
118k
        else if (kind & CO_FAST_CELL) {
406
62.0k
            ncellvars += 1;
407
62.0k
        }
408
56.1k
        else if (kind & CO_FAST_FREE) {
409
56.1k
            nfreevars += 1;
410
56.1k
        }
411
1.04M
    }
412
375k
    if (pnlocals != NULL) {
413
375k
        *pnlocals = nlocals;
414
375k
    }
415
375k
    if (pncellvars != NULL) {
416
187k
        *pncellvars = ncellvars;
417
187k
    }
418
375k
    if (pnfreevars != NULL) {
419
187k
        *pnfreevars = nfreevars;
420
187k
    }
421
375k
}
422
423
static PyObject *
424
get_localsplus_names(PyCodeObject *co, _PyLocals_Kind kind, int num)
425
23.4k
{
426
23.4k
    PyObject *names = PyTuple_New(num);
427
23.4k
    if (names == NULL) {
428
0
        return NULL;
429
0
    }
430
23.4k
    int index = 0;
431
72.9k
    for (int offset = 0; offset < co->co_nlocalsplus; offset++) {
432
49.5k
        _PyLocals_Kind k = _PyLocals_GetKind(co->co_localspluskinds, offset);
433
49.5k
        if ((k & kind) == 0) {
434
32.5k
            continue;
435
32.5k
        }
436
49.5k
        assert(index < num);
437
17.0k
        PyObject *name = PyTuple_GET_ITEM(co->co_localsplusnames, offset);
438
17.0k
        PyTuple_SET_ITEM(names, index, Py_NewRef(name));
439
17.0k
        index += 1;
440
17.0k
    }
441
23.4k
    assert(index == num);
442
23.4k
    return names;
443
23.4k
}
444
445
int
446
_PyCode_Validate(struct _PyCodeConstructor *con)
447
187k
{
448
    /* Check argument types */
449
187k
    if (con->argcount < con->posonlyargcount || con->posonlyargcount < 0 ||
450
187k
        con->kwonlyargcount < 0 ||
451
187k
        con->stacksize < 0 || con->flags < 0 ||
452
187k
        con->code == NULL || !PyBytes_Check(con->code) ||
453
187k
        con->consts == NULL || !PyTuple_Check(con->consts) ||
454
187k
        con->names == NULL || !PyTuple_Check(con->names) ||
455
187k
        con->localsplusnames == NULL || !PyTuple_Check(con->localsplusnames) ||
456
187k
        con->localspluskinds == NULL || !PyBytes_Check(con->localspluskinds) ||
457
187k
        PyTuple_GET_SIZE(con->localsplusnames)
458
187k
            != PyBytes_GET_SIZE(con->localspluskinds) ||
459
187k
        con->name == NULL || !PyUnicode_Check(con->name) ||
460
187k
        con->qualname == NULL || !PyUnicode_Check(con->qualname) ||
461
187k
        con->filename == NULL || !PyUnicode_Check(con->filename) ||
462
187k
        con->linetable == NULL || !PyBytes_Check(con->linetable) ||
463
187k
        con->exceptiontable == NULL || !PyBytes_Check(con->exceptiontable)
464
187k
        ) {
465
0
        PyErr_BadInternalCall();
466
0
        return -1;
467
0
    }
468
469
    /* Make sure that code is indexable with an int, this is
470
       a long running assumption in ceval.c and many parts of
471
       the interpreter. */
472
187k
    if (PyBytes_GET_SIZE(con->code) > INT_MAX) {
473
0
        PyErr_SetString(PyExc_OverflowError,
474
0
                        "code: co_code larger than INT_MAX");
475
0
        return -1;
476
0
    }
477
187k
    if (PyBytes_GET_SIZE(con->code) % sizeof(_Py_CODEUNIT) != 0 ||
478
187k
        !_Py_IS_ALIGNED(PyBytes_AS_STRING(con->code), sizeof(_Py_CODEUNIT))
479
187k
        ) {
480
0
        PyErr_SetString(PyExc_ValueError, "code: co_code is malformed");
481
0
        return -1;
482
0
    }
483
484
    /* Ensure that the co_varnames has enough names to cover the arg counts.
485
     * Note that totalargs = nlocals - nplainlocals.  We check nplainlocals
486
     * here to avoid the possibility of overflow (however remote). */
487
187k
    int nlocals;
488
187k
    get_localsplus_counts(con->localsplusnames, con->localspluskinds,
489
187k
                          &nlocals, NULL, NULL);
490
187k
    int nplainlocals = nlocals -
491
187k
                       con->argcount -
492
187k
                       con->kwonlyargcount -
493
187k
                       ((con->flags & CO_VARARGS) != 0) -
494
187k
                       ((con->flags & CO_VARKEYWORDS) != 0);
495
187k
    if (nplainlocals < 0) {
496
0
        PyErr_SetString(PyExc_ValueError, "code: co_varnames is too small");
497
0
        return -1;
498
0
    }
499
500
187k
    return 0;
501
187k
}
502
503
extern void
504
_PyCode_Quicken(_Py_CODEUNIT *instructions, Py_ssize_t size, int enable_counters, int flags);
505
506
#ifdef Py_GIL_DISABLED
507
static _PyCodeArray * _PyCodeArray_New(Py_ssize_t size);
508
#endif
509
510
static int
511
init_code(PyCodeObject *co, struct _PyCodeConstructor *con)
512
187k
{
513
187k
    int nlocalsplus = (int)PyTuple_GET_SIZE(con->localsplusnames);
514
187k
    int nlocals, ncellvars, nfreevars;
515
187k
    get_localsplus_counts(con->localsplusnames, con->localspluskinds,
516
187k
                          &nlocals, &ncellvars, &nfreevars);
517
187k
    if (con->stacksize == 0) {
518
0
        con->stacksize = 1;
519
0
    }
520
521
187k
    PyInterpreterState *interp = _PyInterpreterState_GET();
522
187k
    co->co_filename = Py_NewRef(con->filename);
523
187k
    co->co_name = Py_NewRef(con->name);
524
187k
    co->co_qualname = Py_NewRef(con->qualname);
525
187k
    _PyUnicode_InternMortal(interp, &co->co_filename);
526
187k
    _PyUnicode_InternMortal(interp, &co->co_name);
527
187k
    _PyUnicode_InternMortal(interp, &co->co_qualname);
528
187k
    co->co_flags = con->flags;
529
530
187k
    co->co_firstlineno = con->firstlineno;
531
187k
    co->co_linetable = Py_NewRef(con->linetable);
532
533
187k
    co->co_consts = Py_NewRef(con->consts);
534
187k
    co->co_names = Py_NewRef(con->names);
535
536
187k
    co->co_localsplusnames = Py_NewRef(con->localsplusnames);
537
187k
    co->co_localspluskinds = Py_NewRef(con->localspluskinds);
538
539
187k
    co->co_argcount = con->argcount;
540
187k
    co->co_posonlyargcount = con->posonlyargcount;
541
187k
    co->co_kwonlyargcount = con->kwonlyargcount;
542
543
187k
    co->co_stacksize = con->stacksize;
544
545
187k
    co->co_exceptiontable = Py_NewRef(con->exceptiontable);
546
547
    /* derived values */
548
187k
    co->co_nlocalsplus = nlocalsplus;
549
187k
    co->co_nlocals = nlocals;
550
187k
    co->co_framesize = nlocalsplus + con->stacksize + FRAME_SPECIALS_SIZE;
551
187k
    co->co_ncellvars = ncellvars;
552
187k
    co->co_nfreevars = nfreevars;
553
187k
    FT_MUTEX_LOCK(&interp->func_state.mutex);
554
187k
    co->co_version = interp->func_state.next_version;
555
187k
    if (interp->func_state.next_version != 0) {
556
187k
        interp->func_state.next_version++;
557
187k
    }
558
187k
    FT_MUTEX_UNLOCK(&interp->func_state.mutex);
559
187k
    co->_co_monitoring = NULL;
560
187k
    co->_co_instrumentation_version = 0;
561
    /* not set */
562
187k
    co->co_weakreflist = NULL;
563
187k
    co->co_extra = NULL;
564
187k
    co->_co_cached = NULL;
565
187k
    co->co_executors = NULL;
566
567
187k
    memcpy(_PyCode_CODE(co), PyBytes_AS_STRING(con->code),
568
187k
           PyBytes_GET_SIZE(con->code));
569
#ifdef Py_GIL_DISABLED
570
    co->co_tlbc = _PyCodeArray_New(INITIAL_SPECIALIZED_CODE_SIZE);
571
    if (co->co_tlbc == NULL) {
572
        return -1;
573
    }
574
    co->co_tlbc->entries[0] = co->co_code_adaptive;
575
#endif
576
187k
    int entry_point = 0;
577
334k
    while (entry_point < Py_SIZE(co)) {
578
334k
        if (_PyCode_CODE(co)[entry_point].op.code == RESUME &&
579
199k
           (_PyCode_CODE(co)[entry_point].op.arg & RESUME_OPARG_LOCATION_MASK) != RESUME_AT_GEN_EXPR_START
580
334k
        ) {
581
187k
            break;
582
187k
        }
583
146k
        entry_point++;
584
146k
    }
585
187k
    co->_co_firsttraceable = entry_point;
586
587
#ifdef Py_GIL_DISABLED
588
    int enable_counters = interp->config.tlbc_enabled && interp->opt_config.specialization_enabled;
589
    _PyCode_Quicken(_PyCode_CODE(co), Py_SIZE(co), enable_counters, co->co_flags);
590
#else
591
187k
    _PyCode_Quicken(_PyCode_CODE(co), Py_SIZE(co), interp->opt_config.specialization_enabled, co->co_flags);
592
187k
#endif
593
187k
    notify_code_watchers(PY_CODE_EVENT_CREATE, co);
594
187k
    return 0;
595
187k
}
596
597
static int
598
scan_varint(const uint8_t *ptr)
599
2.14M
{
600
2.14M
    unsigned int read = *ptr++;
601
2.14M
    unsigned int val = read & 63;
602
2.14M
    unsigned int shift = 0;
603
2.14M
    while (read & 64) {
604
88
        read = *ptr++;
605
88
        shift += 6;
606
88
        val |= (read & 63) << shift;
607
88
    }
608
2.14M
    return val;
609
2.14M
}
610
611
static int
612
scan_signed_varint(const uint8_t *ptr)
613
2.14M
{
614
2.14M
    unsigned int uval = scan_varint(ptr);
615
2.14M
    if (uval & 1) {
616
1.06M
        return -(int)(uval >> 1);
617
1.06M
    }
618
1.08M
    else {
619
1.08M
        return uval >> 1;
620
1.08M
    }
621
2.14M
}
622
623
static int
624
get_line_delta(const uint8_t *ptr)
625
28.8M
{
626
28.8M
    int code = ((*ptr) >> 3) & 15;
627
28.8M
    switch (code) {
628
4.42k
        case PY_CODE_LOCATION_INFO_NONE:
629
4.42k
            return 0;
630
0
        case PY_CODE_LOCATION_INFO_NO_COLUMNS:
631
2.14M
        case PY_CODE_LOCATION_INFO_LONG:
632
2.14M
            return scan_signed_varint(ptr+1);
633
5.38M
        case PY_CODE_LOCATION_INFO_ONE_LINE0:
634
5.38M
            return 0;
635
4.28M
        case PY_CODE_LOCATION_INFO_ONE_LINE1:
636
4.28M
            return 1;
637
9.02k
        case PY_CODE_LOCATION_INFO_ONE_LINE2:
638
9.02k
            return 2;
639
17.0M
        default:
640
            /* Same line */
641
17.0M
            return 0;
642
28.8M
    }
643
28.8M
}
644
645
static PyObject *
646
remove_column_info(PyObject *locations)
647
0
{
648
0
    Py_ssize_t offset = 0;
649
0
    const uint8_t *data = (const uint8_t *)PyBytes_AS_STRING(locations);
650
0
    PyObject *res = PyBytes_FromStringAndSize(NULL, 32);
651
0
    if (res == NULL) {
652
0
        PyErr_NoMemory();
653
0
        return NULL;
654
0
    }
655
0
    uint8_t *output = (uint8_t *)PyBytes_AS_STRING(res);
656
0
    while (offset < PyBytes_GET_SIZE(locations)) {
657
0
        Py_ssize_t write_offset = output - (uint8_t *)PyBytes_AS_STRING(res);
658
0
        if (write_offset + 16 >= PyBytes_GET_SIZE(res)) {
659
0
            if (_PyBytes_Resize(&res, PyBytes_GET_SIZE(res) * 2) < 0) {
660
0
                return NULL;
661
0
            }
662
0
            output = (uint8_t *)PyBytes_AS_STRING(res) + write_offset;
663
0
        }
664
0
        int code = (data[offset] >> 3) & 15;
665
0
        if (code == PY_CODE_LOCATION_INFO_NONE) {
666
0
            *output++ = data[offset];
667
0
        }
668
0
        else {
669
0
            int blength = (data[offset] & 7)+1;
670
0
            output += write_location_entry_start(
671
0
                output, PY_CODE_LOCATION_INFO_NO_COLUMNS, blength);
672
0
            int ldelta = get_line_delta(&data[offset]);
673
0
            output += write_signed_varint(output, ldelta);
674
0
        }
675
0
        offset++;
676
0
        while (offset < PyBytes_GET_SIZE(locations) &&
677
0
            (data[offset] & 128) == 0) {
678
0
            offset++;
679
0
        }
680
0
    }
681
0
    Py_ssize_t write_offset = output - (uint8_t *)PyBytes_AS_STRING(res);
682
0
    if (_PyBytes_Resize(&res, write_offset)) {
683
0
        return NULL;
684
0
    }
685
0
    return res;
686
0
}
687
688
static int
689
intern_code_constants(struct _PyCodeConstructor *con)
690
187k
{
691
#ifdef Py_GIL_DISABLED
692
    PyInterpreterState *interp = _PyInterpreterState_GET();
693
    struct _py_code_state *state = &interp->code_state;
694
    FT_MUTEX_LOCK(&state->mutex);
695
#endif
696
187k
    if (intern_strings(con->names) < 0) {
697
0
        goto error;
698
0
    }
699
187k
    if (intern_constants(con->consts, NULL) < 0) {
700
0
        goto error;
701
0
    }
702
187k
    if (intern_strings(con->localsplusnames) < 0) {
703
0
        goto error;
704
0
    }
705
187k
    FT_MUTEX_UNLOCK(&state->mutex);
706
187k
    return 0;
707
708
0
error:
709
0
    FT_MUTEX_UNLOCK(&state->mutex);
710
0
    return -1;
711
187k
}
712
713
/* The caller is responsible for ensuring that the given data is valid. */
714
715
PyCodeObject *
716
_PyCode_New(struct _PyCodeConstructor *con)
717
187k
{
718
187k
    if (intern_code_constants(con) < 0) {
719
0
        return NULL;
720
0
    }
721
722
187k
    PyObject *replacement_locations = NULL;
723
    // Compact the linetable if we are opted out of debug
724
    // ranges.
725
187k
    if (!_Py_GetConfig()->code_debug_ranges) {
726
0
        replacement_locations = remove_column_info(con->linetable);
727
0
        if (replacement_locations == NULL) {
728
0
            return NULL;
729
0
        }
730
0
        con->linetable = replacement_locations;
731
0
    }
732
733
187k
    Py_ssize_t size = PyBytes_GET_SIZE(con->code) / sizeof(_Py_CODEUNIT);
734
187k
    PyCodeObject *co;
735
#ifdef Py_GIL_DISABLED
736
    co = PyObject_GC_NewVar(PyCodeObject, &PyCode_Type, size);
737
#else
738
187k
    co = PyObject_NewVar(PyCodeObject, &PyCode_Type, size);
739
187k
#endif
740
187k
    if (co == NULL) {
741
0
        Py_XDECREF(replacement_locations);
742
0
        PyErr_NoMemory();
743
0
        return NULL;
744
0
    }
745
746
187k
    if (init_code(co, con) < 0) {
747
0
        Py_DECREF(co);
748
0
        return NULL;
749
0
    }
750
751
#ifdef Py_GIL_DISABLED
752
    co->_co_unique_id = _PyObject_AssignUniqueId((PyObject *)co);
753
    _PyObject_GC_TRACK(co);
754
#endif
755
187k
    Py_XDECREF(replacement_locations);
756
187k
    return co;
757
187k
}
758
759
760
/******************
761
 * the legacy "constructors"
762
 ******************/
763
764
PyCodeObject *
765
PyUnstable_Code_NewWithPosOnlyArgs(
766
                          int argcount, int posonlyargcount, int kwonlyargcount,
767
                          int nlocals, int stacksize, int flags,
768
                          PyObject *code, PyObject *consts, PyObject *names,
769
                          PyObject *varnames, PyObject *freevars, PyObject *cellvars,
770
                          PyObject *filename, PyObject *name,
771
                          PyObject *qualname, int firstlineno,
772
                          PyObject *linetable,
773
                          PyObject *exceptiontable)
774
7.51k
{
775
7.51k
    PyCodeObject *co = NULL;
776
7.51k
    PyObject *localsplusnames = NULL;
777
7.51k
    PyObject *localspluskinds = NULL;
778
779
7.51k
    if (varnames == NULL || !PyTuple_Check(varnames) ||
780
7.51k
        cellvars == NULL || !PyTuple_Check(cellvars) ||
781
7.51k
        freevars == NULL || !PyTuple_Check(freevars)
782
7.51k
        ) {
783
0
        PyErr_BadInternalCall();
784
0
        return NULL;
785
0
    }
786
787
    // Set the "fast locals plus" info.
788
7.51k
    int nvarnames = (int)PyTuple_GET_SIZE(varnames);
789
7.51k
    int ncellvars = (int)PyTuple_GET_SIZE(cellvars);
790
7.51k
    int nfreevars = (int)PyTuple_GET_SIZE(freevars);
791
7.51k
    int nlocalsplus = nvarnames + ncellvars + nfreevars;
792
7.51k
    localsplusnames = PyTuple_New(nlocalsplus);
793
7.51k
    if (localsplusnames == NULL) {
794
0
        goto error;
795
0
    }
796
7.51k
    localspluskinds = PyBytes_FromStringAndSize(NULL, nlocalsplus);
797
7.51k
    if (localspluskinds == NULL) {
798
0
        goto error;
799
0
    }
800
7.51k
    int  offset = 0;
801
15.4k
    for (int i = 0; i < nvarnames; i++, offset++) {
802
7.92k
        PyObject *name = PyTuple_GET_ITEM(varnames, i);
803
7.92k
        _Py_set_localsplus_info(offset, name, CO_FAST_LOCAL,
804
7.92k
                               localsplusnames, localspluskinds);
805
7.92k
    }
806
7.51k
    for (int i = 0; i < ncellvars; i++, offset++) {
807
0
        PyObject *name = PyTuple_GET_ITEM(cellvars, i);
808
0
        int argoffset = -1;
809
0
        for (int j = 0; j < nvarnames; j++) {
810
0
            int cmp = PyUnicode_Compare(PyTuple_GET_ITEM(varnames, j),
811
0
                                        name);
812
0
            assert(!PyErr_Occurred());
813
0
            if (cmp == 0) {
814
0
                argoffset = j;
815
0
                break;
816
0
            }
817
0
        }
818
0
        if (argoffset >= 0) {
819
            // Merge the localsplus indices.
820
0
            nlocalsplus -= 1;
821
0
            offset -= 1;
822
0
            _PyLocals_Kind kind = _PyLocals_GetKind(localspluskinds, argoffset);
823
0
            _PyLocals_SetKind(localspluskinds, argoffset, kind | CO_FAST_CELL);
824
0
            continue;
825
0
        }
826
0
        _Py_set_localsplus_info(offset, name, CO_FAST_CELL,
827
0
                               localsplusnames, localspluskinds);
828
0
    }
829
15.0k
    for (int i = 0; i < nfreevars; i++, offset++) {
830
7.50k
        PyObject *name = PyTuple_GET_ITEM(freevars, i);
831
7.50k
        _Py_set_localsplus_info(offset, name, CO_FAST_FREE,
832
7.50k
                               localsplusnames, localspluskinds);
833
7.50k
    }
834
835
    // gh-110543: Make sure the CO_FAST_HIDDEN flag is set correctly.
836
7.51k
    if (!(flags & CO_OPTIMIZED)) {
837
0
        Py_ssize_t code_len = PyBytes_GET_SIZE(code);
838
0
        _Py_CODEUNIT *code_data = (_Py_CODEUNIT *)PyBytes_AS_STRING(code);
839
0
        Py_ssize_t num_code_units = code_len / sizeof(_Py_CODEUNIT);
840
0
        int extended_arg = 0;
841
0
        for (int i = 0; i < num_code_units; i += 1 + _PyOpcode_Caches[code_data[i].op.code]) {
842
0
            _Py_CODEUNIT *instr = &code_data[i];
843
0
            uint8_t opcode = instr->op.code;
844
0
            if (opcode == EXTENDED_ARG) {
845
0
                extended_arg = extended_arg << 8 | instr->op.arg;
846
0
                continue;
847
0
            }
848
0
            if (opcode == LOAD_FAST_AND_CLEAR) {
849
0
                int oparg = extended_arg << 8 | instr->op.arg;
850
0
                if (oparg >= nlocalsplus) {
851
0
                    PyErr_Format(PyExc_ValueError,
852
0
                                "code: LOAD_FAST_AND_CLEAR oparg %d out of range",
853
0
                                oparg);
854
0
                    goto error;
855
0
                }
856
0
                _PyLocals_Kind kind = _PyLocals_GetKind(localspluskinds, oparg);
857
0
                _PyLocals_SetKind(localspluskinds, oparg, kind | CO_FAST_HIDDEN);
858
0
            }
859
0
            extended_arg = 0;
860
0
        }
861
0
    }
862
863
    // If any cells were args then nlocalsplus will have shrunk.
864
7.51k
    if (nlocalsplus != PyTuple_GET_SIZE(localsplusnames)) {
865
0
        if (_PyTuple_Resize(&localsplusnames, nlocalsplus) < 0
866
0
                || _PyBytes_Resize(&localspluskinds, nlocalsplus) < 0) {
867
0
            goto error;
868
0
        }
869
0
    }
870
871
7.51k
    struct _PyCodeConstructor con = {
872
7.51k
        .filename = filename,
873
7.51k
        .name = name,
874
7.51k
        .qualname = qualname,
875
7.51k
        .flags = flags,
876
877
7.51k
        .code = code,
878
7.51k
        .firstlineno = firstlineno,
879
7.51k
        .linetable = linetable,
880
881
7.51k
        .consts = consts,
882
7.51k
        .names = names,
883
884
7.51k
        .localsplusnames = localsplusnames,
885
7.51k
        .localspluskinds = localspluskinds,
886
887
7.51k
        .argcount = argcount,
888
7.51k
        .posonlyargcount = posonlyargcount,
889
7.51k
        .kwonlyargcount = kwonlyargcount,
890
891
7.51k
        .stacksize = stacksize,
892
893
7.51k
        .exceptiontable = exceptiontable,
894
7.51k
    };
895
896
7.51k
    if (_PyCode_Validate(&con) < 0) {
897
0
        goto error;
898
0
    }
899
7.51k
    assert(PyBytes_GET_SIZE(code) % sizeof(_Py_CODEUNIT) == 0);
900
7.51k
    assert(_Py_IS_ALIGNED(PyBytes_AS_STRING(code), sizeof(_Py_CODEUNIT)));
901
7.51k
    if (nlocals != PyTuple_GET_SIZE(varnames)) {
902
0
        PyErr_SetString(PyExc_ValueError,
903
0
                        "code: co_nlocals != len(co_varnames)");
904
0
        goto error;
905
0
    }
906
907
7.51k
    co = _PyCode_New(&con);
908
7.51k
    if (co == NULL) {
909
0
        goto error;
910
0
    }
911
912
7.51k
error:
913
7.51k
    Py_XDECREF(localsplusnames);
914
7.51k
    Py_XDECREF(localspluskinds);
915
7.51k
    return co;
916
7.51k
}
917
918
PyCodeObject *
919
PyUnstable_Code_New(int argcount, int kwonlyargcount,
920
           int nlocals, int stacksize, int flags,
921
           PyObject *code, PyObject *consts, PyObject *names,
922
           PyObject *varnames, PyObject *freevars, PyObject *cellvars,
923
           PyObject *filename, PyObject *name, PyObject *qualname,
924
           int firstlineno,
925
           PyObject *linetable,
926
           PyObject *exceptiontable)
927
0
{
928
0
    return PyCode_NewWithPosOnlyArgs(argcount, 0, kwonlyargcount, nlocals,
929
0
                                     stacksize, flags, code, consts, names,
930
0
                                     varnames, freevars, cellvars, filename,
931
0
                                     name, qualname, firstlineno,
932
0
                                     linetable,
933
0
                                     exceptiontable);
934
0
}
935
936
// NOTE: When modifying the construction of PyCode_NewEmpty, please also change
937
// test.test_code.CodeLocationTest.test_code_new_empty to keep it in sync!
938
939
static const uint8_t assert0[8] = {
940
    RESUME, RESUME_AT_FUNC_START,
941
    CACHE, 0,
942
    LOAD_COMMON_CONSTANT, CONSTANT_ASSERTIONERROR,
943
    RAISE_VARARGS, 1
944
};
945
946
static const uint8_t linetable[2] = {
947
    (1 << 7)  // New entry.
948
    | (PY_CODE_LOCATION_INFO_NO_COLUMNS << 3)
949
    | (4 - 1),  // Four code units.
950
    0,  // Offset from co_firstlineno.
951
};
952
953
PyCodeObject *
954
PyCode_NewEmpty(const char *filename, const char *funcname, int firstlineno)
955
191
{
956
191
    PyObject *nulltuple = NULL;
957
191
    PyObject *filename_ob = NULL;
958
191
    PyObject *funcname_ob = NULL;
959
191
    PyObject *code_ob = NULL;
960
191
    PyObject *linetable_ob = NULL;
961
191
    PyCodeObject *result = NULL;
962
963
191
    nulltuple = PyTuple_New(0);
964
191
    if (nulltuple == NULL) {
965
0
        goto failed;
966
0
    }
967
191
    funcname_ob = PyUnicode_FromString(funcname);
968
191
    if (funcname_ob == NULL) {
969
0
        goto failed;
970
0
    }
971
191
    filename_ob = PyUnicode_DecodeFSDefault(filename);
972
191
    if (filename_ob == NULL) {
973
0
        goto failed;
974
0
    }
975
191
    code_ob = PyBytes_FromStringAndSize((const char *)assert0, 8);
976
191
    if (code_ob == NULL) {
977
0
        goto failed;
978
0
    }
979
191
    linetable_ob = PyBytes_FromStringAndSize((const char *)linetable, 2);
980
191
    if (linetable_ob == NULL) {
981
0
        goto failed;
982
0
    }
983
984
382
#define emptystring (PyObject *)&_Py_SINGLETON(bytes_empty)
985
191
    struct _PyCodeConstructor con = {
986
191
        .filename = filename_ob,
987
191
        .name = funcname_ob,
988
191
        .qualname = funcname_ob,
989
191
        .code = code_ob,
990
191
        .firstlineno = firstlineno,
991
191
        .linetable = linetable_ob,
992
191
        .consts = nulltuple,
993
191
        .names = nulltuple,
994
191
        .localsplusnames = nulltuple,
995
191
        .localspluskinds = emptystring,
996
191
        .exceptiontable = emptystring,
997
191
        .stacksize = 1,
998
191
    };
999
191
    result = _PyCode_New(&con);
1000
1001
191
failed:
1002
191
    Py_XDECREF(nulltuple);
1003
191
    Py_XDECREF(funcname_ob);
1004
191
    Py_XDECREF(filename_ob);
1005
191
    Py_XDECREF(code_ob);
1006
191
    Py_XDECREF(linetable_ob);
1007
191
    return result;
1008
191
}
1009
1010
1011
/******************
1012
 * source location tracking (co_lines/co_positions)
1013
 ******************/
1014
1015
int
1016
PyCode_Addr2Line(PyCodeObject *co, int addrq)
1017
1.06M
{
1018
1.06M
    if (addrq < 0) {
1019
0
        return co->co_firstlineno;
1020
0
    }
1021
1.06M
    _PyCoMonitoringData *data = _Py_atomic_load_ptr_acquire(&co->_co_monitoring);
1022
1.06M
    if (data) {
1023
0
        _PyCoLineInstrumentationData *lines = _Py_atomic_load_ptr_acquire(&data->lines);
1024
0
        if (lines) {
1025
0
            return _Py_Instrumentation_GetLine(co, lines, addrq/sizeof(_Py_CODEUNIT));
1026
0
        }
1027
0
    }
1028
1.06M
    assert(addrq >= 0 && addrq < _PyCode_NBYTES(co));
1029
1.06M
    PyCodeAddressRange bounds;
1030
1.06M
    _PyCode_InitAddressRange(co, &bounds);
1031
1.06M
    return _PyCode_CheckLineNumber(addrq, &bounds);
1032
1.06M
}
1033
1034
int
1035
_PyCode_SafeAddr2Line(PyCodeObject *co, int addrq)
1036
0
{
1037
0
    if (addrq < 0) {
1038
0
        return co->co_firstlineno;
1039
0
    }
1040
0
    if (co->_co_monitoring && co->_co_monitoring->lines) {
1041
0
        return _Py_Instrumentation_GetLine(co, co->_co_monitoring->lines, addrq/sizeof(_Py_CODEUNIT));
1042
0
    }
1043
0
    if (!(addrq >= 0 && addrq < _PyCode_NBYTES(co))) {
1044
0
        return -1;
1045
0
    }
1046
0
    PyCodeAddressRange bounds;
1047
0
    _PyCode_InitAddressRange(co, &bounds);
1048
0
    return _PyCode_CheckLineNumber(addrq, &bounds);
1049
0
}
1050
1051
void
1052
_PyLineTable_InitAddressRange(const char *linetable, Py_ssize_t length, int firstlineno, PyCodeAddressRange *range)
1053
1.06M
{
1054
1.06M
    range->opaque.lo_next = (const uint8_t *)linetable;
1055
1.06M
    range->opaque.limit = range->opaque.lo_next + length;
1056
1.06M
    range->ar_start = -1;
1057
1.06M
    range->ar_end = 0;
1058
1.06M
    range->opaque.computed_line = firstlineno;
1059
1.06M
    range->ar_line = -1;
1060
1.06M
}
1061
1062
int
1063
_PyCode_InitAddressRange(PyCodeObject* co, PyCodeAddressRange *bounds)
1064
1.06M
{
1065
1.06M
    assert(co->co_linetable != NULL);
1066
1.06M
    const char *linetable = PyBytes_AS_STRING(co->co_linetable);
1067
1.06M
    Py_ssize_t length = PyBytes_GET_SIZE(co->co_linetable);
1068
1.06M
    _PyLineTable_InitAddressRange(linetable, length, co->co_firstlineno, bounds);
1069
1.06M
    return bounds->ar_line;
1070
1.06M
}
1071
1072
/* Update *bounds to describe the first and one-past-the-last instructions in
1073
   the same line as lasti.  Return the number of that line, or -1 if lasti is out of bounds. */
1074
int
1075
_PyCode_CheckLineNumber(int lasti, PyCodeAddressRange *bounds)
1076
1.06M
{
1077
29.9M
    while (bounds->ar_end <= lasti) {
1078
28.8M
        if (!_PyLineTable_NextAddressRange(bounds)) {
1079
0
            return -1;
1080
0
        }
1081
28.8M
    }
1082
1.06M
    while (bounds->ar_start > lasti) {
1083
0
        if (!_PyLineTable_PreviousAddressRange(bounds)) {
1084
0
            return -1;
1085
0
        }
1086
0
    }
1087
1.06M
    return bounds->ar_line;
1088
1.06M
}
1089
1090
static int
1091
is_no_line_marker(uint8_t b)
1092
28.8M
{
1093
28.8M
    return (b >> 3) == 0x1f;
1094
28.8M
}
1095
1096
1097
#define ASSERT_VALID_BOUNDS(bounds) \
1098
57.7M
    assert(bounds->opaque.lo_next <=  bounds->opaque.limit && \
1099
57.7M
        (bounds->ar_line == -1 || bounds->ar_line == bounds->opaque.computed_line) && \
1100
57.7M
        (bounds->opaque.lo_next == bounds->opaque.limit || \
1101
57.7M
        (*bounds->opaque.lo_next) & 128))
1102
1103
static int
1104
next_code_delta(PyCodeAddressRange *bounds)
1105
28.8M
{
1106
28.8M
    assert((*bounds->opaque.lo_next) & 128);
1107
28.8M
    return (((*bounds->opaque.lo_next) & 7) + 1) * sizeof(_Py_CODEUNIT);
1108
28.8M
}
1109
1110
static int
1111
previous_code_delta(PyCodeAddressRange *bounds)
1112
0
{
1113
0
    if (bounds->ar_start == 0) {
1114
        // If we looking at the first entry, the
1115
        // "previous" entry has an implicit length of 1.
1116
0
        return 1;
1117
0
    }
1118
0
    const uint8_t *ptr = bounds->opaque.lo_next-1;
1119
0
    while (((*ptr) & 128) == 0) {
1120
0
        ptr--;
1121
0
    }
1122
0
    return (((*ptr) & 7) + 1) * sizeof(_Py_CODEUNIT);
1123
0
}
1124
1125
static int
1126
read_byte(PyCodeAddressRange *bounds)
1127
0
{
1128
0
    return *bounds->opaque.lo_next++;
1129
0
}
1130
1131
static int
1132
read_varint(PyCodeAddressRange *bounds)
1133
0
{
1134
0
    unsigned int read = read_byte(bounds);
1135
0
    unsigned int val = read & 63;
1136
0
    unsigned int shift = 0;
1137
0
    while (read & 64) {
1138
0
        read = read_byte(bounds);
1139
0
        shift += 6;
1140
0
        val |= (read & 63) << shift;
1141
0
    }
1142
0
    return val;
1143
0
}
1144
1145
static int
1146
read_signed_varint(PyCodeAddressRange *bounds)
1147
0
{
1148
0
    unsigned int uval = read_varint(bounds);
1149
0
    if (uval & 1) {
1150
0
        return -(int)(uval >> 1);
1151
0
    }
1152
0
    else {
1153
0
        return uval >> 1;
1154
0
    }
1155
0
}
1156
1157
static void
1158
retreat(PyCodeAddressRange *bounds)
1159
0
{
1160
0
    ASSERT_VALID_BOUNDS(bounds);
1161
0
    assert(bounds->ar_start >= 0);
1162
0
    do {
1163
0
        bounds->opaque.lo_next--;
1164
0
    } while (((*bounds->opaque.lo_next) & 128) == 0);
1165
0
    bounds->opaque.computed_line -= get_line_delta(bounds->opaque.lo_next);
1166
0
    bounds->ar_end = bounds->ar_start;
1167
0
    bounds->ar_start -= previous_code_delta(bounds);
1168
0
    if (is_no_line_marker(bounds->opaque.lo_next[-1])) {
1169
0
        bounds->ar_line = -1;
1170
0
    }
1171
0
    else {
1172
0
        bounds->ar_line = bounds->opaque.computed_line;
1173
0
    }
1174
0
    ASSERT_VALID_BOUNDS(bounds);
1175
0
}
1176
1177
static void
1178
advance(PyCodeAddressRange *bounds)
1179
28.8M
{
1180
28.8M
    ASSERT_VALID_BOUNDS(bounds);
1181
28.8M
    bounds->opaque.computed_line += get_line_delta(bounds->opaque.lo_next);
1182
28.8M
    if (is_no_line_marker(*bounds->opaque.lo_next)) {
1183
4.42k
        bounds->ar_line = -1;
1184
4.42k
    }
1185
28.8M
    else {
1186
28.8M
        bounds->ar_line = bounds->opaque.computed_line;
1187
28.8M
    }
1188
28.8M
    bounds->ar_start = bounds->ar_end;
1189
28.8M
    bounds->ar_end += next_code_delta(bounds);
1190
73.8M
    do {
1191
73.8M
        bounds->opaque.lo_next++;
1192
73.8M
    } while (bounds->opaque.lo_next < bounds->opaque.limit &&
1193
73.8M
        ((*bounds->opaque.lo_next) & 128) == 0);
1194
28.8M
    ASSERT_VALID_BOUNDS(bounds);
1195
28.8M
}
1196
1197
static void
1198
advance_with_locations(PyCodeAddressRange *bounds, int *endline, int *column, int *endcolumn)
1199
0
{
1200
0
    ASSERT_VALID_BOUNDS(bounds);
1201
0
    int first_byte = read_byte(bounds);
1202
0
    int code = (first_byte >> 3) & 15;
1203
0
    bounds->ar_start = bounds->ar_end;
1204
0
    bounds->ar_end = bounds->ar_start + ((first_byte & 7) + 1) * sizeof(_Py_CODEUNIT);
1205
0
    switch(code) {
1206
0
        case PY_CODE_LOCATION_INFO_NONE:
1207
0
            bounds->ar_line = *endline = -1;
1208
0
            *column =  *endcolumn = -1;
1209
0
            break;
1210
0
        case PY_CODE_LOCATION_INFO_LONG:
1211
0
        {
1212
0
            bounds->opaque.computed_line += read_signed_varint(bounds);
1213
0
            bounds->ar_line = bounds->opaque.computed_line;
1214
0
            *endline = bounds->ar_line + read_varint(bounds);
1215
0
            *column = read_varint(bounds)-1;
1216
0
            *endcolumn = read_varint(bounds)-1;
1217
0
            break;
1218
0
        }
1219
0
        case PY_CODE_LOCATION_INFO_NO_COLUMNS:
1220
0
        {
1221
            /* No column */
1222
0
            bounds->opaque.computed_line += read_signed_varint(bounds);
1223
0
            *endline = bounds->ar_line = bounds->opaque.computed_line;
1224
0
            *column = *endcolumn = -1;
1225
0
            break;
1226
0
        }
1227
0
        case PY_CODE_LOCATION_INFO_ONE_LINE0:
1228
0
        case PY_CODE_LOCATION_INFO_ONE_LINE1:
1229
0
        case PY_CODE_LOCATION_INFO_ONE_LINE2:
1230
0
        {
1231
            /* one line form */
1232
0
            int line_delta = code - 10;
1233
0
            bounds->opaque.computed_line += line_delta;
1234
0
            *endline = bounds->ar_line = bounds->opaque.computed_line;
1235
0
            *column = read_byte(bounds);
1236
0
            *endcolumn = read_byte(bounds);
1237
0
            break;
1238
0
        }
1239
0
        default:
1240
0
        {
1241
            /* Short forms */
1242
0
            int second_byte = read_byte(bounds);
1243
0
            assert((second_byte & 128) == 0);
1244
0
            *endline = bounds->ar_line = bounds->opaque.computed_line;
1245
0
            *column = code << 3 | (second_byte >> 4);
1246
0
            *endcolumn = *column + (second_byte & 15);
1247
0
        }
1248
0
    }
1249
0
    ASSERT_VALID_BOUNDS(bounds);
1250
0
}
1251
int
1252
PyCode_Addr2Location(PyCodeObject *co, int addrq,
1253
                     int *start_line, int *start_column,
1254
                     int *end_line, int *end_column)
1255
0
{
1256
0
    if (addrq < 0) {
1257
0
        *start_line = *end_line = co->co_firstlineno;
1258
0
        *start_column = *end_column = 0;
1259
0
        return 1;
1260
0
    }
1261
0
    assert(addrq >= 0 && addrq < _PyCode_NBYTES(co));
1262
0
    PyCodeAddressRange bounds;
1263
0
    _PyCode_InitAddressRange(co, &bounds);
1264
0
    _PyCode_CheckLineNumber(addrq, &bounds);
1265
0
    retreat(&bounds);
1266
0
    advance_with_locations(&bounds, end_line, start_column, end_column);
1267
0
    *start_line = bounds.ar_line;
1268
0
    return 1;
1269
0
}
1270
1271
1272
static inline int
1273
28.8M
at_end(PyCodeAddressRange *bounds) {
1274
28.8M
    return bounds->opaque.lo_next >= bounds->opaque.limit;
1275
28.8M
}
1276
1277
int
1278
_PyLineTable_PreviousAddressRange(PyCodeAddressRange *range)
1279
0
{
1280
0
    if (range->ar_start <= 0) {
1281
0
        return 0;
1282
0
    }
1283
0
    retreat(range);
1284
0
    assert(range->ar_end > range->ar_start);
1285
0
    return 1;
1286
0
}
1287
1288
int
1289
_PyLineTable_NextAddressRange(PyCodeAddressRange *range)
1290
28.8M
{
1291
28.8M
    if (at_end(range)) {
1292
0
        return 0;
1293
0
    }
1294
28.8M
    advance(range);
1295
28.8M
    assert(range->ar_end > range->ar_start);
1296
28.8M
    return 1;
1297
28.8M
}
1298
1299
1300
typedef struct {
1301
    PyObject_HEAD
1302
    PyCodeObject *li_code;
1303
    PyCodeAddressRange li_line;
1304
} lineiterator;
1305
1306
1307
static void
1308
lineiter_dealloc(PyObject *self)
1309
0
{
1310
0
    lineiterator *li = (lineiterator*)self;
1311
0
    Py_DECREF(li->li_code);
1312
0
    Py_TYPE(li)->tp_free(li);
1313
0
}
1314
1315
static PyObject *
1316
0
_source_offset_converter(void *arg) {
1317
0
    int *value = (int*)arg;
1318
0
    if (*value == -1) {
1319
0
        Py_RETURN_NONE;
1320
0
    }
1321
0
    return PyLong_FromLong(*value);
1322
0
}
1323
1324
static PyObject *
1325
lineiter_next(PyObject *self)
1326
0
{
1327
0
    lineiterator *li = (lineiterator*)self;
1328
0
    PyCodeAddressRange *bounds = &li->li_line;
1329
0
    if (!_PyLineTable_NextAddressRange(bounds)) {
1330
0
        return NULL;
1331
0
    }
1332
0
    int start = bounds->ar_start;
1333
0
    int line = bounds->ar_line;
1334
    // Merge overlapping entries:
1335
0
    while (_PyLineTable_NextAddressRange(bounds)) {
1336
0
        if (bounds->ar_line != line) {
1337
0
            _PyLineTable_PreviousAddressRange(bounds);
1338
0
            break;
1339
0
        }
1340
0
    }
1341
0
    return Py_BuildValue("iiO&", start, bounds->ar_end,
1342
0
                         _source_offset_converter, &line);
1343
0
}
1344
1345
PyTypeObject _PyLineIterator = {
1346
    PyVarObject_HEAD_INIT(&PyType_Type, 0)
1347
    "line_iterator",                    /* tp_name */
1348
    sizeof(lineiterator),               /* tp_basicsize */
1349
    0,                                  /* tp_itemsize */
1350
    /* methods */
1351
    lineiter_dealloc,                   /* tp_dealloc */
1352
    0,                                  /* tp_vectorcall_offset */
1353
    0,                                  /* tp_getattr */
1354
    0,                                  /* tp_setattr */
1355
    0,                                  /* tp_as_async */
1356
    0,                                  /* tp_repr */
1357
    0,                                  /* tp_as_number */
1358
    0,                                  /* tp_as_sequence */
1359
    0,                                  /* tp_as_mapping */
1360
    0,                                  /* tp_hash */
1361
    0,                                  /* tp_call */
1362
    0,                                  /* tp_str */
1363
    0,                                  /* tp_getattro */
1364
    0,                                  /* tp_setattro */
1365
    0,                                  /* tp_as_buffer */
1366
    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,       /* tp_flags */
1367
    0,                                  /* tp_doc */
1368
    0,                                  /* tp_traverse */
1369
    0,                                  /* tp_clear */
1370
    0,                                  /* tp_richcompare */
1371
    0,                                  /* tp_weaklistoffset */
1372
    PyObject_SelfIter,                  /* tp_iter */
1373
    lineiter_next,                      /* tp_iternext */
1374
    0,                                  /* tp_methods */
1375
    0,                                  /* tp_members */
1376
    0,                                  /* tp_getset */
1377
    0,                                  /* tp_base */
1378
    0,                                  /* tp_dict */
1379
    0,                                  /* tp_descr_get */
1380
    0,                                  /* tp_descr_set */
1381
    0,                                  /* tp_dictoffset */
1382
    0,                                  /* tp_init */
1383
    0,                                  /* tp_alloc */
1384
    0,                                  /* tp_new */
1385
    PyObject_Free,                      /* tp_free */
1386
};
1387
1388
static lineiterator *
1389
new_linesiterator(PyCodeObject *code)
1390
0
{
1391
0
    lineiterator *li = (lineiterator *)PyType_GenericAlloc(&_PyLineIterator, 0);
1392
0
    if (li == NULL) {
1393
0
        return NULL;
1394
0
    }
1395
0
    li->li_code = (PyCodeObject*)Py_NewRef(code);
1396
0
    _PyCode_InitAddressRange(code, &li->li_line);
1397
0
    return li;
1398
0
}
1399
1400
/* co_positions iterator object. */
1401
typedef struct {
1402
    PyObject_HEAD
1403
    PyCodeObject* pi_code;
1404
    PyCodeAddressRange pi_range;
1405
    int pi_offset;
1406
    int pi_endline;
1407
    int pi_column;
1408
    int pi_endcolumn;
1409
} positionsiterator;
1410
1411
static void
1412
positionsiter_dealloc(PyObject *self)
1413
0
{
1414
0
    positionsiterator *pi = (positionsiterator*)self;
1415
0
    Py_DECREF(pi->pi_code);
1416
0
    Py_TYPE(pi)->tp_free(pi);
1417
0
}
1418
1419
static PyObject*
1420
positionsiter_next(PyObject *self)
1421
0
{
1422
0
    positionsiterator *pi = (positionsiterator*)self;
1423
0
    if (pi->pi_offset >= pi->pi_range.ar_end) {
1424
0
        assert(pi->pi_offset == pi->pi_range.ar_end);
1425
0
        if (at_end(&pi->pi_range)) {
1426
0
            return NULL;
1427
0
        }
1428
0
        advance_with_locations(&pi->pi_range, &pi->pi_endline, &pi->pi_column, &pi->pi_endcolumn);
1429
0
    }
1430
0
    pi->pi_offset += 2;
1431
0
    return Py_BuildValue("(O&O&O&O&)",
1432
0
        _source_offset_converter, &pi->pi_range.ar_line,
1433
0
        _source_offset_converter, &pi->pi_endline,
1434
0
        _source_offset_converter, &pi->pi_column,
1435
0
        _source_offset_converter, &pi->pi_endcolumn);
1436
0
}
1437
1438
PyTypeObject _PyPositionsIterator = {
1439
    PyVarObject_HEAD_INIT(&PyType_Type, 0)
1440
    "positions_iterator",               /* tp_name */
1441
    sizeof(positionsiterator),          /* tp_basicsize */
1442
    0,                                  /* tp_itemsize */
1443
    /* methods */
1444
    positionsiter_dealloc,              /* tp_dealloc */
1445
    0,                                  /* tp_vectorcall_offset */
1446
    0,                                  /* tp_getattr */
1447
    0,                                  /* tp_setattr */
1448
    0,                                  /* tp_as_async */
1449
    0,                                  /* tp_repr */
1450
    0,                                  /* tp_as_number */
1451
    0,                                  /* tp_as_sequence */
1452
    0,                                  /* tp_as_mapping */
1453
    0,                                  /* tp_hash */
1454
    0,                                  /* tp_call */
1455
    0,                                  /* tp_str */
1456
    0,                                  /* tp_getattro */
1457
    0,                                  /* tp_setattro */
1458
    0,                                  /* tp_as_buffer */
1459
    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,       /* tp_flags */
1460
    0,                                  /* tp_doc */
1461
    0,                                  /* tp_traverse */
1462
    0,                                  /* tp_clear */
1463
    0,                                  /* tp_richcompare */
1464
    0,                                  /* tp_weaklistoffset */
1465
    PyObject_SelfIter,                  /* tp_iter */
1466
    positionsiter_next,                 /* tp_iternext */
1467
    0,                                  /* tp_methods */
1468
    0,                                  /* tp_members */
1469
    0,                                  /* tp_getset */
1470
    0,                                  /* tp_base */
1471
    0,                                  /* tp_dict */
1472
    0,                                  /* tp_descr_get */
1473
    0,                                  /* tp_descr_set */
1474
    0,                                  /* tp_dictoffset */
1475
    0,                                  /* tp_init */
1476
    0,                                  /* tp_alloc */
1477
    0,                                  /* tp_new */
1478
    PyObject_Free,                      /* tp_free */
1479
};
1480
1481
static PyObject*
1482
code_positionsiterator(PyObject *self, PyObject* Py_UNUSED(args))
1483
0
{
1484
0
    PyCodeObject *code = (PyCodeObject*)self;
1485
0
    positionsiterator* pi = (positionsiterator*)PyType_GenericAlloc(&_PyPositionsIterator, 0);
1486
0
    if (pi == NULL) {
1487
0
        return NULL;
1488
0
    }
1489
0
    pi->pi_code = (PyCodeObject*)Py_NewRef(code);
1490
0
    _PyCode_InitAddressRange(code, &pi->pi_range);
1491
0
    pi->pi_offset = pi->pi_range.ar_end;
1492
0
    return (PyObject*)pi;
1493
0
}
1494
1495
1496
/******************
1497
 * "extra" frame eval info (see PEP 523)
1498
 ******************/
1499
1500
/* Holder for co_extra information */
1501
typedef struct {
1502
    Py_ssize_t ce_size;
1503
    void *ce_extras[1];
1504
} _PyCodeObjectExtra;
1505
1506
1507
static inline size_t
1508
code_extra_size(Py_ssize_t n)
1509
0
{
1510
0
    return sizeof(_PyCodeObjectExtra) + (n - 1) * sizeof(void *);
1511
0
}
1512
1513
#ifdef Py_GIL_DISABLED
1514
static int
1515
code_extra_grow_ft(PyCodeObject *co, _PyCodeObjectExtra *old_co_extra,
1516
                   Py_ssize_t old_ce_size, Py_ssize_t new_ce_size,
1517
                   Py_ssize_t index, void *extra)
1518
{
1519
    _Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(co);
1520
    _PyCodeObjectExtra *new_co_extra = PyMem_Malloc(
1521
        code_extra_size(new_ce_size));
1522
    if (new_co_extra == NULL) {
1523
        PyErr_NoMemory();
1524
        return -1;
1525
    }
1526
1527
    if (old_ce_size > 0) {
1528
        memcpy(new_co_extra->ce_extras, old_co_extra->ce_extras,
1529
               old_ce_size * sizeof(void *));
1530
    }
1531
    for (Py_ssize_t i = old_ce_size; i < new_ce_size; i++) {
1532
        new_co_extra->ce_extras[i] = NULL;
1533
    }
1534
    new_co_extra->ce_size = new_ce_size;
1535
    new_co_extra->ce_extras[index] = extra;
1536
1537
    // Publish new buffer and its contents to lock-free readers.
1538
    FT_ATOMIC_STORE_PTR_RELEASE(co->co_extra, new_co_extra);
1539
    if (old_co_extra != NULL) {
1540
        // QSBR: defer old-buffer free until lock-free readers quiesce.
1541
        _PyMem_FreeDelayed(old_co_extra, code_extra_size(old_ce_size));
1542
    }
1543
    return 0;
1544
}
1545
#else
1546
static int
1547
code_extra_grow_gil(PyCodeObject *co, _PyCodeObjectExtra *old_co_extra,
1548
                    Py_ssize_t old_ce_size, Py_ssize_t new_ce_size,
1549
                    Py_ssize_t index, void *extra)
1550
0
{
1551
0
    _PyCodeObjectExtra *new_co_extra = PyMem_Realloc(
1552
0
        old_co_extra, code_extra_size(new_ce_size));
1553
0
    if (new_co_extra == NULL) {
1554
0
        PyErr_NoMemory();
1555
0
        return -1;
1556
0
    }
1557
1558
0
    for (Py_ssize_t i = old_ce_size; i < new_ce_size; i++) {
1559
0
        new_co_extra->ce_extras[i] = NULL;
1560
0
    }
1561
0
    new_co_extra->ce_size = new_ce_size;
1562
0
    new_co_extra->ce_extras[index] = extra;
1563
0
    co->co_extra = new_co_extra;
1564
0
    return 0;
1565
0
}
1566
#endif
1567
1568
int
1569
PyUnstable_Code_GetExtra(PyObject *code, Py_ssize_t index, void **extra)
1570
0
{
1571
0
    if (!PyCode_Check(code)) {
1572
0
        PyErr_BadInternalCall();
1573
0
        return -1;
1574
0
    }
1575
1576
0
    PyCodeObject *co = (PyCodeObject *)code;
1577
0
    *extra = NULL;
1578
1579
0
    if (index < 0) {
1580
0
        return 0;
1581
0
    }
1582
1583
    // Lock-free read; pairs with release stores in SetExtra.
1584
0
    _PyCodeObjectExtra *co_extra = FT_ATOMIC_LOAD_PTR_ACQUIRE(co->co_extra);
1585
0
    if (co_extra != NULL && index < co_extra->ce_size) {
1586
0
        *extra = FT_ATOMIC_LOAD_PTR_ACQUIRE(co_extra->ce_extras[index]);
1587
0
    }
1588
1589
0
    return 0;
1590
0
}
1591
1592
1593
int
1594
PyUnstable_Code_SetExtra(PyObject *code, Py_ssize_t index, void *extra)
1595
0
{
1596
0
    PyInterpreterState *interp = _PyInterpreterState_GET();
1597
1598
    // co_extra_user_count is monotonically increasing and published with
1599
    // release store in RequestCodeExtraIndex, so once an index is valid
1600
    // it stays valid.
1601
0
    Py_ssize_t user_count = FT_ATOMIC_LOAD_SSIZE_ACQUIRE(
1602
0
        interp->co_extra_user_count);
1603
1604
0
    if (!PyCode_Check(code) || index < 0 || index >= user_count) {
1605
0
        PyErr_BadInternalCall();
1606
0
        return -1;
1607
0
    }
1608
1609
0
    PyCodeObject *co = (PyCodeObject *)code;
1610
0
    int result = 0;
1611
0
    void *old_slot_value = NULL;
1612
1613
0
    Py_BEGIN_CRITICAL_SECTION(co);
1614
1615
0
    _PyCodeObjectExtra *old_co_extra = (_PyCodeObjectExtra *)co->co_extra;
1616
0
    Py_ssize_t old_ce_size = (old_co_extra == NULL)
1617
0
        ? 0 : old_co_extra->ce_size;
1618
1619
    // Fast path: slot already exists, update in place.
1620
0
    if (index < old_ce_size) {
1621
0
        old_slot_value = old_co_extra->ce_extras[index];
1622
0
        FT_ATOMIC_STORE_PTR_RELEASE(old_co_extra->ce_extras[index], extra);
1623
0
        goto done;
1624
0
    }
1625
1626
    // Slow path: buffer needs to grow.
1627
0
    Py_ssize_t new_ce_size = user_count;
1628
#ifdef Py_GIL_DISABLED
1629
    // FT build: allocate new buffer and swap; QSBR reclaims the old one.
1630
    result = code_extra_grow_ft(
1631
        co, old_co_extra, old_ce_size, new_ce_size, index, extra);
1632
#else
1633
    // GIL build: grow with realloc.
1634
0
    result = code_extra_grow_gil(
1635
0
        co, old_co_extra, old_ce_size, new_ce_size, index, extra);
1636
0
#endif
1637
1638
0
done:;
1639
0
    Py_END_CRITICAL_SECTION();
1640
0
    if (old_slot_value != NULL) {
1641
        // Free the old slot value if a free function was registered.
1642
        // The caller must ensure no other thread can still access the old
1643
        // value after this overwrite.
1644
0
        freefunc free_extra = interp->co_extra_freefuncs[index];
1645
0
        if (free_extra != NULL) {
1646
0
            free_extra(old_slot_value);
1647
0
        }
1648
0
    }
1649
1650
0
    return result;
1651
0
}
1652
1653
1654
/******************
1655
 * other PyCodeObject accessor functions
1656
 ******************/
1657
1658
static PyObject *
1659
get_cached_locals(PyCodeObject *co, PyObject **cached_field,
1660
    _PyLocals_Kind kind, int num)
1661
97.3k
{
1662
97.3k
    assert(cached_field != NULL);
1663
97.3k
    assert(co->_co_cached != NULL);
1664
97.3k
    PyObject *varnames = FT_ATOMIC_LOAD_PTR(*cached_field);
1665
97.3k
    if (varnames != NULL) {
1666
96.4k
        return Py_NewRef(varnames);
1667
96.4k
    }
1668
1669
887
    Py_BEGIN_CRITICAL_SECTION(co);
1670
887
    varnames = *cached_field;
1671
887
    if (varnames == NULL) {
1672
887
        varnames = get_localsplus_names(co, kind, num);
1673
887
        if (varnames != NULL) {
1674
887
            FT_ATOMIC_STORE_PTR(*cached_field, varnames);
1675
887
        }
1676
887
    }
1677
887
    Py_END_CRITICAL_SECTION();
1678
887
    return Py_XNewRef(varnames);
1679
97.3k
}
1680
1681
PyObject *
1682
_PyCode_GetVarnames(PyCodeObject *co)
1683
55.8k
{
1684
55.8k
    if (init_co_cached(co)) {
1685
0
        return NULL;
1686
0
    }
1687
55.8k
    return get_cached_locals(co, &co->_co_cached->_co_varnames, CO_FAST_LOCAL, co->co_nlocals);
1688
55.8k
}
1689
1690
PyObject *
1691
PyCode_GetVarnames(PyCodeObject *code)
1692
0
{
1693
0
    return _PyCode_GetVarnames(code);
1694
0
}
1695
1696
PyObject *
1697
_PyCode_GetCellvars(PyCodeObject *co)
1698
0
{
1699
0
    if (init_co_cached(co)) {
1700
0
        return NULL;
1701
0
    }
1702
0
    return get_cached_locals(co, &co->_co_cached->_co_cellvars, CO_FAST_CELL, co->co_ncellvars);
1703
0
}
1704
1705
PyObject *
1706
PyCode_GetCellvars(PyCodeObject *code)
1707
0
{
1708
0
    return _PyCode_GetCellvars(code);
1709
0
}
1710
1711
PyObject *
1712
_PyCode_GetFreevars(PyCodeObject *co)
1713
41.5k
{
1714
41.5k
    if (init_co_cached(co)) {
1715
0
        return NULL;
1716
0
    }
1717
41.5k
    return get_cached_locals(co, &co->_co_cached->_co_freevars, CO_FAST_FREE, co->co_nfreevars);
1718
41.5k
}
1719
1720
PyObject *
1721
PyCode_GetFreevars(PyCodeObject *code)
1722
0
{
1723
0
    return _PyCode_GetFreevars(code);
1724
0
}
1725
1726
1727
0
#define GET_OPARG(co, i, initial) (initial)
1728
// We may want to move these macros to pycore_opcode_utils.h
1729
// and use them in Python/bytecodes.c.
1730
#define LOAD_GLOBAL_NAME_INDEX(oparg) ((oparg)>>1)
1731
0
#define LOAD_ATTR_NAME_INDEX(oparg) ((oparg)>>1)
1732
1733
#ifndef Py_DEBUG
1734
0
#define GETITEM(v, i) PyTuple_GET_ITEM((v), (i))
1735
#else
1736
static inline PyObject *
1737
GETITEM(PyObject *v, Py_ssize_t i)
1738
{
1739
    assert(PyTuple_Check(v));
1740
    assert(i >= 0);
1741
    assert(i < PyTuple_GET_SIZE(v));
1742
    assert(PyTuple_GET_ITEM(v, i) != NULL);
1743
    return PyTuple_GET_ITEM(v, i);
1744
}
1745
#endif
1746
1747
static int
1748
identify_unbound_names(PyThreadState *tstate, PyCodeObject *co,
1749
                       PyObject *globalnames, PyObject *attrnames,
1750
                       PyObject *globalsns, PyObject *builtinsns,
1751
                       struct co_unbound_counts *counts, int *p_numdupes)
1752
0
{
1753
    // This function is inspired by inspect.getclosurevars().
1754
    // It would be nicer if we had something similar to co_localspluskinds,
1755
    // but for co_names.
1756
0
    assert(globalnames != NULL);
1757
0
    assert(PySet_Check(globalnames));
1758
0
    assert(PySet_GET_SIZE(globalnames) == 0 || counts != NULL);
1759
0
    assert(attrnames != NULL);
1760
0
    assert(PySet_Check(attrnames));
1761
0
    assert(PySet_GET_SIZE(attrnames) == 0 || counts != NULL);
1762
0
    assert(globalsns == NULL || PyAnyDict_Check(globalsns));
1763
0
    assert(builtinsns == NULL || PyDict_Check(builtinsns));
1764
0
    assert(counts == NULL || counts->total == 0);
1765
0
    struct co_unbound_counts unbound = {0};
1766
0
    int numdupes = 0;
1767
0
    Py_ssize_t len = Py_SIZE(co);
1768
0
    for (int i = 0; i < len; i += _PyInstruction_GetLength(co, i)) {
1769
0
        _Py_CODEUNIT inst = _Py_GetBaseCodeUnit(co, i);
1770
0
        if (inst.op.code == LOAD_ATTR) {
1771
0
            int oparg = GET_OPARG(co, i, inst.op.arg);
1772
0
            int index = LOAD_ATTR_NAME_INDEX(oparg);
1773
0
            PyObject *name = GETITEM(co->co_names, index);
1774
0
            if (PySet_Contains(attrnames, name)) {
1775
0
                if (_PyErr_Occurred(tstate)) {
1776
0
                    return -1;
1777
0
                }
1778
0
                continue;
1779
0
            }
1780
0
            unbound.total += 1;
1781
0
            unbound.numattrs += 1;
1782
0
            if (PySet_Add(attrnames, name) < 0) {
1783
0
                return -1;
1784
0
            }
1785
0
            if (PySet_Contains(globalnames, name)) {
1786
0
                if (_PyErr_Occurred(tstate)) {
1787
0
                    return -1;
1788
0
                }
1789
0
                numdupes += 1;
1790
0
            }
1791
0
        }
1792
0
        else if (inst.op.code == LOAD_GLOBAL) {
1793
0
            int oparg = GET_OPARG(co, i, inst.op.arg);
1794
0
            int index = LOAD_ATTR_NAME_INDEX(oparg);
1795
0
            PyObject *name = GETITEM(co->co_names, index);
1796
0
            if (PySet_Contains(globalnames, name)) {
1797
0
                if (_PyErr_Occurred(tstate)) {
1798
0
                    return -1;
1799
0
                }
1800
0
                continue;
1801
0
            }
1802
0
            unbound.total += 1;
1803
0
            unbound.globals.total += 1;
1804
0
            if (globalsns != NULL && PyDict_Contains(globalsns, name)) {
1805
0
                if (_PyErr_Occurred(tstate)) {
1806
0
                    return -1;
1807
0
                }
1808
0
                unbound.globals.numglobal += 1;
1809
0
            }
1810
0
            else if (builtinsns != NULL && PyDict_Contains(builtinsns, name)) {
1811
0
                if (_PyErr_Occurred(tstate)) {
1812
0
                    return -1;
1813
0
                }
1814
0
                unbound.globals.numbuiltin += 1;
1815
0
            }
1816
0
            else {
1817
0
                unbound.globals.numunknown += 1;
1818
0
            }
1819
0
            if (PySet_Add(globalnames, name) < 0) {
1820
0
                return -1;
1821
0
            }
1822
0
            if (PySet_Contains(attrnames, name)) {
1823
0
                if (_PyErr_Occurred(tstate)) {
1824
0
                    return -1;
1825
0
                }
1826
0
                numdupes += 1;
1827
0
            }
1828
0
        }
1829
0
    }
1830
0
    if (counts != NULL) {
1831
0
        *counts = unbound;
1832
0
    }
1833
0
    if (p_numdupes != NULL) {
1834
0
        *p_numdupes = numdupes;
1835
0
    }
1836
0
    return 0;
1837
0
}
1838
1839
1840
void
1841
_PyCode_GetVarCounts(PyCodeObject *co, _PyCode_var_counts_t *counts)
1842
0
{
1843
0
    assert(counts != NULL);
1844
1845
    // Count the locals, cells, and free vars.
1846
0
    struct co_locals_counts locals = {0};
1847
0
    int numfree = 0;
1848
0
    PyObject *kinds = co->co_localspluskinds;
1849
0
    Py_ssize_t numlocalplusfree = PyBytes_GET_SIZE(kinds);
1850
0
    for (int i = 0; i < numlocalplusfree; i++) {
1851
0
        _PyLocals_Kind kind = _PyLocals_GetKind(co->co_localspluskinds, i);
1852
0
        if (kind & CO_FAST_FREE) {
1853
0
            assert(!(kind & CO_FAST_LOCAL));
1854
0
            assert(!(kind & CO_FAST_HIDDEN));
1855
0
            assert(!(kind & CO_FAST_ARG));
1856
0
            numfree += 1;
1857
0
        }
1858
0
        else {
1859
            // Apparently not all non-free vars a CO_FAST_LOCAL.
1860
0
            assert(kind);
1861
0
            locals.total += 1;
1862
0
            if (kind & CO_FAST_ARG) {
1863
0
                locals.args.total += 1;
1864
0
                if (kind & CO_FAST_ARG_VAR) {
1865
0
                    if (kind & CO_FAST_ARG_POS) {
1866
0
                        assert(!(kind & CO_FAST_ARG_KW));
1867
0
                        assert(!locals.args.varargs);
1868
0
                        locals.args.varargs = 1;
1869
0
                    }
1870
0
                    else {
1871
0
                        assert(kind & CO_FAST_ARG_KW);
1872
0
                        assert(!locals.args.varkwargs);
1873
0
                        locals.args.varkwargs = 1;
1874
0
                    }
1875
0
                }
1876
0
                else if (kind & CO_FAST_ARG_POS) {
1877
0
                    if (kind & CO_FAST_ARG_KW) {
1878
0
                        locals.args.numposorkw += 1;
1879
0
                    }
1880
0
                    else {
1881
0
                        locals.args.numposonly += 1;
1882
0
                    }
1883
0
                }
1884
0
                else {
1885
0
                    assert(kind & CO_FAST_ARG_KW);
1886
0
                    locals.args.numkwonly += 1;
1887
0
                }
1888
0
                if (kind & CO_FAST_CELL) {
1889
0
                    locals.cells.total += 1;
1890
0
                    locals.cells.numargs += 1;
1891
0
                }
1892
                // Args are never hidden currently.
1893
0
                assert(!(kind & CO_FAST_HIDDEN));
1894
0
            }
1895
0
            else {
1896
0
                if (kind & CO_FAST_CELL) {
1897
0
                    locals.cells.total += 1;
1898
0
                    locals.cells.numothers += 1;
1899
0
                    if (kind & CO_FAST_HIDDEN) {
1900
0
                        locals.hidden.total += 1;
1901
0
                        locals.hidden.numcells += 1;
1902
0
                    }
1903
0
                }
1904
0
                else {
1905
0
                    locals.numpure += 1;
1906
0
                    if (kind & CO_FAST_HIDDEN) {
1907
0
                        locals.hidden.total += 1;
1908
0
                        locals.hidden.numpure += 1;
1909
0
                    }
1910
0
                }
1911
0
            }
1912
0
        }
1913
0
    }
1914
0
    assert(locals.args.total == (
1915
0
            co->co_argcount + co->co_kwonlyargcount
1916
0
            + !!(co->co_flags & CO_VARARGS)
1917
0
            + !!(co->co_flags & CO_VARKEYWORDS)));
1918
0
    assert(locals.args.numposonly == co->co_posonlyargcount);
1919
0
    assert(locals.args.numposonly + locals.args.numposorkw == co->co_argcount);
1920
0
    assert(locals.args.numkwonly == co->co_kwonlyargcount);
1921
0
    assert(locals.cells.total == co->co_ncellvars);
1922
0
    assert(locals.args.total + locals.numpure == co->co_nlocals);
1923
0
    assert(locals.total + locals.cells.numargs == co->co_nlocals + co->co_ncellvars);
1924
0
    assert(locals.total + numfree == co->co_nlocalsplus);
1925
0
    assert(numfree == co->co_nfreevars);
1926
1927
    // Get the unbound counts.
1928
0
    assert(PyTuple_GET_SIZE(co->co_names) >= 0);
1929
0
    assert(PyTuple_GET_SIZE(co->co_names) < INT_MAX);
1930
0
    int numunbound = (int)PyTuple_GET_SIZE(co->co_names);
1931
0
    struct co_unbound_counts unbound = {
1932
0
        .total = numunbound,
1933
        // numglobal and numattrs can be set later
1934
        // with _PyCode_SetUnboundVarCounts().
1935
0
        .numunknown = numunbound,
1936
0
    };
1937
1938
    // "Return" the result.
1939
0
    *counts = (_PyCode_var_counts_t){
1940
0
        .total = locals.total + numfree + unbound.total,
1941
0
        .locals = locals,
1942
0
        .numfree = numfree,
1943
0
        .unbound = unbound,
1944
0
    };
1945
0
}
1946
1947
int
1948
_PyCode_SetUnboundVarCounts(PyThreadState *tstate,
1949
                            PyCodeObject *co, _PyCode_var_counts_t *counts,
1950
                            PyObject *globalnames, PyObject *attrnames,
1951
                            PyObject *globalsns, PyObject *builtinsns)
1952
0
{
1953
0
    int res = -1;
1954
0
    PyObject *globalnames_owned = NULL;
1955
0
    PyObject *attrnames_owned = NULL;
1956
1957
    // Prep the name sets.
1958
0
    if (globalnames == NULL) {
1959
0
        globalnames_owned = PySet_New(NULL);
1960
0
        if (globalnames_owned == NULL) {
1961
0
            goto finally;
1962
0
        }
1963
0
        globalnames = globalnames_owned;
1964
0
    }
1965
0
    else if (!PySet_Check(globalnames)) {
1966
0
        _PyErr_Format(tstate, PyExc_TypeError,
1967
0
                     "expected a set for \"globalnames\", got %R", globalnames);
1968
0
        goto finally;
1969
0
    }
1970
0
    if (attrnames == NULL) {
1971
0
        attrnames_owned = PySet_New(NULL);
1972
0
        if (attrnames_owned == NULL) {
1973
0
            goto finally;
1974
0
        }
1975
0
        attrnames = attrnames_owned;
1976
0
    }
1977
0
    else if (!PySet_Check(attrnames)) {
1978
0
        _PyErr_Format(tstate, PyExc_TypeError,
1979
0
                     "expected a set for \"attrnames\", got %R", attrnames);
1980
0
        goto finally;
1981
0
    }
1982
1983
    // Fill in unbound.globals and unbound.numattrs.
1984
0
    struct co_unbound_counts unbound = {0};
1985
0
    int numdupes = 0;
1986
0
    Py_BEGIN_CRITICAL_SECTION(co);
1987
0
    res = identify_unbound_names(
1988
0
            tstate, co, globalnames, attrnames, globalsns, builtinsns,
1989
0
            &unbound, &numdupes);
1990
0
    Py_END_CRITICAL_SECTION();
1991
0
    if (res < 0) {
1992
0
        goto finally;
1993
0
    }
1994
0
    assert(unbound.numunknown == 0);
1995
0
    assert(unbound.total - numdupes <= counts->unbound.total);
1996
0
    assert(counts->unbound.numunknown == counts->unbound.total);
1997
    // There may be a name that is both a global and an attr.
1998
0
    int totalunbound = counts->unbound.total + numdupes;
1999
0
    unbound.numunknown = totalunbound - unbound.total;
2000
0
    unbound.total = totalunbound;
2001
0
    counts->unbound = unbound;
2002
0
    counts->total += numdupes;
2003
0
    res = 0;
2004
2005
0
finally:
2006
0
    Py_XDECREF(globalnames_owned);
2007
0
    Py_XDECREF(attrnames_owned);
2008
0
    return res;
2009
0
}
2010
2011
2012
int
2013
_PyCode_CheckNoInternalState(PyCodeObject *co, const char **p_errmsg)
2014
0
{
2015
0
    const char *errmsg = NULL;
2016
    // We don't worry about co_executors, co_instrumentation,
2017
    // or co_monitoring.  They are essentially ephemeral.
2018
0
    if (co->co_extra != NULL) {
2019
0
        errmsg = "only basic code objects are supported";
2020
0
    }
2021
2022
0
    if (errmsg != NULL) {
2023
0
        if (p_errmsg != NULL) {
2024
0
            *p_errmsg = errmsg;
2025
0
        }
2026
0
        return 0;
2027
0
    }
2028
0
    return 1;
2029
0
}
2030
2031
int
2032
_PyCode_CheckNoExternalState(PyCodeObject *co, _PyCode_var_counts_t *counts,
2033
                             const char **p_errmsg)
2034
0
{
2035
0
    const char *errmsg = NULL;
2036
0
    if (counts->numfree > 0) {  // It's a closure.
2037
0
        errmsg = "closures not supported";
2038
0
    }
2039
0
    else if (counts->unbound.globals.numglobal > 0) {
2040
0
        errmsg = "globals not supported";
2041
0
    }
2042
0
    else if (counts->unbound.globals.numbuiltin > 0
2043
0
             && counts->unbound.globals.numunknown > 0)
2044
0
    {
2045
0
        errmsg = "globals not supported";
2046
0
    }
2047
    // Otherwise we don't check counts.unbound.globals.numunknown since we can't
2048
    // distinguish beween globals and builtins here.
2049
2050
0
    if (errmsg != NULL) {
2051
0
        if (p_errmsg != NULL) {
2052
0
            *p_errmsg = errmsg;
2053
0
        }
2054
0
        return 0;
2055
0
    }
2056
0
    return 1;
2057
0
}
2058
2059
int
2060
_PyCode_VerifyStateless(PyThreadState *tstate,
2061
                        PyCodeObject *co, PyObject *globalnames,
2062
                        PyObject *globalsns, PyObject *builtinsns)
2063
0
{
2064
0
    const char *errmsg;
2065
0
   _PyCode_var_counts_t counts = {0};
2066
0
    _PyCode_GetVarCounts(co, &counts);
2067
0
    if (_PyCode_SetUnboundVarCounts(
2068
0
                            tstate, co, &counts, globalnames, NULL,
2069
0
                            globalsns, builtinsns) < 0)
2070
0
    {
2071
0
        return -1;
2072
0
    }
2073
    // We may consider relaxing the internal state constraints
2074
    // if it becomes a problem.
2075
0
    if (!_PyCode_CheckNoInternalState(co, &errmsg)) {
2076
0
        _PyErr_SetString(tstate, PyExc_ValueError, errmsg);
2077
0
        return -1;
2078
0
    }
2079
0
    if (builtinsns != NULL) {
2080
        // Make sure the next check will fail for globals,
2081
        // even if there aren't any builtins.
2082
0
        counts.unbound.globals.numbuiltin += 1;
2083
0
    }
2084
0
    if (!_PyCode_CheckNoExternalState(co, &counts, &errmsg)) {
2085
0
        _PyErr_SetString(tstate, PyExc_ValueError, errmsg);
2086
0
        return -1;
2087
0
    }
2088
    // Note that we don't check co->co_flags & CO_NESTED for anything here.
2089
0
    return 0;
2090
0
}
2091
2092
2093
int
2094
_PyCode_CheckPureFunction(PyCodeObject *co, const char **p_errmsg)
2095
0
{
2096
0
    const char *errmsg = NULL;
2097
0
    if (co->co_flags & CO_GENERATOR) {
2098
0
        errmsg = "generators not supported";
2099
0
    }
2100
0
    else if (co->co_flags & CO_COROUTINE) {
2101
0
        errmsg = "coroutines not supported";
2102
0
    }
2103
0
    else if (co->co_flags & CO_ITERABLE_COROUTINE) {
2104
0
        errmsg = "coroutines not supported";
2105
0
    }
2106
0
    else if (co->co_flags & CO_ASYNC_GENERATOR) {
2107
0
        errmsg = "generators not supported";
2108
0
    }
2109
2110
0
    if (errmsg != NULL) {
2111
0
        if (p_errmsg != NULL) {
2112
0
            *p_errmsg = errmsg;
2113
0
        }
2114
0
        return 0;
2115
0
    }
2116
0
    return 1;
2117
0
}
2118
2119
/* Here "value" means a non-None value, since a bare return is identical
2120
 * to returning None explicitly.  Likewise a missing return statement
2121
 * at the end of the function is turned into "return None". */
2122
static int
2123
code_returns_only_none(PyCodeObject *co)
2124
0
{
2125
0
    if (!_PyCode_CheckPureFunction(co, NULL)) {
2126
0
        return 0;
2127
0
    }
2128
0
    int len = (int)Py_SIZE(co);
2129
0
    assert(len > 0);
2130
2131
    // Look up None in co_consts.
2132
0
    Py_ssize_t nconsts = PyTuple_Size(co->co_consts);
2133
0
    int none_index = 0;
2134
0
    for (; none_index < nconsts; none_index++) {
2135
0
        if (PyTuple_GET_ITEM(co->co_consts, none_index) == Py_None) {
2136
0
            break;
2137
0
        }
2138
0
    }
2139
    /* We don't worry about EXTENDED_ARG for now. */
2140
0
    for (int i = 0; i < len; i += _PyInstruction_GetLength(co, i)) {
2141
0
        _Py_CODEUNIT inst = _Py_GetBaseCodeUnit(co, i);
2142
0
        if (!IS_RETURN_OPCODE(inst.op.code)) {
2143
0
            continue;
2144
0
        }
2145
0
        assert(i != 0);
2146
0
        _Py_CODEUNIT prev = _Py_GetBaseCodeUnit(co, i-1);
2147
0
        if (prev.op.code == LOAD_COMMON_CONSTANT &&
2148
0
            prev.op.arg == CONSTANT_NONE)
2149
0
        {
2150
0
            continue;
2151
0
        }
2152
0
        if (none_index < nconsts && prev.op.code == LOAD_CONST
2153
0
            && prev.op.arg == none_index)
2154
0
        {
2155
0
            continue;
2156
0
        }
2157
0
        return 0;
2158
0
    }
2159
0
    return 1;
2160
0
}
2161
2162
int
2163
_PyCode_ReturnsOnlyNone(PyCodeObject *co)
2164
0
{
2165
0
    int res;
2166
0
    Py_BEGIN_CRITICAL_SECTION(co);
2167
0
    res = code_returns_only_none(co);
2168
0
    Py_END_CRITICAL_SECTION();
2169
0
    return res;
2170
0
}
2171
2172
2173
#ifdef _Py_TIER2
2174
2175
static void
2176
clear_executors(PyCodeObject *co)
2177
{
2178
    assert(co->co_executors);
2179
    for (int i = 0; i < co->co_executors->size; i++) {
2180
        if (co->co_executors->executors[i]) {
2181
            _Py_ExecutorDetach(co->co_executors->executors[i]);
2182
            assert(co->co_executors->executors[i] == NULL);
2183
        }
2184
    }
2185
    PyMem_Free(co->co_executors);
2186
    co->co_executors = NULL;
2187
}
2188
2189
void
2190
_PyCode_Clear_Executors(PyCodeObject *code)
2191
{
2192
    clear_executors(code);
2193
}
2194
2195
#endif
2196
2197
static void
2198
deopt_code(PyCodeObject *code, _Py_CODEUNIT *instructions)
2199
3.63k
{
2200
3.63k
    Py_ssize_t len = Py_SIZE(code);
2201
158k
    for (int i = 0; i < len; i++) {
2202
155k
        _Py_CODEUNIT inst = _Py_GetBaseCodeUnit(code, i);
2203
155k
        assert(inst.op.code < MIN_SPECIALIZED_OPCODE);
2204
155k
        int caches = _PyOpcode_Caches[inst.op.code];
2205
155k
        instructions[i] = inst;
2206
337k
        for (int j = 1; j <= caches; j++) {
2207
182k
            instructions[i+j].cache = 0;
2208
182k
        }
2209
155k
        i += caches;
2210
155k
    }
2211
3.63k
}
2212
2213
PyObject *
2214
_PyCode_GetCode(PyCodeObject *co)
2215
240k
{
2216
240k
    if (init_co_cached(co)) {
2217
0
        return NULL;
2218
0
    }
2219
2220
240k
    _PyCoCached *cached = co->_co_cached;
2221
240k
    PyObject *code = FT_ATOMIC_LOAD_PTR(cached->_co_code);
2222
240k
    if (code != NULL) {
2223
236k
        return Py_NewRef(code);
2224
236k
    }
2225
2226
3.63k
    Py_BEGIN_CRITICAL_SECTION(co);
2227
3.63k
    code = cached->_co_code;
2228
3.63k
    if (code == NULL) {
2229
3.63k
        code = PyBytes_FromStringAndSize((const char *)_PyCode_CODE(co),
2230
3.63k
                                         _PyCode_NBYTES(co));
2231
3.63k
        if (code != NULL) {
2232
3.63k
            deopt_code(co, (_Py_CODEUNIT *)PyBytes_AS_STRING(code));
2233
3.63k
            assert(cached->_co_code == NULL);
2234
3.63k
            FT_ATOMIC_STORE_PTR(cached->_co_code, code);
2235
3.63k
        }
2236
3.63k
    }
2237
3.63k
    Py_END_CRITICAL_SECTION();
2238
3.63k
    return Py_XNewRef(code);
2239
240k
}
2240
2241
PyObject *
2242
PyCode_GetCode(PyCodeObject *co)
2243
0
{
2244
0
    return _PyCode_GetCode(co);
2245
0
}
2246
2247
/******************
2248
 * PyCode_Type
2249
 ******************/
2250
2251
/*[clinic input]
2252
class code "PyCodeObject *" "&PyCode_Type"
2253
[clinic start generated code]*/
2254
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=78aa5d576683bb4b]*/
2255
2256
/*[clinic input]
2257
@classmethod
2258
code.__new__ as code_new
2259
2260
    argcount: int
2261
    posonlyargcount: int
2262
    kwonlyargcount: int
2263
    nlocals: int
2264
    stacksize: int
2265
    flags: int
2266
    codestring as code: object(subclass_of="&PyBytes_Type")
2267
    constants as consts: object(subclass_of="&PyTuple_Type")
2268
    names: object(subclass_of="&PyTuple_Type")
2269
    varnames: object(subclass_of="&PyTuple_Type")
2270
    filename: unicode
2271
    name: unicode
2272
    qualname: unicode
2273
    firstlineno: int
2274
    linetable: object(subclass_of="&PyBytes_Type")
2275
    exceptiontable: object(subclass_of="&PyBytes_Type")
2276
    freevars: object(subclass_of="&PyTuple_Type", c_default="NULL") = ()
2277
    cellvars: object(subclass_of="&PyTuple_Type", c_default="NULL") = ()
2278
    /
2279
2280
Create a code object.  Not for the faint of heart.
2281
[clinic start generated code]*/
2282
2283
static PyObject *
2284
code_new_impl(PyTypeObject *type, int argcount, int posonlyargcount,
2285
              int kwonlyargcount, int nlocals, int stacksize, int flags,
2286
              PyObject *code, PyObject *consts, PyObject *names,
2287
              PyObject *varnames, PyObject *filename, PyObject *name,
2288
              PyObject *qualname, int firstlineno, PyObject *linetable,
2289
              PyObject *exceptiontable, PyObject *freevars,
2290
              PyObject *cellvars)
2291
/*[clinic end generated code: output=069fa20d299f9dda input=e31da3c41ad8064a]*/
2292
0
{
2293
0
    PyObject *co = NULL;
2294
0
    PyObject *ournames = NULL;
2295
0
    PyObject *ourvarnames = NULL;
2296
0
    PyObject *ourfreevars = NULL;
2297
0
    PyObject *ourcellvars = NULL;
2298
2299
0
    if (PySys_Audit("code.__new__", "OOOiiiiii",
2300
0
                    code, filename, name, argcount, posonlyargcount,
2301
0
                    kwonlyargcount, nlocals, stacksize, flags) < 0) {
2302
0
        goto cleanup;
2303
0
    }
2304
2305
0
    if (argcount < 0) {
2306
0
        PyErr_SetString(
2307
0
            PyExc_ValueError,
2308
0
            "code: argcount must not be negative");
2309
0
        goto cleanup;
2310
0
    }
2311
2312
0
    if (posonlyargcount < 0) {
2313
0
        PyErr_SetString(
2314
0
            PyExc_ValueError,
2315
0
            "code: posonlyargcount must not be negative");
2316
0
        goto cleanup;
2317
0
    }
2318
2319
0
    if (kwonlyargcount < 0) {
2320
0
        PyErr_SetString(
2321
0
            PyExc_ValueError,
2322
0
            "code: kwonlyargcount must not be negative");
2323
0
        goto cleanup;
2324
0
    }
2325
0
    if (nlocals < 0) {
2326
0
        PyErr_SetString(
2327
0
            PyExc_ValueError,
2328
0
            "code: nlocals must not be negative");
2329
0
        goto cleanup;
2330
0
    }
2331
2332
0
    ournames = validate_and_copy_tuple(names);
2333
0
    if (ournames == NULL)
2334
0
        goto cleanup;
2335
0
    ourvarnames = validate_and_copy_tuple(varnames);
2336
0
    if (ourvarnames == NULL)
2337
0
        goto cleanup;
2338
0
    if (freevars)
2339
0
        ourfreevars = validate_and_copy_tuple(freevars);
2340
0
    else
2341
0
        ourfreevars = PyTuple_New(0);
2342
0
    if (ourfreevars == NULL)
2343
0
        goto cleanup;
2344
0
    if (cellvars)
2345
0
        ourcellvars = validate_and_copy_tuple(cellvars);
2346
0
    else
2347
0
        ourcellvars = PyTuple_New(0);
2348
0
    if (ourcellvars == NULL)
2349
0
        goto cleanup;
2350
2351
0
    co = (PyObject *)PyCode_NewWithPosOnlyArgs(argcount, posonlyargcount,
2352
0
                                               kwonlyargcount,
2353
0
                                               nlocals, stacksize, flags,
2354
0
                                               code, consts, ournames,
2355
0
                                               ourvarnames, ourfreevars,
2356
0
                                               ourcellvars, filename,
2357
0
                                               name, qualname, firstlineno,
2358
0
                                               linetable,
2359
0
                                               exceptiontable
2360
0
                                              );
2361
0
  cleanup:
2362
0
    Py_XDECREF(ournames);
2363
0
    Py_XDECREF(ourvarnames);
2364
0
    Py_XDECREF(ourfreevars);
2365
0
    Py_XDECREF(ourcellvars);
2366
0
    return co;
2367
0
}
2368
2369
static void
2370
free_monitoring_data(_PyCoMonitoringData *data)
2371
99.3k
{
2372
99.3k
    if (data == NULL) {
2373
99.3k
        return;
2374
99.3k
    }
2375
0
    if (data->tools) {
2376
0
        PyMem_Free(data->tools);
2377
0
    }
2378
0
    if (data->lines) {
2379
0
        PyMem_Free(data->lines);
2380
0
    }
2381
0
    if (data->line_tools) {
2382
0
        PyMem_Free(data->line_tools);
2383
0
    }
2384
0
    if (data->per_instruction_opcodes) {
2385
0
        PyMem_Free(data->per_instruction_opcodes);
2386
0
    }
2387
0
    if (data->per_instruction_tools) {
2388
0
        PyMem_Free(data->per_instruction_tools);
2389
0
    }
2390
0
    PyMem_Free(data);
2391
0
}
2392
2393
static void
2394
code_dealloc(PyObject *self)
2395
99.3k
{
2396
99.3k
    PyThreadState *tstate = PyThreadState_GET();
2397
99.3k
    _Py_atomic_add_uint64(&tstate->interp->_code_object_generation, 1);
2398
99.3k
    PyCodeObject *co = _PyCodeObject_CAST(self);
2399
99.3k
    _PyObject_ResurrectStart(self);
2400
99.3k
    notify_code_watchers(PY_CODE_EVENT_DESTROY, co);
2401
99.3k
    if (_PyObject_ResurrectEnd(self)) {
2402
0
        return;
2403
0
    }
2404
2405
#ifdef Py_GIL_DISABLED
2406
    PyObject_GC_UnTrack(co);
2407
#endif
2408
2409
99.3k
    _PyFunction_ClearCodeByVersion(co->co_version);
2410
99.3k
    if (co->co_extra != NULL) {
2411
0
        PyInterpreterState *interp = _PyInterpreterState_GET();
2412
0
        _PyCodeObjectExtra *co_extra = co->co_extra;
2413
2414
0
        for (Py_ssize_t i = 0; i < co_extra->ce_size; i++) {
2415
0
            freefunc free_extra = interp->co_extra_freefuncs[i];
2416
2417
0
            if (free_extra != NULL) {
2418
0
                free_extra(co_extra->ce_extras[i]);
2419
0
            }
2420
0
        }
2421
2422
0
        PyMem_Free(co_extra);
2423
0
    }
2424
#ifdef _Py_TIER2
2425
    if (co->co_executors != NULL) {
2426
        clear_executors(co);
2427
    }
2428
#endif
2429
2430
99.3k
    Py_XDECREF(co->co_consts);
2431
99.3k
    Py_XDECREF(co->co_names);
2432
99.3k
    Py_XDECREF(co->co_localsplusnames);
2433
99.3k
    Py_XDECREF(co->co_localspluskinds);
2434
99.3k
    Py_XDECREF(co->co_filename);
2435
99.3k
    Py_XDECREF(co->co_name);
2436
99.3k
    Py_XDECREF(co->co_qualname);
2437
99.3k
    Py_XDECREF(co->co_linetable);
2438
99.3k
    Py_XDECREF(co->co_exceptiontable);
2439
#ifdef Py_GIL_DISABLED
2440
    assert(co->_co_unique_id == _Py_INVALID_UNIQUE_ID);
2441
#endif
2442
99.3k
    if (co->_co_cached != NULL) {
2443
949
        Py_XDECREF(co->_co_cached->_co_code);
2444
949
        Py_XDECREF(co->_co_cached->_co_cellvars);
2445
949
        Py_XDECREF(co->_co_cached->_co_freevars);
2446
949
        Py_XDECREF(co->_co_cached->_co_varnames);
2447
949
        PyMem_Free(co->_co_cached);
2448
949
    }
2449
99.3k
    FT_CLEAR_WEAKREFS(self, co->co_weakreflist);
2450
99.3k
    free_monitoring_data(co->_co_monitoring);
2451
#ifdef Py_GIL_DISABLED
2452
    // The first element always points to the mutable bytecode at the end of
2453
    // the code object, which will be freed when the code object is freed.
2454
    for (Py_ssize_t i = 1; i < co->co_tlbc->size; i++) {
2455
        char *entry = co->co_tlbc->entries[i];
2456
        if (entry != NULL) {
2457
            PyMem_Free(entry);
2458
        }
2459
    }
2460
    PyMem_Free(co->co_tlbc);
2461
#endif
2462
99.3k
    PyObject_Free(co);
2463
99.3k
}
2464
2465
#ifdef Py_GIL_DISABLED
2466
static int
2467
code_traverse(PyObject *self, visitproc visit, void *arg)
2468
{
2469
    PyCodeObject *co = _PyCodeObject_CAST(self);
2470
    Py_VISIT(co->co_consts);
2471
    return 0;
2472
}
2473
#endif
2474
2475
static PyObject *
2476
code_repr(PyObject *self)
2477
1
{
2478
1
    PyCodeObject *co = _PyCodeObject_CAST(self);
2479
1
    int lineno;
2480
1
    if (co->co_firstlineno != 0)
2481
1
        lineno = co->co_firstlineno;
2482
0
    else
2483
0
        lineno = -1;
2484
1
    if (co->co_filename && PyUnicode_Check(co->co_filename)) {
2485
1
        return PyUnicode_FromFormat(
2486
1
            "<code object %U at %p, file \"%U\", line %d>",
2487
1
            co->co_name, co, co->co_filename, lineno);
2488
1
    } else {
2489
0
        return PyUnicode_FromFormat(
2490
0
            "<code object %U at %p, file ???, line %d>",
2491
0
            co->co_name, co, lineno);
2492
0
    }
2493
1
}
2494
2495
static PyObject *
2496
code_richcompare(PyObject *self, PyObject *other, int op)
2497
17
{
2498
17
    PyCodeObject *co, *cp;
2499
17
    int eq;
2500
17
    PyObject *consts1, *consts2;
2501
17
    PyObject *res;
2502
2503
17
    if ((op != Py_EQ && op != Py_NE) ||
2504
17
        !PyCode_Check(self) ||
2505
17
        !PyCode_Check(other)) {
2506
0
        Py_RETURN_NOTIMPLEMENTED;
2507
0
    }
2508
2509
17
    co = (PyCodeObject *)self;
2510
17
    cp = (PyCodeObject *)other;
2511
2512
17
    eq = PyObject_RichCompareBool(co->co_name, cp->co_name, Py_EQ);
2513
17
    if (eq <= 0) goto unequal;
2514
17
    eq = co->co_argcount == cp->co_argcount;
2515
17
    if (!eq) goto unequal;
2516
17
    eq = co->co_posonlyargcount == cp->co_posonlyargcount;
2517
17
    if (!eq) goto unequal;
2518
17
    eq = co->co_kwonlyargcount == cp->co_kwonlyargcount;
2519
17
    if (!eq) goto unequal;
2520
17
    eq = co->co_flags == cp->co_flags;
2521
17
    if (!eq) goto unequal;
2522
17
    eq = co->co_firstlineno == cp->co_firstlineno;
2523
17
    if (!eq) goto unequal;
2524
17
    eq = Py_SIZE(co) == Py_SIZE(cp);
2525
17
    if (!eq) {
2526
0
        goto unequal;
2527
0
    }
2528
440
    for (int i = 0; i < Py_SIZE(co); i++) {
2529
423
        _Py_CODEUNIT co_instr = _Py_GetBaseCodeUnit(co, i);
2530
423
        _Py_CODEUNIT cp_instr = _Py_GetBaseCodeUnit(cp, i);
2531
423
        if (co_instr.cache != cp_instr.cache) {
2532
0
            goto unequal;
2533
0
        }
2534
423
        i += _PyOpcode_Caches[co_instr.op.code];
2535
423
    }
2536
2537
    /* compare constants */
2538
17
    consts1 = _PyCode_ConstantKey(co->co_consts);
2539
17
    if (!consts1)
2540
0
        return NULL;
2541
17
    consts2 = _PyCode_ConstantKey(cp->co_consts);
2542
17
    if (!consts2) {
2543
0
        Py_DECREF(consts1);
2544
0
        return NULL;
2545
0
    }
2546
17
    eq = PyObject_RichCompareBool(consts1, consts2, Py_EQ);
2547
17
    Py_DECREF(consts1);
2548
17
    Py_DECREF(consts2);
2549
17
    if (eq <= 0) goto unequal;
2550
2551
17
    eq = PyObject_RichCompareBool(co->co_names, cp->co_names, Py_EQ);
2552
17
    if (eq <= 0) goto unequal;
2553
17
    eq = PyObject_RichCompareBool(co->co_localsplusnames,
2554
17
                                  cp->co_localsplusnames, Py_EQ);
2555
17
    if (eq <= 0) goto unequal;
2556
17
    eq = PyObject_RichCompareBool(co->co_linetable, cp->co_linetable, Py_EQ);
2557
17
    if (eq <= 0) {
2558
0
        goto unequal;
2559
0
    }
2560
17
    eq = PyObject_RichCompareBool(co->co_exceptiontable,
2561
17
                                  cp->co_exceptiontable, Py_EQ);
2562
17
    if (eq <= 0) {
2563
0
        goto unequal;
2564
0
    }
2565
2566
17
    if (op == Py_EQ)
2567
17
        res = Py_True;
2568
0
    else
2569
0
        res = Py_False;
2570
17
    goto done;
2571
2572
0
  unequal:
2573
0
    if (eq < 0)
2574
0
        return NULL;
2575
0
    if (op == Py_NE)
2576
0
        res = Py_True;
2577
0
    else
2578
0
        res = Py_False;
2579
2580
17
  done:
2581
17
    return Py_NewRef(res);
2582
0
}
2583
2584
static Py_hash_t
2585
code_hash(PyObject *self)
2586
22.8k
{
2587
22.8k
    PyCodeObject *co = _PyCodeObject_CAST(self);
2588
22.8k
    Py_uhash_t uhash = 20221211;
2589
1.80M
    #define SCRAMBLE_IN(H) do {       \
2590
1.80M
        uhash ^= (Py_uhash_t)(H);     \
2591
1.80M
        uhash *= PyHASH_MULTIPLIER;  \
2592
1.80M
    } while (0)
2593
137k
    #define SCRAMBLE_IN_HASH(EXPR) do {     \
2594
137k
        Py_hash_t h = PyObject_Hash(EXPR);  \
2595
137k
        if (h == -1) {                      \
2596
0
            return -1;                      \
2597
0
        }                                   \
2598
137k
        SCRAMBLE_IN(h);                     \
2599
137k
    } while (0)
2600
2601
22.8k
    SCRAMBLE_IN_HASH(co->co_name);
2602
22.8k
    SCRAMBLE_IN_HASH(co->co_consts);
2603
22.8k
    SCRAMBLE_IN_HASH(co->co_names);
2604
22.8k
    SCRAMBLE_IN_HASH(co->co_localsplusnames);
2605
22.8k
    SCRAMBLE_IN_HASH(co->co_linetable);
2606
22.8k
    SCRAMBLE_IN_HASH(co->co_exceptiontable);
2607
22.8k
    SCRAMBLE_IN(co->co_argcount);
2608
22.8k
    SCRAMBLE_IN(co->co_posonlyargcount);
2609
22.8k
    SCRAMBLE_IN(co->co_kwonlyargcount);
2610
22.8k
    SCRAMBLE_IN(co->co_flags);
2611
22.8k
    SCRAMBLE_IN(co->co_firstlineno);
2612
22.8k
    SCRAMBLE_IN(Py_SIZE(co));
2613
787k
    for (int i = 0; i < Py_SIZE(co); i++) {
2614
764k
        _Py_CODEUNIT co_instr = _Py_GetBaseCodeUnit(co, i);
2615
764k
        SCRAMBLE_IN(co_instr.op.code);
2616
764k
        SCRAMBLE_IN(co_instr.op.arg);
2617
764k
        i += _PyOpcode_Caches[co_instr.op.code];
2618
764k
    }
2619
22.8k
    if ((Py_hash_t)uhash == -1) {
2620
0
        return -2;
2621
0
    }
2622
22.8k
    return (Py_hash_t)uhash;
2623
22.8k
}
2624
2625
2626
#define OFF(x) offsetof(PyCodeObject, x)
2627
2628
static PyMemberDef code_memberlist[] = {
2629
    {"co_argcount",        Py_T_INT,     OFF(co_argcount),        Py_READONLY},
2630
    {"co_posonlyargcount", Py_T_INT,     OFF(co_posonlyargcount), Py_READONLY},
2631
    {"co_kwonlyargcount",  Py_T_INT,     OFF(co_kwonlyargcount),  Py_READONLY},
2632
    {"co_stacksize",       Py_T_INT,     OFF(co_stacksize),       Py_READONLY},
2633
    {"co_flags",           Py_T_INT,     OFF(co_flags),           Py_READONLY},
2634
    {"co_nlocals",         Py_T_INT,     OFF(co_nlocals),         Py_READONLY},
2635
    {"co_consts",          _Py_T_OBJECT, OFF(co_consts),          Py_READONLY},
2636
    {"co_names",           _Py_T_OBJECT, OFF(co_names),           Py_READONLY},
2637
    {"co_filename",        _Py_T_OBJECT, OFF(co_filename),        Py_READONLY},
2638
    {"co_name",            _Py_T_OBJECT, OFF(co_name),            Py_READONLY},
2639
    {"co_qualname",        _Py_T_OBJECT, OFF(co_qualname),        Py_READONLY},
2640
    {"co_firstlineno",     Py_T_INT,     OFF(co_firstlineno),     Py_READONLY},
2641
    {"co_linetable",       _Py_T_OBJECT, OFF(co_linetable),       Py_READONLY},
2642
    {"co_exceptiontable",  _Py_T_OBJECT, OFF(co_exceptiontable),  Py_READONLY},
2643
    {NULL}      /* Sentinel */
2644
};
2645
2646
2647
static PyObject *
2648
code_getvarnames(PyObject *self, void *closure)
2649
55.8k
{
2650
55.8k
    PyCodeObject *code = _PyCodeObject_CAST(self);
2651
55.8k
    return _PyCode_GetVarnames(code);
2652
55.8k
}
2653
2654
static PyObject *
2655
code_getcellvars(PyObject *self, void *closure)
2656
0
{
2657
0
    PyCodeObject *code = _PyCodeObject_CAST(self);
2658
0
    return _PyCode_GetCellvars(code);
2659
0
}
2660
2661
static PyObject *
2662
code_getfreevars(PyObject *self, void *closure)
2663
41.5k
{
2664
41.5k
    PyCodeObject *code = _PyCodeObject_CAST(self);
2665
41.5k
    return _PyCode_GetFreevars(code);
2666
41.5k
}
2667
2668
static PyObject *
2669
code_getcodeadaptive(PyObject *self, void *closure)
2670
0
{
2671
0
    PyCodeObject *code = _PyCodeObject_CAST(self);
2672
0
    return PyBytes_FromStringAndSize(code->co_code_adaptive,
2673
0
                                     _PyCode_NBYTES(code));
2674
0
}
2675
2676
static PyObject *
2677
code_getcode(PyObject *self, void *closure)
2678
229k
{
2679
229k
    PyCodeObject *code = _PyCodeObject_CAST(self);
2680
229k
    return _PyCode_GetCode(code);
2681
229k
}
2682
2683
static PyGetSetDef code_getsetlist[] = {
2684
    {"_co_code_adaptive", code_getcodeadaptive, NULL, NULL},
2685
    // The following old names are kept for backward compatibility.
2686
    {"co_varnames",       code_getvarnames,     NULL, NULL},
2687
    {"co_cellvars",       code_getcellvars,     NULL, NULL},
2688
    {"co_freevars",       code_getfreevars,     NULL, NULL},
2689
    {"co_code",           code_getcode,         NULL, NULL},
2690
    {0}
2691
};
2692
2693
2694
static PyObject *
2695
code_sizeof(PyObject *self, PyObject *Py_UNUSED(args))
2696
0
{
2697
0
    PyCodeObject *co = _PyCodeObject_CAST(self);
2698
0
    size_t res = _PyObject_VAR_SIZE(Py_TYPE(co), Py_SIZE(co));
2699
0
    _PyCodeObjectExtra *co_extra = (_PyCodeObjectExtra*) co->co_extra;
2700
0
    if (co_extra != NULL) {
2701
0
        res += sizeof(_PyCodeObjectExtra);
2702
0
        res += ((size_t)co_extra->ce_size - 1) * sizeof(co_extra->ce_extras[0]);
2703
0
    }
2704
0
    return PyLong_FromSize_t(res);
2705
0
}
2706
2707
static PyObject *
2708
code_linesiterator(PyObject *self, PyObject *Py_UNUSED(args))
2709
0
{
2710
0
    PyCodeObject *code = _PyCodeObject_CAST(self);
2711
0
    return (PyObject *)new_linesiterator(code);
2712
0
}
2713
2714
static PyObject *
2715
code_branchesiterator(PyObject *self, PyObject *Py_UNUSED(args))
2716
0
{
2717
0
    PyCodeObject *code = _PyCodeObject_CAST(self);
2718
0
    return _PyInstrumentation_BranchesIterator(code);
2719
0
}
2720
2721
/*[clinic input]
2722
@permit_long_summary
2723
@text_signature "($self, /, **changes)"
2724
code.replace
2725
2726
    *
2727
    co_argcount: int(c_default="((PyCodeObject *)self)->co_argcount") = unchanged
2728
    co_posonlyargcount: int(c_default="((PyCodeObject *)self)->co_posonlyargcount") = unchanged
2729
    co_kwonlyargcount: int(c_default="((PyCodeObject *)self)->co_kwonlyargcount") = unchanged
2730
    co_nlocals: int(c_default="((PyCodeObject *)self)->co_nlocals") = unchanged
2731
    co_stacksize: int(c_default="((PyCodeObject *)self)->co_stacksize") = unchanged
2732
    co_flags: int(c_default="((PyCodeObject *)self)->co_flags") = unchanged
2733
    co_firstlineno: int(c_default="((PyCodeObject *)self)->co_firstlineno") = unchanged
2734
    co_code: object(subclass_of="&PyBytes_Type", c_default="NULL") = unchanged
2735
    co_consts: object(subclass_of="&PyTuple_Type", c_default="((PyCodeObject *)self)->co_consts") = unchanged
2736
    co_names: object(subclass_of="&PyTuple_Type", c_default="((PyCodeObject *)self)->co_names") = unchanged
2737
    co_varnames: object(subclass_of="&PyTuple_Type", c_default="NULL") = unchanged
2738
    co_freevars: object(subclass_of="&PyTuple_Type", c_default="NULL") = unchanged
2739
    co_cellvars: object(subclass_of="&PyTuple_Type", c_default="NULL") = unchanged
2740
    co_filename: unicode(c_default="((PyCodeObject *)self)->co_filename") = unchanged
2741
    co_name: unicode(c_default="((PyCodeObject *)self)->co_name") = unchanged
2742
    co_qualname: unicode(c_default="((PyCodeObject *)self)->co_qualname") = unchanged
2743
    co_linetable: object(subclass_of="&PyBytes_Type", c_default="((PyCodeObject *)self)->co_linetable") = unchanged
2744
    co_exceptiontable: object(subclass_of="&PyBytes_Type", c_default="((PyCodeObject *)self)->co_exceptiontable") = unchanged
2745
2746
Return a copy of the code object with new values for the specified fields.
2747
[clinic start generated code]*/
2748
2749
static PyObject *
2750
code_replace_impl(PyCodeObject *self, int co_argcount,
2751
                  int co_posonlyargcount, int co_kwonlyargcount,
2752
                  int co_nlocals, int co_stacksize, int co_flags,
2753
                  int co_firstlineno, PyObject *co_code, PyObject *co_consts,
2754
                  PyObject *co_names, PyObject *co_varnames,
2755
                  PyObject *co_freevars, PyObject *co_cellvars,
2756
                  PyObject *co_filename, PyObject *co_name,
2757
                  PyObject *co_qualname, PyObject *co_linetable,
2758
                  PyObject *co_exceptiontable)
2759
/*[clinic end generated code: output=e75c48a15def18b9 input=e944fdac8b456114]*/
2760
7.51k
{
2761
7.51k
#define CHECK_INT_ARG(ARG) \
2762
52.5k
        if (ARG < 0) { \
2763
0
            PyErr_SetString(PyExc_ValueError, \
2764
0
                            #ARG " must be a positive integer"); \
2765
0
            return NULL; \
2766
0
        }
2767
2768
7.51k
    CHECK_INT_ARG(co_argcount);
2769
7.51k
    CHECK_INT_ARG(co_posonlyargcount);
2770
7.51k
    CHECK_INT_ARG(co_kwonlyargcount);
2771
7.51k
    CHECK_INT_ARG(co_nlocals);
2772
7.51k
    CHECK_INT_ARG(co_stacksize);
2773
7.51k
    CHECK_INT_ARG(co_flags);
2774
7.51k
    CHECK_INT_ARG(co_firstlineno);
2775
2776
7.51k
#undef CHECK_INT_ARG
2777
2778
7.51k
    PyObject *code = NULL;
2779
7.51k
    if (co_code == NULL) {
2780
7.51k
        code = _PyCode_GetCode(self);
2781
7.51k
        if (code == NULL) {
2782
0
            return NULL;
2783
0
        }
2784
7.51k
        co_code = code;
2785
7.51k
    }
2786
2787
7.51k
    if (PySys_Audit("code.__new__", "OOOiiiiii",
2788
7.51k
                    co_code, co_filename, co_name, co_argcount,
2789
7.51k
                    co_posonlyargcount, co_kwonlyargcount, co_nlocals,
2790
7.51k
                    co_stacksize, co_flags) < 0) {
2791
0
        Py_XDECREF(code);
2792
0
        return NULL;
2793
0
    }
2794
2795
7.51k
    PyCodeObject *co = NULL;
2796
7.51k
    PyObject *varnames = NULL;
2797
7.51k
    PyObject *cellvars = NULL;
2798
7.51k
    PyObject *freevars = NULL;
2799
7.51k
    if (co_varnames == NULL) {
2800
7.51k
        varnames = get_localsplus_names(self, CO_FAST_LOCAL, self->co_nlocals);
2801
7.51k
        if (varnames == NULL) {
2802
0
            goto error;
2803
0
        }
2804
7.51k
        co_varnames = varnames;
2805
7.51k
    }
2806
7.51k
    if (co_cellvars == NULL) {
2807
7.51k
        cellvars = get_localsplus_names(self, CO_FAST_CELL, self->co_ncellvars);
2808
7.51k
        if (cellvars == NULL) {
2809
0
            goto error;
2810
0
        }
2811
7.51k
        co_cellvars = cellvars;
2812
7.51k
    }
2813
7.51k
    if (co_freevars == NULL) {
2814
7.51k
        freevars = get_localsplus_names(self, CO_FAST_FREE, self->co_nfreevars);
2815
7.51k
        if (freevars == NULL) {
2816
0
            goto error;
2817
0
        }
2818
7.51k
        co_freevars = freevars;
2819
7.51k
    }
2820
2821
7.51k
    co = PyCode_NewWithPosOnlyArgs(
2822
7.51k
        co_argcount, co_posonlyargcount, co_kwonlyargcount, co_nlocals,
2823
7.51k
        co_stacksize, co_flags, co_code, co_consts, co_names,
2824
7.51k
        co_varnames, co_freevars, co_cellvars, co_filename, co_name,
2825
7.51k
        co_qualname, co_firstlineno,
2826
7.51k
        co_linetable, co_exceptiontable);
2827
2828
7.51k
error:
2829
7.51k
    Py_XDECREF(code);
2830
7.51k
    Py_XDECREF(varnames);
2831
7.51k
    Py_XDECREF(cellvars);
2832
7.51k
    Py_XDECREF(freevars);
2833
7.51k
    return (PyObject *)co;
2834
7.51k
}
2835
2836
/*[clinic input]
2837
code._varname_from_oparg
2838
2839
    oparg: int
2840
2841
(internal-only) Return the local variable name for the given oparg.
2842
2843
WARNING: this method is for internal use only and may change or go away.
2844
[clinic start generated code]*/
2845
2846
static PyObject *
2847
code__varname_from_oparg_impl(PyCodeObject *self, int oparg)
2848
/*[clinic end generated code: output=1fd1130413184206 input=c5fa3ee9bac7d4ca]*/
2849
0
{
2850
0
    PyObject *name = PyTuple_GetItem(self->co_localsplusnames, oparg);
2851
0
    if (name == NULL) {
2852
0
        return NULL;
2853
0
    }
2854
0
    return Py_NewRef(name);
2855
0
}
2856
2857
/* XXX code objects need to participate in GC? */
2858
2859
static struct PyMethodDef code_methods[] = {
2860
    {"__sizeof__", code_sizeof, METH_NOARGS},
2861
    {"co_lines", code_linesiterator, METH_NOARGS},
2862
    {"co_branches", code_branchesiterator, METH_NOARGS},
2863
    {"co_positions", code_positionsiterator, METH_NOARGS},
2864
    CODE_REPLACE_METHODDEF
2865
    CODE__VARNAME_FROM_OPARG_METHODDEF
2866
    {"__replace__", _PyCFunction_CAST(code_replace), METH_FASTCALL|METH_KEYWORDS,
2867
     PyDoc_STR("__replace__($self, /, **changes)\n--\n\nThe same as replace().")},
2868
    {NULL, NULL}                /* sentinel */
2869
};
2870
2871
2872
PyTypeObject PyCode_Type = {
2873
    PyVarObject_HEAD_INIT(&PyType_Type, 0)
2874
    "code",
2875
    offsetof(PyCodeObject, co_code_adaptive),
2876
    sizeof(_Py_CODEUNIT),
2877
    code_dealloc,                       /* tp_dealloc */
2878
    0,                                  /* tp_vectorcall_offset */
2879
    0,                                  /* tp_getattr */
2880
    0,                                  /* tp_setattr */
2881
    0,                                  /* tp_as_async */
2882
    code_repr,                          /* tp_repr */
2883
    0,                                  /* tp_as_number */
2884
    0,                                  /* tp_as_sequence */
2885
    0,                                  /* tp_as_mapping */
2886
    code_hash,                          /* tp_hash */
2887
    0,                                  /* tp_call */
2888
    0,                                  /* tp_str */
2889
    PyObject_GenericGetAttr,            /* tp_getattro */
2890
    0,                                  /* tp_setattro */
2891
    0,                                  /* tp_as_buffer */
2892
#ifdef Py_GIL_DISABLED
2893
    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
2894
#else
2895
    Py_TPFLAGS_DEFAULT,                 /* tp_flags */
2896
#endif
2897
    code_new__doc__,                    /* tp_doc */
2898
#ifdef Py_GIL_DISABLED
2899
    code_traverse,                      /* tp_traverse */
2900
#else
2901
    0,                                  /* tp_traverse */
2902
#endif
2903
    0,                                  /* tp_clear */
2904
    code_richcompare,                   /* tp_richcompare */
2905
    offsetof(PyCodeObject, co_weakreflist),     /* tp_weaklistoffset */
2906
    0,                                  /* tp_iter */
2907
    0,                                  /* tp_iternext */
2908
    code_methods,                       /* tp_methods */
2909
    code_memberlist,                    /* tp_members */
2910
    code_getsetlist,                    /* tp_getset */
2911
    0,                                  /* tp_base */
2912
    0,                                  /* tp_dict */
2913
    0,                                  /* tp_descr_get */
2914
    0,                                  /* tp_descr_set */
2915
    0,                                  /* tp_dictoffset */
2916
    0,                                  /* tp_init */
2917
    0,                                  /* tp_alloc */
2918
    code_new,                           /* tp_new */
2919
};
2920
2921
2922
/******************
2923
 * other API
2924
 ******************/
2925
2926
PyObject*
2927
_PyCode_ConstantKey(PyObject *op)
2928
141k
{
2929
141k
    PyObject *key;
2930
2931
    /* Py_None and Py_Ellipsis are singletons. */
2932
141k
    if (op == Py_None || op == Py_Ellipsis
2933
141k
       || PyLong_CheckExact(op)
2934
141k
       || PyUnicode_CheckExact(op)
2935
          /* code_richcompare() uses _PyCode_ConstantKey() internally */
2936
48.6k
       || PyCode_Check(op))
2937
101k
    {
2938
        /* Objects of these types are always different from object of other
2939
         * type and from tuples. */
2940
101k
        key = Py_NewRef(op);
2941
101k
    }
2942
39.5k
    else if (PyBool_Check(op) || PyBytes_CheckExact(op)) {
2943
        /* Make booleans different from integers 0 and 1.
2944
         * Avoid BytesWarning from comparing bytes with strings. */
2945
18.0k
        key = _PyTuple_FromPair((PyObject *)Py_TYPE(op), op);
2946
18.0k
    }
2947
21.4k
    else if (PyFloat_CheckExact(op)) {
2948
33
        double d = PyFloat_AS_DOUBLE(op);
2949
        /* all we need is to make the tuple different in either the 0.0
2950
         * or -0.0 case from all others, just to avoid the "coercion".
2951
         */
2952
33
        if (d == 0.0 && copysign(1.0, d) < 0.0)
2953
0
            key = PyTuple_Pack(3, Py_TYPE(op), op, Py_None);
2954
33
        else
2955
33
            key = _PyTuple_FromPair((PyObject *)Py_TYPE(op), op);
2956
33
    }
2957
21.4k
    else if (PyComplex_CheckExact(op)) {
2958
2
        Py_complex z;
2959
2
        int real_negzero, imag_negzero;
2960
        /* For the complex case we must make complex(x, 0.)
2961
           different from complex(x, -0.) and complex(0., y)
2962
           different from complex(-0., y), for any x and y.
2963
           All four complex zeros must be distinguished.*/
2964
2
        z = PyComplex_AsCComplex(op);
2965
2
        real_negzero = z.real == 0.0 && copysign(1.0, z.real) < 0.0;
2966
2
        imag_negzero = z.imag == 0.0 && copysign(1.0, z.imag) < 0.0;
2967
        /* use True, False and None singleton as tags for the real and imag
2968
         * sign, to make tuples different */
2969
2
        if (real_negzero && imag_negzero) {
2970
0
            key = PyTuple_Pack(3, Py_TYPE(op), op, Py_True);
2971
0
        }
2972
2
        else if (imag_negzero) {
2973
0
            key = PyTuple_Pack(3, Py_TYPE(op), op, Py_False);
2974
0
        }
2975
2
        else if (real_negzero) {
2976
0
            key = PyTuple_Pack(3, Py_TYPE(op), op, Py_None);
2977
0
        }
2978
2
        else {
2979
2
            key = _PyTuple_FromPair((PyObject *)Py_TYPE(op), op);
2980
2
        }
2981
2
    }
2982
21.4k
    else if (PyTuple_CheckExact(op)) {
2983
21.0k
        Py_ssize_t i, len;
2984
21.0k
        PyObject *tuple;
2985
2986
21.0k
        len = PyTuple_GET_SIZE(op);
2987
21.0k
        tuple = PyTuple_New(len);
2988
21.0k
        if (tuple == NULL)
2989
0
            return NULL;
2990
2991
89.9k
        for (i=0; i < len; i++) {
2992
68.9k
            PyObject *item, *item_key;
2993
2994
68.9k
            item = PyTuple_GET_ITEM(op, i);
2995
68.9k
            item_key = _PyCode_ConstantKey(item);
2996
68.9k
            if (item_key == NULL) {
2997
0
                Py_DECREF(tuple);
2998
0
                return NULL;
2999
0
            }
3000
3001
68.9k
            PyTuple_SET_ITEM(tuple, i, item_key);
3002
68.9k
        }
3003
3004
21.0k
        key = _PyTuple_FromPair(tuple, op);
3005
21.0k
        Py_DECREF(tuple);
3006
21.0k
    }
3007
380
    else if (PyFrozenSet_CheckExact(op)) {
3008
278
        Py_ssize_t pos = 0;
3009
278
        PyObject *item;
3010
278
        Py_hash_t hash;
3011
278
        Py_ssize_t i, len;
3012
278
        PyObject *tuple, *set;
3013
3014
278
        len = PySet_GET_SIZE(op);
3015
278
        tuple = PyTuple_New(len);
3016
278
        if (tuple == NULL)
3017
0
            return NULL;
3018
3019
278
        i = 0;
3020
1.97k
        while (_PySet_NextEntry(op, &pos, &item, &hash)) {
3021
1.69k
            PyObject *item_key;
3022
3023
1.69k
            item_key = _PyCode_ConstantKey(item);
3024
1.69k
            if (item_key == NULL) {
3025
0
                Py_DECREF(tuple);
3026
0
                return NULL;
3027
0
            }
3028
3029
1.69k
            assert(i < len);
3030
1.69k
            PyTuple_SET_ITEM(tuple, i, item_key);
3031
1.69k
            i++;
3032
1.69k
        }
3033
278
        set = PyFrozenSet_New(tuple);
3034
278
        Py_DECREF(tuple);
3035
278
        if (set == NULL)
3036
0
            return NULL;
3037
3038
278
        key = _PyTuple_FromPair(set, op);
3039
278
        Py_DECREF(set);
3040
278
        return key;
3041
278
    }
3042
102
    else if (PySlice_Check(op)) {
3043
102
        PySliceObject *slice = (PySliceObject *)op;
3044
102
        PyObject *start_key = NULL;
3045
102
        PyObject *stop_key = NULL;
3046
102
        PyObject *step_key = NULL;
3047
102
        key = NULL;
3048
3049
102
        start_key = _PyCode_ConstantKey(slice->start);
3050
102
        if (start_key == NULL) {
3051
0
            goto slice_exit;
3052
0
        }
3053
3054
102
        stop_key = _PyCode_ConstantKey(slice->stop);
3055
102
        if (stop_key == NULL) {
3056
0
            goto slice_exit;
3057
0
        }
3058
3059
102
        step_key = _PyCode_ConstantKey(slice->step);
3060
102
        if (step_key == NULL) {
3061
0
            goto slice_exit;
3062
0
        }
3063
3064
102
        PyObject *slice_key = PySlice_New(start_key, stop_key, step_key);
3065
102
        if (slice_key == NULL) {
3066
0
            goto slice_exit;
3067
0
        }
3068
3069
102
        key = _PyTuple_FromPair(slice_key, op);
3070
102
        Py_DECREF(slice_key);
3071
102
    slice_exit:
3072
102
        Py_XDECREF(start_key);
3073
102
        Py_XDECREF(stop_key);
3074
102
        Py_XDECREF(step_key);
3075
102
    }
3076
0
    else {
3077
        /* for other types, use the object identifier as a unique identifier
3078
         * to ensure that they are seen as unequal. */
3079
0
        PyObject *obj_id = PyLong_FromVoidPtr(op);
3080
0
        if (obj_id == NULL)
3081
0
            return NULL;
3082
3083
0
        key = _PyTuple_FromPair(obj_id, op);
3084
0
        Py_DECREF(obj_id);
3085
0
    }
3086
140k
    return key;
3087
141k
}
3088
3089
#ifdef Py_GIL_DISABLED
3090
static PyObject *
3091
intern_one_constant(PyObject *op)
3092
{
3093
    PyInterpreterState *interp = _PyInterpreterState_GET();
3094
    _Py_hashtable_t *consts = interp->code_state.constants;
3095
3096
    assert(!PyUnicode_CheckExact(op));  // strings are interned separately
3097
3098
    _Py_hashtable_entry_t *entry = _Py_hashtable_get_entry(consts, op);
3099
    if (entry == NULL) {
3100
        if (_Py_hashtable_set(consts, op, op) != 0) {
3101
            PyErr_NoMemory();
3102
            return NULL;
3103
        }
3104
3105
#ifdef Py_REF_DEBUG
3106
        Py_ssize_t refcnt = Py_REFCNT(op);
3107
        if (refcnt != 1) {
3108
            // Adjust the reftotal to account for the fact that we only
3109
            // restore a single reference in _PyCode_Fini.
3110
            _Py_AddRefTotal(_PyThreadState_GET(), -(refcnt - 1));
3111
        }
3112
#endif
3113
3114
        _Py_SetImmortal(op);
3115
        return op;
3116
    }
3117
3118
    assert(_Py_IsImmortal(entry->value));
3119
    return (PyObject *)entry->value;
3120
}
3121
3122
static int
3123
compare_constants(const void *key1, const void *key2)
3124
{
3125
    PyObject *op1 = (PyObject *)key1;
3126
    PyObject *op2 = (PyObject *)key2;
3127
    if (op1 == op2) {
3128
        return 1;
3129
    }
3130
    if (Py_TYPE(op1) != Py_TYPE(op2)) {
3131
        return 0;
3132
    }
3133
    // We compare container contents by identity because we have already
3134
    // internalized the items.
3135
    if (PyTuple_CheckExact(op1)) {
3136
        Py_ssize_t size = PyTuple_GET_SIZE(op1);
3137
        if (size != PyTuple_GET_SIZE(op2)) {
3138
            return 0;
3139
        }
3140
        for (Py_ssize_t i = 0; i < size; i++) {
3141
            if (PyTuple_GET_ITEM(op1, i) != PyTuple_GET_ITEM(op2, i)) {
3142
                return 0;
3143
            }
3144
        }
3145
        return 1;
3146
    }
3147
    else if (PyFrozenSet_CheckExact(op1)) {
3148
        if (PySet_GET_SIZE(op1) != PySet_GET_SIZE(op2)) {
3149
            return 0;
3150
        }
3151
        Py_ssize_t pos1 = 0, pos2 = 0;
3152
        PyObject *obj1, *obj2;
3153
        Py_hash_t hash1, hash2;
3154
        while ((_PySet_NextEntry(op1, &pos1, &obj1, &hash1)) &&
3155
               (_PySet_NextEntry(op2, &pos2, &obj2, &hash2)))
3156
        {
3157
            if (obj1 != obj2) {
3158
                return 0;
3159
            }
3160
        }
3161
        return 1;
3162
    }
3163
    else if (PySlice_Check(op1)) {
3164
        PySliceObject *s1 = (PySliceObject *)op1;
3165
        PySliceObject *s2 = (PySliceObject *)op2;
3166
        return (s1->start == s2->start &&
3167
                s1->stop  == s2->stop  &&
3168
                s1->step  == s2->step);
3169
    }
3170
    else if (PyBytes_CheckExact(op1) || PyLong_CheckExact(op1)) {
3171
        return PyObject_RichCompareBool(op1, op2, Py_EQ);
3172
    }
3173
    else if (PyFloat_CheckExact(op1)) {
3174
        // Ensure that, for example, +0.0 and -0.0 are distinct
3175
        double f1 = PyFloat_AS_DOUBLE(op1);
3176
        double f2 = PyFloat_AS_DOUBLE(op2);
3177
        return memcmp(&f1, &f2, sizeof(double)) == 0;
3178
    }
3179
    else if (PyComplex_CheckExact(op1)) {
3180
        Py_complex c1 = ((PyComplexObject *)op1)->cval;
3181
        Py_complex c2 = ((PyComplexObject *)op2)->cval;
3182
        return memcmp(&c1, &c2, sizeof(Py_complex)) == 0;
3183
    }
3184
    // gh-130851: Treat instances of unexpected types as distinct if they are
3185
    // not the same object.
3186
    return 0;
3187
}
3188
3189
static Py_uhash_t
3190
hash_const(const void *key)
3191
{
3192
    PyObject *op = (PyObject *)key;
3193
    if (PySlice_Check(op)) {
3194
        PySliceObject *s = (PySliceObject *)op;
3195
        PyObject *data[3] = { s->start, s->stop, s->step };
3196
        return Py_HashBuffer(&data, sizeof(data));
3197
    }
3198
    else if (PyTuple_CheckExact(op)) {
3199
        Py_ssize_t size = PyTuple_GET_SIZE(op);
3200
        PyObject **data = _PyTuple_ITEMS(op);
3201
        return Py_HashBuffer(data, sizeof(PyObject *) * size);
3202
    }
3203
    Py_hash_t h = PyObject_Hash(op);
3204
    if (h == -1) {
3205
        // gh-130851: Other than slice objects, every constant that the
3206
        // bytecode compiler generates is hashable. However, users can
3207
        // provide their own constants, when constructing code objects via
3208
        // types.CodeType(). If the user-provided constant is unhashable, we
3209
        // use the memory address of the object as a fallback hash value.
3210
        PyErr_Clear();
3211
        return (Py_uhash_t)(uintptr_t)key;
3212
    }
3213
    return (Py_uhash_t)h;
3214
}
3215
3216
static int
3217
clear_containers(_Py_hashtable_t *ht, const void *key, const void *value,
3218
                 void *user_data)
3219
{
3220
    // First clear containers to avoid recursive deallocation later on in
3221
    // destroy_key.
3222
    PyObject *op = (PyObject *)key;
3223
    if (PyTuple_CheckExact(op)) {
3224
        for (Py_ssize_t i = 0; i < PyTuple_GET_SIZE(op); i++) {
3225
            Py_CLEAR(_PyTuple_ITEMS(op)[i]);
3226
        }
3227
    }
3228
    else if (PySlice_Check(op)) {
3229
        PySliceObject *slice = (PySliceObject *)op;
3230
        Py_SETREF(slice->start, Py_None);
3231
        Py_SETREF(slice->stop, Py_None);
3232
        Py_SETREF(slice->step, Py_None);
3233
    }
3234
    else if (PyFrozenSet_CheckExact(op)) {
3235
        _PySet_ClearInternal((PySetObject *)op);
3236
    }
3237
    return 0;
3238
}
3239
3240
static void
3241
destroy_key(void *key)
3242
{
3243
    _Py_ClearImmortal(key);
3244
}
3245
#endif
3246
3247
PyStatus
3248
_PyCode_Init(PyInterpreterState *interp)
3249
37
{
3250
#ifdef Py_GIL_DISABLED
3251
    struct _py_code_state *state = &interp->code_state;
3252
    state->constants = _Py_hashtable_new_full(&hash_const, &compare_constants,
3253
                                              &destroy_key, NULL, NULL);
3254
    if (state->constants == NULL) {
3255
        return _PyStatus_NO_MEMORY();
3256
    }
3257
#endif
3258
37
    return _PyStatus_OK();
3259
37
}
3260
3261
void
3262
_PyCode_Fini(PyInterpreterState *interp)
3263
0
{
3264
#ifdef Py_GIL_DISABLED
3265
    // Free interned constants
3266
    struct _py_code_state *state = &interp->code_state;
3267
    if (state->constants) {
3268
        _Py_hashtable_foreach(state->constants, &clear_containers, NULL);
3269
        _Py_hashtable_destroy(state->constants);
3270
        state->constants = NULL;
3271
    }
3272
    _PyIndexPool_Fini(&interp->tlbc_indices);
3273
#endif
3274
0
}
3275
3276
#ifdef Py_GIL_DISABLED
3277
3278
// Thread-local bytecode (TLBC)
3279
//
3280
// Each thread specializes a thread-local copy of the bytecode, created on the
3281
// first RESUME, in free-threaded builds. All copies of the bytecode for a code
3282
// object are stored in the `co_tlbc` array. Threads reserve a globally unique
3283
// index identifying its copy of the bytecode in all `co_tlbc` arrays at thread
3284
// creation and release the index at thread destruction. The first entry in
3285
// every `co_tlbc` array always points to the "main" copy of the bytecode that
3286
// is stored at the end of the code object. This ensures that no bytecode is
3287
// copied for programs that do not use threads.
3288
//
3289
// Thread-local bytecode can be disabled at runtime by providing either `-X
3290
// tlbc=0` or `PYTHON_TLBC=0`. Disabling thread-local bytecode also disables
3291
// specialization. All threads share the main copy of the bytecode when
3292
// thread-local bytecode is disabled.
3293
//
3294
// Concurrent modifications to the bytecode made by the specializing
3295
// interpreter and instrumentation use atomics, with specialization taking care
3296
// not to overwrite an instruction that was instrumented concurrently.
3297
3298
int32_t
3299
_Py_ReserveTLBCIndex(PyInterpreterState *interp)
3300
{
3301
    if (interp->config.tlbc_enabled) {
3302
        return _PyIndexPool_AllocIndex(&interp->tlbc_indices);
3303
    }
3304
    // All threads share the main copy of the bytecode when TLBC is disabled
3305
    return 0;
3306
}
3307
3308
void
3309
_Py_ClearTLBCIndex(_PyThreadStateImpl *tstate)
3310
{
3311
    PyInterpreterState *interp = ((PyThreadState *)tstate)->interp;
3312
    if (interp->config.tlbc_enabled) {
3313
        _PyIndexPool_FreeIndex(&interp->tlbc_indices, tstate->tlbc_index);
3314
    }
3315
}
3316
3317
static _PyCodeArray *
3318
_PyCodeArray_New(Py_ssize_t size)
3319
{
3320
    _PyCodeArray *arr = PyMem_Calloc(
3321
        1, offsetof(_PyCodeArray, entries) + sizeof(void *) * size);
3322
    if (arr == NULL) {
3323
        PyErr_NoMemory();
3324
        return NULL;
3325
    }
3326
    arr->size = size;
3327
    return arr;
3328
}
3329
3330
// Get the underlying code unit, leaving instrumentation
3331
static _Py_CODEUNIT
3332
deopt_code_unit(PyCodeObject *code, int i)
3333
{
3334
    _Py_CODEUNIT *src_instr = _PyCode_CODE(code) + i;
3335
    _Py_CODEUNIT inst = {
3336
        .cache = FT_ATOMIC_LOAD_UINT16_RELAXED(*(uint16_t *)src_instr)};
3337
    int opcode = inst.op.code;
3338
    if (opcode < MIN_INSTRUMENTED_OPCODE) {
3339
        inst.op.code = _PyOpcode_Deopt[opcode];
3340
        assert(inst.op.code < MIN_SPECIALIZED_OPCODE);
3341
    }
3342
    // JIT should not be enabled with free-threading
3343
    assert(inst.op.code != ENTER_EXECUTOR);
3344
    return inst;
3345
}
3346
3347
static void
3348
copy_code(PyInterpreterState *interp, _Py_CODEUNIT *dst, PyCodeObject *co)
3349
{
3350
    int code_len = (int) Py_SIZE(co);
3351
    for (int i = 0; i < code_len; i += _PyInstruction_GetLength(co, i)) {
3352
        dst[i] = deopt_code_unit(co, i);
3353
    }
3354
    _PyCode_Quicken(dst, code_len, interp->opt_config.specialization_enabled, co->co_flags);
3355
}
3356
3357
static Py_ssize_t
3358
get_pow2_greater(Py_ssize_t initial, Py_ssize_t limit)
3359
{
3360
    // initial must be a power of two
3361
    assert(!(initial & (initial - 1)));
3362
    Py_ssize_t res = initial;
3363
    while (res && res < limit) {
3364
        res <<= 1;
3365
    }
3366
    return res;
3367
}
3368
3369
static _Py_CODEUNIT *
3370
create_tlbc_lock_held(PyInterpreterState *interp, PyCodeObject *co, Py_ssize_t idx)
3371
{
3372
    _PyCodeArray *tlbc = co->co_tlbc;
3373
    if (idx >= tlbc->size) {
3374
        Py_ssize_t new_size = get_pow2_greater(tlbc->size, idx + 1);
3375
        if (!new_size) {
3376
            PyErr_NoMemory();
3377
            return NULL;
3378
        }
3379
        _PyCodeArray *new_tlbc = _PyCodeArray_New(new_size);
3380
        if (new_tlbc == NULL) {
3381
            return NULL;
3382
        }
3383
        memcpy(new_tlbc->entries, tlbc->entries, tlbc->size * sizeof(void *));
3384
        _Py_atomic_store_ptr_release(&co->co_tlbc, new_tlbc);
3385
        _PyMem_FreeDelayed(tlbc, tlbc->size * sizeof(void *));
3386
        tlbc = new_tlbc;
3387
    }
3388
    char *bc = PyMem_Calloc(1, _PyCode_NBYTES(co));
3389
    if (bc == NULL) {
3390
        PyErr_NoMemory();
3391
        return NULL;
3392
    }
3393
    copy_code(interp, (_Py_CODEUNIT *) bc, co);
3394
    assert(tlbc->entries[idx] == NULL);
3395
    tlbc->entries[idx] = bc;
3396
    return (_Py_CODEUNIT *) bc;
3397
}
3398
3399
static _Py_CODEUNIT *
3400
get_tlbc_lock_held(PyCodeObject *co)
3401
{
3402
    _PyCodeArray *tlbc = co->co_tlbc;
3403
    _PyThreadStateImpl *tstate = (_PyThreadStateImpl *)PyThreadState_GET();
3404
    int32_t idx = tstate->tlbc_index;
3405
    if (idx < tlbc->size && tlbc->entries[idx] != NULL) {
3406
        return (_Py_CODEUNIT *)tlbc->entries[idx];
3407
    }
3408
    PyInterpreterState *interp = tstate->base.interp;
3409
    return create_tlbc_lock_held(interp, co, idx);
3410
}
3411
3412
_Py_CODEUNIT *
3413
_PyCode_GetTLBC(PyCodeObject *co)
3414
{
3415
    _Py_CODEUNIT *result;
3416
    Py_BEGIN_CRITICAL_SECTION(co);
3417
    result = get_tlbc_lock_held(co);
3418
    Py_END_CRITICAL_SECTION();
3419
    return result;
3420
}
3421
3422
// My kingdom for a bitset
3423
struct flag_set {
3424
    uint8_t *flags;
3425
    Py_ssize_t size;
3426
};
3427
3428
static inline int
3429
flag_is_set(struct flag_set *flags, Py_ssize_t idx)
3430
{
3431
    assert(idx >= 0);
3432
    return (idx < flags->size) && flags->flags[idx];
3433
}
3434
3435
// Set the flag for each tlbc index in use
3436
static int
3437
get_indices_in_use(PyInterpreterState *interp, struct flag_set *in_use)
3438
{
3439
    assert(interp->stoptheworld.world_stopped);
3440
    assert(in_use->flags == NULL);
3441
    int32_t max_index = 0;
3442
    _Py_FOR_EACH_TSTATE_BEGIN(interp, p) {
3443
        int32_t idx = ((_PyThreadStateImpl *) p)->tlbc_index;
3444
        if (idx > max_index) {
3445
            max_index = idx;
3446
        }
3447
    }
3448
    _Py_FOR_EACH_TSTATE_END(interp);
3449
    in_use->size = (size_t) max_index + 1;
3450
    in_use->flags = PyMem_Calloc(in_use->size, sizeof(*in_use->flags));
3451
    if (in_use->flags == NULL) {
3452
        return -1;
3453
    }
3454
    _Py_FOR_EACH_TSTATE_BEGIN(interp, p) {
3455
        in_use->flags[((_PyThreadStateImpl *) p)->tlbc_index] = 1;
3456
    }
3457
    _Py_FOR_EACH_TSTATE_END(interp);
3458
    return 0;
3459
}
3460
3461
struct get_code_args {
3462
    _PyObjectStack code_objs;
3463
    struct flag_set indices_in_use;
3464
    int err;
3465
};
3466
3467
static void
3468
clear_get_code_args(struct get_code_args *args)
3469
{
3470
    if (args->indices_in_use.flags != NULL) {
3471
        PyMem_Free(args->indices_in_use.flags);
3472
        args->indices_in_use.flags = NULL;
3473
    }
3474
    _PyObjectStack_Clear(&args->code_objs);
3475
}
3476
3477
static inline int
3478
is_bytecode_unused(_PyCodeArray *tlbc, Py_ssize_t idx,
3479
                   struct flag_set *indices_in_use)
3480
{
3481
    assert(idx > 0 && idx < tlbc->size);
3482
    return tlbc->entries[idx] != NULL && !flag_is_set(indices_in_use, idx);
3483
}
3484
3485
static int
3486
get_code_with_unused_tlbc(PyObject *obj, void *data)
3487
{
3488
    struct get_code_args *args = (struct get_code_args *) data;
3489
    if (!PyCode_Check(obj)) {
3490
        return 1;
3491
    }
3492
    PyCodeObject *co = (PyCodeObject *) obj;
3493
    _PyCodeArray *tlbc = co->co_tlbc;
3494
    // The first index always points at the main copy of the bytecode embedded
3495
    // in the code object.
3496
    for (Py_ssize_t i = 1; i < tlbc->size; i++) {
3497
        if (is_bytecode_unused(tlbc, i, &args->indices_in_use)) {
3498
            if (_PyObjectStack_Push(&args->code_objs, obj) < 0) {
3499
                args->err = -1;
3500
                return 0;
3501
            }
3502
            return 1;
3503
        }
3504
    }
3505
    return 1;
3506
}
3507
3508
static void
3509
free_unused_bytecode(PyCodeObject *co, struct flag_set *indices_in_use)
3510
{
3511
    _PyCodeArray *tlbc = co->co_tlbc;
3512
    // The first index always points at the main copy of the bytecode embedded
3513
    // in the code object.
3514
    for (Py_ssize_t i = 1; i < tlbc->size; i++) {
3515
        if (is_bytecode_unused(tlbc, i, indices_in_use)) {
3516
            PyMem_Free(tlbc->entries[i]);
3517
            tlbc->entries[i] = NULL;
3518
        }
3519
    }
3520
}
3521
3522
int
3523
_Py_ClearUnusedTLBC(PyInterpreterState *interp)
3524
{
3525
    struct get_code_args args = {
3526
        .code_objs = {NULL},
3527
        .indices_in_use = {NULL, 0},
3528
        .err = 0,
3529
    };
3530
    _PyEval_StopTheWorld(interp);
3531
    // Collect in-use tlbc indices
3532
    if (get_indices_in_use(interp, &args.indices_in_use) < 0) {
3533
        goto err;
3534
    }
3535
    // Collect code objects that have bytecode not in use by any thread
3536
    _PyGC_VisitObjectsWorldStopped(
3537
        interp, get_code_with_unused_tlbc, &args);
3538
    if (args.err < 0) {
3539
        goto err;
3540
    }
3541
    // Free unused bytecode. This must happen outside of gc_visit_heaps; it is
3542
    // unsafe to allocate or free any mimalloc managed memory when it's
3543
    // running.
3544
    PyObject *obj;
3545
    while ((obj = _PyObjectStack_Pop(&args.code_objs)) != NULL) {
3546
        free_unused_bytecode((PyCodeObject*) obj, &args.indices_in_use);
3547
    }
3548
    _PyEval_StartTheWorld(interp);
3549
    clear_get_code_args(&args);
3550
    return 0;
3551
3552
err:
3553
    _PyEval_StartTheWorld(interp);
3554
    clear_get_code_args(&args);
3555
    PyErr_NoMemory();
3556
    return -1;
3557
}
3558
3559
#endif