blob: efbf3bc292329777528e74a55e489130e3f7fd8f [file] [log] [blame]
Brian Cain50aa37b2017-02-27 06:22:17 +00001//===--- llvm-mc-fuzzer.cpp - Fuzzer for the MC layer ---------------------===//
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//===----------------------------------------------------------------------===//
11
Brian Cain50aa37b2017-02-27 06:22:17 +000012#include "llvm-c/Target.h"
13#include "llvm/MC/SubtargetFeature.h"
14#include "llvm/MC/MCAsmBackend.h"
15#include "llvm/MC/MCAsmInfo.h"
Mitch Phillips5ebf7a82017-11-06 18:56:36 +000016#include "llvm/MC/MCCodeEmitter.h"
Brian Cain50aa37b2017-02-27 06:22:17 +000017#include "llvm/MC/MCContext.h"
18#include "llvm/MC/MCInstPrinter.h"
19#include "llvm/MC/MCInstrInfo.h"
20#include "llvm/MC/MCObjectFileInfo.h"
21#include "llvm/MC/MCParser/AsmLexer.h"
22#include "llvm/MC/MCParser/MCTargetAsmParser.h"
23#include "llvm/MC/MCRegisterInfo.h"
24#include "llvm/MC/MCSectionMachO.h"
25#include "llvm/MC/MCStreamer.h"
26#include "llvm/MC/MCSubtargetInfo.h"
David Blaikieeef5c232017-11-27 19:55:16 +000027#include "llvm/MC/MCTargetOptionsCommandFlags.def"
Brian Cain50aa37b2017-02-27 06:22:17 +000028#include "llvm/Support/MemoryBuffer.h"
29#include "llvm/Support/CommandLine.h"
30#include "llvm/Support/FileUtilities.h"
31#include "llvm/Support/raw_ostream.h"
32#include "llvm/Support/SourceMgr.h"
33#include "llvm/Support/TargetSelect.h"
34#include "llvm/Support/TargetRegistry.h"
35#include "llvm/Support/ToolOutputFile.h"
36
37using namespace llvm;
38
39static cl::opt<std::string>
40 TripleName("triple", cl::desc("Target triple to assemble for, "
41 "see -version for available targets"));
42
43static cl::opt<std::string>
44 MCPU("mcpu",
45 cl::desc("Target a specific cpu type (-mcpu=help for details)"),
46 cl::value_desc("cpu-name"), cl::init(""));
47
48// This is useful for variable-length instruction sets.
49static cl::opt<unsigned> InsnLimit(
50 "insn-limit",
51 cl::desc("Limit the number of instructions to process (0 for no limit)"),
52 cl::value_desc("count"), cl::init(0));
53
54static cl::list<std::string>
55 MAttrs("mattr", cl::CommaSeparated,
56 cl::desc("Target specific attributes (-mattr=help for details)"),
57 cl::value_desc("a1,+a2,-a3,..."));
58// The feature string derived from -mattr's values.
59std::string FeaturesStr;
60
61static cl::list<std::string>
62 FuzzerArgs("fuzzer-args", cl::Positional,
63 cl::desc("Options to pass to the fuzzer"), cl::ZeroOrMore,
64 cl::PositionalEatsArgs);
65static std::vector<char *> ModifiedArgv;
66
67enum OutputFileType {
68 OFT_Null,
69 OFT_AssemblyFile,
70 OFT_ObjectFile
71};
72static cl::opt<OutputFileType>
73FileType("filetype", cl::init(OFT_AssemblyFile),
74 cl::desc("Choose an output file type:"),
75 cl::values(
76 clEnumValN(OFT_AssemblyFile, "asm",
77 "Emit an assembly ('.s') file"),
78 clEnumValN(OFT_Null, "null",
79 "Don't emit anything (for timing purposes)"),
80 clEnumValN(OFT_ObjectFile, "obj",
81 "Emit a native object ('.o') file")));
82
83
84class LLVMFuzzerInputBuffer : public MemoryBuffer
85{
86 public:
87 LLVMFuzzerInputBuffer(const uint8_t *data_, size_t size_)
Mitch Phillips5ebf7a82017-11-06 18:56:36 +000088 : Data(reinterpret_cast<const char *>(data_)),
Brian Cain50aa37b2017-02-27 06:22:17 +000089 Size(size_) {
90 init(Data, Data+Size, false);
91 }
92
93
94 virtual BufferKind getBufferKind() const {
95 return MemoryBuffer_Malloc; // it's not disk-backed so I think that's
96 // the intent ... though AFAIK it
97 // probably came from an mmap or sbrk
98 }
99
100 private:
101 const char *Data;
102 size_t Size;
103};
104
105static int AssembleInput(const char *ProgName, const Target *TheTarget,
106 SourceMgr &SrcMgr, MCContext &Ctx, MCStreamer &Str,
107 MCAsmInfo &MAI, MCSubtargetInfo &STI,
108 MCInstrInfo &MCII, MCTargetOptions &MCOptions) {
109 static const bool NoInitialTextSection = false;
110
111 std::unique_ptr<MCAsmParser> Parser(
112 createMCAsmParser(SrcMgr, Ctx, Str, MAI));
113
114 std::unique_ptr<MCTargetAsmParser> TAP(
115 TheTarget->createMCAsmParser(STI, *Parser, MCII, MCOptions));
116
117 if (!TAP) {
118 errs() << ProgName
119 << ": error: this target '" << TripleName
120 << "', does not support assembly parsing.\n";
121 abort();
122 }
123
124 Parser->setTargetParser(*TAP);
125
126 return Parser->Run(NoInitialTextSection);
127}
128
129
130int AssembleOneInput(const uint8_t *Data, size_t Size) {
131 const bool ShowInst = false;
132 const bool AsmVerbose = false;
133 const bool UseDwarfDirectory = true;
134
135 Triple TheTriple(Triple::normalize(TripleName));
136
137 SourceMgr SrcMgr;
138
139 std::unique_ptr<MemoryBuffer> BufferPtr(new LLVMFuzzerInputBuffer(Data, Size));
140
141 // Tell SrcMgr about this buffer, which is what the parser will pick up.
142 SrcMgr.AddNewSourceBuffer(std::move(BufferPtr), SMLoc());
143
144 static const std::vector<std::string> NoIncludeDirs;
145 SrcMgr.setIncludeDirs(NoIncludeDirs);
146
147 static std::string ArchName;
148 std::string Error;
149 const Target *TheTarget = TargetRegistry::lookupTarget(ArchName, TheTriple,
150 Error);
151 if (!TheTarget) {
152 errs() << "error: this target '" << TheTriple.normalize()
153 << "/" << ArchName << "', was not found: '" << Error << "'\n";
154
155 abort();
156 }
157
158 std::unique_ptr<MCRegisterInfo> MRI(TheTarget->createMCRegInfo(TripleName));
159 if (!MRI) {
160 errs() << "Unable to create target register info!";
161 abort();
162 }
163
164 std::unique_ptr<MCAsmInfo> MAI(TheTarget->createMCAsmInfo(*MRI, TripleName));
165 if (!MAI) {
166 errs() << "Unable to create target asm info!";
167 abort();
168 }
169
170
171 MCObjectFileInfo MOFI;
172 MCContext Ctx(MAI.get(), MRI.get(), &MOFI, &SrcMgr);
173
174 static const bool UsePIC = false;
Justin Bogner29c5d022017-08-29 17:08:44 +0000175 MOFI.InitMCObjectFileInfo(TheTriple, UsePIC, Ctx);
Brian Cain50aa37b2017-02-27 06:22:17 +0000176
177 const unsigned OutputAsmVariant = 0;
178 std::unique_ptr<MCInstrInfo> MCII(TheTarget->createMCInstrInfo());
179 MCInstPrinter *IP = TheTarget->createMCInstPrinter(Triple(TripleName), OutputAsmVariant,
180 *MAI, *MCII, *MRI);
181 if (!IP) {
182 errs()
183 << "error: unable to create instruction printer for target triple '"
184 << TheTriple.normalize() << "' with assembly variant "
185 << OutputAsmVariant << ".\n";
186
187 abort();
188 }
189
190 const char *ProgName = "llvm-mc-fuzzer";
191 std::unique_ptr<MCSubtargetInfo> STI(
192 TheTarget->createMCSubtargetInfo(TripleName, MCPU, FeaturesStr));
193 MCCodeEmitter *CE = nullptr;
194 MCAsmBackend *MAB = nullptr;
195
196 MCTargetOptions MCOptions = InitMCTargetOptionsFromFlags();
197
198 std::string OutputString;
199 raw_string_ostream Out(OutputString);
200 auto FOut = llvm::make_unique<formatted_raw_ostream>(Out);
201
202 std::unique_ptr<MCStreamer> Str;
203
204 if (FileType == OFT_AssemblyFile) {
205 Str.reset(TheTarget->createAsmStreamer(
206 Ctx, std::move(FOut), AsmVerbose,
207 UseDwarfDirectory, IP, CE, MAB, ShowInst));
208 } else {
209 assert(FileType == OFT_ObjectFile && "Invalid file type!");
210
211 std::error_code EC;
212 const std::string OutputFilename = "-";
Reid Kleckner3fc649c2017-09-23 01:03:17 +0000213 auto Out =
214 llvm::make_unique<ToolOutputFile>(OutputFilename, EC, sys::fs::F_None);
Brian Cain50aa37b2017-02-27 06:22:17 +0000215 if (EC) {
216 errs() << EC.message() << '\n';
217 abort();
218 }
219
220 // Don't waste memory on names of temp labels.
221 Ctx.setUseNamesOnTempLabels(false);
222
223 std::unique_ptr<buffer_ostream> BOS;
224 raw_pwrite_stream *OS = &Out->os();
225 if (!Out->os().supportsSeeking()) {
226 BOS = make_unique<buffer_ostream>(Out->os());
227 OS = BOS.get();
228 }
229
230 MCCodeEmitter *CE = TheTarget->createMCCodeEmitter(*MCII, *MRI, Ctx);
231 MCAsmBackend *MAB = TheTarget->createMCAsmBackend(*MRI, TripleName, MCPU,
232 MCOptions);
233 Str.reset(TheTarget->createMCObjectStreamer(
Mitch Phillips5ebf7a82017-11-06 18:56:36 +0000234 TheTriple, Ctx, std::unique_ptr<MCAsmBackend>(MAB), *OS,
235 std::unique_ptr<MCCodeEmitter>(CE), *STI, MCOptions.MCRelaxAll,
Brian Cain50aa37b2017-02-27 06:22:17 +0000236 MCOptions.MCIncrementalLinkerCompatible,
237 /*DWARFMustBeAtTheEnd*/ false));
238 }
239 const int Res = AssembleInput(ProgName, TheTarget, SrcMgr, Ctx, *Str, *MAI, *STI,
240 *MCII, MCOptions);
241
242 (void) Res;
243
244 return 0;
245}
246
Justin Bogner29c5d022017-08-29 17:08:44 +0000247extern "C" int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
Brian Cain50aa37b2017-02-27 06:22:17 +0000248 return AssembleOneInput(Data, Size);
249}
250
Justin Bogner29c5d022017-08-29 17:08:44 +0000251extern "C" LLVM_ATTRIBUTE_USED int LLVMFuzzerInitialize(int *argc,
252 char ***argv) {
Brian Cain50aa37b2017-02-27 06:22:17 +0000253 // The command line is unusual compared to other fuzzers due to the need to
254 // specify the target. Options like -triple, -mcpu, and -mattr work like
255 // their counterparts in llvm-mc, while -fuzzer-args collects options for the
256 // fuzzer itself.
257 //
258 // Examples:
259 //
260 // Fuzz the big-endian MIPS32R6 disassembler using 100,000 inputs of up to
261 // 4-bytes each and use the contents of ./corpus as the test corpus:
262 // llvm-mc-fuzzer -triple mips-linux-gnu -mcpu=mips32r6 -disassemble \
263 // -fuzzer-args -max_len=4 -runs=100000 ./corpus
264 //
265 // Infinitely fuzz the little-endian MIPS64R2 disassembler with the MSA
266 // feature enabled using up to 64-byte inputs:
267 // llvm-mc-fuzzer -triple mipsel-linux-gnu -mcpu=mips64r2 -mattr=msa \
268 // -disassemble -fuzzer-args ./corpus
269 //
270 // If your aim is to find instructions that are not tested, then it is
271 // advisable to constrain the maximum input size to a single instruction
272 // using -max_len as in the first example. This results in a test corpus of
273 // individual instructions that test unique paths. Without this constraint,
274 // there will be considerable redundancy in the corpus.
275
276 char **OriginalArgv = *argv;
277
278 LLVMInitializeAllTargetInfos();
279 LLVMInitializeAllTargetMCs();
280 LLVMInitializeAllAsmParsers();
281
282 cl::ParseCommandLineOptions(*argc, OriginalArgv);
283
284 // Rebuild the argv without the arguments llvm-mc-fuzzer consumed so that
285 // the driver can parse its arguments.
286 //
287 // FuzzerArgs cannot provide the non-const pointer that OriginalArgv needs.
288 // Re-use the strings from OriginalArgv instead of copying FuzzerArg to a
289 // non-const buffer to avoid the need to clean up when the fuzzer terminates.
290 ModifiedArgv.push_back(OriginalArgv[0]);
291 for (const auto &FuzzerArg : FuzzerArgs) {
292 for (int i = 1; i < *argc; ++i) {
293 if (FuzzerArg == OriginalArgv[i])
294 ModifiedArgv.push_back(OriginalArgv[i]);
295 }
296 }
297 *argc = ModifiedArgv.size();
298 *argv = ModifiedArgv.data();
299
300 // Package up features to be passed to target/subtarget
301 // We have to pass it via a global since the callback doesn't
302 // permit any user data.
303 if (MAttrs.size()) {
304 SubtargetFeatures Features;
305 for (unsigned i = 0; i != MAttrs.size(); ++i)
306 Features.AddFeature(MAttrs[i]);
307 FeaturesStr = Features.getString();
308 }
309
310 if (TripleName.empty())
311 TripleName = sys::getDefaultTargetTriple();
312
313 return 0;
314}