1 // Copyright 2018 The Dawn Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include <cstdint>
16 #include <cstdlib>
17 #include <iostream>
18 #include <vector>
19
20 extern "C" int LLVMFuzzerInitialize(int* argc, char*** argv);
21 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size);
22
main(int argc,char ** argv)23 int main(int argc, char** argv) {
24 if (LLVMFuzzerInitialize(&argc, &argv)) {
25 std::cerr << "Failed to initialize fuzzer target" << std::endl;
26 return 1;
27 }
28
29 if (argc != 2) {
30 std::cout << "Usage: <standalone reproducer> [options] FILE" << std::endl;
31 return 1;
32 }
33
34 std::cout << "WARNING: this is just a best-effort reproducer for fuzzer issues in standalone "
35 << "Dawn builds. For the real fuzzer, please build inside Chromium." << std::endl;
36
37 const char* filename = argv[1];
38 std::cout << "Reproducing using file: " << filename << std::endl;
39
40 std::vector<char> data;
41 {
42 FILE* file = fopen(filename, "rb");
43 if (!file) {
44 std::cerr << "Failed to open " << filename << std::endl;
45 return 1;
46 }
47
48 fseek(file, 0, SEEK_END);
49 long tellFileSize = ftell(file);
50 if (tellFileSize <= 0) {
51 std::cerr << "Input file of incorrect size: " << filename << std::endl;
52 return 1;
53 }
54 fseek(file, 0, SEEK_SET);
55
56 size_t fileSize = static_cast<size_t>(tellFileSize);
57 data.resize(fileSize);
58
59 size_t bytesRead = fread(data.data(), sizeof(char), fileSize, file);
60 fclose(file);
61 if (bytesRead != fileSize) {
62 std::cerr << "Failed to read " << filename << std::endl;
63 return 1;
64 }
65 }
66
67 return LLVMFuzzerTestOneInput(reinterpret_cast<const uint8_t*>(data.data()), data.size());
68 }
69