| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
#include <string> |
| 30 |
#include "file_io_utils.h" |
| 31 |
|
| 32 |
#ifdef OSSFUZZ |
| 33 |
|
| 34 |
#define BEGIN_INIT_SIMPLE_FUZZER() \ |
| 35 |
static int init() \ |
| 36 |
{ \ |
| 37 |
try \ |
| 38 |
{ |
| 39 |
|
| 40 |
#define END_INIT_SIMPLE_FUZZER() \ |
| 41 |
} \ |
| 42 |
catch (const std::exception &e) \ |
| 43 |
{ \ |
| 44 |
fprintf(stderr, "Exception: %s\n", e.what()); \ |
| 45 |
return 1; \ |
| 46 |
} \ |
| 47 |
return 0; \ |
| 48 |
} |
| 49 |
|
| 50 |
#define BEGIN_SIMPLE_FUZZER() \ |
| 51 |
extern "C" { \ |
| 52 |
int LLVMFuzzerTestOneInput(const uint8_t *buf, size_t len) \ |
| 53 |
{ \ |
| 54 |
try \ |
| 55 |
{ \ |
| 56 |
static bool first = true; \ |
| 57 |
if (first) \ |
| 58 |
{ \ |
| 59 |
if (init()) \ |
| 60 |
return 1; \ |
| 61 |
first = false; \ |
| 62 |
} \ |
| 63 |
|
| 64 |
#define END_SIMPLE_FUZZER() \ |
| 65 |
} \ |
| 66 |
catch (const std::exception &e) \ |
| 67 |
{ \ |
| 68 |
fprintf(stderr, "Exception: %s\n", e.what()); \ |
| 69 |
delete el::base::elStorage; \ |
| 70 |
el::base::elStorage = NULL; \ |
| 71 |
return 0; \ |
| 72 |
} \ |
| 73 |
delete el::base::elStorage; \ |
| 74 |
el::base::elStorage = NULL; \ |
| 75 |
return 0; \ |
| 76 |
} \ |
| 77 |
} |
| 78 |
|
| 79 |
#else |
| 80 |
|
| 81 |
class Fuzzer |
| 82 |
{ |
| 83 |
public: |
| 84 |
virtual int init() { return 0; } |
| 85 |
virtual int run(const std::string &filename) = 0; |
| 86 |
}; |
| 87 |
|
| 88 |
int run_fuzzer(int argc, const char **argv, Fuzzer &fuzzer); |
| 89 |
|
| 90 |
#define BEGIN_INIT_SIMPLE_FUZZER() \ |
| 91 |
class SimpleFuzzer: public Fuzzer \ |
| 92 |
{ \ |
| 93 |
virtual int init() \ |
| 94 |
{ \ |
| 95 |
try \ |
| 96 |
{ |
| 97 |
|
| 98 |
#define END_INIT_SIMPLE_FUZZER() \ |
| 99 |
} \ |
| 100 |
catch (const std::exception &e) \ |
| 101 |
{ \ |
| 102 |
fprintf(stderr, "Exception: %s\n", e.what()); \ |
| 103 |
return 1; \ |
| 104 |
} \ |
| 105 |
return 0; \ |
| 106 |
} |
| 107 |
|
| 108 |
#define BEGIN_SIMPLE_FUZZER() \ |
| 109 |
virtual int run(const std::string &filename) \ |
| 110 |
{ \ |
| 111 |
try \ |
| 112 |
{ \ |
| 113 |
std::string s; \ |
| 114 |
if (!epee::file_io_utils::load_file_to_string(filename, s)) \ |
| 115 |
{ \ |
| 116 |
std::cout << "Error: failed to load file " << filename << std::endl; \ |
| 117 |
return 1; \ |
| 118 |
} \ |
| 119 |
const uint8_t *buf = (const uint8_t*)s.data(); \ |
| 120 |
const size_t len = s.size(); \ |
| 121 |
{ |
| 122 |
|
| 123 |
#define END_SIMPLE_FUZZER() \ |
| 124 |
} \ |
| 125 |
} \ |
| 126 |
catch (const std::exception &e) \ |
| 127 |
{ \ |
| 128 |
fprintf(stderr, "Exception: %s\n", e.what()); \ |
| 129 |
delete el::base::elStorage; \ |
| 130 |
el::base::elStorage = NULL; \ |
| 131 |
return 0; \ |
| 132 |
} \ |
| 133 |
delete el::base::elStorage; \ |
| 134 |
el::base::elStorage = NULL; \ |
| 135 |
return 0; \ |
| 136 |
} \ |
| 137 |
}; \ |
| 138 |
int main(int argc, const char **argv) \ |
| 139 |
{ \ |
| 140 |
TRY_ENTRY(); \ |
| 141 |
SimpleFuzzer fuzzer; \ |
| 142 |
return run_fuzzer(argc, argv, fuzzer); \ |
| 143 |
CATCH_ENTRY_L0("main", 1); \ |
| 144 |
} |
| 145 |
|
| 146 |
#endif |