Coverage Report

Created: 2026-02-09 07:07

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/cpython/Python/marshal.c
Line
Count
Source
1
2
/* Write Python objects to files and read them back.
3
   This is primarily intended for writing and reading compiled Python code,
4
   even though dicts, lists, sets and frozensets, not commonly seen in
5
   code objects, are supported.
6
   Version 3 of this protocol properly supports circular links
7
   and sharing. */
8
9
#include "Python.h"
10
#include "pycore_call.h"             // _PyObject_CallNoArgs()
11
#include "pycore_code.h"             // _PyCode_New()
12
#include "pycore_hashtable.h"        // _Py_hashtable_t
13
#include "pycore_long.h"             // _PyLong_IsZero()
14
#include "pycore_object.h"           // _PyObject_IsUniquelyReferenced
15
#include "pycore_pystate.h"          // _PyInterpreterState_GET()
16
#include "pycore_setobject.h"        // _PySet_NextEntryRef()
17
#include "pycore_unicodeobject.h"    // _PyUnicode_InternImmortal()
18
19
#include "marshal.h"                 // Py_MARSHAL_VERSION
20
21
#ifdef __APPLE__
22
#  include "TargetConditionals.h"
23
#endif /* __APPLE__ */
24
25
26
/*[clinic input]
27
module marshal
28
[clinic start generated code]*/
29
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=c982b7930dee17db]*/
30
31
#include "clinic/marshal.c.h"
32
33
/* High water mark to determine when the marshalled object is dangerously deep
34
 * and risks coring the interpreter.  When the object stack gets this deep,
35
 * raise an exception instead of continuing.
36
 * On Windows debug builds, reduce this value.
37
 *
38
 * BUG: https://bugs.python.org/issue33720
39
 * On Windows PGO builds, the r_object function overallocates its stack and
40
 * can cause a stack overflow. We reduce the maximum depth for all Windows
41
 * releases to protect against this.
42
 * #if defined(MS_WINDOWS) && defined(Py_DEBUG)
43
 */
44
#if defined(MS_WINDOWS)
45
#  define MAX_MARSHAL_STACK_DEPTH 1000
46
#elif defined(__wasi__)
47
#  define MAX_MARSHAL_STACK_DEPTH 1500
48
// TARGET_OS_IPHONE covers any non-macOS Apple platform.
49
// It won't be defined on older macOS SDKs
50
#elif defined(__APPLE__) && defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE
51
#  define MAX_MARSHAL_STACK_DEPTH 1500
52
#else
53
3.07M
#  define MAX_MARSHAL_STACK_DEPTH 2000
54
#endif
55
56
/* Supported types */
57
0
#define TYPE_NULL               '0'
58
64.0k
#define TYPE_NONE               'N'
59
8.59k
#define TYPE_FALSE              'F'
60
7.77k
#define TYPE_TRUE               'T'
61
0
#define TYPE_STOPITER           'S'
62
247
#define TYPE_ELLIPSIS           '.'
63
445
#define TYPE_BINARY_FLOAT       'g'  // Version 0 uses TYPE_FLOAT instead.
64
1
#define TYPE_BINARY_COMPLEX     'y'  // Version 0 uses TYPE_COMPLEX instead.
65
310
#define TYPE_LONG               'l'  // See also TYPE_INT.
66
352k
#define TYPE_STRING             's'  // Bytes. (Name comes from Python 2.)
67
54
#define TYPE_TUPLE              '('  // See also TYPE_SMALL_TUPLE.
68
0
#define TYPE_LIST               '['
69
0
#define TYPE_DICT               '{'
70
116k
#define TYPE_CODE               'c'
71
3.47k
#define TYPE_UNICODE            'u'
72
#define TYPE_UNKNOWN            '?'
73
// added in version 2:
74
1.26k
#define TYPE_SET                '<'
75
421
#define TYPE_FROZENSET          '>'
76
// added in version 5:
77
2.85k
#define TYPE_SLICE              ':'
78
// Remember to update the version and documentation when adding new types.
79
80
/* Special cases for unicode strings (added in version 4) */
81
202
#define TYPE_INTERNED           't' // Version 1+
82
16.1k
#define TYPE_ASCII              'a'
83
0
#define TYPE_ASCII_INTERNED     'A'
84
817k
#define TYPE_SHORT_ASCII        'z'
85
709k
#define TYPE_SHORT_ASCII_INTERNED 'Z'
86
87
/* Special cases for small objects */
88
21.0k
#define TYPE_INT                'i'  // All versions. 32-bit encoding.
89
306k
#define TYPE_SMALL_TUPLE        ')'  // Version 4+
90
91
/* Supported for backwards compatibility */
92
0
#define TYPE_COMPLEX            'x'  // Generated for version 0 only.
93
0
#define TYPE_FLOAT              'f'  // Generated for version 0 only.
94
0
#define TYPE_INT64              'I'  // Not generated any more.
95
96
/* References (added in version 3) */
97
1.28M
#define TYPE_REF                'r'
98
6.02M
#define FLAG_REF                '\x80' /* with a type, add obj to index */
99
100
101
// Error codes:
102
77.4k
#define WFERR_OK 0
103
22
#define WFERR_UNMARSHALLABLE 1
104
0
#define WFERR_NESTEDTOODEEP 2
105
11
#define WFERR_NOMEMORY 3
106
0
#define WFERR_CODE_NOT_ALLOWED 4
107
108
typedef struct {
109
    FILE *fp;
110
    int error;  /* see WFERR_* values */
111
    int depth;
112
    PyObject *str;
113
    char *ptr;
114
    const char *end;
115
    char *buf;
116
    _Py_hashtable_t *hashtable;
117
    int version;
118
    int allow_code;
119
} WFILE;
120
121
367k
#define w_byte(c, p) do {                               \
122
367k
        if ((p)->ptr != (p)->end || w_reserve((p), 1))  \
123
367k
            *(p)->ptr++ = (c);                          \
124
367k
    } while(0)
125
126
static void
127
w_flush(WFILE *p)
128
0
{
129
0
    assert(p->fp != NULL);
130
0
    fwrite(p->buf, 1, p->ptr - p->buf, p->fp);
131
0
    p->ptr = p->buf;
132
0
}
133
134
static int
135
w_reserve(WFILE *p, Py_ssize_t needed)
136
519
{
137
519
    Py_ssize_t pos, size, delta;
138
519
    if (p->ptr == NULL)
139
0
        return 0; /* An error already occurred */
140
519
    if (p->fp != NULL) {
141
0
        w_flush(p);
142
0
        return needed <= p->end - p->ptr;
143
0
    }
144
519
    assert(p->str != NULL);
145
519
    pos = p->ptr - p->buf;
146
519
    size = PyBytes_GET_SIZE(p->str);
147
519
    if (size > 16*1024*1024)
148
0
        delta = (size >> 3);            /* 12.5% overallocation */
149
519
    else
150
519
        delta = size + 1024;
151
519
    delta = Py_MAX(delta, needed);
152
519
    if (delta > PY_SSIZE_T_MAX - size) {
153
0
        p->error = WFERR_NOMEMORY;
154
0
        return 0;
155
0
    }
156
519
    size += delta;
157
519
    if (_PyBytes_Resize(&p->str, size) != 0) {
158
0
        p->end = p->ptr = p->buf = NULL;
159
0
        return 0;
160
0
    }
161
519
    else {
162
519
        p->buf = PyBytes_AS_STRING(p->str);
163
519
        p->ptr = p->buf + pos;
164
519
        p->end = p->buf + size;
165
519
        return 1;
166
519
    }
167
519
}
168
169
static void
170
w_string(const void *s, Py_ssize_t n, WFILE *p)
171
25.7k
{
172
25.7k
    Py_ssize_t m;
173
25.7k
    if (!n || p->ptr == NULL)
174
203
        return;
175
25.5k
    m = p->end - p->ptr;
176
25.5k
    if (p->fp != NULL) {
177
0
        if (n <= m) {
178
0
            memcpy(p->ptr, s, n);
179
0
            p->ptr += n;
180
0
        }
181
0
        else {
182
0
            w_flush(p);
183
0
            fwrite(s, 1, n, p->fp);
184
0
        }
185
0
    }
186
25.5k
    else {
187
25.5k
        if (n <= m || w_reserve(p, n - m)) {
188
25.5k
            memcpy(p->ptr, s, n);
189
25.5k
            p->ptr += n;
190
25.5k
        }
191
25.5k
    }
192
25.5k
}
193
194
static void
195
w_short(int x, WFILE *p)
196
18
{
197
18
    w_byte((char)( x      & 0xff), p);
198
18
    w_byte((char)((x>> 8) & 0xff), p);
199
18
}
200
201
static void
202
w_long(long x, WFILE *p)
203
66.7k
{
204
66.7k
    w_byte((char)( x      & 0xff), p);
205
66.7k
    w_byte((char)((x>> 8) & 0xff), p);
206
66.7k
    w_byte((char)((x>>16) & 0xff), p);
207
66.7k
    w_byte((char)((x>>24) & 0xff), p);
208
66.7k
}
209
210
383k
#define SIZE32_MAX  0x7FFFFFFF
211
212
#if SIZEOF_SIZE_T > 4
213
9.66k
# define W_SIZE(n, p)  do {                     \
214
9.66k
        if ((n) > SIZE32_MAX) {                 \
215
0
            (p)->depth--;                       \
216
0
            (p)->error = WFERR_UNMARSHALLABLE;  \
217
0
            return;                             \
218
0
        }                                       \
219
9.66k
        w_long((long)(n), p);                   \
220
9.66k
    } while(0)
221
#else
222
# define W_SIZE  w_long
223
#endif
224
225
static void
226
w_pstring(const void *s, Py_ssize_t n, WFILE *p)
227
9.65k
{
228
9.65k
        W_SIZE(n, p);
229
9.65k
        w_string(s, n, p);
230
9.65k
}
231
232
static void
233
w_short_pstring(const void *s, Py_ssize_t n, WFILE *p)
234
16.0k
{
235
16.0k
    w_byte(Py_SAFE_DOWNCAST(n, Py_ssize_t, unsigned char), p);
236
16.0k
    w_string(s, n, p);
237
16.0k
}
238
239
/* We assume that Python ints are stored internally in base some power of
240
   2**15; for the sake of portability we'll always read and write them in base
241
   exactly 2**15. */
242
243
2.65k
#define PyLong_MARSHAL_SHIFT 15
244
1.16k
#define PyLong_MARSHAL_BASE ((short)1 << PyLong_MARSHAL_SHIFT)
245
18
#define PyLong_MARSHAL_MASK (PyLong_MARSHAL_BASE - 1)
246
247
38.7k
#define W_TYPE(t, p) do { \
248
38.7k
    w_byte((t) | flag, (p)); \
249
38.7k
} while(0)
250
251
static PyObject *
252
_PyMarshal_WriteObjectToString(PyObject *x, int version, int allow_code);
253
254
#define _r_digits(bitsize)                                                \
255
static void                                                               \
256
_r_digits##bitsize(const uint ## bitsize ## _t *digits, Py_ssize_t n,     \
257
2
                   uint8_t negative, Py_ssize_t marshal_ratio, WFILE *p)  \
258
2
{                                                                         \
259
2
    /* set l to number of base PyLong_MARSHAL_BASE digits */              \
260
2
    Py_ssize_t l = (n - 1)*marshal_ratio;                                 \
261
2
    uint ## bitsize ## _t d = digits[n - 1];                              \
262
2
                                                                          \
263
2
    assert(marshal_ratio > 0);                                            \
264
2
    assert(n >= 1);                                                       \
265
2
    assert(d != 0); /* a PyLong is always normalized */                   \
266
2
    do {                                                                  \
267
2
        d >>= PyLong_MARSHAL_SHIFT;                                       \
268
2
        l++;                                                              \
269
2
    } while (d != 0);                                                     \
270
2
    if (l > SIZE32_MAX) {                                                 \
271
0
        p->depth--;                                                       \
272
0
        p->error = WFERR_UNMARSHALLABLE;                                  \
273
0
        return;                                                           \
274
0
    }                                                                     \
275
2
    w_long((long)(negative ? -l : l), p);                                 \
276
2
                                                                          \
277
6
    for (Py_ssize_t i = 0; i < n - 1; i++) {                              \
278
4
        d = digits[i];                                                    \
279
12
        for (Py_ssize_t j = 0; j < marshal_ratio; j++) {                  \
280
8
            w_short(d & PyLong_MARSHAL_MASK, p);                          \
281
8
            d >>= PyLong_MARSHAL_SHIFT;                                   \
282
8
        }                                                                 \
283
4
        assert(d == 0);                                                   \
284
4
    }                                                                     \
285
2
    d = digits[n - 1];                                                    \
286
2
    do {                                                                  \
287
2
        w_short(d & PyLong_MARSHAL_MASK, p);                              \
288
2
        d >>= PyLong_MARSHAL_SHIFT;                                       \
289
2
    } while (d != 0);                                                     \
290
2
}
291
0
_r_digits(16)
292
2
_r_digits(32)
293
#undef _r_digits
294
295
static void
296
w_PyLong(const PyLongObject *ob, char flag, WFILE *p)
297
4
{
298
4
    W_TYPE(TYPE_LONG, p);
299
4
    if (_PyLong_IsZero(ob)) {
300
0
        w_long((long)0, p);
301
0
        return;
302
0
    }
303
304
4
    PyLongExport long_export;
305
306
4
    if (PyLong_Export((PyObject *)ob, &long_export) < 0) {
307
0
        p->depth--;
308
0
        p->error = WFERR_UNMARSHALLABLE;
309
0
        return;
310
0
    }
311
4
    if (!long_export.digits) {
312
2
        int8_t sign = long_export.value < 0 ? -1 : 1;
313
2
        uint64_t abs_value = _Py_ABS_CAST(uint64_t, long_export.value);
314
2
        uint64_t d = abs_value;
315
2
        long l = 0;
316
317
        /* set l to number of base PyLong_MARSHAL_BASE digits */
318
8
        do {
319
8
            d >>= PyLong_MARSHAL_SHIFT;
320
8
            l += sign;
321
8
        } while (d);
322
2
        w_long(l, p);
323
324
2
        d = abs_value;
325
8
        do {
326
8
            w_short(d & PyLong_MARSHAL_MASK, p);
327
8
            d >>= PyLong_MARSHAL_SHIFT;
328
8
        } while (d);
329
2
        return;
330
2
    }
331
332
2
    const PyLongLayout *layout = PyLong_GetNativeLayout();
333
2
    Py_ssize_t marshal_ratio = layout->bits_per_digit/PyLong_MARSHAL_SHIFT;
334
335
    /* must be a multiple of PyLong_MARSHAL_SHIFT */
336
2
    assert(layout->bits_per_digit % PyLong_MARSHAL_SHIFT == 0);
337
2
    assert(layout->bits_per_digit >= PyLong_MARSHAL_SHIFT);
338
339
    /* other assumptions on PyLongObject internals */
340
2
    assert(layout->bits_per_digit <= 32);
341
2
    assert(layout->digits_order == -1);
342
2
    assert(layout->digit_endianness == (PY_LITTLE_ENDIAN ? -1 : 1));
343
2
    assert(layout->digit_size == 2 || layout->digit_size == 4);
344
345
2
    if (layout->digit_size == 4) {
346
2
        _r_digits32(long_export.digits, long_export.ndigits,
347
2
                    long_export.negative, marshal_ratio, p);
348
2
    }
349
0
    else {
350
0
        _r_digits16(long_export.digits, long_export.ndigits,
351
0
                    long_export.negative, marshal_ratio, p);
352
0
    }
353
2
    PyLong_FreeExport(&long_export);
354
2
}
355
356
static void
357
w_float_bin(double v, WFILE *p)
358
15
{
359
15
    char buf[8];
360
15
    if (PyFloat_Pack8(v, buf, 1) < 0) {
361
0
        p->error = WFERR_UNMARSHALLABLE;
362
0
        return;
363
0
    }
364
15
    w_string(buf, 8, p);
365
15
}
366
367
static void
368
w_float_str(double v, WFILE *p)
369
0
{
370
0
    char *buf = PyOS_double_to_string(v, 'g', 17, 0, NULL);
371
0
    if (!buf) {
372
0
        p->error = WFERR_NOMEMORY;
373
0
        return;
374
0
    }
375
0
    w_short_pstring(buf, strlen(buf), p);
376
0
    PyMem_Free(buf);
377
0
}
378
379
static int
380
w_ref(PyObject *v, char *flag, WFILE *p)
381
74.3k
{
382
74.3k
    _Py_hashtable_entry_t *entry;
383
74.3k
    int w;
384
385
74.3k
    if (p->version < 3 || p->hashtable == NULL)
386
0
        return 0; /* not writing object references */
387
388
    /* If it has only one reference, it definitely isn't shared.
389
     * But we use TYPE_REF always for interned string, to PYC file stable
390
     * as possible.
391
     */
392
74.3k
    if (_PyObject_IsUniquelyReferenced(v) &&
393
20.0k
            !(PyUnicode_CheckExact(v) && PyUnicode_CHECK_INTERNED(v))) {
394
18.4k
        return 0;
395
18.4k
    }
396
397
55.9k
    entry = _Py_hashtable_get_entry(p->hashtable, v);
398
55.9k
    if (entry != NULL) {
399
        /* write the reference index to the stream */
400
35.6k
        w = (int)(uintptr_t)entry->value;
401
        /* we don't store "long" indices in the dict */
402
35.6k
        assert(0 <= w && w <= 0x7fffffff);
403
35.6k
        w_byte(TYPE_REF, p);
404
35.6k
        w_long(w, p);
405
35.6k
        return 1;
406
35.6k
    } else {
407
20.2k
        size_t s = p->hashtable->nentries;
408
        /* we don't support long indices */
409
20.2k
        if (s >= 0x7fffffff) {
410
0
            PyErr_SetString(PyExc_ValueError, "too many objects");
411
0
            goto err;
412
0
        }
413
20.2k
        w = (int)s;
414
20.2k
        if (_Py_hashtable_set(p->hashtable, Py_NewRef(v),
415
20.2k
                              (void *)(uintptr_t)w) < 0) {
416
0
            Py_DECREF(v);
417
0
            goto err;
418
0
        }
419
20.2k
        *flag |= FLAG_REF;
420
20.2k
        return 0;
421
20.2k
    }
422
0
err:
423
0
    p->error = WFERR_UNMARSHALLABLE;
424
0
    return 1;
425
55.9k
}
426
427
static void
428
w_complex_object(PyObject *v, char flag, WFILE *p);
429
430
static void
431
w_object(PyObject *v, WFILE *p)
432
76.9k
{
433
76.9k
    char flag = '\0';
434
435
76.9k
    if (p->error != WFERR_OK) {
436
0
        return;
437
0
    }
438
439
76.9k
    p->depth++;
440
441
76.9k
    if (p->depth > MAX_MARSHAL_STACK_DEPTH) {
442
0
        p->error = WFERR_NESTEDTOODEEP;
443
0
    }
444
76.9k
    else if (v == NULL) {
445
0
        w_byte(TYPE_NULL, p);
446
0
    }
447
76.9k
    else if (v == Py_None) {
448
1.99k
        w_byte(TYPE_NONE, p);
449
1.99k
    }
450
74.9k
    else if (v == PyExc_StopIteration) {
451
0
        w_byte(TYPE_STOPITER, p);
452
0
    }
453
74.9k
    else if (v == Py_Ellipsis) {
454
3
        w_byte(TYPE_ELLIPSIS, p);
455
3
    }
456
74.9k
    else if (v == Py_False) {
457
343
        w_byte(TYPE_FALSE, p);
458
343
    }
459
74.6k
    else if (v == Py_True) {
460
228
        w_byte(TYPE_TRUE, p);
461
228
    }
462
74.3k
    else if (!w_ref(v, &flag, p))
463
38.7k
        w_complex_object(v, flag, p);
464
465
76.9k
    p->depth--;
466
76.9k
}
467
468
static void
469
w_complex_object(PyObject *v, char flag, WFILE *p)
470
38.7k
{
471
38.7k
    Py_ssize_t i, n;
472
473
38.7k
    if (PyLong_CheckExact(v)) {
474
1.80k
        int overflow;
475
1.80k
        long x = PyLong_AsLongAndOverflow(v, &overflow);
476
1.80k
        if (overflow) {
477
2
            w_PyLong((PyLongObject *)v, flag, p);
478
2
        }
479
1.80k
        else {
480
1.80k
#if SIZEOF_LONG > 4
481
1.80k
            long y = Py_ARITHMETIC_RIGHT_SHIFT(long, x, 31);
482
1.80k
            if (y && y != -1) {
483
                /* Too large for TYPE_INT */
484
2
                w_PyLong((PyLongObject*)v, flag, p);
485
2
            }
486
1.80k
            else
487
1.80k
#endif
488
1.80k
            {
489
1.80k
                W_TYPE(TYPE_INT, p);
490
1.80k
                w_long(x, p);
491
1.80k
            }
492
1.80k
        }
493
1.80k
    }
494
36.9k
    else if (PyFloat_CheckExact(v)) {
495
13
        if (p->version > 1) {
496
13
            W_TYPE(TYPE_BINARY_FLOAT, p);
497
13
            w_float_bin(PyFloat_AS_DOUBLE(v), p);
498
13
        }
499
0
        else {
500
0
            W_TYPE(TYPE_FLOAT, p);
501
0
            w_float_str(PyFloat_AS_DOUBLE(v), p);
502
0
        }
503
13
    }
504
36.8k
    else if (PyComplex_CheckExact(v)) {
505
1
        if (p->version > 1) {
506
1
            W_TYPE(TYPE_BINARY_COMPLEX, p);
507
1
            w_float_bin(PyComplex_RealAsDouble(v), p);
508
1
            w_float_bin(PyComplex_ImagAsDouble(v), p);
509
1
        }
510
0
        else {
511
0
            W_TYPE(TYPE_COMPLEX, p);
512
0
            w_float_str(PyComplex_RealAsDouble(v), p);
513
0
            w_float_str(PyComplex_ImagAsDouble(v), p);
514
0
        }
515
1
    }
516
36.8k
    else if (PyBytes_CheckExact(v)) {
517
9.39k
        W_TYPE(TYPE_STRING, p);
518
9.39k
        w_pstring(PyBytes_AS_STRING(v), PyBytes_GET_SIZE(v), p);
519
9.39k
    }
520
27.5k
    else if (PyUnicode_CheckExact(v)) {
521
16.3k
        if (p->version >= 4 && PyUnicode_IS_ASCII(v)) {
522
16.2k
            int is_short = PyUnicode_GET_LENGTH(v) < 256;
523
16.2k
            if (is_short) {
524
16.0k
                if (PyUnicode_CHECK_INTERNED(v))
525
14.3k
                    W_TYPE(TYPE_SHORT_ASCII_INTERNED, p);
526
1.73k
                else
527
1.73k
                    W_TYPE(TYPE_SHORT_ASCII, p);
528
16.0k
                w_short_pstring(PyUnicode_1BYTE_DATA(v),
529
16.0k
                                PyUnicode_GET_LENGTH(v), p);
530
16.0k
            }
531
202
            else {
532
202
                if (PyUnicode_CHECK_INTERNED(v))
533
0
                    W_TYPE(TYPE_ASCII_INTERNED, p);
534
202
                else
535
202
                    W_TYPE(TYPE_ASCII, p);
536
202
                w_pstring(PyUnicode_1BYTE_DATA(v),
537
202
                          PyUnicode_GET_LENGTH(v), p);
538
202
            }
539
16.2k
        }
540
65
        else {
541
65
            PyObject *utf8;
542
65
            utf8 = PyUnicode_AsEncodedString(v, "utf8", "surrogatepass");
543
65
            if (utf8 == NULL) {
544
0
                p->depth--;
545
0
                p->error = WFERR_UNMARSHALLABLE;
546
0
                return;
547
0
            }
548
65
            if (p->version >= 3 &&  PyUnicode_CHECK_INTERNED(v))
549
0
                W_TYPE(TYPE_INTERNED, p);
550
65
            else
551
65
                W_TYPE(TYPE_UNICODE, p);
552
65
            w_pstring(PyBytes_AS_STRING(utf8), PyBytes_GET_SIZE(utf8), p);
553
65
            Py_DECREF(utf8);
554
65
        }
555
16.3k
    }
556
11.1k
    else if (PyTuple_CheckExact(v)) {
557
7.87k
        n = PyTuple_GET_SIZE(v);
558
7.87k
        if (p->version >= 4 && n < 256) {
559
7.87k
            W_TYPE(TYPE_SMALL_TUPLE, p);
560
7.87k
            w_byte((unsigned char)n, p);
561
7.87k
        }
562
0
        else {
563
0
            W_TYPE(TYPE_TUPLE, p);
564
0
            W_SIZE(n, p);
565
0
        }
566
51.8k
        for (i = 0; i < n; i++) {
567
43.9k
            w_object(PyTuple_GET_ITEM(v, i), p);
568
43.9k
        }
569
7.87k
    }
570
3.30k
    else if (PyList_CheckExact(v)) {
571
0
        W_TYPE(TYPE_LIST, p);
572
0
        n = PyList_GET_SIZE(v);
573
0
        W_SIZE(n, p);
574
0
        for (i = 0; i < n; i++) {
575
0
            w_object(PyList_GET_ITEM(v, i), p);
576
0
        }
577
0
    }
578
3.30k
    else if (PyDict_CheckExact(v)) {
579
0
        Py_ssize_t pos;
580
0
        PyObject *key, *value;
581
0
        W_TYPE(TYPE_DICT, p);
582
        /* This one is NULL object terminated! */
583
0
        pos = 0;
584
0
        while (PyDict_Next(v, &pos, &key, &value)) {
585
0
            w_object(key, p);
586
0
            w_object(value, p);
587
0
        }
588
0
        w_object((PyObject *)NULL, p);
589
0
    }
590
3.30k
    else if (PyAnySet_CheckExact(v)) {
591
11
        PyObject *value;
592
11
        Py_ssize_t pos = 0;
593
11
        Py_hash_t hash;
594
595
11
        if (PyFrozenSet_CheckExact(v))
596
11
            W_TYPE(TYPE_FROZENSET, p);
597
0
        else
598
0
            W_TYPE(TYPE_SET, p);
599
11
        n = PySet_GET_SIZE(v);
600
11
        W_SIZE(n, p);
601
        // bpo-37596: To support reproducible builds, sets and frozensets need
602
        // to have their elements serialized in a consistent order (even when
603
        // they have been scrambled by hash randomization). To ensure this, we
604
        // use an order equivalent to sorted(v, key=marshal.dumps):
605
11
        PyObject *pairs = PyList_New(n);
606
11
        if (pairs == NULL) {
607
0
            p->error = WFERR_NOMEMORY;
608
0
            return;
609
0
        }
610
11
        Py_ssize_t i = 0;
611
11
        Py_BEGIN_CRITICAL_SECTION(v);
612
76
        while (_PySet_NextEntryRef(v, &pos, &value, &hash)) {
613
65
            PyObject *dump = _PyMarshal_WriteObjectToString(value,
614
65
                                    p->version, p->allow_code);
615
65
            if (dump == NULL) {
616
0
                p->error = WFERR_UNMARSHALLABLE;
617
0
                Py_DECREF(value);
618
0
                break;
619
0
            }
620
65
            PyObject *pair = PyTuple_Pack(2, dump, value);
621
65
            Py_DECREF(dump);
622
65
            Py_DECREF(value);
623
65
            if (pair == NULL) {
624
0
                p->error = WFERR_NOMEMORY;
625
0
                break;
626
0
            }
627
65
            PyList_SET_ITEM(pairs, i++, pair);
628
65
        }
629
11
        Py_END_CRITICAL_SECTION();
630
11
        if (p->error == WFERR_UNMARSHALLABLE || p->error == WFERR_NOMEMORY) {
631
0
            Py_DECREF(pairs);
632
0
            return;
633
0
        }
634
11
        assert(i == n);
635
11
        if (PyList_Sort(pairs)) {
636
0
            p->error = WFERR_NOMEMORY;
637
0
            Py_DECREF(pairs);
638
0
            return;
639
0
        }
640
76
        for (Py_ssize_t i = 0; i < n; i++) {
641
65
            PyObject *pair = PyList_GET_ITEM(pairs, i);
642
65
            value = PyTuple_GET_ITEM(pair, 1);
643
65
            w_object(value, p);
644
65
        }
645
11
        Py_DECREF(pairs);
646
11
    }
647
3.28k
    else if (PyCode_Check(v)) {
648
3.26k
        if (!p->allow_code) {
649
0
            p->error = WFERR_CODE_NOT_ALLOWED;
650
0
            return;
651
0
        }
652
3.26k
        PyCodeObject *co = (PyCodeObject *)v;
653
3.26k
        PyObject *co_code = _PyCode_GetCode(co);
654
3.26k
        if (co_code == NULL) {
655
0
            p->error = WFERR_NOMEMORY;
656
0
            return;
657
0
        }
658
3.26k
        W_TYPE(TYPE_CODE, p);
659
3.26k
        w_long(co->co_argcount, p);
660
3.26k
        w_long(co->co_posonlyargcount, p);
661
3.26k
        w_long(co->co_kwonlyargcount, p);
662
3.26k
        w_long(co->co_stacksize, p);
663
3.26k
        w_long(co->co_flags, p);
664
3.26k
        w_object(co_code, p);
665
3.26k
        w_object(co->co_consts, p);
666
3.26k
        w_object(co->co_names, p);
667
3.26k
        w_object(co->co_localsplusnames, p);
668
3.26k
        w_object(co->co_localspluskinds, p);
669
3.26k
        w_object(co->co_filename, p);
670
3.26k
        w_object(co->co_name, p);
671
3.26k
        w_object(co->co_qualname, p);
672
3.26k
        w_long(co->co_firstlineno, p);
673
3.26k
        w_object(co->co_linetable, p);
674
3.26k
        w_object(co->co_exceptiontable, p);
675
3.26k
        Py_DECREF(co_code);
676
3.26k
    }
677
25
    else if (PyObject_CheckBuffer(v)) {
678
        /* Write unknown bytes-like objects as a bytes object */
679
0
        Py_buffer view;
680
0
        if (PyObject_GetBuffer(v, &view, PyBUF_SIMPLE) != 0) {
681
0
            w_byte(TYPE_UNKNOWN, p);
682
0
            p->depth--;
683
0
            p->error = WFERR_UNMARSHALLABLE;
684
0
            return;
685
0
        }
686
0
        W_TYPE(TYPE_STRING, p);
687
0
        w_pstring(view.buf, view.len, p);
688
0
        PyBuffer_Release(&view);
689
0
    }
690
25
    else if (PySlice_Check(v)) {
691
25
        if (p->version < 5) {
692
0
            w_byte(TYPE_UNKNOWN, p);
693
0
            p->error = WFERR_UNMARSHALLABLE;
694
0
            return;
695
0
        }
696
25
        PySliceObject *slice = (PySliceObject *)v;
697
25
        W_TYPE(TYPE_SLICE, p);
698
25
        w_object(slice->start, p);
699
25
        w_object(slice->stop, p);
700
25
        w_object(slice->step, p);
701
25
    }
702
0
    else {
703
0
        W_TYPE(TYPE_UNKNOWN, p);
704
0
        p->error = WFERR_UNMARSHALLABLE;
705
0
    }
706
38.7k
}
707
708
static void
709
w_decref_entry(void *key)
710
20.2k
{
711
20.2k
    PyObject *entry_key = (PyObject *)key;
712
20.2k
    Py_XDECREF(entry_key);
713
20.2k
}
714
715
static int
716
w_init_refs(WFILE *wf, int version)
717
228
{
718
228
    if (version >= 3) {
719
228
        wf->hashtable = _Py_hashtable_new_full(_Py_hashtable_hash_ptr,
720
228
                                               _Py_hashtable_compare_direct,
721
228
                                               w_decref_entry, NULL, NULL);
722
228
        if (wf->hashtable == NULL) {
723
0
            PyErr_NoMemory();
724
0
            return -1;
725
0
        }
726
228
    }
727
228
    return 0;
728
228
}
729
730
static void
731
w_clear_refs(WFILE *wf)
732
228
{
733
228
    if (wf->hashtable != NULL) {
734
228
        _Py_hashtable_destroy(wf->hashtable);
735
228
    }
736
228
}
737
738
/* version currently has no effect for writing ints. */
739
/* Note that while the documentation states that this function
740
 * can error, currently it never does. Setting an exception in
741
 * this function should be regarded as an API-breaking change.
742
 */
743
void
744
PyMarshal_WriteLongToFile(long x, FILE *fp, int version)
745
0
{
746
0
    char buf[4];
747
0
    WFILE wf;
748
0
    memset(&wf, 0, sizeof(wf));
749
0
    wf.fp = fp;
750
0
    wf.ptr = wf.buf = buf;
751
0
    wf.end = wf.ptr + sizeof(buf);
752
0
    wf.error = WFERR_OK;
753
0
    wf.version = version;
754
0
    w_long(x, &wf);
755
0
    w_flush(&wf);
756
0
}
757
758
void
759
PyMarshal_WriteObjectToFile(PyObject *x, FILE *fp, int version)
760
0
{
761
0
    char buf[BUFSIZ];
762
0
    WFILE wf;
763
0
    if (PySys_Audit("marshal.dumps", "Oi", x, version) < 0) {
764
0
        return; /* caller must check PyErr_Occurred() */
765
0
    }
766
0
    memset(&wf, 0, sizeof(wf));
767
0
    wf.fp = fp;
768
0
    wf.ptr = wf.buf = buf;
769
0
    wf.end = wf.ptr + sizeof(buf);
770
0
    wf.error = WFERR_OK;
771
0
    wf.version = version;
772
0
    wf.allow_code = 1;
773
0
    if (w_init_refs(&wf, version)) {
774
0
        return; /* caller must check PyErr_Occurred() */
775
0
    }
776
0
    w_object(x, &wf);
777
0
    w_clear_refs(&wf);
778
0
    w_flush(&wf);
779
0
}
780
781
typedef struct {
782
    FILE *fp;
783
    int depth;
784
    PyObject *readable;  /* Stream-like object being read from */
785
    const char *ptr;
786
    const char *end;
787
    char *buf;
788
    Py_ssize_t buf_size;
789
    PyObject *refs;  /* a list */
790
    int allow_code;
791
} RFILE;
792
793
static const char *
794
r_string(Py_ssize_t n, RFILE *p)
795
3.56M
{
796
3.56M
    Py_ssize_t read = -1;
797
798
3.56M
    if (p->ptr != NULL) {
799
        /* Fast path for loads() */
800
3.56M
        const char *res = p->ptr;
801
3.56M
        Py_ssize_t left = p->end - p->ptr;
802
3.56M
        if (left < n) {
803
0
            PyErr_SetString(PyExc_EOFError,
804
0
                            "marshal data too short");
805
0
            return NULL;
806
0
        }
807
3.56M
        p->ptr += n;
808
3.56M
        return res;
809
3.56M
    }
810
0
    if (p->buf == NULL) {
811
0
        p->buf = PyMem_Malloc(n);
812
0
        if (p->buf == NULL) {
813
0
            PyErr_NoMemory();
814
0
            return NULL;
815
0
        }
816
0
        p->buf_size = n;
817
0
    }
818
0
    else if (p->buf_size < n) {
819
0
        char *tmp = PyMem_Realloc(p->buf, n);
820
0
        if (tmp == NULL) {
821
0
            PyErr_NoMemory();
822
0
            return NULL;
823
0
        }
824
0
        p->buf = tmp;
825
0
        p->buf_size = n;
826
0
    }
827
828
0
    if (!p->readable) {
829
0
        assert(p->fp != NULL);
830
0
        read = fread(p->buf, 1, n, p->fp);
831
0
    }
832
0
    else {
833
0
        PyObject *res, *mview;
834
0
        Py_buffer buf;
835
836
0
        if (PyBuffer_FillInfo(&buf, NULL, p->buf, n, 0, PyBUF_CONTIG) == -1)
837
0
            return NULL;
838
0
        mview = PyMemoryView_FromBuffer(&buf);
839
0
        if (mview == NULL)
840
0
            return NULL;
841
842
0
        res = _PyObject_CallMethod(p->readable, &_Py_ID(readinto), "N", mview);
843
0
        if (res != NULL) {
844
0
            read = PyNumber_AsSsize_t(res, PyExc_ValueError);
845
0
            Py_DECREF(res);
846
0
        }
847
0
    }
848
0
    if (read != n) {
849
0
        if (!PyErr_Occurred()) {
850
0
            if (read > n)
851
0
                PyErr_Format(PyExc_ValueError,
852
0
                             "read() returned too much data: "
853
0
                             "%zd bytes requested, %zd returned",
854
0
                             n, read);
855
0
            else
856
0
                PyErr_SetString(PyExc_EOFError,
857
0
                                "EOF read where not expected");
858
0
        }
859
0
        return NULL;
860
0
    }
861
0
    return p->buf;
862
0
}
863
864
static int
865
r_byte(RFILE *p)
866
4.12M
{
867
4.12M
    if (p->ptr != NULL) {
868
4.12M
        if (p->ptr < p->end) {
869
4.12M
            return (unsigned char) *p->ptr++;
870
4.12M
        }
871
4.12M
    }
872
0
    else if (!p->readable) {
873
0
        assert(p->fp);
874
0
        int c = getc(p->fp);
875
0
        if (c != EOF) {
876
0
            return c;
877
0
        }
878
0
    }
879
0
    else {
880
0
        const char *ptr = r_string(1, p);
881
0
        if (ptr != NULL) {
882
0
            return *(const unsigned char *) ptr;
883
0
        }
884
0
        return EOF;
885
0
    }
886
0
    PyErr_SetString(PyExc_EOFError,
887
0
                    "EOF read where not expected");
888
0
    return EOF;
889
4.12M
}
890
891
static int
892
r_short(RFILE *p)
893
1.15k
{
894
1.15k
    short x = -1;
895
1.15k
    const unsigned char *buffer;
896
897
1.15k
    buffer = (const unsigned char *) r_string(2, p);
898
1.15k
    if (buffer != NULL) {
899
1.15k
        x = buffer[0];
900
1.15k
        x |= buffer[1] << 8;
901
        /* Sign-extension, in case short greater than 16 bits */
902
1.15k
        x |= -(x & 0x8000);
903
1.15k
    }
904
1.15k
    return x;
905
1.15k
}
906
907
static long
908
r_long(RFILE *p)
909
2.37M
{
910
2.37M
    long x = -1;
911
2.37M
    const unsigned char *buffer;
912
913
2.37M
    buffer = (const unsigned char *) r_string(4, p);
914
2.37M
    if (buffer != NULL) {
915
2.37M
        x = buffer[0];
916
2.37M
        x |= (long)buffer[1] << 8;
917
2.37M
        x |= (long)buffer[2] << 16;
918
2.37M
        x |= (long)buffer[3] << 24;
919
2.37M
#if SIZEOF_LONG > 4
920
        /* Sign extension for 64-bit machines */
921
2.37M
        x |= -(x & 0x80000000L);
922
2.37M
#endif
923
2.37M
    }
924
2.37M
    return x;
925
2.37M
}
926
927
/* r_long64 deals with the TYPE_INT64 code. */
928
static PyObject *
929
r_long64(RFILE *p)
930
0
{
931
0
    const unsigned char *buffer = (const unsigned char *) r_string(8, p);
932
0
    if (buffer == NULL) {
933
0
        return NULL;
934
0
    }
935
0
    return _PyLong_FromByteArray(buffer, 8,
936
0
                                 1 /* little endian */,
937
0
                                 1 /* signed */);
938
0
}
939
940
#define _w_digits(bitsize)                                              \
941
static int                                                              \
942
_w_digits##bitsize(uint ## bitsize ## _t *digits, Py_ssize_t size,      \
943
                   Py_ssize_t marshal_ratio,                            \
944
310
                   int shorts_in_top_digit, RFILE *p)                   \
945
310
{                                                                       \
946
310
    uint ## bitsize ## _t d;                                            \
947
310
                                                                        \
948
310
    assert(size >= 1);                                                  \
949
711
    for (Py_ssize_t i = 0; i < size - 1; i++) {                         \
950
401
        d = 0;                                                          \
951
1.20k
        for (Py_ssize_t j = 0; j < marshal_ratio; j++) {                \
952
802
            int md = r_short(p);                                        \
953
802
            if (md < 0 || md > PyLong_MARSHAL_BASE) {                   \
954
0
                goto bad_digit;                                         \
955
0
            }                                                           \
956
802
            d += (uint ## bitsize ## _t)md << j*PyLong_MARSHAL_SHIFT;   \
957
802
        }                                                               \
958
401
        digits[i] = d;                                                  \
959
401
    }                                                                   \
960
310
                                                                        \
961
310
    d = 0;                                                              \
962
658
    for (Py_ssize_t j = 0; j < shorts_in_top_digit; j++) {              \
963
348
        int md = r_short(p);                                            \
964
348
        if (md < 0 || md > PyLong_MARSHAL_BASE) {                       \
965
0
            goto bad_digit;                                             \
966
0
        }                                                               \
967
348
        /* topmost marshal digit should be nonzero */                   \
968
348
        if (md == 0 && j == shorts_in_top_digit - 1) {                  \
969
0
            PyErr_SetString(PyExc_ValueError,                           \
970
0
                "bad marshal data (unnormalized long data)");           \
971
0
            return -1;                                                  \
972
0
        }                                                               \
973
348
        d += (uint ## bitsize ## _t)md << j*PyLong_MARSHAL_SHIFT;       \
974
348
    }                                                                   \
975
310
    assert(!PyErr_Occurred());                                          \
976
310
    /* top digit should be nonzero, else the resulting PyLong won't be  \
977
310
       normalized */                                                    \
978
310
    digits[size - 1] = d;                                               \
979
310
    return 0;                                                           \
980
310
                                                                        \
981
0
bad_digit:                                                              \
982
0
    if (!PyErr_Occurred()) {                                            \
983
0
        PyErr_SetString(PyExc_ValueError,                               \
984
0
            "bad marshal data (digit out of range in long)");           \
985
0
    }                                                                   \
986
0
    return -1;                                                          \
987
310
}
988
310
_w_digits(32)
989
0
_w_digits(16)
990
#undef _w_digits
991
992
static PyObject *
993
r_PyLong(RFILE *p)
994
310
{
995
310
    long n = r_long(p);
996
310
    if (n == -1 && PyErr_Occurred()) {
997
0
        return NULL;
998
0
    }
999
310
    if (n < -SIZE32_MAX || n > SIZE32_MAX) {
1000
0
        PyErr_SetString(PyExc_ValueError,
1001
0
                       "bad marshal data (long size out of range)");
1002
0
        return NULL;
1003
0
    }
1004
1005
310
    const PyLongLayout *layout = PyLong_GetNativeLayout();
1006
310
    Py_ssize_t marshal_ratio = layout->bits_per_digit/PyLong_MARSHAL_SHIFT;
1007
1008
    /* must be a multiple of PyLong_MARSHAL_SHIFT */
1009
310
    assert(layout->bits_per_digit % PyLong_MARSHAL_SHIFT == 0);
1010
310
    assert(layout->bits_per_digit >= PyLong_MARSHAL_SHIFT);
1011
1012
    /* other assumptions on PyLongObject internals */
1013
310
    assert(layout->bits_per_digit <= 32);
1014
310
    assert(layout->digits_order == -1);
1015
310
    assert(layout->digit_endianness == (PY_LITTLE_ENDIAN ? -1 : 1));
1016
310
    assert(layout->digit_size == 2 || layout->digit_size == 4);
1017
1018
310
    Py_ssize_t size = 1 + (Py_ABS(n) - 1) / marshal_ratio;
1019
1020
310
    assert(size >= 1);
1021
1022
310
    int shorts_in_top_digit = 1 + (Py_ABS(n) - 1) % marshal_ratio;
1023
310
    void *digits;
1024
310
    PyLongWriter *writer = PyLongWriter_Create(n < 0, size, &digits);
1025
1026
310
    if (writer == NULL) {
1027
0
        return NULL;
1028
0
    }
1029
1030
310
    int ret;
1031
1032
310
    if (layout->digit_size == 4) {
1033
310
        ret = _w_digits32(digits, size, marshal_ratio, shorts_in_top_digit, p);
1034
310
    }
1035
0
    else {
1036
0
        ret = _w_digits16(digits, size, marshal_ratio, shorts_in_top_digit, p);
1037
0
    }
1038
310
    if (ret < 0) {
1039
0
        PyLongWriter_Discard(writer);
1040
0
        return NULL;
1041
0
    }
1042
310
    return PyLongWriter_Finish(writer);
1043
310
}
1044
1045
static double
1046
r_float_bin(RFILE *p)
1047
447
{
1048
447
    const char *buf = r_string(8, p);
1049
447
    if (buf == NULL)
1050
0
        return -1;
1051
447
    return PyFloat_Unpack8(buf, 1);
1052
447
}
1053
1054
/* Issue #33720: Disable inlining for reducing the C stack consumption
1055
   on PGO builds. */
1056
Py_NO_INLINE static double
1057
r_float_str(RFILE *p)
1058
0
{
1059
0
    int n;
1060
0
    char buf[256];
1061
0
    const char *ptr;
1062
0
    n = r_byte(p);
1063
0
    if (n == EOF) {
1064
0
        return -1;
1065
0
    }
1066
0
    ptr = r_string(n, p);
1067
0
    if (ptr == NULL) {
1068
0
        return -1;
1069
0
    }
1070
0
    memcpy(buf, ptr, n);
1071
0
    buf[n] = '\0';
1072
0
    return PyOS_string_to_double(buf, NULL, NULL);
1073
0
}
1074
1075
/* allocate the reflist index for a new object. Return -1 on failure */
1076
static Py_ssize_t
1077
r_ref_reserve(int flag, RFILE *p)
1078
119k
{
1079
119k
    if (flag) { /* currently only FLAG_REF is defined */
1080
4.15k
        Py_ssize_t idx = PyList_GET_SIZE(p->refs);
1081
4.15k
        if (idx >= 0x7ffffffe) {
1082
0
            PyErr_SetString(PyExc_ValueError, "bad marshal data (index list too large)");
1083
0
            return -1;
1084
0
        }
1085
4.15k
        if (PyList_Append(p->refs, Py_None) < 0)
1086
0
            return -1;
1087
4.15k
        return idx;
1088
4.15k
    } else
1089
115k
        return 0;
1090
119k
}
1091
1092
/* insert the new object 'o' to the reflist at previously
1093
 * allocated index 'idx'.
1094
 * 'o' can be NULL, in which case nothing is done.
1095
 * if 'o' was non-NULL, and the function succeeds, 'o' is returned.
1096
 * if 'o' was non-NULL, and the function fails, 'o' is released and
1097
 * NULL returned. This simplifies error checking at the call site since
1098
 * a single test for NULL for the function result is enough.
1099
 */
1100
static PyObject *
1101
r_ref_insert(PyObject *o, Py_ssize_t idx, int flag, RFILE *p)
1102
119k
{
1103
119k
    if (o != NULL && flag) { /* currently only FLAG_REF is defined */
1104
4.15k
        PyObject *tmp = PyList_GET_ITEM(p->refs, idx);
1105
4.15k
        PyList_SET_ITEM(p->refs, idx, Py_NewRef(o));
1106
4.15k
        Py_DECREF(tmp);
1107
4.15k
    }
1108
119k
    return o;
1109
119k
}
1110
1111
/* combination of both above, used when an object can be
1112
 * created whenever it is seen in the file, as opposed to
1113
 * after having loaded its sub-objects.
1114
 */
1115
static PyObject *
1116
r_ref(PyObject *o, int flag, RFILE *p)
1117
897k
{
1118
897k
    assert(flag & FLAG_REF);
1119
897k
    if (o == NULL)
1120
0
        return NULL;
1121
897k
    if (PyList_Append(p->refs, o) < 0) {
1122
0
        Py_DECREF(o); /* release the new object */
1123
0
        return NULL;
1124
0
    }
1125
897k
    return o;
1126
897k
}
1127
1128
static PyObject *
1129
r_object(RFILE *p)
1130
3.00M
{
1131
    /* NULL is a valid return value, it does not necessarily means that
1132
       an exception is set. */
1133
3.00M
    PyObject *v, *v2;
1134
3.00M
    Py_ssize_t idx = 0;
1135
3.00M
    long i, n;
1136
3.00M
    int type, code = r_byte(p);
1137
3.00M
    int flag, is_interned = 0;
1138
3.00M
    PyObject *retval = NULL;
1139
1140
3.00M
    if (code == EOF) {
1141
0
        if (PyErr_ExceptionMatches(PyExc_EOFError)) {
1142
0
            PyErr_SetString(PyExc_EOFError,
1143
0
                            "EOF read where object expected");
1144
0
        }
1145
0
        return NULL;
1146
0
    }
1147
1148
3.00M
    p->depth++;
1149
1150
3.00M
    if (p->depth > MAX_MARSHAL_STACK_DEPTH) {
1151
0
        p->depth--;
1152
0
        PyErr_SetString(PyExc_ValueError, "recursion limit exceeded");
1153
0
        return NULL;
1154
0
    }
1155
1156
3.00M
    flag = code & FLAG_REF;
1157
3.00M
    type = code & ~FLAG_REF;
1158
1159
3.00M
#define R_REF(O) do{\
1160
1.51M
    if (flag) \
1161
1.51M
        O = r_ref(O, flag, p);\
1162
1.51M
} while (0)
1163
1164
3.00M
    switch (type) {
1165
1166
0
    case TYPE_NULL:
1167
0
        break;
1168
1169
64.0k
    case TYPE_NONE:
1170
64.0k
        retval = Py_None;
1171
64.0k
        break;
1172
1173
0
    case TYPE_STOPITER:
1174
0
        retval = Py_NewRef(PyExc_StopIteration);
1175
0
        break;
1176
1177
247
    case TYPE_ELLIPSIS:
1178
247
        retval = Py_Ellipsis;
1179
247
        break;
1180
1181
8.59k
    case TYPE_FALSE:
1182
8.59k
        retval = Py_False;
1183
8.59k
        break;
1184
1185
7.77k
    case TYPE_TRUE:
1186
7.77k
        retval = Py_True;
1187
7.77k
        break;
1188
1189
21.0k
    case TYPE_INT:
1190
21.0k
        n = r_long(p);
1191
21.0k
        if (n == -1 && PyErr_Occurred()) {
1192
0
            break;
1193
0
        }
1194
21.0k
        retval = PyLong_FromLong(n);
1195
21.0k
        R_REF(retval);
1196
21.0k
        break;
1197
1198
0
    case TYPE_INT64:
1199
0
        retval = r_long64(p);
1200
0
        R_REF(retval);
1201
0
        break;
1202
1203
310
    case TYPE_LONG:
1204
310
        retval = r_PyLong(p);
1205
310
        R_REF(retval);
1206
310
        break;
1207
1208
0
    case TYPE_FLOAT:
1209
0
        {
1210
0
            double x = r_float_str(p);
1211
0
            if (x == -1.0 && PyErr_Occurred())
1212
0
                break;
1213
0
            retval = PyFloat_FromDouble(x);
1214
0
            R_REF(retval);
1215
0
            break;
1216
0
        }
1217
1218
445
    case TYPE_BINARY_FLOAT:
1219
445
        {
1220
445
            double x = r_float_bin(p);
1221
445
            if (x == -1.0 && PyErr_Occurred())
1222
0
                break;
1223
445
            retval = PyFloat_FromDouble(x);
1224
445
            R_REF(retval);
1225
445
            break;
1226
445
        }
1227
1228
0
    case TYPE_COMPLEX:
1229
0
        {
1230
0
            Py_complex c;
1231
0
            c.real = r_float_str(p);
1232
0
            if (c.real == -1.0 && PyErr_Occurred())
1233
0
                break;
1234
0
            c.imag = r_float_str(p);
1235
0
            if (c.imag == -1.0 && PyErr_Occurred())
1236
0
                break;
1237
0
            retval = PyComplex_FromCComplex(c);
1238
0
            R_REF(retval);
1239
0
            break;
1240
0
        }
1241
1242
1
    case TYPE_BINARY_COMPLEX:
1243
1
        {
1244
1
            Py_complex c;
1245
1
            c.real = r_float_bin(p);
1246
1
            if (c.real == -1.0 && PyErr_Occurred())
1247
0
                break;
1248
1
            c.imag = r_float_bin(p);
1249
1
            if (c.imag == -1.0 && PyErr_Occurred())
1250
0
                break;
1251
1
            retval = PyComplex_FromCComplex(c);
1252
1
            R_REF(retval);
1253
1
            break;
1254
1
        }
1255
1256
352k
    case TYPE_STRING:
1257
352k
        {
1258
352k
            const char *ptr;
1259
352k
            n = r_long(p);
1260
352k
            if (n < 0 || n > SIZE32_MAX) {
1261
0
                if (!PyErr_Occurred()) {
1262
0
                    PyErr_SetString(PyExc_ValueError,
1263
0
                        "bad marshal data (bytes object size out of range)");
1264
0
                }
1265
0
                break;
1266
0
            }
1267
352k
            v = PyBytes_FromStringAndSize((char *)NULL, n);
1268
352k
            if (v == NULL)
1269
0
                break;
1270
352k
            ptr = r_string(n, p);
1271
352k
            if (ptr == NULL) {
1272
0
                Py_DECREF(v);
1273
0
                break;
1274
0
            }
1275
352k
            memcpy(PyBytes_AS_STRING(v), ptr, n);
1276
352k
            retval = v;
1277
352k
            R_REF(retval);
1278
352k
            break;
1279
352k
        }
1280
1281
0
    case TYPE_ASCII_INTERNED:
1282
0
        is_interned = 1;
1283
0
        _Py_FALLTHROUGH;
1284
16.1k
    case TYPE_ASCII:
1285
16.1k
        n = r_long(p);
1286
16.1k
        if (n < 0 || n > SIZE32_MAX) {
1287
0
            if (!PyErr_Occurred()) {
1288
0
                PyErr_SetString(PyExc_ValueError,
1289
0
                    "bad marshal data (string size out of range)");
1290
0
            }
1291
0
            break;
1292
0
        }
1293
16.1k
        goto _read_ascii;
1294
1295
709k
    case TYPE_SHORT_ASCII_INTERNED:
1296
709k
        is_interned = 1;
1297
709k
        _Py_FALLTHROUGH;
1298
817k
    case TYPE_SHORT_ASCII:
1299
817k
        n = r_byte(p);
1300
817k
        if (n == EOF) {
1301
0
            break;
1302
0
        }
1303
833k
    _read_ascii:
1304
833k
        {
1305
833k
            const char *ptr;
1306
833k
            ptr = r_string(n, p);
1307
833k
            if (ptr == NULL)
1308
0
                break;
1309
833k
            v = PyUnicode_FromKindAndData(PyUnicode_1BYTE_KIND, ptr, n);
1310
833k
            if (v == NULL)
1311
0
                break;
1312
833k
            if (is_interned) {
1313
                // marshal is meant to serialize .pyc files with code
1314
                // objects, and code-related strings are currently immortal.
1315
709k
                PyInterpreterState *interp = _PyInterpreterState_GET();
1316
709k
                _PyUnicode_InternImmortal(interp, &v);
1317
709k
            }
1318
833k
            retval = v;
1319
833k
            R_REF(retval);
1320
833k
            break;
1321
833k
        }
1322
1323
202
    case TYPE_INTERNED:
1324
202
        is_interned = 1;
1325
202
        _Py_FALLTHROUGH;
1326
3.47k
    case TYPE_UNICODE:
1327
3.47k
        {
1328
3.47k
        const char *buffer;
1329
1330
3.47k
        n = r_long(p);
1331
3.47k
        if (n < 0 || n > SIZE32_MAX) {
1332
0
            if (!PyErr_Occurred()) {
1333
0
                PyErr_SetString(PyExc_ValueError,
1334
0
                    "bad marshal data (string size out of range)");
1335
0
            }
1336
0
            break;
1337
0
        }
1338
3.47k
        if (n != 0) {
1339
3.47k
            buffer = r_string(n, p);
1340
3.47k
            if (buffer == NULL)
1341
0
                break;
1342
3.47k
            v = PyUnicode_DecodeUTF8(buffer, n, "surrogatepass");
1343
3.47k
        }
1344
0
        else {
1345
0
            v = Py_GetConstant(Py_CONSTANT_EMPTY_STR);
1346
0
        }
1347
3.47k
        if (v == NULL)
1348
0
            break;
1349
3.47k
        if (is_interned) {
1350
            // marshal is meant to serialize .pyc files with code
1351
            // objects, and code-related strings are currently immortal.
1352
202
            PyInterpreterState *interp = _PyInterpreterState_GET();
1353
202
            _PyUnicode_InternImmortal(interp, &v);
1354
202
        }
1355
3.47k
        retval = v;
1356
3.47k
        R_REF(retval);
1357
3.47k
        break;
1358
3.47k
        }
1359
1360
306k
    case TYPE_SMALL_TUPLE:
1361
306k
        n = r_byte(p);
1362
306k
        if (n == EOF) {
1363
0
            break;
1364
0
        }
1365
306k
        goto _read_tuple;
1366
306k
    case TYPE_TUPLE:
1367
54
        n = r_long(p);
1368
54
        if (n < 0 || n > SIZE32_MAX) {
1369
0
            if (!PyErr_Occurred()) {
1370
0
                PyErr_SetString(PyExc_ValueError,
1371
0
                    "bad marshal data (tuple size out of range)");
1372
0
            }
1373
0
            break;
1374
0
        }
1375
306k
    _read_tuple:
1376
306k
        v = PyTuple_New(n);
1377
306k
        R_REF(v);
1378
306k
        if (v == NULL)
1379
0
            break;
1380
1381
2.12M
        for (i = 0; i < n; i++) {
1382
1.81M
            v2 = r_object(p);
1383
1.81M
            if ( v2 == NULL ) {
1384
0
                if (!PyErr_Occurred())
1385
0
                    PyErr_SetString(PyExc_TypeError,
1386
0
                        "NULL object in marshal data for tuple");
1387
0
                Py_SETREF(v, NULL);
1388
0
                break;
1389
0
            }
1390
1.81M
            PyTuple_SET_ITEM(v, i, v2);
1391
1.81M
        }
1392
306k
        retval = v;
1393
306k
        break;
1394
1395
0
    case TYPE_LIST:
1396
0
        n = r_long(p);
1397
0
        if (n < 0 || n > SIZE32_MAX) {
1398
0
            if (!PyErr_Occurred()) {
1399
0
                PyErr_SetString(PyExc_ValueError,
1400
0
                    "bad marshal data (list size out of range)");
1401
0
            }
1402
0
            break;
1403
0
        }
1404
0
        v = PyList_New(n);
1405
0
        R_REF(v);
1406
0
        if (v == NULL)
1407
0
            break;
1408
0
        for (i = 0; i < n; i++) {
1409
0
            v2 = r_object(p);
1410
0
            if ( v2 == NULL ) {
1411
0
                if (!PyErr_Occurred())
1412
0
                    PyErr_SetString(PyExc_TypeError,
1413
0
                        "NULL object in marshal data for list");
1414
0
                Py_SETREF(v, NULL);
1415
0
                break;
1416
0
            }
1417
0
            PyList_SET_ITEM(v, i, v2);
1418
0
        }
1419
0
        retval = v;
1420
0
        break;
1421
1422
0
    case TYPE_DICT:
1423
0
        v = PyDict_New();
1424
0
        R_REF(v);
1425
0
        if (v == NULL)
1426
0
            break;
1427
0
        for (;;) {
1428
0
            PyObject *key, *val;
1429
0
            key = r_object(p);
1430
0
            if (key == NULL)
1431
0
                break;
1432
0
            val = r_object(p);
1433
0
            if (val == NULL) {
1434
0
                Py_DECREF(key);
1435
0
                break;
1436
0
            }
1437
0
            if (PyDict_SetItem(v, key, val) < 0) {
1438
0
                Py_DECREF(key);
1439
0
                Py_DECREF(val);
1440
0
                break;
1441
0
            }
1442
0
            Py_DECREF(key);
1443
0
            Py_DECREF(val);
1444
0
        }
1445
0
        if (PyErr_Occurred()) {
1446
0
            Py_SETREF(v, NULL);
1447
0
        }
1448
0
        retval = v;
1449
0
        break;
1450
1451
0
    case TYPE_SET:
1452
421
    case TYPE_FROZENSET:
1453
421
        n = r_long(p);
1454
421
        if (n < 0 || n > SIZE32_MAX) {
1455
0
            if (!PyErr_Occurred()) {
1456
0
                PyErr_SetString(PyExc_ValueError,
1457
0
                    "bad marshal data (set size out of range)");
1458
0
            }
1459
0
            break;
1460
0
        }
1461
1462
421
        if (n == 0 && type == TYPE_FROZENSET) {
1463
            /* call frozenset() to get the empty frozenset singleton */
1464
0
            v = _PyObject_CallNoArgs((PyObject*)&PyFrozenSet_Type);
1465
0
            if (v == NULL)
1466
0
                break;
1467
0
            R_REF(v);
1468
0
            retval = v;
1469
0
        }
1470
421
        else {
1471
421
            v = (type == TYPE_SET) ? PySet_New(NULL) : PyFrozenSet_New(NULL);
1472
421
            if (type == TYPE_SET) {
1473
0
                R_REF(v);
1474
421
            } else {
1475
                /* must use delayed registration of frozensets because they must
1476
                 * be init with a refcount of 1
1477
                 */
1478
421
                idx = r_ref_reserve(flag, p);
1479
421
                if (idx < 0)
1480
0
                    Py_CLEAR(v); /* signal error */
1481
421
            }
1482
421
            if (v == NULL)
1483
0
                break;
1484
1485
2.16k
            for (i = 0; i < n; i++) {
1486
1.74k
                v2 = r_object(p);
1487
1.74k
                if ( v2 == NULL ) {
1488
0
                    if (!PyErr_Occurred())
1489
0
                        PyErr_SetString(PyExc_TypeError,
1490
0
                            "NULL object in marshal data for set");
1491
0
                    Py_SETREF(v, NULL);
1492
0
                    break;
1493
0
                }
1494
1.74k
                if (PySet_Add(v, v2) == -1) {
1495
0
                    Py_DECREF(v);
1496
0
                    Py_DECREF(v2);
1497
0
                    v = NULL;
1498
0
                    break;
1499
0
                }
1500
1.74k
                Py_DECREF(v2);
1501
1.74k
            }
1502
421
            if (type != TYPE_SET)
1503
421
                v = r_ref_insert(v, idx, flag, p);
1504
421
            retval = v;
1505
421
        }
1506
421
        break;
1507
1508
116k
    case TYPE_CODE:
1509
116k
        {
1510
116k
            int argcount;
1511
116k
            int posonlyargcount;
1512
116k
            int kwonlyargcount;
1513
116k
            int stacksize;
1514
116k
            int flags;
1515
116k
            PyObject *code = NULL;
1516
116k
            PyObject *consts = NULL;
1517
116k
            PyObject *names = NULL;
1518
116k
            PyObject *localsplusnames = NULL;
1519
116k
            PyObject *localspluskinds = NULL;
1520
116k
            PyObject *filename = NULL;
1521
116k
            PyObject *name = NULL;
1522
116k
            PyObject *qualname = NULL;
1523
116k
            int firstlineno;
1524
116k
            PyObject* linetable = NULL;
1525
116k
            PyObject *exceptiontable = NULL;
1526
1527
116k
            if (!p->allow_code) {
1528
0
                PyErr_SetString(PyExc_ValueError,
1529
0
                                "unmarshalling code objects is disallowed");
1530
0
                break;
1531
0
            }
1532
116k
            idx = r_ref_reserve(flag, p);
1533
116k
            if (idx < 0)
1534
0
                break;
1535
1536
116k
            v = NULL;
1537
1538
            /* XXX ignore long->int overflows for now */
1539
116k
            argcount = (int)r_long(p);
1540
116k
            if (argcount == -1 && PyErr_Occurred())
1541
0
                goto code_error;
1542
116k
            posonlyargcount = (int)r_long(p);
1543
116k
            if (posonlyargcount == -1 && PyErr_Occurred()) {
1544
0
                goto code_error;
1545
0
            }
1546
116k
            kwonlyargcount = (int)r_long(p);
1547
116k
            if (kwonlyargcount == -1 && PyErr_Occurred())
1548
0
                goto code_error;
1549
116k
            stacksize = (int)r_long(p);
1550
116k
            if (stacksize == -1 && PyErr_Occurred())
1551
0
                goto code_error;
1552
116k
            flags = (int)r_long(p);
1553
116k
            if (flags == -1 && PyErr_Occurred())
1554
0
                goto code_error;
1555
116k
            code = r_object(p);
1556
116k
            if (code == NULL)
1557
0
                goto code_error;
1558
116k
            consts = r_object(p);
1559
116k
            if (consts == NULL)
1560
0
                goto code_error;
1561
116k
            names = r_object(p);
1562
116k
            if (names == NULL)
1563
0
                goto code_error;
1564
116k
            localsplusnames = r_object(p);
1565
116k
            if (localsplusnames == NULL)
1566
0
                goto code_error;
1567
116k
            localspluskinds = r_object(p);
1568
116k
            if (localspluskinds == NULL)
1569
0
                goto code_error;
1570
116k
            filename = r_object(p);
1571
116k
            if (filename == NULL)
1572
0
                goto code_error;
1573
116k
            name = r_object(p);
1574
116k
            if (name == NULL)
1575
0
                goto code_error;
1576
116k
            qualname = r_object(p);
1577
116k
            if (qualname == NULL)
1578
0
                goto code_error;
1579
116k
            firstlineno = (int)r_long(p);
1580
116k
            if (firstlineno == -1 && PyErr_Occurred())
1581
0
                break;
1582
116k
            linetable = r_object(p);
1583
116k
            if (linetable == NULL)
1584
0
                goto code_error;
1585
116k
            exceptiontable = r_object(p);
1586
116k
            if (exceptiontable == NULL)
1587
0
                goto code_error;
1588
1589
116k
            struct _PyCodeConstructor con = {
1590
116k
                .filename = filename,
1591
116k
                .name = name,
1592
116k
                .qualname = qualname,
1593
116k
                .flags = flags,
1594
1595
116k
                .code = code,
1596
116k
                .firstlineno = firstlineno,
1597
116k
                .linetable = linetable,
1598
1599
116k
                .consts = consts,
1600
116k
                .names = names,
1601
1602
116k
                .localsplusnames = localsplusnames,
1603
116k
                .localspluskinds = localspluskinds,
1604
1605
116k
                .argcount = argcount,
1606
116k
                .posonlyargcount = posonlyargcount,
1607
116k
                .kwonlyargcount = kwonlyargcount,
1608
1609
116k
                .stacksize = stacksize,
1610
1611
116k
                .exceptiontable = exceptiontable,
1612
116k
            };
1613
1614
116k
            if (_PyCode_Validate(&con) < 0) {
1615
0
                goto code_error;
1616
0
            }
1617
1618
116k
            v = (PyObject *)_PyCode_New(&con);
1619
116k
            if (v == NULL) {
1620
0
                goto code_error;
1621
0
            }
1622
1623
116k
            v = r_ref_insert(v, idx, flag, p);
1624
1625
116k
          code_error:
1626
116k
            if (v == NULL && !PyErr_Occurred()) {
1627
0
                PyErr_SetString(PyExc_TypeError,
1628
0
                    "NULL object in marshal data for code object");
1629
0
            }
1630
116k
            Py_XDECREF(code);
1631
116k
            Py_XDECREF(consts);
1632
116k
            Py_XDECREF(names);
1633
116k
            Py_XDECREF(localsplusnames);
1634
116k
            Py_XDECREF(localspluskinds);
1635
116k
            Py_XDECREF(filename);
1636
116k
            Py_XDECREF(name);
1637
116k
            Py_XDECREF(qualname);
1638
116k
            Py_XDECREF(linetable);
1639
116k
            Py_XDECREF(exceptiontable);
1640
116k
        }
1641
0
        retval = v;
1642
116k
        break;
1643
1644
1.28M
    case TYPE_REF:
1645
1.28M
        n = r_long(p);
1646
1.28M
        if (n < 0 || n >= PyList_GET_SIZE(p->refs)) {
1647
0
            if (!PyErr_Occurred()) {
1648
0
                PyErr_SetString(PyExc_ValueError,
1649
0
                    "bad marshal data (invalid reference)");
1650
0
            }
1651
0
            break;
1652
0
        }
1653
1.28M
        v = PyList_GET_ITEM(p->refs, n);
1654
1.28M
        if (v == Py_None) {
1655
0
            PyErr_SetString(PyExc_ValueError, "bad marshal data (invalid reference)");
1656
0
            break;
1657
0
        }
1658
1.28M
        retval = Py_NewRef(v);
1659
1.28M
        break;
1660
1661
2.85k
    case TYPE_SLICE:
1662
2.85k
    {
1663
2.85k
        Py_ssize_t idx = r_ref_reserve(flag, p);
1664
2.85k
        if (idx < 0) {
1665
0
            break;
1666
0
        }
1667
2.85k
        PyObject *stop = NULL;
1668
2.85k
        PyObject *step = NULL;
1669
2.85k
        PyObject *start = r_object(p);
1670
2.85k
        if (start == NULL) {
1671
0
            goto cleanup;
1672
0
        }
1673
2.85k
        stop = r_object(p);
1674
2.85k
        if (stop == NULL) {
1675
0
            goto cleanup;
1676
0
        }
1677
2.85k
        step = r_object(p);
1678
2.85k
        if (step == NULL) {
1679
0
            goto cleanup;
1680
0
        }
1681
2.85k
        retval = PySlice_New(start, stop, step);
1682
2.85k
        r_ref_insert(retval, idx, flag, p);
1683
2.85k
    cleanup:
1684
2.85k
        Py_XDECREF(start);
1685
2.85k
        Py_XDECREF(stop);
1686
2.85k
        Py_XDECREF(step);
1687
2.85k
        break;
1688
2.85k
    }
1689
1690
0
    default:
1691
        /* Bogus data got written, which isn't ideal.
1692
           This will let you keep working and recover. */
1693
0
        PyErr_SetString(PyExc_ValueError, "bad marshal data (unknown type code)");
1694
0
        break;
1695
1696
3.00M
    }
1697
3.00M
    p->depth--;
1698
3.00M
    return retval;
1699
3.00M
}
1700
1701
static PyObject *
1702
read_object(RFILE *p)
1703
4.17k
{
1704
4.17k
    PyObject *v;
1705
4.17k
    if (PyErr_Occurred()) {
1706
0
        fprintf(stderr, "XXX readobject called with exception set\n");
1707
0
        return NULL;
1708
0
    }
1709
4.17k
    if (p->ptr && p->end) {
1710
4.17k
        if (PySys_Audit("marshal.loads", "y#", p->ptr, (Py_ssize_t)(p->end - p->ptr)) < 0) {
1711
0
            return NULL;
1712
0
        }
1713
4.17k
    } else if (p->fp || p->readable) {
1714
0
        if (PySys_Audit("marshal.load", NULL) < 0) {
1715
0
            return NULL;
1716
0
        }
1717
0
    }
1718
4.17k
    v = r_object(p);
1719
4.17k
    if (v == NULL && !PyErr_Occurred())
1720
0
        PyErr_SetString(PyExc_TypeError, "NULL object in marshal data for object");
1721
4.17k
    return v;
1722
4.17k
}
1723
1724
int
1725
PyMarshal_ReadShortFromFile(FILE *fp)
1726
0
{
1727
0
    RFILE rf;
1728
0
    int res;
1729
0
    assert(fp);
1730
0
    rf.readable = NULL;
1731
0
    rf.fp = fp;
1732
0
    rf.end = rf.ptr = NULL;
1733
0
    rf.buf = NULL;
1734
0
    res = r_short(&rf);
1735
0
    if (rf.buf != NULL)
1736
0
        PyMem_Free(rf.buf);
1737
0
    return res;
1738
0
}
1739
1740
long
1741
PyMarshal_ReadLongFromFile(FILE *fp)
1742
0
{
1743
0
    RFILE rf;
1744
0
    long res;
1745
0
    rf.fp = fp;
1746
0
    rf.readable = NULL;
1747
0
    rf.ptr = rf.end = NULL;
1748
0
    rf.buf = NULL;
1749
0
    res = r_long(&rf);
1750
0
    if (rf.buf != NULL)
1751
0
        PyMem_Free(rf.buf);
1752
0
    return res;
1753
0
}
1754
1755
/* Return size of file in bytes; < 0 if unknown or INT_MAX if too big */
1756
static off_t
1757
getfilesize(FILE *fp)
1758
0
{
1759
0
    struct _Py_stat_struct st;
1760
0
    if (_Py_fstat_noraise(fileno(fp), &st) != 0)
1761
0
        return -1;
1762
#if SIZEOF_OFF_T == 4
1763
    else if (st.st_size >= INT_MAX)
1764
        return (off_t)INT_MAX;
1765
#endif
1766
0
    else
1767
0
        return (off_t)st.st_size;
1768
0
}
1769
1770
/* If we can get the size of the file up-front, and it's reasonably small,
1771
 * read it in one gulp and delegate to ...FromString() instead.  Much quicker
1772
 * than reading a byte at a time from file; speeds .pyc imports.
1773
 * CAUTION:  since this may read the entire remainder of the file, don't
1774
 * call it unless you know you're done with the file.
1775
 */
1776
PyObject *
1777
PyMarshal_ReadLastObjectFromFile(FILE *fp)
1778
0
{
1779
/* REASONABLE_FILE_LIMIT is by defn something big enough for Tkinter.pyc. */
1780
0
#define REASONABLE_FILE_LIMIT (1L << 18)
1781
0
    off_t filesize;
1782
0
    filesize = getfilesize(fp);
1783
0
    if (filesize > 0 && filesize <= REASONABLE_FILE_LIMIT) {
1784
0
        char* pBuf = (char *)PyMem_Malloc(filesize);
1785
0
        if (pBuf != NULL) {
1786
0
            size_t n = fread(pBuf, 1, (size_t)filesize, fp);
1787
0
            PyObject* v = PyMarshal_ReadObjectFromString(pBuf, n);
1788
0
            PyMem_Free(pBuf);
1789
0
            return v;
1790
0
        }
1791
1792
0
    }
1793
    /* We don't have fstat, or we do but the file is larger than
1794
     * REASONABLE_FILE_LIMIT or malloc failed -- read a byte at a time.
1795
     */
1796
0
    return PyMarshal_ReadObjectFromFile(fp);
1797
1798
0
#undef REASONABLE_FILE_LIMIT
1799
0
}
1800
1801
PyObject *
1802
PyMarshal_ReadObjectFromFile(FILE *fp)
1803
0
{
1804
0
    RFILE rf;
1805
0
    PyObject *result;
1806
0
    rf.allow_code = 1;
1807
0
    rf.fp = fp;
1808
0
    rf.readable = NULL;
1809
0
    rf.depth = 0;
1810
0
    rf.ptr = rf.end = NULL;
1811
0
    rf.buf = NULL;
1812
0
    rf.refs = PyList_New(0);
1813
0
    if (rf.refs == NULL)
1814
0
        return NULL;
1815
0
    result = read_object(&rf);
1816
0
    Py_DECREF(rf.refs);
1817
0
    if (rf.buf != NULL)
1818
0
        PyMem_Free(rf.buf);
1819
0
    return result;
1820
0
}
1821
1822
PyObject *
1823
PyMarshal_ReadObjectFromString(const char *str, Py_ssize_t len)
1824
455
{
1825
455
    RFILE rf;
1826
455
    PyObject *result;
1827
455
    rf.allow_code = 1;
1828
455
    rf.fp = NULL;
1829
455
    rf.readable = NULL;
1830
455
    rf.ptr = str;
1831
455
    rf.end = str + len;
1832
455
    rf.buf = NULL;
1833
455
    rf.depth = 0;
1834
455
    rf.refs = PyList_New(0);
1835
455
    if (rf.refs == NULL)
1836
0
        return NULL;
1837
455
    result = read_object(&rf);
1838
455
    Py_DECREF(rf.refs);
1839
455
    if (rf.buf != NULL)
1840
0
        PyMem_Free(rf.buf);
1841
455
    return result;
1842
455
}
1843
1844
static PyObject *
1845
_PyMarshal_WriteObjectToString(PyObject *x, int version, int allow_code)
1846
228
{
1847
228
    WFILE wf;
1848
1849
228
    if (PySys_Audit("marshal.dumps", "Oi", x, version) < 0) {
1850
0
        return NULL;
1851
0
    }
1852
228
    memset(&wf, 0, sizeof(wf));
1853
228
    wf.str = PyBytes_FromStringAndSize((char *)NULL, 50);
1854
228
    if (wf.str == NULL)
1855
0
        return NULL;
1856
228
    wf.ptr = wf.buf = PyBytes_AS_STRING(wf.str);
1857
228
    wf.end = wf.ptr + PyBytes_GET_SIZE(wf.str);
1858
228
    wf.error = WFERR_OK;
1859
228
    wf.version = version;
1860
228
    wf.allow_code = allow_code;
1861
228
    if (w_init_refs(&wf, version)) {
1862
0
        Py_DECREF(wf.str);
1863
0
        return NULL;
1864
0
    }
1865
228
    w_object(x, &wf);
1866
228
    w_clear_refs(&wf);
1867
228
    if (wf.str != NULL) {
1868
228
        const char *base = PyBytes_AS_STRING(wf.str);
1869
228
        if (_PyBytes_Resize(&wf.str, (Py_ssize_t)(wf.ptr - base)) < 0)
1870
0
            return NULL;
1871
228
    }
1872
228
    if (wf.error != WFERR_OK) {
1873
0
        Py_XDECREF(wf.str);
1874
0
        switch (wf.error) {
1875
0
        case WFERR_NOMEMORY:
1876
0
            PyErr_NoMemory();
1877
0
            break;
1878
0
        case WFERR_NESTEDTOODEEP:
1879
0
            PyErr_SetString(PyExc_ValueError,
1880
0
                            "object too deeply nested to marshal");
1881
0
            break;
1882
0
        case WFERR_CODE_NOT_ALLOWED:
1883
0
            PyErr_SetString(PyExc_ValueError,
1884
0
                            "marshalling code objects is disallowed");
1885
0
            break;
1886
0
        default:
1887
0
        case WFERR_UNMARSHALLABLE:
1888
0
            PyErr_SetString(PyExc_ValueError,
1889
0
                            "unmarshallable object");
1890
0
            break;
1891
0
        }
1892
0
        return NULL;
1893
0
    }
1894
228
    return wf.str;
1895
228
}
1896
1897
PyObject *
1898
PyMarshal_WriteObjectToString(PyObject *x, int version)
1899
0
{
1900
0
    return _PyMarshal_WriteObjectToString(x, version, 1);
1901
0
}
1902
1903
/* And an interface for Python programs... */
1904
/*[clinic input]
1905
marshal.dump
1906
1907
    value: object
1908
        Must be a supported type.
1909
    file: object
1910
        Must be a writeable binary file.
1911
    version: int(c_default="Py_MARSHAL_VERSION") = version
1912
        Indicates the data format that dump should use.
1913
    /
1914
    *
1915
    allow_code: bool = True
1916
        Allow to write code objects.
1917
1918
Write the value on the open file.
1919
1920
If the value has (or contains an object that has) an unsupported type, a
1921
ValueError exception is raised - but garbage data will also be written
1922
to the file. The object will not be properly read back by load().
1923
[clinic start generated code]*/
1924
1925
static PyObject *
1926
marshal_dump_impl(PyObject *module, PyObject *value, PyObject *file,
1927
                  int version, int allow_code)
1928
/*[clinic end generated code: output=429e5fd61c2196b9 input=041f7f6669b0aafb]*/
1929
0
{
1930
    /* XXX Quick hack -- need to do this differently */
1931
0
    PyObject *s;
1932
0
    PyObject *res;
1933
1934
0
    s = _PyMarshal_WriteObjectToString(value, version, allow_code);
1935
0
    if (s == NULL)
1936
0
        return NULL;
1937
0
    res = PyObject_CallMethodOneArg(file, &_Py_ID(write), s);
1938
0
    Py_DECREF(s);
1939
0
    return res;
1940
0
}
1941
1942
/*[clinic input]
1943
marshal.load
1944
1945
    file: object
1946
        Must be readable binary file.
1947
    /
1948
    *
1949
    allow_code: bool = True
1950
        Allow to load code objects.
1951
1952
Read one value from the open file and return it.
1953
1954
If no valid value is read (e.g. because the data has a different Python
1955
version's incompatible marshal format), raise EOFError, ValueError or
1956
TypeError.
1957
1958
Note: If an object containing an unsupported type was marshalled with
1959
dump(), load() will substitute None for the unmarshallable type.
1960
[clinic start generated code]*/
1961
1962
static PyObject *
1963
marshal_load_impl(PyObject *module, PyObject *file, int allow_code)
1964
/*[clinic end generated code: output=0c1aaf3546ae3ed3 input=2dca7b570653b82f]*/
1965
0
{
1966
0
    PyObject *data, *result;
1967
0
    RFILE rf;
1968
1969
    /*
1970
     * Make a call to the read method, but read zero bytes.
1971
     * This is to ensure that the object passed in at least
1972
     * has a read method which returns bytes.
1973
     * This can be removed if we guarantee good error handling
1974
     * for r_string()
1975
     */
1976
0
    data = _PyObject_CallMethod(file, &_Py_ID(read), "i", 0);
1977
0
    if (data == NULL)
1978
0
        return NULL;
1979
0
    if (!PyBytes_Check(data)) {
1980
0
        PyErr_Format(PyExc_TypeError,
1981
0
                     "file.read() returned not bytes but %.100s",
1982
0
                     Py_TYPE(data)->tp_name);
1983
0
        result = NULL;
1984
0
    }
1985
0
    else {
1986
0
        rf.allow_code = allow_code;
1987
0
        rf.depth = 0;
1988
0
        rf.fp = NULL;
1989
0
        rf.readable = file;
1990
0
        rf.ptr = rf.end = NULL;
1991
0
        rf.buf = NULL;
1992
0
        if ((rf.refs = PyList_New(0)) != NULL) {
1993
0
            result = read_object(&rf);
1994
0
            Py_DECREF(rf.refs);
1995
0
            if (rf.buf != NULL)
1996
0
                PyMem_Free(rf.buf);
1997
0
        } else
1998
0
            result = NULL;
1999
0
    }
2000
0
    Py_DECREF(data);
2001
0
    return result;
2002
0
}
2003
2004
/*[clinic input]
2005
@permit_long_summary
2006
@permit_long_docstring_body
2007
marshal.dumps
2008
2009
    value: object
2010
        Must be a supported type.
2011
    version: int(c_default="Py_MARSHAL_VERSION") = version
2012
        Indicates the data format that dumps should use.
2013
    /
2014
    *
2015
    allow_code: bool = True
2016
        Allow to write code objects.
2017
2018
Return the bytes object that would be written to a file by dump(value, file).
2019
2020
Raise a ValueError exception if value has (or contains an object that has) an
2021
unsupported type.
2022
[clinic start generated code]*/
2023
2024
static PyObject *
2025
marshal_dumps_impl(PyObject *module, PyObject *value, int version,
2026
                   int allow_code)
2027
/*[clinic end generated code: output=115f90da518d1d49 input=80cd3f30c1637ade]*/
2028
163
{
2029
163
    return _PyMarshal_WriteObjectToString(value, version, allow_code);
2030
163
}
2031
2032
/*[clinic input]
2033
marshal.loads
2034
2035
    bytes: Py_buffer
2036
    /
2037
    *
2038
    allow_code: bool = True
2039
        Allow to load code objects.
2040
2041
Convert the bytes-like object to a value.
2042
2043
If no valid value is found, raise EOFError, ValueError or TypeError.  Extra
2044
bytes in the input are ignored.
2045
[clinic start generated code]*/
2046
2047
static PyObject *
2048
marshal_loads_impl(PyObject *module, Py_buffer *bytes, int allow_code)
2049
/*[clinic end generated code: output=62c0c538d3edc31f input=14de68965b45aaa7]*/
2050
3.72k
{
2051
3.72k
    RFILE rf;
2052
3.72k
    char *s = bytes->buf;
2053
3.72k
    Py_ssize_t n = bytes->len;
2054
3.72k
    PyObject* result;
2055
3.72k
    rf.allow_code = allow_code;
2056
3.72k
    rf.fp = NULL;
2057
3.72k
    rf.readable = NULL;
2058
3.72k
    rf.ptr = s;
2059
3.72k
    rf.end = s + n;
2060
3.72k
    rf.depth = 0;
2061
3.72k
    if ((rf.refs = PyList_New(0)) == NULL)
2062
0
        return NULL;
2063
3.72k
    result = read_object(&rf);
2064
3.72k
    Py_DECREF(rf.refs);
2065
3.72k
    return result;
2066
3.72k
}
2067
2068
static PyMethodDef marshal_methods[] = {
2069
    MARSHAL_DUMP_METHODDEF
2070
    MARSHAL_LOAD_METHODDEF
2071
    MARSHAL_DUMPS_METHODDEF
2072
    MARSHAL_LOADS_METHODDEF
2073
    {NULL,              NULL}           /* sentinel */
2074
};
2075
2076
2077
PyDoc_STRVAR(module_doc,
2078
"This module contains functions that can read and write Python values in\n\
2079
a binary format. The format is specific to Python, but independent of\n\
2080
machine architecture issues.\n\
2081
\n\
2082
Not all Python object types are supported; in general, only objects\n\
2083
whose value is independent from a particular invocation of Python can be\n\
2084
written and read by this module. The following types are supported:\n\
2085
None, integers, floating-point numbers, strings, bytes, bytearrays,\n\
2086
tuples, lists, sets, dictionaries, and code objects, where it\n\
2087
should be understood that tuples, lists and dictionaries are only\n\
2088
supported as long as the values contained therein are themselves\n\
2089
supported; and recursive lists and dictionaries should not be written\n\
2090
(they will cause infinite loops).\n\
2091
\n\
2092
Variables:\n\
2093
\n\
2094
version -- indicates the format that the module uses. Version 0 is the\n\
2095
    historical format, version 1 shares interned strings and version 2\n\
2096
    uses a binary format for floating-point numbers.\n\
2097
    Version 3 shares common object references (New in version 3.4).\n\
2098
\n\
2099
Functions:\n\
2100
\n\
2101
dump() -- write value to a file\n\
2102
load() -- read value from a file\n\
2103
dumps() -- marshal value as a bytes object\n\
2104
loads() -- read value from a bytes-like object");
2105
2106
2107
static int
2108
marshal_module_exec(PyObject *mod)
2109
32
{
2110
32
    if (PyModule_AddIntConstant(mod, "version", Py_MARSHAL_VERSION) < 0) {
2111
0
        return -1;
2112
0
    }
2113
32
    return 0;
2114
32
}
2115
2116
static PyModuleDef_Slot marshalmodule_slots[] = {
2117
    {Py_mod_exec, marshal_module_exec},
2118
    {Py_mod_multiple_interpreters, Py_MOD_PER_INTERPRETER_GIL_SUPPORTED},
2119
    {Py_mod_gil, Py_MOD_GIL_NOT_USED},
2120
    {0, NULL}
2121
};
2122
2123
static struct PyModuleDef marshalmodule = {
2124
    PyModuleDef_HEAD_INIT,
2125
    .m_name = "marshal",
2126
    .m_doc = module_doc,
2127
    .m_methods = marshal_methods,
2128
    .m_slots = marshalmodule_slots,
2129
};
2130
2131
PyMODINIT_FUNC
2132
PyMarshal_Init(void)
2133
32
{
2134
32
    return PyModuleDef_Init(&marshalmodule);
2135
32
}