blob: 643afe64073e6eb23afc2d0f1dd2b5f9adc7de10 [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
Daniel Sanders205d1992015-09-16 11:49:49 +000023static cl::opt<std::string>
24 TripleName("triple", cl::desc("Target triple to assemble for, "
25 "see -version for available targets"));
26
27static cl::opt<std::string>
28 MCPU("mcpu",
29 cl::desc("Target a specific cpu type (-mcpu=help for details)"),
30 cl::value_desc("cpu-name"), cl::init(""));
31
Daniel Sandersb45deab2015-09-22 09:22:53 +000032// This is useful for variable-length instruction sets.
33static cl::opt<unsigned> InsnLimit(
34 "insn-limit",
35 cl::desc("Limit the number of instructions to process (0 for no limit)"),
36 cl::value_desc("count"), cl::init(0));
37
Daniel Sanders205d1992015-09-16 11:49:49 +000038static cl::list<std::string>
39 MAttrs("mattr", cl::CommaSeparated,
40 cl::desc("Target specific attributes (-mattr=help for details)"),
41 cl::value_desc("a1,+a2,-a3,..."));
42// The feature string derived from -mattr's values.
43std::string FeaturesStr;
44
45static cl::list<std::string>
Daniel Sanders031da592016-05-13 10:23:04 +000046 FuzzerArgs("fuzzer-args", cl::Positional,
Daniel Sanders205d1992015-09-16 11:49:49 +000047 cl::desc("Options to pass to the fuzzer"), cl::ZeroOrMore,
48 cl::PositionalEatsArgs);
Daniel Sanders031da592016-05-13 10:23:04 +000049static std::vector<char *> ModifiedArgv;
Daniel Sanders205d1992015-09-16 11:49:49 +000050
Kostya Serebryany20bb5e72015-10-02 23:34:06 +000051int DisassembleOneInput(const uint8_t *Data, size_t Size) {
Daniel Sanders205d1992015-09-16 11:49:49 +000052 char AssemblyText[AssemblyTextBufSize];
53
54 std::vector<uint8_t> DataCopy(Data, Data + Size);
55
56 LLVMDisasmContextRef Ctx = LLVMCreateDisasmCPUFeatures(
57 TripleName.c_str(), MCPU.c_str(), FeaturesStr.c_str(), nullptr, 0,
58 nullptr, nullptr);
59 assert(Ctx);
60 uint8_t *p = DataCopy.data();
61 unsigned Consumed;
Daniel Sandersb45deab2015-09-22 09:22:53 +000062 unsigned InstructionsProcessed = 0;
Daniel Sanders205d1992015-09-16 11:49:49 +000063 do {
64 Consumed = LLVMDisasmInstruction(Ctx, p, Size, 0, AssemblyText,
65 AssemblyTextBufSize);
66 Size -= Consumed;
67 p += Consumed;
Daniel Sandersb45deab2015-09-22 09:22:53 +000068
69 InstructionsProcessed ++;
70 if (InsnLimit != 0 && InstructionsProcessed < InsnLimit)
71 break;
Daniel Sanders205d1992015-09-16 11:49:49 +000072 } while (Consumed != 0);
73 LLVMDisasmDispose(Ctx);
Kostya Serebryany20bb5e72015-10-02 23:34:06 +000074 return 0;
Daniel Sanders205d1992015-09-16 11:49:49 +000075}
76
Daniel Sanders031da592016-05-13 10:23:04 +000077int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
Brian Cain50aa37b2017-02-27 06:22:17 +000078 return DisassembleOneInput(Data, Size);
Daniel Sanders031da592016-05-13 10:23:04 +000079}
80
81int LLVMFuzzerInitialize(int *argc, char ***argv) {
Daniel Sanders205d1992015-09-16 11:49:49 +000082 // The command line is unusual compared to other fuzzers due to the need to
83 // specify the target. Options like -triple, -mcpu, and -mattr work like
84 // their counterparts in llvm-mc, while -fuzzer-args collects options for the
85 // fuzzer itself.
86 //
87 // Examples:
88 //
89 // Fuzz the big-endian MIPS32R6 disassembler using 100,000 inputs of up to
90 // 4-bytes each and use the contents of ./corpus as the test corpus:
91 // llvm-mc-fuzzer -triple mips-linux-gnu -mcpu=mips32r6 -disassemble \
92 // -fuzzer-args -max_len=4 -runs=100000 ./corpus
93 //
94 // Infinitely fuzz the little-endian MIPS64R2 disassembler with the MSA
95 // feature enabled using up to 64-byte inputs:
96 // llvm-mc-fuzzer -triple mipsel-linux-gnu -mcpu=mips64r2 -mattr=msa \
97 // -disassemble -fuzzer-args ./corpus
98 //
99 // If your aim is to find instructions that are not tested, then it is
100 // advisable to constrain the maximum input size to a single instruction
101 // using -max_len as in the first example. This results in a test corpus of
102 // individual instructions that test unique paths. Without this constraint,
103 // there will be considerable redundancy in the corpus.
104
Daniel Sanders031da592016-05-13 10:23:04 +0000105 char **OriginalArgv = *argv;
106
Daniel Sanders205d1992015-09-16 11:49:49 +0000107 LLVMInitializeAllTargetInfos();
108 LLVMInitializeAllTargetMCs();
109 LLVMInitializeAllDisassemblers();
110
Daniel Sanders031da592016-05-13 10:23:04 +0000111 cl::ParseCommandLineOptions(*argc, OriginalArgv);
112
113 // Rebuild the argv without the arguments llvm-mc-fuzzer consumed so that
114 // the driver can parse its arguments.
115 //
116 // FuzzerArgs cannot provide the non-const pointer that OriginalArgv needs.
117 // Re-use the strings from OriginalArgv instead of copying FuzzerArg to a
118 // non-const buffer to avoid the need to clean up when the fuzzer terminates.
119 ModifiedArgv.push_back(OriginalArgv[0]);
120 for (const auto &FuzzerArg : FuzzerArgs) {
121 for (int i = 1; i < *argc; ++i) {
122 if (FuzzerArg == OriginalArgv[i])
123 ModifiedArgv.push_back(OriginalArgv[i]);
124 }
125 }
126 *argc = ModifiedArgv.size();
127 *argv = ModifiedArgv.data();
Daniel Sanders205d1992015-09-16 11:49:49 +0000128
129 // Package up features to be passed to target/subtarget
130 // We have to pass it via a global since the callback doesn't
131 // permit any user data.
132 if (MAttrs.size()) {
133 SubtargetFeatures Features;
134 for (unsigned i = 0; i != MAttrs.size(); ++i)
135 Features.AddFeature(MAttrs[i]);
136 FeaturesStr = Features.getString();
137 }
138
Brian Cain50aa37b2017-02-27 06:22:17 +0000139 if (TripleName.empty())
140 TripleName = sys::getDefaultTargetTriple();
141
Daniel Sanders031da592016-05-13 10:23:04 +0000142 return 0;
Daniel Sanders205d1992015-09-16 11:49:49 +0000143}