Debian Codegraph @

Modules/_xxtestfuzz/fuzzer.c

raw
1 /* A fuzz test for CPython.
2
3 The only exposed function is LLVMFuzzerTestOneInput, which is called by
4 fuzzers and by the _fuzz module for smoke tests.
5
6 To build exactly one fuzz test, as when running in oss-fuzz etc.,
7 build with -D _Py_FUZZ_ONE and -D _Py_FUZZ_<test_name>. e.g. to build
8 LLVMFuzzerTestOneInput to only run "fuzz_builtin_float", build this file with
9 -D _Py_FUZZ_ONE -D _Py_FUZZ_fuzz_builtin_float.
10
11 See the source code for LLVMFuzzerTestOneInput for details. */
12
13 #ifndef Py_BUILD_CORE
14 # define Py_BUILD_CORE 1
15 #endif
16
17 #include <Python.h>
18 #include "pycore_pyhash.h" // _Py_HashBytes()
19 #include <stdlib.h>
20 #include <inttypes.h>
21
22 /* Fuzz PyFloat_FromString as a proxy for float(str). */
23 static int fuzz_builtin_float(const char* data, size_t size) {
24 PyObject* s = PyBytes_FromStringAndSize(data, size);
25 if (s == NULL) return 0;
26 PyObject* f = PyFloat_FromString(s);
27 if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_ValueError)) {
28 PyErr_Clear();
29 }
30
31 Py_XDECREF(f);
32 Py_DECREF(s);
33 return 0;
34 }
35
36 #define MAX_INT_TEST_SIZE 0x10000
37
38 /* Fuzz PyLong_FromUnicodeObject as a proxy for int(str). */
39 static int fuzz_builtin_int(const char* data, size_t size) {
40 /* Ignore test cases with very long ints to avoid timeouts
41 int("9" * 1000000) is not a very interesting test caase */
42 if (size > MAX_INT_TEST_SIZE) {
43 return 0;
44 }
45 /* Pick a random valid base. (When the fuzzed function takes extra
46 parameters, it's somewhat normal to hash the input to generate those
47 parameters. We want to exercise all code paths, so we do so here.) */
48 int base = _Py_HashBytes(data, size) % 37;
49 if (base == 1) {
50 // 1 is the only number between 0 and 36 that is not a valid base.
51 base = 0;
52 }
53 if (base == -1) {
54 return 0; // An error occurred, bail early.
55 }
56 if (base < 0) {
57 base = -base;
58 }
59
60 PyObject* s = PyUnicode_FromStringAndSize(data, size);
61 if (s == NULL) {
62 if (PyErr_ExceptionMatches(PyExc_UnicodeDecodeError)) {
63 PyErr_Clear();
64 }
65 return 0;
66 }
67 PyObject* l = PyLong_FromUnicodeObject(s, base);
68 if (l == NULL && PyErr_ExceptionMatches(PyExc_ValueError)) {
69 PyErr_Clear();
70 }
71 PyErr_Clear();
72 Py_XDECREF(l);
73 Py_DECREF(s);
74 return 0;
75 }
76
77 /* Fuzz PyUnicode_FromStringAndSize as a proxy for unicode(str). */
78 static int fuzz_builtin_unicode(const char* data, size_t size) {
79 PyObject* s = PyUnicode_FromStringAndSize(data, size);
80 if (s == NULL && PyErr_ExceptionMatches(PyExc_UnicodeDecodeError)) {
81 PyErr_Clear();
82 }
83 Py_XDECREF(s);
84 return 0;
85 }
86
87
88 PyObject* struct_unpack_method = NULL;
89 PyObject* struct_error = NULL;
90 /* Called by LLVMFuzzerTestOneInput for initialization */
91 static int init_struct_unpack(void) {
92 /* Import struct.unpack */
93 PyObject* struct_module = PyImport_ImportModule("struct");
94 if (struct_module == NULL) {
95 return 0;
96 }
97 struct_error = PyObject_GetAttrString(struct_module, "error");
98 if (struct_error == NULL) {
99 return 0;
100 }
101 struct_unpack_method = PyObject_GetAttrString(struct_module, "unpack");
102 return struct_unpack_method != NULL;
103 }
104 /* Fuzz struct.unpack(x, y) */
105 static int fuzz_struct_unpack(const char* data, size_t size) {
106 /* Everything up to the first null byte is considered the
107 format. Everything after is the buffer */
108 const char* first_null = memchr(data, '\0', size);
109 if (first_null == NULL) {
110 return 0;
111 }
112
113 size_t format_length = first_null - data;
114 size_t buffer_length = size - format_length - 1;
115
116 PyObject* pattern = PyBytes_FromStringAndSize(data, format_length);
117 if (pattern == NULL) {
118 return 0;
119 }
120 PyObject* buffer = PyBytes_FromStringAndSize(first_null + 1, buffer_length);
121 if (buffer == NULL) {
122 Py_DECREF(pattern);
123 return 0;
124 }
125
126 PyObject* unpacked = PyObject_CallFunctionObjArgs(
127 struct_unpack_method, pattern, buffer, NULL);
128 /* Ignore any overflow errors, these are easily triggered accidentally */
129 if (unpacked == NULL && PyErr_ExceptionMatches(PyExc_OverflowError)) {
130 PyErr_Clear();
131 }
132 /* The pascal format string will throw a negative size when passing 0
133 like: struct.unpack('0p', b'') */
134 if (unpacked == NULL && PyErr_ExceptionMatches(PyExc_SystemError)) {
135 PyErr_Clear();
136 }
137 /* Ignore any struct.error exceptions, these can be caused by invalid
138 formats or incomplete buffers both of which are common. */
139 if (unpacked == NULL && PyErr_ExceptionMatches(struct_error)) {
140 PyErr_Clear();
141 }
142
143 Py_XDECREF(unpacked);
144 Py_DECREF(pattern);
145 Py_DECREF(buffer);
146 return 0;
147 }
148
149
150 #define MAX_JSON_TEST_SIZE 0x100000
151
152 PyObject* json_loads_method = NULL;
153 /* Called by LLVMFuzzerTestOneInput for initialization */
154 static int init_json_loads(void) {
155 /* Import json.loads */
156 PyObject* json_module = PyImport_ImportModule("json");
157 if (json_module == NULL) {
158 return 0;
159 }
160 json_loads_method = PyObject_GetAttrString(json_module, "loads");
161 return json_loads_method != NULL;
162 }
163 /* Fuzz json.loads(x) */
164 static int fuzz_json_loads(const char* data, size_t size) {
165 /* Since python supports arbitrarily large ints in JSON,
166 long inputs can lead to timeouts on boring inputs like
167 `json.loads("9" * 100000)` */
168 if (size > MAX_JSON_TEST_SIZE) {
169 return 0;
170 }
171 PyObject* input_bytes = PyBytes_FromStringAndSize(data, size);
172 if (input_bytes == NULL) {
173 return 0;
174 }
175 PyObject* parsed = PyObject_CallOneArg(json_loads_method, input_bytes);
176 if (parsed == NULL) {
177 /* Ignore ValueError as the fuzzer will more than likely
178 generate some invalid json and values */
179 if (PyErr_ExceptionMatches(PyExc_ValueError) ||
180 /* Ignore RecursionError as the fuzzer generates long sequences of
181 arrays such as `[[[...` */
182 PyErr_ExceptionMatches(PyExc_RecursionError) ||
183 /* Ignore unicode errors, invalid byte sequences are common */
184 PyErr_ExceptionMatches(PyExc_UnicodeDecodeError)
185 ) {
186 PyErr_Clear();
187 }
188 }
189 Py_DECREF(input_bytes);
190 Py_XDECREF(parsed);
191 return 0;
192 }
193
194 #define MAX_RE_TEST_SIZE 0x10000
195
196 PyObject* re_compile_method = NULL;
197 PyObject* re_error_exception = NULL;
198 int RE_FLAG_DEBUG = 0;
199 /* Called by LLVMFuzzerTestOneInput for initialization */
200 static int init_sre_compile(void) {
201 /* Import sre_compile.compile and sre.error */
202 PyObject* re_module = PyImport_ImportModule("re");
203 if (re_module == NULL) {
204 return 0;
205 }
206 re_compile_method = PyObject_GetAttrString(re_module, "compile");
207 if (re_compile_method == NULL) {
208 return 0;
209 }
210
211 re_error_exception = PyObject_GetAttrString(re_module, "error");
212 if (re_error_exception == NULL) {
213 return 0;
214 }
215 PyObject* debug_flag = PyObject_GetAttrString(re_module, "DEBUG");
216 if (debug_flag == NULL) {
217 return 0;
218 }
219 RE_FLAG_DEBUG = PyLong_AsLong(debug_flag);
220 return 1;
221 }
222 /* Fuzz re.compile(x) */
223 static int fuzz_sre_compile(const char* data, size_t size) {
224 /* Ignore really long regex patterns that will timeout the fuzzer */
225 if (size > MAX_RE_TEST_SIZE) {
226 return 0;
227 }
228 /* We treat the first 2 bytes of the input as a number for the flags */
229 if (size < 2) {
230 return 0;
231 }
232 uint16_t flags = ((uint16_t*) data)[0];
233 /* We remove the SRE_FLAG_DEBUG if present. This is because it
234 prints to stdout which greatly decreases fuzzing speed */
235 flags &= ~RE_FLAG_DEBUG;
236
237 /* Pull the pattern from the remaining bytes */
238 PyObject* pattern_bytes = PyBytes_FromStringAndSize(data + 2, size - 2);
239 if (pattern_bytes == NULL) {
240 return 0;
241 }
242 PyObject* flags_obj = PyLong_FromUnsignedLong(flags);
243 if (flags_obj == NULL) {
244 Py_DECREF(pattern_bytes);
245 return 0;
246 }
247
248 /* compiled = re.compile(data[2:], data[0:2] */
249 PyObject* compiled = PyObject_CallFunctionObjArgs(
250 re_compile_method, pattern_bytes, flags_obj, NULL);
251 /* Ignore ValueError as the fuzzer will more than likely
252 generate some invalid combination of flags */
253 if (compiled == NULL && PyErr_ExceptionMatches(PyExc_ValueError)) {
254 PyErr_Clear();
255 }
256 /* Ignore some common errors thrown by sre_parse:
257 Overflow, Assertion, Recursion and Index */
258 if (compiled == NULL && (PyErr_ExceptionMatches(PyExc_OverflowError) ||
259 PyErr_ExceptionMatches(PyExc_AssertionError) ||
260 PyErr_ExceptionMatches(PyExc_RecursionError) ||
261 PyErr_ExceptionMatches(PyExc_IndexError))
262 ) {
263 PyErr_Clear();
264 }
265 /* Ignore re.error */
266 if (compiled == NULL && PyErr_ExceptionMatches(re_error_exception)) {
267 PyErr_Clear();
268 }
269
270 Py_DECREF(pattern_bytes);
271 Py_DECREF(flags_obj);
272 Py_XDECREF(compiled);
273 return 0;
274 }
275
276 /* Some random patterns used to test re.match.
277 Be careful not to add catostraphically slow regexes here, we want to
278 exercise the matching code without causing timeouts.*/
279 static const char* regex_patterns[] = {
280 ".", "^", "abc", "abc|def", "^xxx$", "\\b", "()", "[a-zA-Z0-9]",
281 "abc+", "[^A-Z]", "[x]", "(?=)", "a{z}", "a+b", "a*?", "a??", "a+?",
282 "{}", "a{,}", "{", "}", "^\\(*\\d{3}\\)*( |-)*\\d{3}( |-)*\\d{4}$",
283 "(?:a*)*", "a{1,2}?"
284 };
285 const size_t NUM_PATTERNS = sizeof(regex_patterns) / sizeof(regex_patterns[0]);
286 PyObject** compiled_patterns = NULL;
287 /* Called by LLVMFuzzerTestOneInput for initialization */
288 static int init_sre_match(void) {
289 PyObject* re_module = PyImport_ImportModule("re");
290 if (re_module == NULL) {
291 return 0;
292 }
293 compiled_patterns = (PyObject**) PyMem_RawMalloc(
294 sizeof(PyObject*) * NUM_PATTERNS);
295 if (compiled_patterns == NULL) {
296 PyErr_NoMemory();
297 return 0;
298 }
299
300 /* Precompile all the regex patterns on the first run for faster fuzzing */
301 for (size_t i = 0; i < NUM_PATTERNS; i++) {
302 PyObject* compiled = PyObject_CallMethod(
303 re_module, "compile", "y", regex_patterns[i]);
304 /* Bail if any of the patterns fail to compile */
305 if (compiled == NULL) {
306 return 0;
307 }
308 compiled_patterns[i] = compiled;
309 }
310 return 1;
311 }
312 /* Fuzz re.match(x) */
313 static int fuzz_sre_match(const char* data, size_t size) {
314 if (size < 1 || size > MAX_RE_TEST_SIZE) {
315 return 0;
316 }
317 /* Use the first byte as a uint8_t specifying the index of the
318 regex to use */
319 unsigned char idx = (unsigned char) data[0];
320 idx = idx % NUM_PATTERNS;
321
322 /* Pull the string to match from the remaining bytes */
323 PyObject* to_match = PyBytes_FromStringAndSize(data + 1, size - 1);
324 if (to_match == NULL) {
325 return 0;
326 }
327
328 PyObject* pattern = compiled_patterns[idx];
329 PyObject* match_callable = PyObject_GetAttrString(pattern, "match");
330
331 PyObject* matches = PyObject_CallOneArg(match_callable, to_match);
332
333 Py_XDECREF(matches);
334 Py_DECREF(match_callable);
335 Py_DECREF(to_match);
336 return 0;
337 }
338
339 #define MAX_CSV_TEST_SIZE 0x100000
340 PyObject* csv_module = NULL;
341 PyObject* csv_error = NULL;
342 /* Called by LLVMFuzzerTestOneInput for initialization */
343 static int init_csv_reader(void) {
344 /* Import csv and csv.Error */
345 csv_module = PyImport_ImportModule("csv");
346 if (csv_module == NULL) {
347 return 0;
348 }
349 csv_error = PyObject_GetAttrString(csv_module, "Error");
350 return csv_error != NULL;
351 }
352 /* Fuzz csv.reader([x]) */
353 static int fuzz_csv_reader(const char* data, size_t size) {
354 if (size < 1 || size > MAX_CSV_TEST_SIZE) {
355 return 0;
356 }
357 /* Ignore non null-terminated strings since _csv can't handle
358 embedded nulls */
359 if (memchr(data, '\0', size) == NULL) {
360 return 0;
361 }
362
363 PyObject* s = PyUnicode_FromString(data);
364 /* Ignore exceptions until we have a valid string */
365 if (s == NULL) {
366 PyErr_Clear();
367 return 0;
368 }
369
370 /* Split on \n so we can test multiple lines */
371 PyObject* lines = PyObject_CallMethod(s, "split", "s", "\n");
372 if (lines == NULL) {
373 Py_DECREF(s);
374 return 0;
375 }
376
377 PyObject* reader = PyObject_CallMethod(csv_module, "reader", "N", lines);
378 if (reader) {
379 /* Consume all of the reader as an iterator */
380 PyObject* parsed_line;
381 while ((parsed_line = PyIter_Next(reader))) {
382 Py_DECREF(parsed_line);
383 }
384 }
385
386 /* Ignore csv.Error because we're probably going to generate
387 some bad files (embedded new-lines, unterminated quotes etc) */
388 if (PyErr_ExceptionMatches(csv_error)) {
389 PyErr_Clear();
390 }
391
392 Py_XDECREF(reader);
393 Py_DECREF(s);
394 return 0;
395 }
396
397 #define MAX_AST_LITERAL_EVAL_TEST_SIZE 0x100000
398 PyObject* ast_literal_eval_method = NULL;
399 /* Called by LLVMFuzzerTestOneInput for initialization */
400 static int init_ast_literal_eval(void) {
401 PyObject* ast_module = PyImport_ImportModule("ast");
402 if (ast_module == NULL) {
403 return 0;
404 }
405 ast_literal_eval_method = PyObject_GetAttrString(ast_module, "literal_eval");
406 return ast_literal_eval_method != NULL;
407 }
408 /* Fuzz ast.literal_eval(x) */
409 static int fuzz_ast_literal_eval(const char* data, size_t size) {
410 if (size > MAX_AST_LITERAL_EVAL_TEST_SIZE) {
411 return 0;
412 }
413 /* Ignore non null-terminated strings since ast can't handle
414 embedded nulls */
415 if (memchr(data, '\0', size) == NULL) {
416 return 0;
417 }
418
419 PyObject* s = PyUnicode_FromString(data);
420 /* Ignore exceptions until we have a valid string */
421 if (s == NULL) {
422 PyErr_Clear();
423 return 0;
424 }
425
426 PyObject* literal = PyObject_CallOneArg(ast_literal_eval_method, s);
427 /* Ignore some common errors thrown by ast.literal_eval */
428 if (literal == NULL && (PyErr_ExceptionMatches(PyExc_ValueError) ||
429 PyErr_ExceptionMatches(PyExc_TypeError) ||
430 PyErr_ExceptionMatches(PyExc_SyntaxError) ||
431 PyErr_ExceptionMatches(PyExc_MemoryError) ||
432 PyErr_ExceptionMatches(PyExc_RecursionError))
433 ) {
434 PyErr_Clear();
435 }
436
437 Py_XDECREF(literal);
438 Py_DECREF(s);
439 return 0;
440 }
441
442 #define MAX_ELEMENTTREE_PARSEWHOLE_TEST_SIZE 0x100000
443 PyObject* xmlparser_type = NULL;
444 PyObject* bytesio_type = NULL;
445 /* Called by LLVMFuzzerTestOneInput for initialization */
446 static int init_elementtree_parsewhole(void) {
447 PyObject* elementtree_module = PyImport_ImportModule("_elementtree");
448 if (elementtree_module == NULL) {
449 return 0;
450 }
451 xmlparser_type = PyObject_GetAttrString(elementtree_module, "XMLParser");
452 Py_DECREF(elementtree_module);
453 if (xmlparser_type == NULL) {
454 return 0;
455 }
456
457
458 PyObject* io_module = PyImport_ImportModule("io");
459 if (io_module == NULL) {
460 return 0;
461 }
462 bytesio_type = PyObject_GetAttrString(io_module, "BytesIO");
463 Py_DECREF(io_module);
464 if (bytesio_type == NULL) {
465 return 0;
466 }
467
468 return 1;
469 }
470 /* Fuzz _elementtree.XMLParser._parse_whole(x) */
471 static int fuzz_elementtree_parsewhole(const char* data, size_t size) {
472 if (size > MAX_ELEMENTTREE_PARSEWHOLE_TEST_SIZE) {
473 return 0;
474 }
475
476 PyObject *input = PyObject_CallFunction(bytesio_type, "y#", data, (Py_ssize_t)size);
477 if (input == NULL) {
478 assert(PyErr_Occurred());
479 PyErr_Print();
480 abort();
481 }
482
483 PyObject *xmlparser_instance = PyObject_CallObject(xmlparser_type, NULL);
484 if (xmlparser_instance == NULL) {
485 assert(PyErr_Occurred());
486 PyErr_Print();
487 abort();
488 }
489
490 PyObject *result = PyObject_CallMethod(xmlparser_instance, "_parse_whole", "O", input);
491 if (result == NULL) {
492 /* Ignore exception here, which can be caused by invalid XML input */
493 PyErr_Clear();
494 } else {
495 Py_DECREF(result);
496 }
497
498 Py_DECREF(xmlparser_instance);
499 Py_DECREF(input);
500
501 return 0;
502 }
503
504 #define MAX_PYCOMPILE_TEST_SIZE 16384
505
506 static const int start_vals[] = {Py_eval_input, Py_single_input, Py_file_input};
507 const size_t NUM_START_VALS = sizeof(start_vals) / sizeof(start_vals[0]);
508
509 static const int optimize_vals[] = {-1, 0, 1, 2};
510 const size_t NUM_OPTIMIZE_VALS = sizeof(optimize_vals) / sizeof(optimize_vals[0]);
511
512 /* Fuzz `PyCompileStringExFlags` using a variety of input parameters.
513 * That function is essentially behind the `compile` builtin */
514 static int fuzz_pycompile(const char* data, size_t size) {
515 // Ignore overly-large inputs, and account for a NUL terminator
516 if (size > MAX_PYCOMPILE_TEST_SIZE - 1) {
517 return 0;
518 }
519
520 // Need 2 bytes for parameter selection
521 if (size < 2) {
522 return 0;
523 }
524
525 // Use first byte to determine element of `start_vals` to use
526 unsigned char start_idx = (unsigned char) data[0];
527 int start = start_vals[start_idx % NUM_START_VALS];
528
529 // Use second byte to determine element of `optimize_vals` to use
530 unsigned char optimize_idx = (unsigned char) data[1];
531 int optimize = optimize_vals[optimize_idx % NUM_OPTIMIZE_VALS];
532
533 char pycompile_scratch[MAX_PYCOMPILE_TEST_SIZE];
534
535 // Create a NUL-terminated C string from the remaining input
536 memcpy(pycompile_scratch, data + 2, size - 2);
537 // Put a NUL terminator just after the copied data. (Space was reserved already.)
538 pycompile_scratch[size - 2] = '\0';
539
540 // XXX: instead of always using NULL for the `flags` value to
541 // `Py_CompileStringExFlags`, there are many flags that conditionally
542 // change parser behavior:
543 //
544 // #define PyCF_TYPE_COMMENTS 0x1000
545 // #define PyCF_ALLOW_TOP_LEVEL_AWAIT 0x2000
546 // #define PyCF_ONLY_AST 0x0400
547 //
548 // It would be good to test various combinations of these, too.
549 PyCompilerFlags *flags = NULL;
550
551 PyObject *result = Py_CompileStringExFlags(pycompile_scratch, "<fuzz input>", start, flags, optimize);
552 if (result == NULL) {
553 /* Compilation failed, most likely from a syntax error. If it was a
554 SystemError we abort. There's no non-bug reason to raise a
555 SystemError. */
556 if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_SystemError)) {
557 PyErr_Print();
558 abort();
559 }
560 PyErr_Clear();
561 } else {
562 Py_DECREF(result);
563 }
564
565 return 0;
566 }
567
568 /* Run fuzzer and abort on failure. */
569 static int _run_fuzz(const uint8_t *data, size_t size, int(*fuzzer)(const char* , size_t)) {
570 int rv = fuzzer((const char*) data, size);
571 if (PyErr_Occurred()) {
572 /* Fuzz tests should handle expected errors for themselves.
573 This is last-ditch check in case they didn't. */
574 PyErr_Print();
575 abort();
576 }
577 /* Someday the return value might mean something, propagate it. */
578 return rv;
579 }
580
581 /* CPython generates a lot of leak warnings for whatever reason. */
582 int __lsan_is_turned_off(void) { return 1; }
583
584
585 int LLVMFuzzerInitialize(int *argc, char ***argv) {
586 PyConfig config;
587 PyConfig_InitPythonConfig(&config);
588 config.install_signal_handlers = 0;
589 /* Raise the limit above the default allows exercising larger things
590 * now that we fall back to the _pylong module for large values. */
591 config.int_max_str_digits = 8086;
592 PyStatus status;
593 status = PyConfig_SetBytesString(&config, &config.program_name, *argv[0]);
594 if (PyStatus_Exception(status)) {
595 goto fail;
596 }
597
598 status = Py_InitializeFromConfig(&config);
599 if (PyStatus_Exception(status)) {
600 goto fail;
601 }
602 PyConfig_Clear(&config);
603
604 return 0;
605
606 fail:
607 PyConfig_Clear(&config);
608 Py_ExitStatusException(status);
609 }
610
611 /* Fuzz test interface.
612 This returns the bitwise or of all fuzz test's return values.
613
614 All fuzz tests must return 0, as all nonzero return codes are reserved for
615 future use -- we propagate the return values for that future case.
616 (And we bitwise or when running multiple tests to verify that normally we
617 only return 0.) */
618 int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
619 assert(Py_IsInitialized());
620
621 int rv = 0;
622
623 #if !defined(_Py_FUZZ_ONE) || defined(_Py_FUZZ_fuzz_builtin_float)
624 rv |= _run_fuzz(data, size, fuzz_builtin_float);
625 #endif
626 #if !defined(_Py_FUZZ_ONE) || defined(_Py_FUZZ_fuzz_builtin_int)
627 rv |= _run_fuzz(data, size, fuzz_builtin_int);
628 #endif
629 #if !defined(_Py_FUZZ_ONE) || defined(_Py_FUZZ_fuzz_builtin_unicode)
630 rv |= _run_fuzz(data, size, fuzz_builtin_unicode);
631 #endif
632 #if !defined(_Py_FUZZ_ONE) || defined(_Py_FUZZ_fuzz_struct_unpack)
633 static int STRUCT_UNPACK_INITIALIZED = 0;
634 if (!STRUCT_UNPACK_INITIALIZED && !init_struct_unpack()) {
635 PyErr_Print();
636 abort();
637 } else {
638 STRUCT_UNPACK_INITIALIZED = 1;
639 }
640 rv |= _run_fuzz(data, size, fuzz_struct_unpack);
641 #endif
642 #if !defined(_Py_FUZZ_ONE) || defined(_Py_FUZZ_fuzz_json_loads)
643 static int JSON_LOADS_INITIALIZED = 0;
644 if (!JSON_LOADS_INITIALIZED && !init_json_loads()) {
645 PyErr_Print();
646 abort();
647 } else {
648 JSON_LOADS_INITIALIZED = 1;
649 }
650
651 rv |= _run_fuzz(data, size, fuzz_json_loads);
652 #endif
653 #if !defined(_Py_FUZZ_ONE) || defined(_Py_FUZZ_fuzz_sre_compile)
654 static int SRE_COMPILE_INITIALIZED = 0;
655 if (!SRE_COMPILE_INITIALIZED && !init_sre_compile()) {
656 PyErr_Print();
657 abort();
658 } else {
659 SRE_COMPILE_INITIALIZED = 1;
660 }
661
662 if (SRE_COMPILE_INITIALIZED) {
663 rv |= _run_fuzz(data, size, fuzz_sre_compile);
664 }
665 #endif
666 #if !defined(_Py_FUZZ_ONE) || defined(_Py_FUZZ_fuzz_sre_match)
667 static int SRE_MATCH_INITIALIZED = 0;
668 if (!SRE_MATCH_INITIALIZED && !init_sre_match()) {
669 PyErr_Print();
670 abort();
671 } else {
672 SRE_MATCH_INITIALIZED = 1;
673 }
674
675 rv |= _run_fuzz(data, size, fuzz_sre_match);
676 #endif
677 #if !defined(_Py_FUZZ_ONE) || defined(_Py_FUZZ_fuzz_csv_reader)
678 static int CSV_READER_INITIALIZED = 0;
679 if (!CSV_READER_INITIALIZED && !init_csv_reader()) {
680 PyErr_Print();
681 abort();
682 } else {
683 CSV_READER_INITIALIZED = 1;
684 }
685
686 rv |= _run_fuzz(data, size, fuzz_csv_reader);
687 #endif
688 #if !defined(_Py_FUZZ_ONE) || defined(_Py_FUZZ_fuzz_ast_literal_eval)
689 static int AST_LITERAL_EVAL_INITIALIZED = 0;
690 if (!AST_LITERAL_EVAL_INITIALIZED && !init_ast_literal_eval()) {
691 PyErr_Print();
692 abort();
693 } else {
694 AST_LITERAL_EVAL_INITIALIZED = 1;
695 }
696
697 rv |= _run_fuzz(data, size, fuzz_ast_literal_eval);
698 #endif
699 #if !defined(_Py_FUZZ_ONE) || defined(_Py_FUZZ_fuzz_elementtree_parsewhole)
700 static int ELEMENTTREE_PARSEWHOLE_INITIALIZED = 0;
701 if (!ELEMENTTREE_PARSEWHOLE_INITIALIZED && !init_elementtree_parsewhole()) {
702 PyErr_Print();
703 abort();
704 } else {
705 ELEMENTTREE_PARSEWHOLE_INITIALIZED = 1;
706 }
707
708 rv |= _run_fuzz(data, size, fuzz_elementtree_parsewhole);
709 #endif
710 #if !defined(_Py_FUZZ_ONE) || defined(_Py_FUZZ_fuzz_pycompile)
711 rv |= _run_fuzz(data, size, fuzz_pycompile);
712 #endif
713 return rv;
714 }