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 | |
reillyg | 21fe4450 | 2016-05-20 22:28:50 | [diff] [blame^] | 8 | #include <stddef.h> |
| 9 | #include <stdint.h> |
| 10 | |
aizatsky | c562c13 | 2016-01-20 00:00:14 | [diff] [blame] | 11 | #include <fstream> |
| 12 | #include <iostream> |
| 13 | #include <iterator> |
| 14 | #include <vector> |
| 15 | |
aizatsky | e4599ba5 | 2016-01-29 23:09:33 | [diff] [blame] | 16 | // Libfuzzer API. |
| 17 | extern "C" { |
| 18 | // User function. |
reillyg | 21fe4450 | 2016-05-20 22:28:50 | [diff] [blame^] | 19 | int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size); |
aizatsky | e4599ba5 | 2016-01-29 23:09:33 | [diff] [blame] | 20 | // Initialization function. |
thakis | 76b9e046 | 2016-02-12 12:11:37 | [diff] [blame] | 21 | __attribute__((weak)) int LLVMFuzzerInitialize(int *argc, char ***argv); |
aizatsky | e4599ba5 | 2016-01-29 23:09:33 | [diff] [blame] | 22 | } |
aizatsky | c562c13 | 2016-01-20 00:00:14 | [diff] [blame] | 23 | |
reillyg | 21fe4450 | 2016-05-20 22:28:50 | [diff] [blame^] | 24 | std::vector<uint8_t> readFile(std::string path) { |
aizatsky | c562c13 | 2016-01-20 00:00:14 | [diff] [blame] | 25 | std::ifstream in(path); |
reillyg | 21fe4450 | 2016-05-20 22:28:50 | [diff] [blame^] | 26 | return std::vector<uint8_t>((std::istreambuf_iterator<char>(in)), |
aizatsky | c562c13 | 2016-01-20 00:00:14 | [diff] [blame] | 27 | std::istreambuf_iterator<char>()); |
| 28 | } |
| 29 | |
| 30 | int main(int argc, char **argv) { |
| 31 | if (argc == 1) { |
| 32 | std::cerr << "Usage: " << argv[0] << " <file>..." << std::endl; |
| 33 | exit(1); |
| 34 | } |
| 35 | |
aizatsky | e4599ba5 | 2016-01-29 23:09:33 | [diff] [blame] | 36 | if (LLVMFuzzerInitialize) |
| 37 | LLVMFuzzerInitialize(&argc, &argv); |
| 38 | |
aizatsky | c562c13 | 2016-01-20 00:00:14 | [diff] [blame] | 39 | for (int i = 1; i < argc; ++i) { |
| 40 | std::cout << argv[i] << std::endl; |
| 41 | auto v = readFile(argv[i]); |
reillyg | 21fe4450 | 2016-05-20 22:28:50 | [diff] [blame^] | 42 | LLVMFuzzerTestOneInput(v.data(), v.size()); |
aizatsky | c562c13 | 2016-01-20 00:00:14 | [diff] [blame] | 43 | } |
| 44 | } |