Coverage Report

Created: 2025-11-11 06:44

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