aboutsummaryrefslogtreecommitdiff
path: root/tests/fuzz/fuzzer.h
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--tests/fuzz/fuzzer.h100
1 files changed, 100 insertions, 0 deletions
diff --git a/tests/fuzz/fuzzer.h b/tests/fuzz/fuzzer.h
index 5cbd1abc2..2d0a29dfc 100644
--- a/tests/fuzz/fuzzer.h
+++ b/tests/fuzz/fuzzer.h
@@ -27,6 +27,52 @@
// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <string>
+#include "file_io_utils.h"
+
+#ifdef OSSFUZZ
+
+#define BEGIN_INIT_SIMPLE_FUZZER() \
+ static int init() \
+ { \
+ try \
+ {
+
+#define END_INIT_SIMPLE_FUZZER() \
+ } \
+ catch (const std::exception &e) \
+ { \
+ fprintf(stderr, "Exception: %s\n", e.what()); \
+ return 1; \
+ } \
+ return 0; \
+ }
+
+#define BEGIN_SIMPLE_FUZZER() \
+extern "C" { \
+ int LLVMFuzzerTestOneInput(const uint8_t *buf, size_t len) \
+ { \
+ try \
+ { \
+ static bool first = true; \
+ if (first) \
+ { \
+ if (!init()) \
+ return 1; \
+ first = false; \
+ } \
+
+#define END_SIMPLE_FUZZER() \
+ } \
+ catch (const std::exception &e) \
+ { \
+ fprintf(stderr, "Exception: %s\n", e.what()); \
+ return 1; \
+ } \
+ return 0; \
+ } \
+}
+
+#else
class Fuzzer
{
@@ -36,3 +82,57 @@ public:
};
int run_fuzzer(int argc, const char **argv, Fuzzer &fuzzer);
+
+#define BEGIN_INIT_SIMPLE_FUZZER() \
+ class SimpleFuzzer: public Fuzzer \
+ { \
+ virtual int init() \
+ { \
+ try \
+ {
+
+#define END_INIT_SIMPLE_FUZZER() \
+ } \
+ catch (const std::exception &e) \
+ { \
+ fprintf(stderr, "Exception: %s\n", e.what()); \
+ return 1; \
+ } \
+ return 0; \
+ }
+
+#define BEGIN_SIMPLE_FUZZER() \
+ virtual int run(const std::string &filename) \
+ { \
+ try \
+ { \
+ std::string s; \
+ if (!epee::file_io_utils::load_file_to_string(filename, s)) \
+ { \
+ std::cout << "Error: failed to load file " << filename << std::endl; \
+ return 1; \
+ } \
+ const uint8_t *buf = (const uint8_t*)s.data(); \
+ const size_t len = s.size(); \
+ {
+
+#define END_SIMPLE_FUZZER() \
+ } \
+ } \
+ catch (const std::exception &e) \
+ { \
+ fprintf(stderr, "Exception: %s\n", e.what()); \
+ return 1; \
+ } \
+ return 0; \
+ } \
+ }; \
+ int main(int argc, const char **argv) \
+ { \
+ TRY_ENTRY(); \
+ SimpleFuzzer fuzzer; \
+ return run_fuzzer(argc, argv, fuzzer); \
+ CATCH_ENTRY_L0("main", 1); \
+ }
+
+#endif