blob: f2d9fc33e8d2ff11fee181a360de3e42900b0c71 [file] [log] [blame]
aizatskyc562c132016-01-20 00:00:141// 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
reillyg21fe44502016-05-20 22:28:508#include <stddef.h>
9#include <stdint.h>
10
aizatskyc562c132016-01-20 00:00:1411#include <fstream>
12#include <iostream>
13#include <iterator>
14#include <vector>
15
aizatskye4599ba52016-01-29 23:09:3316// Libfuzzer API.
17extern "C" {
18 // User function.
reillyg21fe44502016-05-20 22:28:5019 int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size);
aizatskye4599ba52016-01-29 23:09:3320 // Initialization function.
thakis76b9e0462016-02-12 12:11:3721 __attribute__((weak)) int LLVMFuzzerInitialize(int *argc, char ***argv);
aizatskye4599ba52016-01-29 23:09:3322}
aizatskyc562c132016-01-20 00:00:1423
reillyg21fe44502016-05-20 22:28:5024std::vector<uint8_t> readFile(std::string path) {
aizatskyc562c132016-01-20 00:00:1425 std::ifstream in(path);
reillyg21fe44502016-05-20 22:28:5026 return std::vector<uint8_t>((std::istreambuf_iterator<char>(in)),
aizatskyc562c132016-01-20 00:00:1427 std::istreambuf_iterator<char>());
28}
29
30int main(int argc, char **argv) {
31 if (argc == 1) {
32 std::cerr << "Usage: " << argv[0] << " <file>..." << std::endl;
33 exit(1);
34 }
35
aizatskye4599ba52016-01-29 23:09:3336 if (LLVMFuzzerInitialize)
37 LLVMFuzzerInitialize(&argc, &argv);
38
aizatskyc562c132016-01-20 00:00:1439 for (int i = 1; i < argc; ++i) {
40 std::cout << argv[i] << std::endl;
41 auto v = readFile(argv[i]);
reillyg21fe44502016-05-20 22:28:5042 LLVMFuzzerTestOneInput(v.data(), v.size());
aizatskyc562c132016-01-20 00:00:1443 }
44}