comparison contrib/fuzz/fncache.cc @ 43859:8766728dbce6

fuzz: add support for fuzzing under either Python 2 or 3 This was more of a hairball than I hoped, but it appears to work. The hg-py3 branch of my oss-fuzz fork on github has the remaining changes to switch us to Python 3, but we may as well retain Python 2 fuzzing support for at least a little while. Differential Revision: https://phab.mercurial-scm.org/D7592
author Augie Fackler <augie@google.com>
date Mon, 09 Dec 2019 22:20:35 -0500
parents b37dd26935ee
children
comparison
equal deleted inserted replaced
43858:b0867b7751ba 43859:8766728dbce6
8 #include <iostream> 8 #include <iostream>
9 #include <string> 9 #include <string>
10 10
11 extern "C" { 11 extern "C" {
12 12
13 static PyCodeObject *code; 13 static PYCODETYPE *code;
14 14
15 extern "C" int LLVMFuzzerInitialize(int *argc, char ***argv) 15 extern "C" int LLVMFuzzerInitialize(int *argc, char ***argv)
16 { 16 {
17 contrib::initpy(*argv[0]); 17 contrib::initpy(*argv[0]);
18 code = (PyCodeObject *)Py_CompileString(R"py( 18 code = (PYCODETYPE *)Py_CompileString(R"py(
19 from parsers import (
20 isasciistr,
21 asciilower,
22 asciiupper,
23 encodedir,
24 pathencode,
25 lowerencode,
26 )
27
28 try: 19 try:
29 for fn in ( 20 for fn in (
30 isasciistr, 21 parsers.isasciistr,
31 asciilower, 22 parsers.asciilower,
32 asciiupper, 23 parsers.asciiupper,
33 encodedir, 24 parsers.encodedir,
34 pathencode, 25 parsers.pathencode,
35 lowerencode, 26 parsers.lowerencode,
36 ): 27 ):
37 try: 28 try:
38 fn(data) 29 fn(data)
39 except UnicodeDecodeError: 30 except UnicodeDecodeError:
40 pass # some functions emit this exception 31 pass # some functions emit this exception
51 pass 42 pass
52 # uncomment this print if you're editing this Python code 43 # uncomment this print if you're editing this Python code
53 # to debug failures. 44 # to debug failures.
54 # print(e) 45 # print(e)
55 )py", 46 )py",
56 "fuzzer", Py_file_input); 47 "fuzzer", Py_file_input);
57 if (!code) { 48 if (!code) {
58 std::cerr << "failed to compile Python code!" << std::endl; 49 std::cerr << "failed to compile Python code!" << std::endl;
59 } 50 }
60 return 0; 51 return 0;
61 } 52 }