blob: b21149c8d340bbf1490ac8185f3fa556016025f7 [file] [log] [blame]
Daniel Sanders205d1992015-09-16 11:49:49 +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
Mehdi Aminib550cb12016-04-18 09:17:29 +000012#include "FuzzerInterface.h"
Daniel Sanders205d1992015-09-16 11:49:49 +000013#include "llvm-c/Disassembler.h"
14#include "llvm-c/Target.h"
Daniel Sanders205d1992015-09-16 11:49:49 +000015#include "llvm/MC/SubtargetFeature.h"
16#include "llvm/Support/CommandLine.h"
17#include "llvm/Support/raw_ostream.h"
Daniel Sanders205d1992015-09-16 11:49:49 +000018
19using namespace llvm;
20
21const unsigned AssemblyTextBufSize = 80;
22
23enum ActionType {
24 AC_Assemble,
25 AC_Disassemble
26};
27
28static cl::opt<ActionType>
29Action(cl::desc("Action to perform:"),
30 cl::init(AC_Assemble),
31 cl::values(clEnumValN(AC_Assemble, "assemble",
32 "Assemble a .s file (default)"),
33 clEnumValN(AC_Disassemble, "disassemble",
34 "Disassemble strings of hex bytes"),
35 clEnumValEnd));
36
37static cl::opt<std::string>
38 TripleName("triple", cl::desc("Target triple to assemble for, "
39 "see -version for available targets"));
40
41static cl::opt<std::string>
42 MCPU("mcpu",
43 cl::desc("Target a specific cpu type (-mcpu=help for details)"),
44 cl::value_desc("cpu-name"), cl::init(""));
45
Daniel Sandersb45deab2015-09-22 09:22:53 +000046// This is useful for variable-length instruction sets.
47static cl::opt<unsigned> InsnLimit(
48 "insn-limit",
49 cl::desc("Limit the number of instructions to process (0 for no limit)"),
50 cl::value_desc("count"), cl::init(0));
51
Daniel Sanders205d1992015-09-16 11:49:49 +000052static cl::list<std::string>
53 MAttrs("mattr", cl::CommaSeparated,
54 cl::desc("Target specific attributes (-mattr=help for details)"),
55 cl::value_desc("a1,+a2,-a3,..."));
56// The feature string derived from -mattr's values.
57std::string FeaturesStr;
58
59static cl::list<std::string>
60 FuzzerArgv("fuzzer-args", cl::Positional,
61 cl::desc("Options to pass to the fuzzer"), cl::ZeroOrMore,
62 cl::PositionalEatsArgs);
63
Kostya Serebryany20bb5e72015-10-02 23:34:06 +000064int DisassembleOneInput(const uint8_t *Data, size_t Size) {
Daniel Sanders205d1992015-09-16 11:49:49 +000065 char AssemblyText[AssemblyTextBufSize];
66
67 std::vector<uint8_t> DataCopy(Data, Data + Size);
68
69 LLVMDisasmContextRef Ctx = LLVMCreateDisasmCPUFeatures(
70 TripleName.c_str(), MCPU.c_str(), FeaturesStr.c_str(), nullptr, 0,
71 nullptr, nullptr);
72 assert(Ctx);
73 uint8_t *p = DataCopy.data();
74 unsigned Consumed;
Daniel Sandersb45deab2015-09-22 09:22:53 +000075 unsigned InstructionsProcessed = 0;
Daniel Sanders205d1992015-09-16 11:49:49 +000076 do {
77 Consumed = LLVMDisasmInstruction(Ctx, p, Size, 0, AssemblyText,
78 AssemblyTextBufSize);
79 Size -= Consumed;
80 p += Consumed;
Daniel Sandersb45deab2015-09-22 09:22:53 +000081
82 InstructionsProcessed ++;
83 if (InsnLimit != 0 && InstructionsProcessed < InsnLimit)
84 break;
Daniel Sanders205d1992015-09-16 11:49:49 +000085 } while (Consumed != 0);
86 LLVMDisasmDispose(Ctx);
Kostya Serebryany20bb5e72015-10-02 23:34:06 +000087 return 0;
Daniel Sanders205d1992015-09-16 11:49:49 +000088}
89
90int main(int argc, char **argv) {
91 // The command line is unusual compared to other fuzzers due to the need to
92 // specify the target. Options like -triple, -mcpu, and -mattr work like
93 // their counterparts in llvm-mc, while -fuzzer-args collects options for the
94 // fuzzer itself.
95 //
96 // Examples:
97 //
98 // Fuzz the big-endian MIPS32R6 disassembler using 100,000 inputs of up to
99 // 4-bytes each and use the contents of ./corpus as the test corpus:
100 // llvm-mc-fuzzer -triple mips-linux-gnu -mcpu=mips32r6 -disassemble \
101 // -fuzzer-args -max_len=4 -runs=100000 ./corpus
102 //
103 // Infinitely fuzz the little-endian MIPS64R2 disassembler with the MSA
104 // feature enabled using up to 64-byte inputs:
105 // llvm-mc-fuzzer -triple mipsel-linux-gnu -mcpu=mips64r2 -mattr=msa \
106 // -disassemble -fuzzer-args ./corpus
107 //
108 // If your aim is to find instructions that are not tested, then it is
109 // advisable to constrain the maximum input size to a single instruction
110 // using -max_len as in the first example. This results in a test corpus of
111 // individual instructions that test unique paths. Without this constraint,
112 // there will be considerable redundancy in the corpus.
113
114 LLVMInitializeAllTargetInfos();
115 LLVMInitializeAllTargetMCs();
116 LLVMInitializeAllDisassemblers();
117
118 cl::ParseCommandLineOptions(argc, argv);
119
120 // Package up features to be passed to target/subtarget
121 // We have to pass it via a global since the callback doesn't
122 // permit any user data.
123 if (MAttrs.size()) {
124 SubtargetFeatures Features;
125 for (unsigned i = 0; i != MAttrs.size(); ++i)
126 Features.AddFeature(MAttrs[i]);
127 FeaturesStr = Features.getString();
128 }
129
Daniel Sanders205d1992015-09-16 11:49:49 +0000130 if (Action == AC_Assemble)
131 errs() << "error: -assemble is not implemented\n";
132 else if (Action == AC_Disassemble)
Daniel Sanders4fe1c8b2015-09-26 17:09:01 +0000133 return fuzzer::FuzzerDriver(argc, argv, DisassembleOneInput);
Daniel Sanders205d1992015-09-16 11:49:49 +0000134
135 llvm_unreachable("Unknown action");
136 return 1;
137}