/src/mercurial/contrib/fuzz/jsonescapeu8fast.cc
Line | Count | Source |
1 | | #include <Python.h> |
2 | | #include <assert.h> |
3 | | #include <stdlib.h> |
4 | | #include <unistd.h> |
5 | | |
6 | | #include "pyutil.h" |
7 | | |
8 | | #include <iostream> |
9 | | #include <string> |
10 | | #include "FuzzedDataProvider.h" |
11 | | |
12 | | extern "C" { |
13 | | |
14 | | static PYCODETYPE *code; |
15 | | |
16 | | extern "C" int LLVMFuzzerInitialize(int *argc, char ***argv) |
17 | 4 | { |
18 | 4 | contrib::initpy(*argv[0]); |
19 | 4 | code = (PYCODETYPE *)Py_CompileString(R"py( |
20 | 4 | try: |
21 | 4 | parsers.jsonescapeu8fast(data, paranoid) |
22 | 4 | except Exception as e: |
23 | 4 | pass |
24 | 4 | # uncomment this print if you're editing this Python code |
25 | 4 | # to debug failures. |
26 | 4 | # print(e) |
27 | 4 | )py", |
28 | 4 | "fuzzer", Py_file_input); |
29 | 4 | if (!code) { |
30 | 0 | std::cerr << "failed to compile Python code!" << std::endl; |
31 | 0 | } |
32 | 4 | return 0; |
33 | 4 | } |
34 | | |
35 | | int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) |
36 | 329 | { |
37 | 329 | FuzzedDataProvider provider(Data, Size); |
38 | 329 | bool paranoid = provider.ConsumeBool(); |
39 | 329 | std::string remainder = provider.ConsumeRemainingBytesAsString(); |
40 | | |
41 | 329 | PyObject *mtext = PyBytes_FromStringAndSize( |
42 | 329 | (const char *)remainder.c_str(), remainder.size()); |
43 | 329 | PyObject *locals = PyDict_New(); |
44 | 329 | PyDict_SetItemString(locals, "data", mtext); |
45 | 329 | PyDict_SetItemString(locals, "paranoid", paranoid ? Py_True : Py_False); |
46 | 329 | PyObject *res = PyEval_EvalCode(code, contrib::pyglobals(), locals); |
47 | 329 | if (!res) { |
48 | 0 | PyErr_Print(); |
49 | 0 | } |
50 | 329 | Py_XDECREF(res); |
51 | 329 | Py_DECREF(locals); |
52 | 329 | Py_DECREF(mtext); |
53 | 329 | return 0; // Non-zero return values are reserved for future use. |
54 | 329 | } |
55 | | } |