Debian Codegraph @

regressions/ck_ec/validate/fuzz_harness.h

raw
1 #ifndef FUZZ_HARNESS_H
2 #define FUZZ_HARNESS_H
3 #include <assert.h>
4 #include <ck_stddef.h>
5 #include <ck_string.h>
6 #include <stdio.h>
7 #include <unistd.h>
8
9 #if defined(USE_LIBFUZZER)
10 #define TEST(function, examples) \
11 void LLVMFuzzerInitialize(int *argcp, char ***argvp); \
12 int LLVMFuzzerTestOneInput(const void *data, size_t n); \
13 \
14 void LLVMFuzzerInitialize(int *argcp, char ***argvp) \
15 { \
16 static char size[128]; \
17 static char *argv[1024]; \
18 int argc = *argcp; \
19 \
20 assert(argc < 1023); \
21 \
22 int r = snprintf(size, sizeof(size), \
23 "-max_len=%zu", sizeof(examples[0])); \
24 assert((size_t)r < sizeof(size)); \
25 \
26 memcpy(argv, *argvp, argc * sizeof(argv[0])); \
27 argv[argc++] = size; \
28 \
29 *argcp = argc; \
30 *argvp = argv; \
31 \
32 for (size_t i = 0; \
33 i < sizeof(examples) / sizeof(examples[0]); \
34 i++) { \
35 assert(function(&examples[i]) == 0); \
36 } \
37 \
38 return; \
39 } \
40 \
41 int LLVMFuzzerTestOneInput(const void *data, size_t n) \
42 { \
43 char buf[sizeof(examples[0])]; \
44 \
45 memset(buf, 0, sizeof(buf)); \
46 if (n < sizeof(buf)) { \
47 memcpy(buf, data, n); \
48 } else { \
49 memcpy(buf, data, sizeof(buf)); \
50 } \
51 \
52 assert(function((const void *)buf) == 0); \
53 return 0; \
54 }
55 #elif defined(USE_AFL)
56 #define TEST(function, examples) \
57 int main(int argc, char **argv) \
58 { \
59 char buf[sizeof(examples[0])]; \
60 \
61 (void)argc; \
62 (void)argv; \
63 for (size_t i = 0; \
64 i < sizeof(examples) / sizeof(examples[0]); \
65 i++) { \
66 assert(function(&examples[i]) == 0); \
67 } \
68 \
69 \
70 while (__AFL_LOOP(10000)) { \
71 memset(buf, 0, sizeof(buf)); \
72 read(0, buf, sizeof(buf)); \
73 \
74 assert(function((const void *)buf) == 0); \
75 } \
76 \
77 return 0; \
78 }
79 #else
80 #define TEST(function, examples) \
81 int main(int argc, char **argv) \
82 { \
83 (void)argc; \
84 (void)argv; \
85 \
86 for (size_t i = 0; \
87 i < sizeof(examples) / sizeof(examples[0]); \
88 i++) { \
89 assert(function(&examples[i]) == 0); \
90 } \
91 \
92 return 0; \
93 }
94 #endif
95 #endif /* !FUZZ_HARNESS_H */