| 1 |
// Copyright 2017 Google Inc. All Rights Reserved. |
| 2 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 |
|
| 4 |
// Example of a standalone runner for "fuzz targets". |
| 5 |
// It reads all files passed as parameters and feeds their contents |
| 6 |
// one by one into the fuzz target (LLVMFuzzerTestOneInput). |
| 7 |
// This runner does not do any fuzzing, but allows us to run the fuzz target |
| 8 |
// on the test corpus (e.g. "do_stuff_test_data") or on a single file, |
| 9 |
// e.g. the one that comes from a bug report. |
| 10 |
|
| 11 |
#include <cassert> |
| 12 |
#include <fstream> |
| 13 |
#include <iostream> |
| 14 |
#include <vector> |
| 15 |
|
| 16 |
// Forward declare the "fuzz target" interface. |
| 17 |
// We deliberately keep this inteface simple and header-free. |
| 18 |
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size); |
| 19 |
|
| 20 |
extern "C" int LLVMFuzzerInitialize(int *argc, char ***argv); |
| 21 |
|
| 22 |
int main(int argc, char **argv) |
| 23 |
{ |
| 24 |
LLVMFuzzerInitialize(&argc, &argv); |
| 25 |
|
| 26 |
for (int i = 1; i < argc; i++) { |
| 27 |
std::ifstream in(argv[i]); |
| 28 |
in.seekg(0, in.end); |
| 29 |
size_t length = in.tellg(); |
| 30 |
in.seekg(0, in.beg); |
| 31 |
std::cout << "Reading " << length << " bytes from " << argv[i] |
| 32 |
<< std::endl; |
| 33 |
// Allocate exactly length bytes so that we reliably catch |
| 34 |
// buffer overflows. |
| 35 |
std::vector<char> bytes(length); |
| 36 |
in.read(bytes.data(), bytes.size()); |
| 37 |
assert(in); |
| 38 |
LLVMFuzzerTestOneInput( |
| 39 |
reinterpret_cast<const uint8_t *>(bytes.data()), |
| 40 |
bytes.size()); |
| 41 |
std::cout << "Execution successful" << std::endl; |
| 42 |
} |
| 43 |
return 0; |
| 44 |
} |
| 45 |
// no-check-code since this is from a third party |