aizatsky | c562c13 | 2016-01-20 00:00:14 | [diff] [blame] | 1 | // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | // A simple unit-test style driver for libfuzzer tests. |
| 6 | // Usage: <fuzzer_test> <file>... |
| 7 | |
| 8 | #include <fstream> |
| 9 | #include <iostream> |
| 10 | #include <iterator> |
| 11 | #include <vector> |
| 12 | |
aizatsky | e4599ba5 | 2016-01-29 23:09:33 | [diff] [blame^] | 13 | // Libfuzzer API. |
| 14 | extern "C" { |
| 15 | // User function. |
| 16 | int LLVMFuzzerTestOneInput(const unsigned char* data, size_t size); |
| 17 | // Initialization function. |
| 18 | __attribute__((weak)) int LLVMFuzzerInitialize(int *argc, char ***argv); |
| 19 | } |
aizatsky | c562c13 | 2016-01-20 00:00:14 | [diff] [blame] | 20 | |
| 21 | std::vector<char> readFile(std::string path) { |
| 22 | std::ifstream in(path); |
| 23 | return std::vector<char>((std::istreambuf_iterator<char>(in)), |
| 24 | std::istreambuf_iterator<char>()); |
| 25 | } |
| 26 | |
| 27 | int main(int argc, char **argv) { |
| 28 | if (argc == 1) { |
| 29 | std::cerr << "Usage: " << argv[0] << " <file>..." << std::endl; |
| 30 | exit(1); |
| 31 | } |
| 32 | |
aizatsky | e4599ba5 | 2016-01-29 23:09:33 | [diff] [blame^] | 33 | if (LLVMFuzzerInitialize) |
| 34 | LLVMFuzzerInitialize(&argc, &argv); |
| 35 | |
aizatsky | c562c13 | 2016-01-20 00:00:14 | [diff] [blame] | 36 | for (int i = 1; i < argc; ++i) { |
| 37 | std::cout << argv[i] << std::endl; |
| 38 | auto v = readFile(argv[i]); |
| 39 | LLVMFuzzerTestOneInput((const unsigned char *)v.data(), v.size()); |
| 40 | } |
| 41 | } |