blob: 87c70694402f30daf9157a20947eccda4a0a64bf [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);
aizatsky69a64822016-07-19 21:28:4722 // Mutation function provided by libFuzzer.
23 size_t LLVMFuzzerMutate(uint8_t *Data, size_t Size, size_t MaxSize);
aizatskye4599ba52016-01-29 23:09:3324}
aizatskyc562c132016-01-20 00:00:1425
reillyg21fe44502016-05-20 22:28:5026std::vector<uint8_t> readFile(std::string path) {
aizatskyc562c132016-01-20 00:00:1427 std::ifstream in(path);
reillyg21fe44502016-05-20 22:28:5028 return std::vector<uint8_t>((std::istreambuf_iterator<char>(in)),
aizatskyc562c132016-01-20 00:00:1429 std::istreambuf_iterator<char>());
30}
31
aizatsky69a64822016-07-19 21:28:4732size_t LLVMFuzzerMutate(uint8_t *Data, size_t Size, size_t MaxSize) {
33 return 0;
34}
35
aizatskyc562c132016-01-20 00:00:1436int main(int argc, char **argv) {
37 if (argc == 1) {
eroman4e40c072016-08-10 19:41:1638 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;
aizatskyc562c132016-01-20 00:00:1448 exit(1);
49 }
50
aizatskye4599ba52016-01-29 23:09:3351 if (LLVMFuzzerInitialize)
52 LLVMFuzzerInitialize(&argc, &argv);
53
aizatskyc562c132016-01-20 00:00:1454 for (int i = 1; i < argc; ++i) {
55 std::cout << argv[i] << std::endl;
56 auto v = readFile(argv[i]);
reillyg21fe44502016-05-20 22:28:5057 LLVMFuzzerTestOneInput(v.data(), v.size());
aizatskyc562c132016-01-20 00:00:1458 }
59}