Coverage Report

Created: 2026-04-12 06:54

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