Coverage Report

Created: 2025-07-04 06:49

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