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