Monero
fuzzer.h
Go to the documentation of this file.
1 // Copyright (c) 2017-2022, The Monero Project
2 //
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without modification, are
6 // permitted provided that the following conditions are met:
7 //
8 // 1. Redistributions of source code must retain the above copyright notice, this list of
9 // conditions and the following disclaimer.
10 //
11 // 2. Redistributions in binary form must reproduce the above copyright notice, this list
12 // of conditions and the following disclaimer in the documentation and/or other
13 // materials provided with the distribution.
14 //
15 // 3. Neither the name of the copyright holder nor the names of its contributors may be
16 // used to endorse or promote products derived from this software without specific
17 // prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
20 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
22 // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26 // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
27 // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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
virtual int init()
Definition: fuzzer.h:84
::std::string string
Definition: gtest-port.h:1097
int run_fuzzer(int argc, const char **argv, Fuzzer &fuzzer)
Definition: fuzzer.cpp:49
Definition: fuzzer.h:81
virtual int run(const std::string &filename)=0