blob: 89c1c81cdbe47496fdba4b0606ae312727b10280 [file] [log] [blame]
Justin Bognerc3b67fb2017-09-01 17:02:22 +00001//===--- DummyFuzzerMain.cpp - Entry point to sanity check the fuzzer -----===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// Implementation of main so we can build and test without linking libFuzzer.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/ADT/StringRef.h"
15#include "llvm/Support/Compiler.h"
16#include "llvm/Support/Error.h"
17#include "llvm/Support/MemoryBuffer.h"
18#include "llvm/Support/raw_ostream.h"
19
20using namespace llvm;
21
22extern "C" int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size);
Justin Bognerb79a4ef2017-09-01 19:37:49 +000023extern "C" int LLVMFuzzerInitialize(int *argc, char ***argv);
Justin Bognerc3b67fb2017-09-01 17:02:22 +000024
25int main(int argc, char *argv[]) {
26 errs() << "*** This tool was not linked to libFuzzer.\n"
27 << "*** No fuzzing will be performed.\n";
28 if (int RC = LLVMFuzzerInitialize(&argc, &argv)) {
29 errs() << "Initialization failed\n";
30 return RC;
31 }
32
33 for (int I = 1; I < argc; ++I) {
34 StringRef Arg(argv[I]);
35 if (Arg.startswith("-")) {
36 if (Arg.equals("-ignore_remaining_args=1"))
37 break;
38 continue;
39 }
40
41 auto BufOrErr = MemoryBuffer::getFile(Arg, /*FileSize-*/ -1,
42 /*RequiresNullTerminator=*/false);
43 if (std::error_code EC = BufOrErr.getError()) {
44 errs() << "Error reading file: " << Arg << ": " << EC.message() << "\n";
45 return 1;
46 }
47 std::unique_ptr<MemoryBuffer> Buf = std::move(BufOrErr.get());
48 errs() << "Running: " << Arg << " (" << Buf->getBufferSize() << " bytes)\n";
49 LLVMFuzzerTestOneInput(
50 reinterpret_cast<const uint8_t *>(Buf->getBufferStart()),
51 Buf->getBufferSize());
52 }
53}