Daniel Sanders | 205d199 | 2015-09-16 11:49:49 +0000 | [diff] [blame] | 1 | //===--- 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 Amini | b550cb1 | 2016-04-18 09:17:29 +0000 | [diff] [blame] | 12 | #include "FuzzerInterface.h" |
Daniel Sanders | 205d199 | 2015-09-16 11:49:49 +0000 | [diff] [blame] | 13 | #include "llvm-c/Disassembler.h" |
| 14 | #include "llvm-c/Target.h" |
Daniel Sanders | 205d199 | 2015-09-16 11:49:49 +0000 | [diff] [blame] | 15 | #include "llvm/MC/SubtargetFeature.h" |
| 16 | #include "llvm/Support/CommandLine.h" |
| 17 | #include "llvm/Support/raw_ostream.h" |
Daniel Sanders | 205d199 | 2015-09-16 11:49:49 +0000 | [diff] [blame] | 18 | |
| 19 | using namespace llvm; |
| 20 | |
| 21 | const unsigned AssemblyTextBufSize = 80; |
| 22 | |
| 23 | enum ActionType { |
| 24 | AC_Assemble, |
| 25 | AC_Disassemble |
| 26 | }; |
| 27 | |
| 28 | static cl::opt<ActionType> |
| 29 | Action(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 Amini | 732afdd | 2016-10-08 19:41:06 +0000 | [diff] [blame^] | 34 | "Disassemble strings of hex bytes"))); |
Daniel Sanders | 205d199 | 2015-09-16 11:49:49 +0000 | [diff] [blame] | 35 | |
| 36 | static cl::opt<std::string> |
| 37 | TripleName("triple", cl::desc("Target triple to assemble for, " |
| 38 | "see -version for available targets")); |
| 39 | |
| 40 | static 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 Sanders | b45deab | 2015-09-22 09:22:53 +0000 | [diff] [blame] | 45 | // This is useful for variable-length instruction sets. |
| 46 | static 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 Sanders | 205d199 | 2015-09-16 11:49:49 +0000 | [diff] [blame] | 51 | static 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. |
| 56 | std::string FeaturesStr; |
| 57 | |
| 58 | static cl::list<std::string> |
Daniel Sanders | 031da59 | 2016-05-13 10:23:04 +0000 | [diff] [blame] | 59 | FuzzerArgs("fuzzer-args", cl::Positional, |
Daniel Sanders | 205d199 | 2015-09-16 11:49:49 +0000 | [diff] [blame] | 60 | cl::desc("Options to pass to the fuzzer"), cl::ZeroOrMore, |
| 61 | cl::PositionalEatsArgs); |
Daniel Sanders | 031da59 | 2016-05-13 10:23:04 +0000 | [diff] [blame] | 62 | static std::vector<char *> ModifiedArgv; |
Daniel Sanders | 205d199 | 2015-09-16 11:49:49 +0000 | [diff] [blame] | 63 | |
Kostya Serebryany | 20bb5e7 | 2015-10-02 23:34:06 +0000 | [diff] [blame] | 64 | int DisassembleOneInput(const uint8_t *Data, size_t Size) { |
Daniel Sanders | 205d199 | 2015-09-16 11:49:49 +0000 | [diff] [blame] | 65 | 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 Sanders | b45deab | 2015-09-22 09:22:53 +0000 | [diff] [blame] | 75 | unsigned InstructionsProcessed = 0; |
Daniel Sanders | 205d199 | 2015-09-16 11:49:49 +0000 | [diff] [blame] | 76 | do { |
| 77 | Consumed = LLVMDisasmInstruction(Ctx, p, Size, 0, AssemblyText, |
| 78 | AssemblyTextBufSize); |
| 79 | Size -= Consumed; |
| 80 | p += Consumed; |
Daniel Sanders | b45deab | 2015-09-22 09:22:53 +0000 | [diff] [blame] | 81 | |
| 82 | InstructionsProcessed ++; |
| 83 | if (InsnLimit != 0 && InstructionsProcessed < InsnLimit) |
| 84 | break; |
Daniel Sanders | 205d199 | 2015-09-16 11:49:49 +0000 | [diff] [blame] | 85 | } while (Consumed != 0); |
| 86 | LLVMDisasmDispose(Ctx); |
Kostya Serebryany | 20bb5e7 | 2015-10-02 23:34:06 +0000 | [diff] [blame] | 87 | return 0; |
Daniel Sanders | 205d199 | 2015-09-16 11:49:49 +0000 | [diff] [blame] | 88 | } |
| 89 | |
Daniel Sanders | 031da59 | 2016-05-13 10:23:04 +0000 | [diff] [blame] | 90 | int 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 | |
| 100 | int LLVMFuzzerInitialize(int *argc, char ***argv) { |
Daniel Sanders | 205d199 | 2015-09-16 11:49:49 +0000 | [diff] [blame] | 101 | // 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 Sanders | 031da59 | 2016-05-13 10:23:04 +0000 | [diff] [blame] | 124 | char **OriginalArgv = *argv; |
| 125 | |
Daniel Sanders | 205d199 | 2015-09-16 11:49:49 +0000 | [diff] [blame] | 126 | LLVMInitializeAllTargetInfos(); |
| 127 | LLVMInitializeAllTargetMCs(); |
| 128 | LLVMInitializeAllDisassemblers(); |
| 129 | |
Daniel Sanders | 031da59 | 2016-05-13 10:23:04 +0000 | [diff] [blame] | 130 | 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 Sanders | 205d199 | 2015-09-16 11:49:49 +0000 | [diff] [blame] | 147 | |
| 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 Sanders | 031da59 | 2016-05-13 10:23:04 +0000 | [diff] [blame] | 158 | return 0; |
Daniel Sanders | 205d199 | 2015-09-16 11:49:49 +0000 | [diff] [blame] | 159 | } |