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 | 69a6482 | 2016-07-19 21:28:47 | [diff] [blame] | 22 | // Mutation function provided by libFuzzer. |
| 23 | size_t LLVMFuzzerMutate(uint8_t *Data, size_t Size, size_t MaxSize); |
aizatsky | e4599ba5 | 2016-01-29 23:09:33 | [diff] [blame] | 24 | } |
aizatsky | c562c13 | 2016-01-20 00:00:14 | [diff] [blame] | 25 | |
reillyg | 21fe4450 | 2016-05-20 22:28:50 | [diff] [blame] | 26 | std::vector<uint8_t> readFile(std::string path) { |
aizatsky | c562c13 | 2016-01-20 00:00:14 | [diff] [blame] | 27 | std::ifstream in(path); |
reillyg | 21fe4450 | 2016-05-20 22:28:50 | [diff] [blame] | 28 | return std::vector<uint8_t>((std::istreambuf_iterator<char>(in)), |
aizatsky | c562c13 | 2016-01-20 00:00:14 | [diff] [blame] | 29 | std::istreambuf_iterator<char>()); |
| 30 | } |
| 31 | |
aizatsky | 69a6482 | 2016-07-19 21:28:47 | [diff] [blame] | 32 | size_t LLVMFuzzerMutate(uint8_t *Data, size_t Size, size_t MaxSize) { |
| 33 | return 0; |
| 34 | } |
| 35 | |
aizatsky | c562c13 | 2016-01-20 00:00:14 | [diff] [blame] | 36 | int main(int argc, char **argv) { |
| 37 | if (argc == 1) { |
eroman | 4e40c07 | 2016-08-10 19:41:16 | [diff] [blame] | 38 | std::cerr |
| 39 | << "Usage: " << argv[0] |
| 40 | << " <file>...\n" |
| 41 | "\n" |
| 42 | "Alternatively, try building this target with " |
| 43 | "use_libfuzzer=true for a better test driver. For details see:\n" |
| 44 | "\n" |
| 45 | "https://chromium.googlesource.com/chromium/src/+/master/" |
| 46 | "testing/libfuzzer/getting_started.md" |
| 47 | << std::endl; |
aizatsky | c562c13 | 2016-01-20 00:00:14 | [diff] [blame] | 48 | exit(1); |
| 49 | } |
| 50 | |
aizatsky | e4599ba5 | 2016-01-29 23:09:33 | [diff] [blame] | 51 | if (LLVMFuzzerInitialize) |
| 52 | LLVMFuzzerInitialize(&argc, &argv); |
| 53 | |
aizatsky | c562c13 | 2016-01-20 00:00:14 | [diff] [blame] | 54 | for (int i = 1; i < argc; ++i) { |
| 55 | std::cout << argv[i] << std::endl; |
| 56 | auto v = readFile(argv[i]); |
reillyg | 21fe4450 | 2016-05-20 22:28:50 | [diff] [blame] | 57 | LLVMFuzzerTestOneInput(v.data(), v.size()); |
aizatsky | c562c13 | 2016-01-20 00:00:14 | [diff] [blame] | 58 | } |
| 59 | } |