|
2 | 2 |
|
3 | 3 | #include <assert.h> |
4 | 4 | #include <ccan/isaac/isaac64.h> |
| 5 | +#include <ccan/short_types/short_types.h> |
| 6 | +#include <ccan/tal/grab_file/grab_file.h> |
| 7 | +#include <ccan/tal/path/path.h> |
| 8 | +#include <ccan/tal/tal.h> |
5 | 9 | #include <common/pseudorand.h> |
| 10 | +#include <common/setup.h> |
| 11 | +#include <dirent.h> |
6 | 12 | #include <stdlib.h> |
7 | 13 | #include <string.h> |
| 14 | +#include <sys/types.h> |
8 | 15 | #include <tests/fuzz/libfuzz.h> |
| 16 | +#include <unistd.h> |
9 | 17 |
|
10 | 18 | int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size); |
11 | 19 | int LLVMFuzzerInitialize(int *argc, char ***argv); |
@@ -118,3 +126,38 @@ size_t cross_over(const u8 *in1, size_t in1_size, const u8 *in2, |
118 | 126 | max_out_size); |
119 | 127 | return overwrite_part(in1, in1_size, in2, in2_size, out, max_out_size); |
120 | 128 | } |
| 129 | + |
| 130 | +/* In non-fuzzing builds, these become unit tests which just run the corpora: |
| 131 | + * this is also good for attaching a debugger to! */ |
| 132 | +#ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION |
| 133 | +int main(int argc, char *argv[]) |
| 134 | +{ |
| 135 | + DIR *d; |
| 136 | + struct dirent *di; |
| 137 | + |
| 138 | + common_setup(argv[0]); |
| 139 | + assert(chdir("tests/fuzz/corpora") == 0); |
| 140 | + assert(chdir(path_basename(tmpctx, argv[0])) == 0); |
| 141 | + |
| 142 | + /* FIXME: Support explicit path args? */ |
| 143 | + init(&argc, &argv); |
| 144 | + d = opendir("."); |
| 145 | + while ((di = readdir(d)) != NULL) { |
| 146 | + u8 *contents; |
| 147 | + if (streq(di->d_name, ".") || streq(di->d_name, "..")) |
| 148 | + continue; |
| 149 | + contents = grab_file(tmpctx, di->d_name); |
| 150 | + assert(contents); |
| 151 | + run(contents, tal_bytelen(contents)-1); |
| 152 | + } |
| 153 | + closedir(d); |
| 154 | + common_shutdown(); |
| 155 | +} |
| 156 | + |
| 157 | +/* We never call any functions which might call these */ |
| 158 | +size_t LLVMFuzzerMutate(uint8_t *data, size_t size, size_t max_size); |
| 159 | +size_t LLVMFuzzerMutate(uint8_t *data, size_t size, size_t max_size) |
| 160 | +{ |
| 161 | + abort(); |
| 162 | +} |
| 163 | +#endif /* !FUZZING */ |
0 commit comments