Justin Bogner | 2ebcca2 | 2017-08-29 00:22:08 +0000 | [diff] [blame] | 1 | //===--- llvm-isel-fuzzer.cpp - Fuzzer for instruction selection ----------===// |
| 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 | // Tool to fuzz instruction selection using libFuzzer. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Justin Bogner | 2ebcca2 | 2017-08-29 00:22:08 +0000 | [diff] [blame] | 14 | #include "llvm/ADT/StringRef.h" |
| 15 | #include "llvm/Analysis/TargetLibraryInfo.h" |
| 16 | #include "llvm/Bitcode/BitcodeReader.h" |
| 17 | #include "llvm/Bitcode/BitcodeWriter.h" |
| 18 | #include "llvm/CodeGen/CommandFlags.h" |
Justin Bogner | 7f28d73 | 2017-09-02 23:43:04 +0000 | [diff] [blame^] | 19 | #include "llvm/FuzzMutate/FuzzerCLI.h" |
Justin Bogner | 2ebcca2 | 2017-08-29 00:22:08 +0000 | [diff] [blame] | 20 | #include "llvm/FuzzMutate/IRMutator.h" |
| 21 | #include "llvm/FuzzMutate/Operations.h" |
| 22 | #include "llvm/FuzzMutate/Random.h" |
| 23 | #include "llvm/IR/Constants.h" |
| 24 | #include "llvm/IR/LLVMContext.h" |
| 25 | #include "llvm/IR/LegacyPassManager.h" |
| 26 | #include "llvm/IR/Module.h" |
| 27 | #include "llvm/IR/Verifier.h" |
| 28 | #include "llvm/IRReader/IRReader.h" |
| 29 | #include "llvm/Support/DataTypes.h" |
| 30 | #include "llvm/Support/Debug.h" |
| 31 | #include "llvm/Support/SourceMgr.h" |
| 32 | #include "llvm/Support/TargetRegistry.h" |
| 33 | #include "llvm/Support/TargetSelect.h" |
| 34 | #include "llvm/Target/TargetMachine.h" |
| 35 | #include <random> |
| 36 | |
| 37 | #define DEBUG_TYPE "isel-fuzzer" |
| 38 | |
| 39 | using namespace llvm; |
| 40 | |
| 41 | static cl::opt<char> |
| 42 | OptLevel("O", |
| 43 | cl::desc("Optimization level. [-O0, -O1, -O2, or -O3] " |
| 44 | "(default = '-O2')"), |
| 45 | cl::Prefix, |
| 46 | cl::ZeroOrMore, |
| 47 | cl::init(' ')); |
| 48 | |
| 49 | static cl::opt<std::string> |
| 50 | TargetTriple("mtriple", cl::desc("Override target triple for module")); |
| 51 | |
| 52 | static std::unique_ptr<TargetMachine> TM; |
| 53 | static std::unique_ptr<IRMutator> Mutator; |
| 54 | |
| 55 | static std::unique_ptr<Module> parseModule(const uint8_t *Data, size_t Size, |
| 56 | LLVMContext &Context) { |
| 57 | auto Buffer = MemoryBuffer::getMemBuffer( |
| 58 | StringRef(reinterpret_cast<const char *>(Data), Size), "Fuzzer input", |
| 59 | /*RequiresNullTerminator=*/false); |
| 60 | |
| 61 | SMDiagnostic Err; |
| 62 | auto M = parseBitcodeFile(Buffer->getMemBufferRef(), Context); |
| 63 | if (Error E = M.takeError()) { |
| 64 | errs() << toString(std::move(E)) << "\n"; |
| 65 | return nullptr; |
| 66 | } |
| 67 | return std::move(M.get()); |
| 68 | } |
| 69 | |
| 70 | static size_t writeModule(const Module &M, uint8_t *Dest, size_t MaxSize) { |
| 71 | std::string Buf; |
| 72 | { |
| 73 | raw_string_ostream OS(Buf); |
| 74 | WriteBitcodeToFile(&M, OS); |
| 75 | } |
| 76 | if (Buf.size() > MaxSize) |
| 77 | return 0; |
| 78 | memcpy(Dest, Buf.data(), Buf.size()); |
| 79 | return Buf.size(); |
| 80 | } |
| 81 | |
| 82 | std::unique_ptr<IRMutator> createISelMutator() { |
| 83 | std::vector<TypeGetter> Types{ |
| 84 | Type::getInt1Ty, Type::getInt8Ty, Type::getInt16Ty, Type::getInt32Ty, |
| 85 | Type::getInt64Ty, Type::getFloatTy, Type::getDoubleTy}; |
| 86 | |
| 87 | std::vector<std::unique_ptr<IRMutationStrategy>> Strategies; |
| 88 | Strategies.emplace_back( |
| 89 | new InjectorIRStrategy(InjectorIRStrategy::getDefaultOps())); |
| 90 | Strategies.emplace_back(new InstDeleterIRStrategy()); |
| 91 | |
Justin Bogner | f483817 | 2017-09-01 17:26:24 +0000 | [diff] [blame] | 92 | return llvm::make_unique<IRMutator>(std::move(Types), std::move(Strategies)); |
Justin Bogner | 2ebcca2 | 2017-08-29 00:22:08 +0000 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | extern "C" LLVM_ATTRIBUTE_USED size_t LLVMFuzzerCustomMutator( |
| 96 | uint8_t *Data, size_t Size, size_t MaxSize, unsigned int Seed) { |
| 97 | LLVMContext Context; |
| 98 | std::unique_ptr<Module> M; |
| 99 | if (Size <= 1) |
| 100 | // We get bogus data given an empty corpus - just create a new module. |
| 101 | M.reset(new Module("M", Context)); |
| 102 | else |
| 103 | M = parseModule(Data, Size, Context); |
| 104 | |
| 105 | Mutator->mutateModule(*M, Seed, Size, MaxSize); |
| 106 | |
| 107 | return writeModule(*M, Data, MaxSize); |
| 108 | } |
| 109 | |
| 110 | extern "C" int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) { |
| 111 | if (Size <= 1) |
| 112 | // We get bogus data given an empty corpus - ignore it. |
| 113 | return 0; |
| 114 | |
| 115 | LLVMContext Context; |
| 116 | auto M = parseModule(Data, Size, Context); |
| 117 | if (!M || verifyModule(*M, &errs())) { |
| 118 | errs() << "error: input module is broken!\n"; |
| 119 | return 1; |
| 120 | } |
| 121 | |
| 122 | // Set up the module to build for our target. |
| 123 | M->setTargetTriple(TM->getTargetTriple().normalize()); |
| 124 | M->setDataLayout(TM->createDataLayout()); |
| 125 | |
| 126 | // Build up a PM to do instruction selection. |
| 127 | legacy::PassManager PM; |
| 128 | TargetLibraryInfoImpl TLII(TM->getTargetTriple()); |
| 129 | PM.add(new TargetLibraryInfoWrapperPass(TLII)); |
| 130 | raw_null_ostream OS; |
| 131 | TM->addPassesToEmitFile(PM, OS, TargetMachine::CGFT_Null); |
| 132 | PM.run(*M); |
| 133 | |
| 134 | return 0; |
| 135 | } |
| 136 | |
Justin Bogner | 2ebcca2 | 2017-08-29 00:22:08 +0000 | [diff] [blame] | 137 | static void handleLLVMFatalError(void *, const std::string &Message, bool) { |
| 138 | // TODO: Would it be better to call into the fuzzer internals directly? |
| 139 | dbgs() << "LLVM ERROR: " << Message << "\n" |
| 140 | << "Aborting to trigger fuzzer exit handling.\n"; |
| 141 | abort(); |
| 142 | } |
| 143 | |
| 144 | extern "C" LLVM_ATTRIBUTE_USED int LLVMFuzzerInitialize(int *argc, |
| 145 | char ***argv) { |
| 146 | EnableDebugBuffering = true; |
| 147 | |
| 148 | InitializeAllTargets(); |
| 149 | InitializeAllTargetMCs(); |
| 150 | InitializeAllAsmPrinters(); |
| 151 | InitializeAllAsmParsers(); |
| 152 | |
Justin Bogner | 7f28d73 | 2017-09-02 23:43:04 +0000 | [diff] [blame^] | 153 | parseFuzzerCLOpts(*argc, *argv); |
Justin Bogner | 2ebcca2 | 2017-08-29 00:22:08 +0000 | [diff] [blame] | 154 | |
| 155 | if (TargetTriple.empty()) { |
| 156 | errs() << *argv[0] << ": -mtriple must be specified\n"; |
| 157 | return 1; |
| 158 | } |
| 159 | |
| 160 | Triple TheTriple = Triple(Triple::normalize(TargetTriple)); |
| 161 | |
| 162 | // Get the target specific parser. |
| 163 | std::string Error; |
| 164 | const Target *TheTarget = |
| 165 | TargetRegistry::lookupTarget(MArch, TheTriple, Error); |
| 166 | if (!TheTarget) { |
| 167 | errs() << argv[0] << ": " << Error; |
| 168 | return 1; |
| 169 | } |
| 170 | |
| 171 | // Set up the pipeline like llc does. |
| 172 | std::string CPUStr = getCPUStr(), FeaturesStr = getFeaturesStr(); |
| 173 | |
| 174 | CodeGenOpt::Level OLvl = CodeGenOpt::Default; |
| 175 | switch (OptLevel) { |
| 176 | default: |
| 177 | errs() << argv[0] << ": invalid optimization level.\n"; |
| 178 | return 1; |
| 179 | case ' ': break; |
| 180 | case '0': OLvl = CodeGenOpt::None; break; |
| 181 | case '1': OLvl = CodeGenOpt::Less; break; |
| 182 | case '2': OLvl = CodeGenOpt::Default; break; |
| 183 | case '3': OLvl = CodeGenOpt::Aggressive; break; |
| 184 | } |
| 185 | |
| 186 | TargetOptions Options = InitTargetOptionsFromCodeGenFlags(); |
| 187 | TM.reset(TheTarget->createTargetMachine(TheTriple.getTriple(), CPUStr, |
| 188 | FeaturesStr, Options, getRelocModel(), |
| 189 | getCodeModel(), OLvl)); |
| 190 | assert(TM && "Could not allocate target machine!"); |
| 191 | |
| 192 | // Make sure we print the summary and the current unit when LLVM errors out. |
| 193 | install_fatal_error_handler(handleLLVMFatalError, nullptr); |
| 194 | |
| 195 | // Finally, create our mutator. |
| 196 | Mutator = createISelMutator(); |
| 197 | return 0; |
| 198 | } |