]>
Commit | Line | Data |
---|---|---|
af6f3d53 RG |
1 | |
2 | #include <fstream> | |
3 | #include <iostream> | |
badc98ae | 4 | #include <stdexcept> |
af6f3d53 RG |
5 | #include <sys/stat.h> |
6 | #include <vector> | |
7 | ||
8 | extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size); | |
9 | extern "C" __attribute__((weak)) int LLVMFuzzerInitialize(int* argc, char*** argv); | |
10 | ||
11 | int main(int argc, char** argv) | |
12 | { | |
13 | std::cerr<<"StandaloneFuzzTargetMain: running "<<(argc-1)<<" inputs"<<std::endl; | |
14 | ||
15 | if (LLVMFuzzerInitialize) { | |
16 | LLVMFuzzerInitialize(&argc, &argv); | |
17 | } | |
18 | ||
19 | for (int i = 1; i < argc; i++) { | |
20 | ||
21 | struct stat st; | |
22 | if (stat(argv[i], &st) || !S_ISREG(st.st_mode)) { | |
23 | std::cerr<<"Skipping non-regular file: "<<std::string(argv[i])<<std::endl; | |
24 | continue; | |
25 | } | |
26 | ||
27 | std::cerr<<"Running: "<<std::string(argv[i])<<std::endl; | |
28 | ||
29 | std::ifstream file(argv[i], std::ios::binary); | |
30 | file.seekg(0, std::ios::end); | |
31 | size_t fileSize = file.tellg(); | |
32 | file.seekg(0, std::ios::beg); | |
33 | ||
34 | std::vector<char> buffer; | |
35 | buffer.resize(fileSize); | |
36 | ||
37 | file.read(reinterpret_cast<char*>(buffer.data()), fileSize); | |
38 | ||
39 | if (file.fail()) { | |
40 | file.close(); | |
41 | throw std::runtime_error("Error reading fuzzing input from file '" + std::string(argv[i]) + '"'); | |
42 | } | |
43 | ||
44 | file.close(); | |
45 | ||
46 | LLVMFuzzerTestOneInput(reinterpret_cast<const uint8_t*>(buffer.data()), fileSize); | |
47 | ||
48 | std::cerr<<"Done: '"<<std::string(argv[i])<<"': ("<<fileSize<<" bytes)"<<std::endl; | |
49 | } | |
50 | ||
51 | return 0; | |
52 | } |