blob: e31ea762add53defd32dd4c4e29bc3d4e6894b82 [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",
Mehdi Amini732afdd2016-10-08 19:41:06 +000034 "Disassemble strings of hex bytes")));
Daniel Sanders205d1992015-09-16 11:49:49 +000035
36static cl::opt<std::string>
37 TripleName("triple", cl::desc("Target triple to assemble for, "
38 "see -version for available targets"));
39
40static cl::opt<std::string>
41 MCPU("mcpu",
42 cl::desc("Target a specific cpu type (-mcpu=help for details)"),
43 cl::value_desc("cpu-name"), cl::init(""));
44
Daniel Sandersb45deab2015-09-22 09:22:53 +000045// This is useful for variable-length instruction sets.
46static cl::opt<unsigned> InsnLimit(
47 "insn-limit",
48 cl::desc("Limit the number of instructions to process (0 for no limit)"),
49 cl::value_desc("count"), cl::init(0));
50
Daniel Sanders205d1992015-09-16 11:49:49 +000051static cl::list<std::string>
52 MAttrs("mattr", cl::CommaSeparated,
53 cl::desc("Target specific attributes (-mattr=help for details)"),
54 cl::value_desc("a1,+a2,-a3,..."));
55// The feature string derived from -mattr's values.
56std::string FeaturesStr;
57
58static cl::list<std::string>
Daniel Sanders031da592016-05-13 10:23:04 +000059 FuzzerArgs("fuzzer-args", cl::Positional,
Daniel Sanders205d1992015-09-16 11:49:49 +000060 cl::desc("Options to pass to the fuzzer"), cl::ZeroOrMore,
61 cl::PositionalEatsArgs);
Daniel Sanders031da592016-05-13 10:23:04 +000062static std::vector<char *> ModifiedArgv;
Daniel Sanders205d1992015-09-16 11:49:49 +000063
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
Daniel Sanders031da592016-05-13 10:23:04 +000090int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
91 if (Action == AC_Assemble)
92 errs() << "error: -assemble is not implemented\n";
93 else if (Action == AC_Disassemble)
94 return DisassembleOneInput(Data, Size);
95
96 llvm_unreachable("Unknown action");
97 return 0;
98}
99
100int LLVMFuzzerInitialize(int *argc, char ***argv) {
Daniel Sanders205d1992015-09-16 11:49:49 +0000101 // The command line is unusual compared to other fuzzers due to the need to
102 // specify the target. Options like -triple, -mcpu, and -mattr work like
103 // their counterparts in llvm-mc, while -fuzzer-args collects options for the
104 // fuzzer itself.
105 //
106 // Examples:
107 //
108 // Fuzz the big-endian MIPS32R6 disassembler using 100,000 inputs of up to
109 // 4-bytes each and use the contents of ./corpus as the test corpus:
110 // llvm-mc-fuzzer -triple mips-linux-gnu -mcpu=mips32r6 -disassemble \
111 // -fuzzer-args -max_len=4 -runs=100000 ./corpus
112 //
113 // Infinitely fuzz the little-endian MIPS64R2 disassembler with the MSA
114 // feature enabled using up to 64-byte inputs:
115 // llvm-mc-fuzzer -triple mipsel-linux-gnu -mcpu=mips64r2 -mattr=msa \
116 // -disassemble -fuzzer-args ./corpus
117 //
118 // If your aim is to find instructions that are not tested, then it is
119 // advisable to constrain the maximum input size to a single instruction
120 // using -max_len as in the first example. This results in a test corpus of
121 // individual instructions that test unique paths. Without this constraint,
122 // there will be considerable redundancy in the corpus.
123
Daniel Sanders031da592016-05-13 10:23:04 +0000124 char **OriginalArgv = *argv;
125
Daniel Sanders205d1992015-09-16 11:49:49 +0000126 LLVMInitializeAllTargetInfos();
127 LLVMInitializeAllTargetMCs();
128 LLVMInitializeAllDisassemblers();
129
Daniel Sanders031da592016-05-13 10:23:04 +0000130 cl::ParseCommandLineOptions(*argc, OriginalArgv);
131
132 // Rebuild the argv without the arguments llvm-mc-fuzzer consumed so that
133 // the driver can parse its arguments.
134 //
135 // FuzzerArgs cannot provide the non-const pointer that OriginalArgv needs.
136 // Re-use the strings from OriginalArgv instead of copying FuzzerArg to a
137 // non-const buffer to avoid the need to clean up when the fuzzer terminates.
138 ModifiedArgv.push_back(OriginalArgv[0]);
139 for (const auto &FuzzerArg : FuzzerArgs) {
140 for (int i = 1; i < *argc; ++i) {
141 if (FuzzerArg == OriginalArgv[i])
142 ModifiedArgv.push_back(OriginalArgv[i]);
143 }
144 }
145 *argc = ModifiedArgv.size();
146 *argv = ModifiedArgv.data();
Daniel Sanders205d1992015-09-16 11:49:49 +0000147
148 // Package up features to be passed to target/subtarget
149 // We have to pass it via a global since the callback doesn't
150 // permit any user data.
151 if (MAttrs.size()) {
152 SubtargetFeatures Features;
153 for (unsigned i = 0; i != MAttrs.size(); ++i)
154 Features.AddFeature(MAttrs[i]);
155 FeaturesStr = Features.getString();
156 }
157
Daniel Sanders031da592016-05-13 10:23:04 +0000158 return 0;
Daniel Sanders205d1992015-09-16 11:49:49 +0000159}