Coverage Report

Created: 2026-02-26 06:53

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