LLVM  15.0.0git
FuzzerCLI.cpp
Go to the documentation of this file.
1 //===-- FuzzerCLI.cpp -----------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
10 #include "llvm/ADT/StringRef.h"
11 #include "llvm/ADT/Triple.h"
15 
16 using namespace llvm;
17 
18 void llvm::parseFuzzerCLOpts(int ArgC, char *ArgV[]) {
19  std::vector<const char *> CLArgs;
20  CLArgs.push_back(ArgV[0]);
21 
22  int I = 1;
23  while (I < ArgC)
24  if (StringRef(ArgV[I++]).equals("-ignore_remaining_args=1"))
25  break;
26  while (I < ArgC)
27  CLArgs.push_back(ArgV[I++]);
28 
29  cl::ParseCommandLineOptions(CLArgs.size(), CLArgs.data());
30 }
31 
33  std::vector<std::string> Args{std::string(ExecName)};
34 
35  auto NameAndArgs = ExecName.split("--");
36  if (NameAndArgs.second.empty())
37  return;
38 
40  NameAndArgs.second.split(Opts, '-');
41  for (StringRef Opt : Opts) {
42  if (Opt.equals("gisel")) {
43  Args.push_back("-global-isel");
44  // For now we default GlobalISel to -O0
45  Args.push_back("-O0");
46  } else if (Opt.startswith("O")) {
47  Args.push_back("-" + Opt.str());
48  } else if (Triple(Opt).getArch()) {
49  Args.push_back("-mtriple=" + Opt.str());
50  } else {
51  errs() << ExecName << ": Unknown option: " << Opt << ".\n";
52  exit(1);
53  }
54  }
55  errs() << NameAndArgs.first << ": Injected args:";
56  for (int I = 1, E = Args.size(); I < E; ++I)
57  errs() << " " << Args[I];
58  errs() << "\n";
59 
60  std::vector<const char *> CLArgs;
61  CLArgs.reserve(Args.size());
62  for (std::string &S : Args)
63  CLArgs.push_back(S.c_str());
64 
65  cl::ParseCommandLineOptions(CLArgs.size(), CLArgs.data());
66 }
67 
69  // TODO: Refactor parts common with the 'handleExecNameEncodedBEOpts'
70  std::vector<std::string> Args{std::string(ExecName)};
71 
72  auto NameAndArgs = ExecName.split("--");
73  if (NameAndArgs.second.empty())
74  return;
75 
77  NameAndArgs.second.split(Opts, '-');
78  for (StringRef Opt : Opts) {
79  if (Opt == "instcombine") {
80  Args.push_back("-passes=instcombine");
81  } else if (Opt == "earlycse") {
82  Args.push_back("-passes=early-cse");
83  } else if (Opt == "simplifycfg") {
84  Args.push_back("-passes=simplifycfg");
85  } else if (Opt == "gvn") {
86  Args.push_back("-passes=gvn");
87  } else if (Opt == "sccp") {
88  Args.push_back("-passes=sccp");
89 
90  } else if (Opt == "loop_predication") {
91  Args.push_back("-passes=loop-predication");
92  } else if (Opt == "guard_widening") {
93  Args.push_back("-passes=guard-widening");
94  } else if (Opt == "loop_rotate") {
95  Args.push_back("-passes=loop(rotate)");
96  } else if (Opt == "loop_unswitch") {
97  Args.push_back("-passes=loop(simple-loop-unswitch)");
98  } else if (Opt == "loop_unroll") {
99  Args.push_back("-passes=unroll");
100  } else if (Opt == "loop_vectorize") {
101  Args.push_back("-passes=loop-vectorize");
102  } else if (Opt == "licm") {
103  Args.push_back("-passes=licm");
104  } else if (Opt == "indvars") {
105  Args.push_back("-passes=indvars");
106  } else if (Opt == "strength_reduce") {
107  Args.push_back("-passes=loop-reduce");
108  } else if (Opt == "irce") {
109  Args.push_back("-passes=irce");
110 
111  } else if (Triple(Opt).getArch()) {
112  Args.push_back("-mtriple=" + Opt.str());
113  } else {
114  errs() << ExecName << ": Unknown option: " << Opt << ".\n";
115  exit(1);
116  }
117  }
118 
119  errs() << NameAndArgs.first << ": Injected args:";
120  for (int I = 1, E = Args.size(); I < E; ++I)
121  errs() << " " << Args[I];
122  errs() << "\n";
123 
124  std::vector<const char *> CLArgs;
125  CLArgs.reserve(Args.size());
126  for (std::string &S : Args)
127  CLArgs.push_back(S.c_str());
128 
129  cl::ParseCommandLineOptions(CLArgs.size(), CLArgs.data());
130 }
131 
132 int llvm::runFuzzerOnInputs(int ArgC, char *ArgV[], FuzzerTestFun TestOne,
134  errs() << "*** This tool was not linked to libFuzzer.\n"
135  << "*** No fuzzing will be performed.\n";
136  if (int RC = Init(&ArgC, &ArgV)) {
137  errs() << "Initialization failed\n";
138  return RC;
139  }
140 
141  for (int I = 1; I < ArgC; ++I) {
142  StringRef Arg(ArgV[I]);
143  if (Arg.startswith("-")) {
144  if (Arg.equals("-ignore_remaining_args=1"))
145  break;
146  continue;
147  }
148 
149  auto BufOrErr = MemoryBuffer::getFile(Arg, /*IsText=*/false,
150  /*RequiresNullTerminator=*/false);
151  if (std::error_code EC = BufOrErr.getError()) {
152  errs() << "Error reading file: " << Arg << ": " << EC.message() << "\n";
153  return 1;
154  }
155  std::unique_ptr<MemoryBuffer> Buf = std::move(BufOrErr.get());
156  errs() << "Running: " << Arg << " (" << Buf->getBufferSize() << " bytes)\n";
157  TestOne(reinterpret_cast<const uint8_t *>(Buf->getBufferStart()),
158  Buf->getBufferSize());
159  }
160  return 0;
161 }
MemoryBuffer.h
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:17
StringRef.h
llvm::SmallVector
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1185
llvm::Triple
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:44
llvm::errs
raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
Definition: raw_ostream.cpp:893
llvm::handleExecNameEncodedOptimizerOpts
void handleExecNameEncodedOptimizerOpts(StringRef ExecName)
Handle optimizer options which are encoded in the executable name.
Definition: FuzzerCLI.cpp:68
Arg
amdgpu Simplify well known AMD library false FunctionCallee Value * Arg
Definition: AMDGPULibCalls.cpp:186
CommandLine.h
llvm::MemoryBuffer::getFile
static ErrorOr< std::unique_ptr< MemoryBuffer > > getFile(const Twine &Filename, bool IsText=false, bool RequiresNullTerminator=true, bool IsVolatile=false)
Open the specified file as a MemoryBuffer, returning a new MemoryBuffer if successful,...
Definition: MemoryBuffer.cpp:239
E
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
llvm::StringRef::split
LLVM_NODISCARD std::pair< StringRef, StringRef > split(char Separator) const
Split into two substrings around the first occurrence of a separator character.
Definition: StringRef.h:749
llvm::cl::ParseCommandLineOptions
bool ParseCommandLineOptions(int argc, const char *const *argv, StringRef Overview="", raw_ostream *Errs=nullptr, const char *EnvVar=nullptr, bool LongOptionsUseDoubleDash=false)
Definition: CommandLine.cpp:1377
llvm::FuzzerInitFun
int(*)(int *argc, char ***argv) FuzzerInitFun
Definition: FuzzerCLI.h:46
move
compiles ldr LCPI1_0 ldr ldr mov lsr tst moveq r1 ldr LCPI1_1 and r0 bx lr It would be better to do something like to fold the shift into the conditional move
Definition: README.txt:546
I
#define I(x, y, z)
Definition: MD5.cpp:58
Triple.h
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
FuzzerCLI.h
llvm::handleExecNameEncodedBEOpts
void handleExecNameEncodedBEOpts(StringRef ExecName)
Handle backend options that are encoded in the executable name.
Definition: FuzzerCLI.cpp:32
S
add sub stmia L5 ldr r0 bl L_printf $stub Instead of a and a wouldn t it be better to do three moves *Return an aggregate type is even return S
Definition: README.txt:210
llvm::Init
Definition: Record.h:281
llvm::parseFuzzerCLOpts
void parseFuzzerCLOpts(int ArgC, char *ArgV[])
Parse cl::opts from a fuzz target commandline.
Definition: FuzzerCLI.cpp:18
exit
declare void exit(i32) noreturn nounwind This compiles into
Definition: README.txt:1072
llvm::FuzzerTestFun
int(*)(const uint8_t *Data, size_t Size) FuzzerTestFun
Definition: FuzzerCLI.h:45
llvm::runFuzzerOnInputs
int runFuzzerOnInputs(int ArgC, char *ArgV[], FuzzerTestFun TestOne, FuzzerInitFun Init=[](int *, char ***) { return 0;})
Runs a fuzz target on the inputs specified on the command line.
Definition: FuzzerCLI.cpp:132
llvm::AMDGPU::HSAMD::Kernel::Key::Args
constexpr char Args[]
Key for Kernel::Metadata::mArgs.
Definition: AMDGPUMetadata.h:394
raw_ostream.h