Coverage Report

Created: 2026-01-17 06:16

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.85M
#  define MAX_MARSHAL_STACK_DEPTH 2000
54
#endif
55
56
/* Supported types */
57
0
#define TYPE_NULL               '0'
58
67.3k
#define TYPE_NONE               'N'
59
10.8k
#define TYPE_FALSE              'F'
60
7.75k
#define TYPE_TRUE               'T'
61
0
#define TYPE_STOPITER           'S'
62
81
#define TYPE_ELLIPSIS           '.'
63
166
#define TYPE_BINARY_FLOAT       'g'  // Version 0 uses TYPE_FLOAT instead.
64
0
#define TYPE_BINARY_COMPLEX     'y'  // Version 0 uses TYPE_COMPLEX instead.
65
159
#define TYPE_LONG               'l'  // See also TYPE_INT.
66
418k
#define TYPE_STRING             's'  // Bytes. (Name comes from Python 2.)
67
34
#define TYPE_TUPLE              '('  // See also TYPE_SMALL_TUPLE.
68
0
#define TYPE_LIST               '['
69
0
#define TYPE_DICT               '{'
70
138k
#define TYPE_CODE               'c'
71
1.77k
#define TYPE_UNICODE            'u'
72
#define TYPE_UNKNOWN            '?'
73
// added in version 2:
74
765
#define TYPE_SET                '<'
75
255
#define TYPE_FROZENSET          '>'
76
// added in version 5:
77
2.01k
#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
101
#define TYPE_INTERNED           't' // Version 1+
82
25.3k
#define TYPE_ASCII              'a'
83
0
#define TYPE_ASCII_INTERNED     'A'
84
1.10M
#define TYPE_SHORT_ASCII        'z'
85
968k
#define TYPE_SHORT_ASCII_INTERNED 'Z'
86
87
/* Special cases for small objects */
88
16.3k
#define TYPE_INT                'i'  // All versions. 32-bit encoding.
89
385k
#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.47M
#define TYPE_REF                'r'
98
7.35M
#define FLAG_REF                '\x80' /* with a type, add obj to index */
99
100
101
// Error codes:
102
201k
#define WFERR_OK 0
103
88
#define WFERR_UNMARSHALLABLE 1
104
0
#define WFERR_NESTEDTOODEEP 2
105
44
#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
937k
#define w_byte(c, p) do {                               \
122
937k
        if ((p)->ptr != (p)->end || w_reserve((p), 1))  \
123
937k
            *(p)->ptr++ = (c);                          \
124
937k
    } 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
958
{
137
958
    Py_ssize_t pos, size, delta;
138
958
    if (p->ptr == NULL)
139
0
        return 0; /* An error already occurred */
140
958
    if (p->fp != NULL) {
141
0
        w_flush(p);
142
0
        return needed <= p->end - p->ptr;
143
0
    }
144
958
    assert(p->str != NULL);
145
958
    pos = p->ptr - p->buf;
146
958
    size = PyBytes_GET_SIZE(p->str);
147
958
    if (size > 16*1024*1024)
148
0
        delta = (size >> 3);            /* 12.5% overallocation */
149
958
    else
150
958
        delta = size + 1024;
151
958
    delta = Py_MAX(delta, needed);
152
958
    if (delta > PY_SSIZE_T_MAX - size) {
153
0
        p->error = WFERR_NOMEMORY;
154
0
        return 0;
155
0
    }
156
958
    size += delta;
157
958
    if (_PyBytes_Resize(&p->str, size) != 0) {
158
0
        p->end = p->ptr = p->buf = NULL;
159
0
        return 0;
160
0
    }
161
958
    else {
162
958
        p->buf = PyBytes_AS_STRING(p->str);
163
958
        p->ptr = p->buf + pos;
164
958
        p->end = p->buf + size;
165
958
        return 1;
166
958
    }
167
958
}
168
169
static void
170
w_string(const void *s, Py_ssize_t n, WFILE *p)
171
72.7k
{
172
72.7k
    Py_ssize_t m;
173
72.7k
    if (!n || p->ptr == NULL)
174
359
        return;
175
72.3k
    m = p->end - p->ptr;
176
72.3k
    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
72.3k
    else {
187
72.3k
        if (n <= m || w_reserve(p, n - m)) {
188
72.3k
            memcpy(p->ptr, s, n);
189
72.3k
            p->ptr += n;
190
72.3k
        }
191
72.3k
    }
192
72.3k
}
193
194
static void
195
w_short(int x, WFILE *p)
196
90
{
197
90
    w_byte((char)( x      & 0xff), p);
198
90
    w_byte((char)((x>> 8) & 0xff), p);
199
90
}
200
201
static void
202
w_long(long x, WFILE *p)
203
167k
{
204
167k
    w_byte((char)( x      & 0xff), p);
205
167k
    w_byte((char)((x>> 8) & 0xff), p);
206
167k
    w_byte((char)((x>>16) & 0xff), p);
207
167k
    w_byte((char)((x>>24) & 0xff), p);
208
167k
}
209
210
472k
#define SIZE32_MAX  0x7FFFFFFF
211
212
#if SIZEOF_SIZE_T > 4
213
25.8k
# define W_SIZE(n, p)  do {                     \
214
25.8k
        if ((n) > SIZE32_MAX) {                 \
215
0
            (p)->depth--;                       \
216
0
            (p)->error = WFERR_UNMARSHALLABLE;  \
217
0
            return;                             \
218
0
        }                                       \
219
25.8k
        w_long((long)(n), p);                   \
220
25.8k
    } 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
25.7k
{
228
25.7k
        W_SIZE(n, p);
229
25.7k
        w_string(s, n, p);
230
25.7k
}
231
232
static void
233
w_short_pstring(const void *s, Py_ssize_t n, WFILE *p)
234
46.9k
{
235
46.9k
    w_byte(Py_SAFE_DOWNCAST(n, Py_ssize_t, unsigned char), p);
236
46.9k
    w_string(s, n, p);
237
46.9k
}
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
1.45k
#define PyLong_MARSHAL_SHIFT 15
244
619
#define PyLong_MARSHAL_BASE ((short)1 << PyLong_MARSHAL_SHIFT)
245
90
#define PyLong_MARSHAL_MASK (PyLong_MARSHAL_BASE - 1)
246
247
104k
#define W_TYPE(t, p) do { \
248
104k
    w_byte((t) | flag, (p)); \
249
104k
} 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
11
                   uint8_t negative, Py_ssize_t marshal_ratio, WFILE *p)  \
258
11
{                                                                         \
259
11
    /* set l to number of base PyLong_MARSHAL_BASE digits */              \
260
11
    Py_ssize_t l = (n - 1)*marshal_ratio;                                 \
261
11
    uint ## bitsize ## _t d = digits[n - 1];                              \
262
11
                                                                          \
263
11
    assert(marshal_ratio > 0);                                            \
264
11
    assert(n >= 1);                                                       \
265
11
    assert(d != 0); /* a PyLong is always normalized */                   \
266
11
    do {                                                                  \
267
11
        d >>= PyLong_MARSHAL_SHIFT;                                       \
268
11
        l++;                                                              \
269
11
    } while (d != 0);                                                     \
270
11
    if (l > SIZE32_MAX) {                                                 \
271
0
        p->depth--;                                                       \
272
0
        p->error = WFERR_UNMARSHALLABLE;                                  \
273
0
        return;                                                           \
274
0
    }                                                                     \
275
11
    w_long((long)(negative ? -l : l), p);                                 \
276
11
                                                                          \
277
33
    for (Py_ssize_t i = 0; i < n - 1; i++) {                              \
278
22
        d = digits[i];                                                    \
279
66
        for (Py_ssize_t j = 0; j < marshal_ratio; j++) {                  \
280
44
            w_short(d & PyLong_MARSHAL_MASK, p);                          \
281
44
            d >>= PyLong_MARSHAL_SHIFT;                                   \
282
44
        }                                                                 \
283
22
        assert(d == 0);                                                   \
284
22
    }                                                                     \
285
11
    d = digits[n - 1];                                                    \
286
11
    do {                                                                  \
287
11
        w_short(d & PyLong_MARSHAL_MASK, p);                              \
288
11
        d >>= PyLong_MARSHAL_SHIFT;                                       \
289
11
    } while (d != 0);                                                     \
290
11
}
291
0
_r_digits(16)
292
11
_r_digits(32)
293
#undef _r_digits
294
295
static void
296
w_PyLong(const PyLongObject *ob, char flag, WFILE *p)
297
22
{
298
22
    W_TYPE(TYPE_LONG, p);
299
22
    if (_PyLong_IsZero(ob)) {
300
0
        w_long((long)0, p);
301
0
        return;
302
0
    }
303
304
22
    PyLongExport long_export;
305
306
22
    if (PyLong_Export((PyObject *)ob, &long_export) < 0) {
307
0
        p->depth--;
308
0
        p->error = WFERR_UNMARSHALLABLE;
309
0
        return;
310
0
    }
311
22
    if (!long_export.digits) {
312
11
        int8_t sign = long_export.value < 0 ? -1 : 1;
313
11
        uint64_t abs_value = _Py_ABS_CAST(uint64_t, long_export.value);
314
11
        uint64_t d = abs_value;
315
11
        long l = 0;
316
317
        /* set l to number of base PyLong_MARSHAL_BASE digits */
318
35
        do {
319
35
            d >>= PyLong_MARSHAL_SHIFT;
320
35
            l += sign;
321
35
        } while (d);
322
11
        w_long(l, p);
323
324
11
        d = abs_value;
325
35
        do {
326
35
            w_short(d & PyLong_MARSHAL_MASK, p);
327
35
            d >>= PyLong_MARSHAL_SHIFT;
328
35
        } while (d);
329
11
        return;
330
11
    }
331
332
11
    const PyLongLayout *layout = PyLong_GetNativeLayout();
333
11
    Py_ssize_t marshal_ratio = layout->bits_per_digit/PyLong_MARSHAL_SHIFT;
334
335
    /* must be a multiple of PyLong_MARSHAL_SHIFT */
336
11
    assert(layout->bits_per_digit % PyLong_MARSHAL_SHIFT == 0);
337
11
    assert(layout->bits_per_digit >= PyLong_MARSHAL_SHIFT);
338
339
    /* other assumptions on PyLongObject internals */
340
11
    assert(layout->bits_per_digit <= 32);
341
11
    assert(layout->digits_order == -1);
342
11
    assert(layout->digit_endianness == (PY_LITTLE_ENDIAN ? -1 : 1));
343
11
    assert(layout->digit_size == 2 || layout->digit_size == 4);
344
345
11
    if (layout->digit_size == 4) {
346
11
        _r_digits32(long_export.digits, long_export.ndigits,
347
11
                    long_export.negative, marshal_ratio, p);
348
11
    }
349
0
    else {
350
0
        _r_digits16(long_export.digits, long_export.ndigits,
351
0
                    long_export.negative, marshal_ratio, p);
352
0
    }
353
11
    PyLong_FreeExport(&long_export);
354
11
}
355
356
static void
357
w_float_bin(double v, WFILE *p)
358
42
{
359
42
    char buf[8];
360
42
    if (PyFloat_Pack8(v, buf, 1) < 0) {
361
0
        p->error = WFERR_UNMARSHALLABLE;
362
0
        return;
363
0
    }
364
42
    w_string(buf, 8, p);
365
42
}
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
195k
{
382
195k
    _Py_hashtable_entry_t *entry;
383
195k
    int w;
384
385
195k
    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
195k
    if (_PyObject_IsUniquelyReferenced(v) &&
393
57.9k
            !(PyUnicode_CheckExact(v) && PyUnicode_CHECK_INTERNED(v))) {
394
52.6k
        return 0;
395
52.6k
    }
396
397
142k
    entry = _Py_hashtable_get_entry(p->hashtable, v);
398
142k
    if (entry != NULL) {
399
        /* write the reference index to the stream */
400
91.0k
        w = (int)(uintptr_t)entry->value;
401
        /* we don't store "long" indices in the dict */
402
91.0k
        assert(0 <= w && w <= 0x7fffffff);
403
91.0k
        w_byte(TYPE_REF, p);
404
91.0k
        w_long(w, p);
405
91.0k
        return 1;
406
91.0k
    } else {
407
51.5k
        size_t s = p->hashtable->nentries;
408
        /* we don't support long indices */
409
51.5k
        if (s >= 0x7fffffff) {
410
0
            PyErr_SetString(PyExc_ValueError, "too many objects");
411
0
            goto err;
412
0
        }
413
51.5k
        w = (int)s;
414
51.5k
        if (_Py_hashtable_set(p->hashtable, Py_NewRef(v),
415
51.5k
                              (void *)(uintptr_t)w) < 0) {
416
0
            Py_DECREF(v);
417
0
            goto err;
418
0
        }
419
51.5k
        *flag |= FLAG_REF;
420
51.5k
        return 0;
421
51.5k
    }
422
0
err:
423
0
    p->error = WFERR_UNMARSHALLABLE;
424
0
    return 1;
425
142k
}
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
201k
{
433
201k
    char flag = '\0';
434
435
201k
    if (p->error != WFERR_OK) {
436
0
        return;
437
0
    }
438
439
201k
    p->depth++;
440
441
201k
    if (p->depth > MAX_MARSHAL_STACK_DEPTH) {
442
0
        p->error = WFERR_NESTEDTOODEEP;
443
0
    }
444
201k
    else if (v == NULL) {
445
0
        w_byte(TYPE_NULL, p);
446
0
    }
447
201k
    else if (v == Py_None) {
448
4.38k
        w_byte(TYPE_NONE, p);
449
4.38k
    }
450
196k
    else if (v == PyExc_StopIteration) {
451
0
        w_byte(TYPE_STOPITER, p);
452
0
    }
453
196k
    else if (v == Py_Ellipsis) {
454
13
        w_byte(TYPE_ELLIPSIS, p);
455
13
    }
456
196k
    else if (v == Py_False) {
457
742
        w_byte(TYPE_FALSE, p);
458
742
    }
459
195k
    else if (v == Py_True) {
460
590
        w_byte(TYPE_TRUE, p);
461
590
    }
462
195k
    else if (!w_ref(v, &flag, p))
463
104k
        w_complex_object(v, flag, p);
464
465
201k
    p->depth--;
466
201k
}
467
468
static void
469
w_complex_object(PyObject *v, char flag, WFILE *p)
470
104k
{
471
104k
    Py_ssize_t i, n;
472
473
104k
    if (PyLong_CheckExact(v)) {
474
4.02k
        int overflow;
475
4.02k
        long x = PyLong_AsLongAndOverflow(v, &overflow);
476
4.02k
        if (overflow) {
477
11
            w_PyLong((PyLongObject *)v, flag, p);
478
11
        }
479
4.01k
        else {
480
4.01k
#if SIZEOF_LONG > 4
481
4.01k
            long y = Py_ARITHMETIC_RIGHT_SHIFT(long, x, 31);
482
4.01k
            if (y && y != -1) {
483
                /* Too large for TYPE_INT */
484
11
                w_PyLong((PyLongObject*)v, flag, p);
485
11
            }
486
4.00k
            else
487
4.00k
#endif
488
4.00k
            {
489
4.00k
                W_TYPE(TYPE_INT, p);
490
4.00k
                w_long(x, p);
491
4.00k
            }
492
4.01k
        }
493
4.02k
    }
494
100k
    else if (PyFloat_CheckExact(v)) {
495
42
        if (p->version > 1) {
496
42
            W_TYPE(TYPE_BINARY_FLOAT, p);
497
42
            w_float_bin(PyFloat_AS_DOUBLE(v), p);
498
42
        }
499
0
        else {
500
0
            W_TYPE(TYPE_FLOAT, p);
501
0
            w_float_str(PyFloat_AS_DOUBLE(v), p);
502
0
        }
503
42
    }
504
100k
    else if (PyComplex_CheckExact(v)) {
505
0
        if (p->version > 1) {
506
0
            W_TYPE(TYPE_BINARY_COMPLEX, p);
507
0
            w_float_bin(PyComplex_RealAsDouble(v), p);
508
0
            w_float_bin(PyComplex_ImagAsDouble(v), p);
509
0
        }
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
0
    }
516
100k
    else if (PyBytes_CheckExact(v)) {
517
23.3k
        W_TYPE(TYPE_STRING, p);
518
23.3k
        w_pstring(PyBytes_AS_STRING(v), PyBytes_GET_SIZE(v), p);
519
23.3k
    }
520
76.8k
    else if (PyUnicode_CheckExact(v)) {
521
49.3k
        if (p->version >= 4 && PyUnicode_IS_ASCII(v)) {
522
47.6k
            int is_short = PyUnicode_GET_LENGTH(v) < 256;
523
47.6k
            if (is_short) {
524
46.9k
                if (PyUnicode_CHECK_INTERNED(v))
525
37.9k
                    W_TYPE(TYPE_SHORT_ASCII_INTERNED, p);
526
8.93k
                else
527
8.93k
                    W_TYPE(TYPE_SHORT_ASCII, p);
528
46.9k
                w_short_pstring(PyUnicode_1BYTE_DATA(v),
529
46.9k
                                PyUnicode_GET_LENGTH(v), p);
530
46.9k
            }
531
695
            else {
532
695
                if (PyUnicode_CHECK_INTERNED(v))
533
0
                    W_TYPE(TYPE_ASCII_INTERNED, p);
534
695
                else
535
695
                    W_TYPE(TYPE_ASCII, p);
536
695
                w_pstring(PyUnicode_1BYTE_DATA(v),
537
695
                          PyUnicode_GET_LENGTH(v), p);
538
695
            }
539
47.6k
        }
540
1.68k
        else {
541
1.68k
            PyObject *utf8;
542
1.68k
            utf8 = PyUnicode_AsEncodedString(v, "utf8", "surrogatepass");
543
1.68k
            if (utf8 == NULL) {
544
0
                p->depth--;
545
0
                p->error = WFERR_UNMARSHALLABLE;
546
0
                return;
547
0
            }
548
1.68k
            if (p->version >= 3 &&  PyUnicode_CHECK_INTERNED(v))
549
101
                W_TYPE(TYPE_INTERNED, p);
550
1.58k
            else
551
1.58k
                W_TYPE(TYPE_UNICODE, p);
552
1.68k
            w_pstring(PyBytes_AS_STRING(utf8), PyBytes_GET_SIZE(utf8), p);
553
1.68k
            Py_DECREF(utf8);
554
1.68k
        }
555
49.3k
    }
556
27.4k
    else if (PyTuple_CheckExact(v)) {
557
19.4k
        n = PyTuple_GET_SIZE(v);
558
19.4k
        if (p->version >= 4 && n < 256) {
559
19.4k
            W_TYPE(TYPE_SMALL_TUPLE, p);
560
19.4k
            w_byte((unsigned char)n, p);
561
19.4k
        }
562
6
        else {
563
6
            W_TYPE(TYPE_TUPLE, p);
564
6
            W_SIZE(n, p);
565
6
        }
566
141k
        for (i = 0; i < n; i++) {
567
121k
            w_object(PyTuple_GET_ITEM(v, i), p);
568
121k
        }
569
19.4k
    }
570
8.05k
    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
8.05k
    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
8.05k
    else if (PyAnySet_CheckExact(v)) {
591
44
        PyObject *value;
592
44
        Py_ssize_t pos = 0;
593
44
        Py_hash_t hash;
594
595
44
        if (PyFrozenSet_CheckExact(v))
596
44
            W_TYPE(TYPE_FROZENSET, p);
597
0
        else
598
0
            W_TYPE(TYPE_SET, p);
599
44
        n = PySet_GET_SIZE(v);
600
44
        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
44
        PyObject *pairs = PyList_New(n);
606
44
        if (pairs == NULL) {
607
0
            p->error = WFERR_NOMEMORY;
608
0
            return;
609
0
        }
610
44
        Py_ssize_t i = 0;
611
44
        Py_BEGIN_CRITICAL_SECTION(v);
612
214
        while (_PySet_NextEntryRef(v, &pos, &value, &hash)) {
613
170
            PyObject *dump = _PyMarshal_WriteObjectToString(value,
614
170
                                    p->version, p->allow_code);
615
170
            if (dump == NULL) {
616
0
                p->error = WFERR_UNMARSHALLABLE;
617
0
                Py_DECREF(value);
618
0
                break;
619
0
            }
620
170
            PyObject *pair = PyTuple_Pack(2, dump, value);
621
170
            Py_DECREF(dump);
622
170
            Py_DECREF(value);
623
170
            if (pair == NULL) {
624
0
                p->error = WFERR_NOMEMORY;
625
0
                break;
626
0
            }
627
170
            PyList_SET_ITEM(pairs, i++, pair);
628
170
        }
629
44
        Py_END_CRITICAL_SECTION();
630
44
        if (p->error == WFERR_UNMARSHALLABLE || p->error == WFERR_NOMEMORY) {
631
0
            Py_DECREF(pairs);
632
0
            return;
633
0
        }
634
44
        assert(i == n);
635
44
        if (PyList_Sort(pairs)) {
636
0
            p->error = WFERR_NOMEMORY;
637
0
            Py_DECREF(pairs);
638
0
            return;
639
0
        }
640
214
        for (Py_ssize_t i = 0; i < n; i++) {
641
170
            PyObject *pair = PyList_GET_ITEM(pairs, i);
642
170
            value = PyTuple_GET_ITEM(pair, 1);
643
170
            w_object(value, p);
644
170
        }
645
44
        Py_DECREF(pairs);
646
44
    }
647
8.01k
    else if (PyCode_Check(v)) {
648
7.77k
        if (!p->allow_code) {
649
0
            p->error = WFERR_CODE_NOT_ALLOWED;
650
0
            return;
651
0
        }
652
7.77k
        PyCodeObject *co = (PyCodeObject *)v;
653
7.77k
        PyObject *co_code = _PyCode_GetCode(co);
654
7.77k
        if (co_code == NULL) {
655
0
            p->error = WFERR_NOMEMORY;
656
0
            return;
657
0
        }
658
7.77k
        W_TYPE(TYPE_CODE, p);
659
7.77k
        w_long(co->co_argcount, p);
660
7.77k
        w_long(co->co_posonlyargcount, p);
661
7.77k
        w_long(co->co_kwonlyargcount, p);
662
7.77k
        w_long(co->co_stacksize, p);
663
7.77k
        w_long(co->co_flags, p);
664
7.77k
        w_object(co_code, p);
665
7.77k
        w_object(co->co_consts, p);
666
7.77k
        w_object(co->co_names, p);
667
7.77k
        w_object(co->co_localsplusnames, p);
668
7.77k
        w_object(co->co_localspluskinds, p);
669
7.77k
        w_object(co->co_filename, p);
670
7.77k
        w_object(co->co_name, p);
671
7.77k
        w_object(co->co_qualname, p);
672
7.77k
        w_long(co->co_firstlineno, p);
673
7.77k
        w_object(co->co_linetable, p);
674
7.77k
        w_object(co->co_exceptiontable, p);
675
7.77k
        Py_DECREF(co_code);
676
7.77k
    }
677
238
    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
238
    else if (PySlice_Check(v)) {
691
238
        if (p->version < 5) {
692
0
            w_byte(TYPE_UNKNOWN, p);
693
0
            p->error = WFERR_UNMARSHALLABLE;
694
0
            return;
695
0
        }
696
238
        PySliceObject *slice = (PySliceObject *)v;
697
238
        W_TYPE(TYPE_SLICE, p);
698
238
        w_object(slice->start, p);
699
238
        w_object(slice->stop, p);
700
238
        w_object(slice->step, p);
701
238
    }
702
0
    else {
703
0
        W_TYPE(TYPE_UNKNOWN, p);
704
0
        p->error = WFERR_UNMARSHALLABLE;
705
0
    }
706
104k
}
707
708
static void
709
w_decref_entry(void *key)
710
51.5k
{
711
51.5k
    PyObject *entry_key = (PyObject *)key;
712
51.5k
    Py_XDECREF(entry_key);
713
51.5k
}
714
715
static int
716
w_init_refs(WFILE *wf, int version)
717
430
{
718
430
    if (version >= 3) {
719
430
        wf->hashtable = _Py_hashtable_new_full(_Py_hashtable_hash_ptr,
720
430
                                               _Py_hashtable_compare_direct,
721
430
                                               w_decref_entry, NULL, NULL);
722
430
        if (wf->hashtable == NULL) {
723
0
            PyErr_NoMemory();
724
0
            return -1;
725
0
        }
726
430
    }
727
430
    return 0;
728
430
}
729
730
static void
731
w_clear_refs(WFILE *wf)
732
430
{
733
430
    if (wf->hashtable != NULL) {
734
430
        _Py_hashtable_destroy(wf->hashtable);
735
430
    }
736
430
}
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
4.31M
{
796
4.31M
    Py_ssize_t read = -1;
797
798
4.31M
    if (p->ptr != NULL) {
799
        /* Fast path for loads() */
800
4.31M
        const char *res = p->ptr;
801
4.31M
        Py_ssize_t left = p->end - p->ptr;
802
4.31M
        if (left < n) {
803
0
            PyErr_SetString(PyExc_EOFError,
804
0
                            "marshal data too short");
805
0
            return NULL;
806
0
        }
807
4.31M
        p->ptr += n;
808
4.31M
        return res;
809
4.31M
    }
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
5.14M
{
867
5.14M
    if (p->ptr != NULL) {
868
5.14M
        if (p->ptr < p->end) {
869
5.14M
            return (unsigned char) *p->ptr++;
870
5.14M
        }
871
5.14M
    }
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
5.14M
}
890
891
static int
892
r_short(RFILE *p)
893
529
{
894
529
    short x = -1;
895
529
    const unsigned char *buffer;
896
897
529
    buffer = (const unsigned char *) r_string(2, p);
898
529
    if (buffer != NULL) {
899
529
        x = buffer[0];
900
529
        x |= buffer[1] << 8;
901
        /* Sign-extension, in case short greater than 16 bits */
902
529
        x |= -(x & 0x8000);
903
529
    }
904
529
    return x;
905
529
}
906
907
static long
908
r_long(RFILE *p)
909
2.76M
{
910
2.76M
    long x = -1;
911
2.76M
    const unsigned char *buffer;
912
913
2.76M
    buffer = (const unsigned char *) r_string(4, p);
914
2.76M
    if (buffer != NULL) {
915
2.76M
        x = buffer[0];
916
2.76M
        x |= (long)buffer[1] << 8;
917
2.76M
        x |= (long)buffer[2] << 16;
918
2.76M
        x |= (long)buffer[3] << 24;
919
2.76M
#if SIZEOF_LONG > 4
920
        /* Sign extension for 64-bit machines */
921
2.76M
        x |= -(x & 0x80000000L);
922
2.76M
#endif
923
2.76M
    }
924
2.76M
    return x;
925
2.76M
}
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
159
                   int shorts_in_top_digit, RFILE *p)                   \
945
159
{                                                                       \
946
159
    uint ## bitsize ## _t d;                                            \
947
159
                                                                        \
948
159
    assert(size >= 1);                                                  \
949
344
    for (Py_ssize_t i = 0; i < size - 1; i++) {                         \
950
185
        d = 0;                                                          \
951
555
        for (Py_ssize_t j = 0; j < marshal_ratio; j++) {                \
952
370
            int md = r_short(p);                                        \
953
370
            if (md < 0 || md > PyLong_MARSHAL_BASE) {                   \
954
0
                goto bad_digit;                                         \
955
0
            }                                                           \
956
370
            d += (uint ## bitsize ## _t)md << j*PyLong_MARSHAL_SHIFT;   \
957
370
        }                                                               \
958
185
        digits[i] = d;                                                  \
959
185
    }                                                                   \
960
159
                                                                        \
961
159
    d = 0;                                                              \
962
318
    for (Py_ssize_t j = 0; j < shorts_in_top_digit; j++) {              \
963
159
        int md = r_short(p);                                            \
964
159
        if (md < 0 || md > PyLong_MARSHAL_BASE) {                       \
965
0
            goto bad_digit;                                             \
966
0
        }                                                               \
967
159
        /* topmost marshal digit should be nonzero */                   \
968
159
        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
159
        d += (uint ## bitsize ## _t)md << j*PyLong_MARSHAL_SHIFT;       \
974
159
    }                                                                   \
975
159
    assert(!PyErr_Occurred());                                          \
976
159
    /* top digit should be nonzero, else the resulting PyLong won't be  \
977
159
       normalized */                                                    \
978
159
    digits[size - 1] = d;                                               \
979
159
    return 0;                                                           \
980
159
                                                                        \
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
159
}
988
159
_w_digits(32)
989
0
_w_digits(16)
990
#undef _w_digits
991
992
static PyObject *
993
r_PyLong(RFILE *p)
994
159
{
995
159
    long n = r_long(p);
996
159
    if (n == -1 && PyErr_Occurred()) {
997
0
        return NULL;
998
0
    }
999
159
    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
159
    const PyLongLayout *layout = PyLong_GetNativeLayout();
1006
159
    Py_ssize_t marshal_ratio = layout->bits_per_digit/PyLong_MARSHAL_SHIFT;
1007
1008
    /* must be a multiple of PyLong_MARSHAL_SHIFT */
1009
159
    assert(layout->bits_per_digit % PyLong_MARSHAL_SHIFT == 0);
1010
159
    assert(layout->bits_per_digit >= PyLong_MARSHAL_SHIFT);
1011
1012
    /* other assumptions on PyLongObject internals */
1013
159
    assert(layout->bits_per_digit <= 32);
1014
159
    assert(layout->digits_order == -1);
1015
159
    assert(layout->digit_endianness == (PY_LITTLE_ENDIAN ? -1 : 1));
1016
159
    assert(layout->digit_size == 2 || layout->digit_size == 4);
1017
1018
159
    Py_ssize_t size = 1 + (Py_ABS(n) - 1) / marshal_ratio;
1019
1020
159
    assert(size >= 1);
1021
1022
159
    int shorts_in_top_digit = 1 + (Py_ABS(n) - 1) % marshal_ratio;
1023
159
    void *digits;
1024
159
    PyLongWriter *writer = PyLongWriter_Create(n < 0, size, &digits);
1025
1026
159
    if (writer == NULL) {
1027
0
        return NULL;
1028
0
    }
1029
1030
159
    int ret;
1031
1032
159
    if (layout->digit_size == 4) {
1033
159
        ret = _w_digits32(digits, size, marshal_ratio, shorts_in_top_digit, p);
1034
159
    }
1035
0
    else {
1036
0
        ret = _w_digits16(digits, size, marshal_ratio, shorts_in_top_digit, p);
1037
0
    }
1038
159
    if (ret < 0) {
1039
0
        PyLongWriter_Discard(writer);
1040
0
        return NULL;
1041
0
    }
1042
159
    return PyLongWriter_Finish(writer);
1043
159
}
1044
1045
static double
1046
r_float_bin(RFILE *p)
1047
166
{
1048
166
    const char *buf = r_string(8, p);
1049
166
    if (buf == NULL)
1050
0
        return -1;
1051
166
    return PyFloat_Unpack8(buf, 1);
1052
166
}
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
140k
{
1079
140k
    if (flag) { /* currently only FLAG_REF is defined */
1080
5.99k
        Py_ssize_t idx = PyList_GET_SIZE(p->refs);
1081
5.99k
        if (idx >= 0x7ffffffe) {
1082
0
            PyErr_SetString(PyExc_ValueError, "bad marshal data (index list too large)");
1083
0
            return -1;
1084
0
        }
1085
5.99k
        if (PyList_Append(p->refs, Py_None) < 0)
1086
0
            return -1;
1087
5.99k
        return idx;
1088
5.99k
    } else
1089
134k
        return 0;
1090
140k
}
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
140k
{
1103
140k
    if (o != NULL && flag) { /* currently only FLAG_REF is defined */
1104
5.99k
        PyObject *tmp = PyList_GET_ITEM(p->refs, idx);
1105
5.99k
        PyList_SET_ITEM(p->refs, idx, Py_NewRef(o));
1106
5.99k
        Py_DECREF(tmp);
1107
5.99k
    }
1108
140k
    return o;
1109
140k
}
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
1.17M
{
1118
1.17M
    assert(flag & FLAG_REF);
1119
1.17M
    if (o == NULL)
1120
0
        return NULL;
1121
1.17M
    if (PyList_Append(p->refs, o) < 0) {
1122
0
        Py_DECREF(o); /* release the new object */
1123
0
        return NULL;
1124
0
    }
1125
1.17M
    return o;
1126
1.17M
}
1127
1128
static PyObject *
1129
r_object(RFILE *p)
1130
3.65M
{
1131
    /* NULL is a valid return value, it does not necessarily means that
1132
       an exception is set. */
1133
3.65M
    PyObject *v, *v2;
1134
3.65M
    Py_ssize_t idx = 0;
1135
3.65M
    long i, n;
1136
3.65M
    int type, code = r_byte(p);
1137
3.65M
    int flag, is_interned = 0;
1138
3.65M
    PyObject *retval = NULL;
1139
1140
3.65M
    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.65M
    p->depth++;
1149
1150
3.65M
    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.65M
    flag = code & FLAG_REF;
1157
3.65M
    type = code & ~FLAG_REF;
1158
1159
3.65M
#define R_REF(O) do{\
1160
1.95M
    if (flag) \
1161
1.95M
        O = r_ref(O, flag, p);\
1162
1.95M
} while (0)
1163
1164
3.65M
    switch (type) {
1165
1166
0
    case TYPE_NULL:
1167
0
        break;
1168
1169
67.3k
    case TYPE_NONE:
1170
67.3k
        retval = Py_None;
1171
67.3k
        break;
1172
1173
0
    case TYPE_STOPITER:
1174
0
        retval = Py_NewRef(PyExc_StopIteration);
1175
0
        break;
1176
1177
81
    case TYPE_ELLIPSIS:
1178
81
        retval = Py_Ellipsis;
1179
81
        break;
1180
1181
10.8k
    case TYPE_FALSE:
1182
10.8k
        retval = Py_False;
1183
10.8k
        break;
1184
1185
7.75k
    case TYPE_TRUE:
1186
7.75k
        retval = Py_True;
1187
7.75k
        break;
1188
1189
16.3k
    case TYPE_INT:
1190
16.3k
        n = r_long(p);
1191
16.3k
        if (n == -1 && PyErr_Occurred()) {
1192
0
            break;
1193
0
        }
1194
16.3k
        retval = PyLong_FromLong(n);
1195
16.3k
        R_REF(retval);
1196
16.3k
        break;
1197
1198
0
    case TYPE_INT64:
1199
0
        retval = r_long64(p);
1200
0
        R_REF(retval);
1201
0
        break;
1202
1203
159
    case TYPE_LONG:
1204
159
        retval = r_PyLong(p);
1205
159
        R_REF(retval);
1206
159
        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
166
    case TYPE_BINARY_FLOAT:
1219
166
        {
1220
166
            double x = r_float_bin(p);
1221
166
            if (x == -1.0 && PyErr_Occurred())
1222
0
                break;
1223
166
            retval = PyFloat_FromDouble(x);
1224
166
            R_REF(retval);
1225
166
            break;
1226
166
        }
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
0
    case TYPE_BINARY_COMPLEX:
1243
0
        {
1244
0
            Py_complex c;
1245
0
            c.real = r_float_bin(p);
1246
0
            if (c.real == -1.0 && PyErr_Occurred())
1247
0
                break;
1248
0
            c.imag = r_float_bin(p);
1249
0
            if (c.imag == -1.0 && PyErr_Occurred())
1250
0
                break;
1251
0
            retval = PyComplex_FromCComplex(c);
1252
0
            R_REF(retval);
1253
0
            break;
1254
0
        }
1255
1256
418k
    case TYPE_STRING:
1257
418k
        {
1258
418k
            const char *ptr;
1259
418k
            n = r_long(p);
1260
418k
            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
418k
            v = PyBytes_FromStringAndSize((char *)NULL, n);
1268
418k
            if (v == NULL)
1269
0
                break;
1270
418k
            ptr = r_string(n, p);
1271
418k
            if (ptr == NULL) {
1272
0
                Py_DECREF(v);
1273
0
                break;
1274
0
            }
1275
418k
            memcpy(PyBytes_AS_STRING(v), ptr, n);
1276
418k
            retval = v;
1277
418k
            R_REF(retval);
1278
418k
            break;
1279
418k
        }
1280
1281
0
    case TYPE_ASCII_INTERNED:
1282
0
        is_interned = 1;
1283
0
        _Py_FALLTHROUGH;
1284
25.3k
    case TYPE_ASCII:
1285
25.3k
        n = r_long(p);
1286
25.3k
        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
25.3k
        goto _read_ascii;
1294
1295
968k
    case TYPE_SHORT_ASCII_INTERNED:
1296
968k
        is_interned = 1;
1297
968k
        _Py_FALLTHROUGH;
1298
1.10M
    case TYPE_SHORT_ASCII:
1299
1.10M
        n = r_byte(p);
1300
1.10M
        if (n == EOF) {
1301
0
            break;
1302
0
        }
1303
1.13M
    _read_ascii:
1304
1.13M
        {
1305
1.13M
            const char *ptr;
1306
1.13M
            ptr = r_string(n, p);
1307
1.13M
            if (ptr == NULL)
1308
0
                break;
1309
1.13M
            v = PyUnicode_FromKindAndData(PyUnicode_1BYTE_KIND, ptr, n);
1310
1.13M
            if (v == NULL)
1311
0
                break;
1312
1.13M
            if (is_interned) {
1313
                // marshal is meant to serialize .pyc files with code
1314
                // objects, and code-related strings are currently immortal.
1315
968k
                PyInterpreterState *interp = _PyInterpreterState_GET();
1316
968k
                _PyUnicode_InternImmortal(interp, &v);
1317
968k
            }
1318
1.13M
            retval = v;
1319
1.13M
            R_REF(retval);
1320
1.13M
            break;
1321
1.13M
        }
1322
1323
101
    case TYPE_INTERNED:
1324
101
        is_interned = 1;
1325
101
        _Py_FALLTHROUGH;
1326
1.77k
    case TYPE_UNICODE:
1327
1.77k
        {
1328
1.77k
        const char *buffer;
1329
1330
1.77k
        n = r_long(p);
1331
1.77k
        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
1.77k
        if (n != 0) {
1339
1.77k
            buffer = r_string(n, p);
1340
1.77k
            if (buffer == NULL)
1341
0
                break;
1342
1.77k
            v = PyUnicode_DecodeUTF8(buffer, n, "surrogatepass");
1343
1.77k
        }
1344
0
        else {
1345
0
            v = Py_GetConstant(Py_CONSTANT_EMPTY_STR);
1346
0
        }
1347
1.77k
        if (v == NULL)
1348
0
            break;
1349
1.77k
        if (is_interned) {
1350
            // marshal is meant to serialize .pyc files with code
1351
            // objects, and code-related strings are currently immortal.
1352
101
            PyInterpreterState *interp = _PyInterpreterState_GET();
1353
101
            _PyUnicode_InternImmortal(interp, &v);
1354
101
        }
1355
1.77k
        retval = v;
1356
1.77k
        R_REF(retval);
1357
1.77k
        break;
1358
1.77k
        }
1359
1360
385k
    case TYPE_SMALL_TUPLE:
1361
385k
        n = r_byte(p);
1362
385k
        if (n == EOF) {
1363
0
            break;
1364
0
        }
1365
385k
        goto _read_tuple;
1366
385k
    case TYPE_TUPLE:
1367
34
        n = r_long(p);
1368
34
        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
385k
    _read_tuple:
1376
385k
        v = PyTuple_New(n);
1377
385k
        R_REF(v);
1378
385k
        if (v == NULL)
1379
0
            break;
1380
1381
2.64M
        for (i = 0; i < n; i++) {
1382
2.25M
            v2 = r_object(p);
1383
2.25M
            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
2.25M
            PyTuple_SET_ITEM(v, i, v2);
1391
2.25M
        }
1392
385k
        retval = v;
1393
385k
        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
255
    case TYPE_FROZENSET:
1453
255
        n = r_long(p);
1454
255
        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
255
        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
255
        else {
1471
255
            v = (type == TYPE_SET) ? PySet_New(NULL) : PyFrozenSet_New(NULL);
1472
255
            if (type == TYPE_SET) {
1473
0
                R_REF(v);
1474
255
            } else {
1475
                /* must use delayed registration of frozensets because they must
1476
                 * be init with a refcount of 1
1477
                 */
1478
255
                idx = r_ref_reserve(flag, p);
1479
255
                if (idx < 0)
1480
0
                    Py_CLEAR(v); /* signal error */
1481
255
            }
1482
255
            if (v == NULL)
1483
0
                break;
1484
1485
1.30k
            for (i = 0; i < n; i++) {
1486
1.05k
                v2 = r_object(p);
1487
1.05k
                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.05k
                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.05k
                Py_DECREF(v2);
1501
1.05k
            }
1502
255
            if (type != TYPE_SET)
1503
255
                v = r_ref_insert(v, idx, flag, p);
1504
255
            retval = v;
1505
255
        }
1506
255
        break;
1507
1508
138k
    case TYPE_CODE:
1509
138k
        {
1510
138k
            int argcount;
1511
138k
            int posonlyargcount;
1512
138k
            int kwonlyargcount;
1513
138k
            int stacksize;
1514
138k
            int flags;
1515
138k
            PyObject *code = NULL;
1516
138k
            PyObject *consts = NULL;
1517
138k
            PyObject *names = NULL;
1518
138k
            PyObject *localsplusnames = NULL;
1519
138k
            PyObject *localspluskinds = NULL;
1520
138k
            PyObject *filename = NULL;
1521
138k
            PyObject *name = NULL;
1522
138k
            PyObject *qualname = NULL;
1523
138k
            int firstlineno;
1524
138k
            PyObject* linetable = NULL;
1525
138k
            PyObject *exceptiontable = NULL;
1526
1527
138k
            if (!p->allow_code) {
1528
0
                PyErr_SetString(PyExc_ValueError,
1529
0
                                "unmarshalling code objects is disallowed");
1530
0
                break;
1531
0
            }
1532
138k
            idx = r_ref_reserve(flag, p);
1533
138k
            if (idx < 0)
1534
0
                break;
1535
1536
138k
            v = NULL;
1537
1538
            /* XXX ignore long->int overflows for now */
1539
138k
            argcount = (int)r_long(p);
1540
138k
            if (argcount == -1 && PyErr_Occurred())
1541
0
                goto code_error;
1542
138k
            posonlyargcount = (int)r_long(p);
1543
138k
            if (posonlyargcount == -1 && PyErr_Occurred()) {
1544
0
                goto code_error;
1545
0
            }
1546
138k
            kwonlyargcount = (int)r_long(p);
1547
138k
            if (kwonlyargcount == -1 && PyErr_Occurred())
1548
0
                goto code_error;
1549
138k
            stacksize = (int)r_long(p);
1550
138k
            if (stacksize == -1 && PyErr_Occurred())
1551
0
                goto code_error;
1552
138k
            flags = (int)r_long(p);
1553
138k
            if (flags == -1 && PyErr_Occurred())
1554
0
                goto code_error;
1555
138k
            code = r_object(p);
1556
138k
            if (code == NULL)
1557
0
                goto code_error;
1558
138k
            consts = r_object(p);
1559
138k
            if (consts == NULL)
1560
0
                goto code_error;
1561
138k
            names = r_object(p);
1562
138k
            if (names == NULL)
1563
0
                goto code_error;
1564
138k
            localsplusnames = r_object(p);
1565
138k
            if (localsplusnames == NULL)
1566
0
                goto code_error;
1567
138k
            localspluskinds = r_object(p);
1568
138k
            if (localspluskinds == NULL)
1569
0
                goto code_error;
1570
138k
            filename = r_object(p);
1571
138k
            if (filename == NULL)
1572
0
                goto code_error;
1573
138k
            name = r_object(p);
1574
138k
            if (name == NULL)
1575
0
                goto code_error;
1576
138k
            qualname = r_object(p);
1577
138k
            if (qualname == NULL)
1578
0
                goto code_error;
1579
138k
            firstlineno = (int)r_long(p);
1580
138k
            if (firstlineno == -1 && PyErr_Occurred())
1581
0
                break;
1582
138k
            linetable = r_object(p);
1583
138k
            if (linetable == NULL)
1584
0
                goto code_error;
1585
138k
            exceptiontable = r_object(p);
1586
138k
            if (exceptiontable == NULL)
1587
0
                goto code_error;
1588
1589
138k
            struct _PyCodeConstructor con = {
1590
138k
                .filename = filename,
1591
138k
                .name = name,
1592
138k
                .qualname = qualname,
1593
138k
                .flags = flags,
1594
1595
138k
                .code = code,
1596
138k
                .firstlineno = firstlineno,
1597
138k
                .linetable = linetable,
1598
1599
138k
                .consts = consts,
1600
138k
                .names = names,
1601
1602
138k
                .localsplusnames = localsplusnames,
1603
138k
                .localspluskinds = localspluskinds,
1604
1605
138k
                .argcount = argcount,
1606
138k
                .posonlyargcount = posonlyargcount,
1607
138k
                .kwonlyargcount = kwonlyargcount,
1608
1609
138k
                .stacksize = stacksize,
1610
1611
138k
                .exceptiontable = exceptiontable,
1612
138k
            };
1613
1614
138k
            if (_PyCode_Validate(&con) < 0) {
1615
0
                goto code_error;
1616
0
            }
1617
1618
138k
            v = (PyObject *)_PyCode_New(&con);
1619
138k
            if (v == NULL) {
1620
0
                goto code_error;
1621
0
            }
1622
1623
138k
            v = r_ref_insert(v, idx, flag, p);
1624
1625
138k
          code_error:
1626
138k
            if (v == NULL && !PyErr_Occurred()) {
1627
0
                PyErr_SetString(PyExc_TypeError,
1628
0
                    "NULL object in marshal data for code object");
1629
0
            }
1630
138k
            Py_XDECREF(code);
1631
138k
            Py_XDECREF(consts);
1632
138k
            Py_XDECREF(names);
1633
138k
            Py_XDECREF(localsplusnames);
1634
138k
            Py_XDECREF(localspluskinds);
1635
138k
            Py_XDECREF(filename);
1636
138k
            Py_XDECREF(name);
1637
138k
            Py_XDECREF(qualname);
1638
138k
            Py_XDECREF(linetable);
1639
138k
            Py_XDECREF(exceptiontable);
1640
138k
        }
1641
0
        retval = v;
1642
138k
        break;
1643
1644
1.47M
    case TYPE_REF:
1645
1.47M
        n = r_long(p);
1646
1.47M
        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.47M
        v = PyList_GET_ITEM(p->refs, n);
1654
1.47M
        if (v == Py_None) {
1655
0
            PyErr_SetString(PyExc_ValueError, "bad marshal data (invalid reference)");
1656
0
            break;
1657
0
        }
1658
1.47M
        retval = Py_NewRef(v);
1659
1.47M
        break;
1660
1661
2.01k
    case TYPE_SLICE:
1662
2.01k
    {
1663
2.01k
        Py_ssize_t idx = r_ref_reserve(flag, p);
1664
2.01k
        if (idx < 0) {
1665
0
            break;
1666
0
        }
1667
2.01k
        PyObject *stop = NULL;
1668
2.01k
        PyObject *step = NULL;
1669
2.01k
        PyObject *start = r_object(p);
1670
2.01k
        if (start == NULL) {
1671
0
            goto cleanup;
1672
0
        }
1673
2.01k
        stop = r_object(p);
1674
2.01k
        if (stop == NULL) {
1675
0
            goto cleanup;
1676
0
        }
1677
2.01k
        step = r_object(p);
1678
2.01k
        if (step == NULL) {
1679
0
            goto cleanup;
1680
0
        }
1681
2.01k
        retval = PySlice_New(start, stop, step);
1682
2.01k
        r_ref_insert(retval, idx, flag, p);
1683
2.01k
    cleanup:
1684
2.01k
        Py_XDECREF(start);
1685
2.01k
        Py_XDECREF(stop);
1686
2.01k
        Py_XDECREF(step);
1687
2.01k
        break;
1688
2.01k
    }
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.65M
    }
1697
3.65M
    p->depth--;
1698
3.65M
    return retval;
1699
3.65M
}
1700
1701
static PyObject *
1702
read_object(RFILE *p)
1703
5.98k
{
1704
5.98k
    PyObject *v;
1705
5.98k
    if (PyErr_Occurred()) {
1706
0
        fprintf(stderr, "XXX readobject called with exception set\n");
1707
0
        return NULL;
1708
0
    }
1709
5.98k
    if (p->ptr && p->end) {
1710
5.98k
        if (PySys_Audit("marshal.loads", "y#", p->ptr, (Py_ssize_t)(p->end - p->ptr)) < 0) {
1711
0
            return NULL;
1712
0
        }
1713
5.98k
    } else if (p->fp || p->readable) {
1714
0
        if (PySys_Audit("marshal.load", NULL) < 0) {
1715
0
            return NULL;
1716
0
        }
1717
0
    }
1718
5.98k
    v = r_object(p);
1719
5.98k
    if (v == NULL && !PyErr_Occurred())
1720
0
        PyErr_SetString(PyExc_TypeError, "NULL object in marshal data for object");
1721
5.98k
    return v;
1722
5.98k
}
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
393
{
1825
393
    RFILE rf;
1826
393
    PyObject *result;
1827
393
    rf.allow_code = 1;
1828
393
    rf.fp = NULL;
1829
393
    rf.readable = NULL;
1830
393
    rf.ptr = str;
1831
393
    rf.end = str + len;
1832
393
    rf.buf = NULL;
1833
393
    rf.depth = 0;
1834
393
    rf.refs = PyList_New(0);
1835
393
    if (rf.refs == NULL)
1836
0
        return NULL;
1837
393
    result = read_object(&rf);
1838
393
    Py_DECREF(rf.refs);
1839
393
    if (rf.buf != NULL)
1840
0
        PyMem_Free(rf.buf);
1841
393
    return result;
1842
393
}
1843
1844
static PyObject *
1845
_PyMarshal_WriteObjectToString(PyObject *x, int version, int allow_code)
1846
430
{
1847
430
    WFILE wf;
1848
1849
430
    if (PySys_Audit("marshal.dumps", "Oi", x, version) < 0) {
1850
0
        return NULL;
1851
0
    }
1852
430
    memset(&wf, 0, sizeof(wf));
1853
430
    wf.str = PyBytes_FromStringAndSize((char *)NULL, 50);
1854
430
    if (wf.str == NULL)
1855
0
        return NULL;
1856
430
    wf.ptr = wf.buf = PyBytes_AS_STRING(wf.str);
1857
430
    wf.end = wf.ptr + PyBytes_GET_SIZE(wf.str);
1858
430
    wf.error = WFERR_OK;
1859
430
    wf.version = version;
1860
430
    wf.allow_code = allow_code;
1861
430
    if (w_init_refs(&wf, version)) {
1862
0
        Py_DECREF(wf.str);
1863
0
        return NULL;
1864
0
    }
1865
430
    w_object(x, &wf);
1866
430
    w_clear_refs(&wf);
1867
430
    if (wf.str != NULL) {
1868
430
        const char *base = PyBytes_AS_STRING(wf.str);
1869
430
        if (_PyBytes_Resize(&wf.str, (Py_ssize_t)(wf.ptr - base)) < 0)
1870
0
            return NULL;
1871
430
    }
1872
430
    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
430
    return wf.str;
1895
430
}
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
260
{
2029
260
    return _PyMarshal_WriteObjectToString(value, version, allow_code);
2030
260
}
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
5.59k
{
2051
5.59k
    RFILE rf;
2052
5.59k
    char *s = bytes->buf;
2053
5.59k
    Py_ssize_t n = bytes->len;
2054
5.59k
    PyObject* result;
2055
5.59k
    rf.allow_code = allow_code;
2056
5.59k
    rf.fp = NULL;
2057
5.59k
    rf.readable = NULL;
2058
5.59k
    rf.ptr = s;
2059
5.59k
    rf.end = s + n;
2060
5.59k
    rf.depth = 0;
2061
5.59k
    if ((rf.refs = PyList_New(0)) == NULL)
2062
0
        return NULL;
2063
5.59k
    result = read_object(&rf);
2064
5.59k
    Py_DECREF(rf.refs);
2065
5.59k
    return result;
2066
5.59k
}
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
28
{
2110
28
    if (PyModule_AddIntConstant(mod, "version", Py_MARSHAL_VERSION) < 0) {
2111
0
        return -1;
2112
0
    }
2113
28
    return 0;
2114
28
}
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
28
{
2134
28
    return PyModuleDef_Init(&marshalmodule);
2135
28
}