1 //===-- FuzzerCLI.cpp -----------------------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/FuzzMutate/FuzzerCLI.h"
11 #include "llvm/ADT/Triple.h"
12 #include "llvm/Bitcode/BitcodeReader.h"
13 #include "llvm/Bitcode/BitcodeWriter.h"
14 #include "llvm/IR/LLVMContext.h"
15 #include "llvm/Support/CommandLine.h"
16 #include "llvm/Support/Compiler.h"
17 #include "llvm/Support/Error.h"
18 #include "llvm/Support/MemoryBuffer.h"
19 #include "llvm/Support/SourceMgr.h"
20 #include "llvm/Support/raw_ostream.h"
21 #include "llvm/IR/Verifier.h"
25 void llvm::parseFuzzerCLOpts(int ArgC, char *ArgV[]) {
26 std::vector<const char *> CLArgs;
27 CLArgs.push_back(ArgV[0]);
31 if (StringRef(ArgV[I++]).equals("-ignore_remaining_args=1"))
34 CLArgs.push_back(ArgV[I++]);
36 cl::ParseCommandLineOptions(CLArgs.size(), CLArgs.data());
39 void llvm::handleExecNameEncodedBEOpts(StringRef ExecName) {
40 std::vector<std::string> Args{ExecName};
42 auto NameAndArgs = ExecName.split("--");
43 if (NameAndArgs.second.empty())
46 SmallVector<StringRef, 4> Opts;
47 NameAndArgs.second.split(Opts, '-');
48 for (StringRef Opt : Opts) {
49 if (Opt.equals("gisel")) {
50 Args.push_back("-global-isel");
51 // For now we default GlobalISel to -O0
52 Args.push_back("-O0");
53 } else if (Opt.startswith("O")) {
54 Args.push_back("-" + Opt.str());
55 } else if (Triple(Opt).getArch()) {
56 Args.push_back("-mtriple=" + Opt.str());
58 errs() << ExecName << ": Unknown option: " << Opt << ".\n";
62 errs() << NameAndArgs.first << ": Injected args:";
63 for (int I = 1, E = Args.size(); I < E; ++I)
64 errs() << " " << Args[I];
67 std::vector<const char *> CLArgs;
68 CLArgs.reserve(Args.size());
69 for (std::string &S : Args)
70 CLArgs.push_back(S.c_str());
72 cl::ParseCommandLineOptions(CLArgs.size(), CLArgs.data());
75 void llvm::handleExecNameEncodedOptimizerOpts(StringRef ExecName) {
76 // TODO: Refactor parts common with the 'handleExecNameEncodedBEOpts'
77 std::vector<std::string> Args{ExecName};
79 auto NameAndArgs = ExecName.split("--");
80 if (NameAndArgs.second.empty())
83 SmallVector<StringRef, 4> Opts;
84 NameAndArgs.second.split(Opts, '-');
85 for (StringRef Opt : Opts) {
86 if (Opt == "instcombine") {
87 Args.push_back("-passes=instcombine");
88 } else if (Opt == "earlycse") {
89 Args.push_back("-passes=early-cse");
90 } else if (Opt == "simplifycfg") {
91 Args.push_back("-passes=simplify-cfg");
92 } else if (Opt == "gvn") {
93 Args.push_back("-passes=gvn");
94 } else if (Opt == "sccp") {
95 Args.push_back("-passes=sccp");
97 } else if (Opt == "loop_predication") {
98 Args.push_back("-passes=loop-predication");
99 } else if (Opt == "guard_widening") {
100 Args.push_back("-passes=guard-widening");
101 } else if (Opt == "loop_rotate") {
102 Args.push_back("-passes=loop(rotate)");
103 } else if (Opt == "loop_unswitch") {
104 Args.push_back("-passes=loop(unswitch)");
105 } else if (Opt == "loop_unroll") {
106 Args.push_back("-passes=unroll");
107 } else if (Opt == "loop_vectorize") {
108 Args.push_back("-passes=loop-vectorize");
109 } else if (Opt == "licm") {
110 Args.push_back("-passes=licm");
111 } else if (Opt == "indvars") {
112 Args.push_back("-passes=indvars");
113 } else if (Opt == "strength_reduce") {
114 Args.push_back("-passes=strength-reduce");
115 } else if (Opt == "irce") {
116 Args.push_back("-passes=irce");
118 } else if (Triple(Opt).getArch()) {
119 Args.push_back("-mtriple=" + Opt.str());
121 errs() << ExecName << ": Unknown option: " << Opt << ".\n";
126 errs() << NameAndArgs.first << ": Injected args:";
127 for (int I = 1, E = Args.size(); I < E; ++I)
128 errs() << " " << Args[I];
131 std::vector<const char *> CLArgs;
132 CLArgs.reserve(Args.size());
133 for (std::string &S : Args)
134 CLArgs.push_back(S.c_str());
136 cl::ParseCommandLineOptions(CLArgs.size(), CLArgs.data());
139 int llvm::runFuzzerOnInputs(int ArgC, char *ArgV[], FuzzerTestFun TestOne,
140 FuzzerInitFun Init) {
141 errs() << "*** This tool was not linked to libFuzzer.\n"
142 << "*** No fuzzing will be performed.\n";
143 if (int RC = Init(&ArgC, &ArgV)) {
144 errs() << "Initialization failed\n";
148 for (int I = 1; I < ArgC; ++I) {
149 StringRef Arg(ArgV[I]);
150 if (Arg.startswith("-")) {
151 if (Arg.equals("-ignore_remaining_args=1"))
156 auto BufOrErr = MemoryBuffer::getFile(Arg, /*FileSize-*/ -1,
157 /*RequiresNullTerminator=*/false);
158 if (std::error_code EC = BufOrErr.getError()) {
159 errs() << "Error reading file: " << Arg << ": " << EC.message() << "\n";
162 std::unique_ptr<MemoryBuffer> Buf = std::move(BufOrErr.get());
163 errs() << "Running: " << Arg << " (" << Buf->getBufferSize() << " bytes)\n";
164 TestOne(reinterpret_cast<const uint8_t *>(Buf->getBufferStart()),
165 Buf->getBufferSize());
170 std::unique_ptr<Module> llvm::parseModule(
171 const uint8_t *Data, size_t Size, LLVMContext &Context) {
174 // We get bogus data given an empty corpus - just create a new module.
175 return llvm::make_unique<Module>("M", Context);
177 auto Buffer = MemoryBuffer::getMemBuffer(
178 StringRef(reinterpret_cast<const char *>(Data), Size), "Fuzzer input",
179 /*RequiresNullTerminator=*/false);
182 auto M = parseBitcodeFile(Buffer->getMemBufferRef(), Context);
183 if (Error E = M.takeError()) {
184 errs() << toString(std::move(E)) << "\n";
187 return std::move(M.get());
190 size_t llvm::writeModule(const Module &M, uint8_t *Dest, size_t MaxSize) {
193 raw_string_ostream OS(Buf);
194 WriteBitcodeToFile(M, OS);
196 if (Buf.size() > MaxSize)
198 memcpy(Dest, Buf.data(), Buf.size());
202 std::unique_ptr<Module> llvm::parseAndVerify(const uint8_t *Data, size_t Size,
203 LLVMContext &Context) {
204 auto M = parseModule(Data, Size, Context);
205 if (!M || verifyModule(*M, &errs()))