1 //===-- FuzzerCLI.cpp -----------------------------------------------------===// 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 #include "llvm/FuzzMutate/FuzzerCLI.h" 11 #include "llvm/ADT/StringRef.h" 12 #include "llvm/Support/CommandLine.h" 13 #include "llvm/Support/Compiler.h" 14 #include "llvm/Support/Error.h" 15 #include "llvm/Support/MemoryBuffer.h" 16 #include "llvm/Support/raw_ostream.h" 17 18 using namespace llvm; 19 20 void llvm::parseFuzzerCLOpts(int ArgC, char *ArgV[]) { 21 std::vector<const char *> CLArgs; 22 CLArgs.push_back(ArgV[0]); 23 24 int I = 1; 25 while (I < ArgC) 26 if (StringRef(ArgV[I++]).equals("-ignore_remaining_args=1")) 27 break; 28 while (I < ArgC) 29 CLArgs.push_back(ArgV[I++]); 30 31 cl::ParseCommandLineOptions(CLArgs.size(), CLArgs.data()); 32 } 33 34 int llvm::runFuzzerOnInputs(int ArgC, char *ArgV[], FuzzerTestFun TestOne, 35 FuzzerInitFun Init) { 36 errs() << "*** This tool was not linked to libFuzzer.\n" 37 << "*** No fuzzing will be performed.\n"; 38 if (int RC = Init(&ArgC, &ArgV)) { 39 errs() << "Initialization failed\n"; 40 return RC; 41 } 42 43 for (int I = 1; I < ArgC; ++I) { 44 StringRef Arg(ArgV[I]); 45 if (Arg.startswith("-")) { 46 if (Arg.equals("-ignore_remaining_args=1")) 47 break; 48 continue; 49 } 50 51 auto BufOrErr = MemoryBuffer::getFile(Arg, /*FileSize-*/ -1, 52 /*RequiresNullTerminator=*/false); 53 if (std::error_code EC = BufOrErr.getError()) { 54 errs() << "Error reading file: " << Arg << ": " << EC.message() << "\n"; 55 return 1; 56 } 57 std::unique_ptr<MemoryBuffer> Buf = std::move(BufOrErr.get()); 58 errs() << "Running: " << Arg << " (" << Buf->getBufferSize() << " bytes)\n"; 59 TestOne(reinterpret_cast<const uint8_t *>(Buf->getBufferStart()), 60 Buf->getBufferSize()); 61 } 62 return 0; 63 } 64