Diego Trevino Ferrer | ddc64eb | 2019-08-08 22:16:33 +0000 | [diff] [blame] | 1 | //===- Delta.cpp - Delta Debugging Algorithm Implementation ---------------===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file contains the implementation for the Delta Debugging Algorithm: |
| 10 | // it splits a given set of Targets (i.e. Functions, Instructions, BBs, etc.) |
| 11 | // into chunks and tries to reduce the number chunks that are interesting. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "Delta.h" |
| 16 | #include "llvm/ADT/STLExtras.h" |
| 17 | |
| 18 | /// Writes IR code to the given Filepath |
| 19 | static bool writeProgramToFile(StringRef Filepath, int FD, const Module &M) { |
| 20 | ToolOutputFile Out(Filepath, FD); |
| 21 | M.print(Out.os(), /*AnnotationWriter=*/nullptr); |
| 22 | Out.os().close(); |
| 23 | |
| 24 | if (!Out.os().has_error()) { |
| 25 | Out.keep(); |
| 26 | return false; |
| 27 | } |
| 28 | return true; |
| 29 | } |
| 30 | |
| 31 | /// Creates a temporary (and unique) file inside the tmp folder and writes |
| 32 | /// the given module IR. |
| 33 | static SmallString<128> createTmpFile(Module *M, StringRef TmpDir) { |
| 34 | SmallString<128> UniqueFilepath; |
| 35 | int UniqueFD; |
| 36 | |
| 37 | SmallString<128> TmpFilepath; |
| 38 | sys::path::append(TmpFilepath, TmpDir, "tmp-%%%.ll"); |
| 39 | std::error_code EC = |
| 40 | sys::fs::createUniqueFile(TmpFilepath, UniqueFD, UniqueFilepath); |
| 41 | if (EC) { |
| 42 | errs() << "Error making unique filename: " << EC.message() << "!\n"; |
| 43 | exit(1); |
| 44 | } |
| 45 | |
| 46 | if (writeProgramToFile(UniqueFilepath, UniqueFD, *M)) { |
| 47 | errs() << "Error emitting bitcode to file '" << UniqueFilepath << "'!\n"; |
| 48 | exit(1); |
| 49 | } |
| 50 | return UniqueFilepath; |
| 51 | } |
| 52 | |
| 53 | /// Prints the Chunk Indexes with the following format: [start, end], if |
| 54 | /// chunk is at minimum size (1), then it just displays [start]. |
| 55 | static void printChunks(std::vector<Chunk> Chunks, bool Oneline = false) { |
| 56 | if (Chunks.empty()) { |
| 57 | outs() << "No Chunks"; |
| 58 | return; |
| 59 | } |
| 60 | |
| 61 | for (auto C : Chunks) { |
| 62 | if (!Oneline) |
| 63 | outs() << '\t'; |
| 64 | C.print(); |
| 65 | if (!Oneline) |
| 66 | outs() << '\n'; |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | /// Counts the amount of lines for a given file |
| 71 | static unsigned getLines(StringRef Filepath) { |
| 72 | unsigned Lines = 0; |
| 73 | std::string CurrLine; |
| 74 | std::ifstream FileStream(Filepath); |
| 75 | |
| 76 | while (std::getline(FileStream, CurrLine)) |
| 77 | ++Lines; |
| 78 | |
| 79 | return Lines; |
| 80 | } |
| 81 | |
| 82 | /// Splits Chunks in half and prints them. |
| 83 | /// If unable to split (when chunk size is 1) returns false. |
| 84 | static bool increaseGranularity(std::vector<Chunk> &Chunks) { |
| 85 | outs() << "Increasing granularity..."; |
| 86 | std::vector<Chunk> NewChunks; |
| 87 | bool SplitOne = false; |
| 88 | |
| 89 | for (auto &C : Chunks) { |
| 90 | if (C.end - C.begin == 0) |
| 91 | NewChunks.push_back(C); |
| 92 | else { |
| 93 | unsigned Half = (C.begin + C.end) / 2; |
| 94 | NewChunks.push_back({C.begin, Half}); |
| 95 | NewChunks.push_back({Half + 1, C.end}); |
| 96 | SplitOne = true; |
| 97 | } |
| 98 | } |
| 99 | if (SplitOne) { |
| 100 | Chunks = NewChunks; |
| 101 | outs() << "Success! New Chunks:\n"; |
| 102 | printChunks(Chunks); |
| 103 | } |
| 104 | return SplitOne; |
| 105 | } |
| 106 | |
| 107 | /// Runs the Delta Debugging algorithm, splits the code into chunks and |
| 108 | /// reduces the amount of chunks that are considered interesting by the |
| 109 | /// given test. |
| 110 | void llvm::runDeltaPass( |
| 111 | TestRunner &Test, unsigned Targets, |
| 112 | std::function<void(const std::vector<Chunk> &, Module *)> |
| 113 | ExtractChunksFromModule) { |
| 114 | if (!Targets) { |
| 115 | outs() << "\nNothing to reduce\n"; |
| 116 | return; |
| 117 | } |
Diego Trevino Ferrer | 376f642 | 2019-08-14 20:34:12 +0000 | [diff] [blame^] | 118 | if (!Test.run(Test.getReducedFilepath())) { |
| 119 | outs() << "\nInput isn't interesting! Verify interesting-ness test\n"; |
| 120 | exit(1); |
| 121 | } |
Diego Trevino Ferrer | ddc64eb | 2019-08-08 22:16:33 +0000 | [diff] [blame] | 122 | |
| 123 | std::vector<Chunk> Chunks = {{1, Targets}}; |
| 124 | std::set<Chunk> UninterestingChunks; |
| 125 | std::unique_ptr<Module> ReducedProgram; |
| 126 | |
Diego Trevino Ferrer | ddc64eb | 2019-08-08 22:16:33 +0000 | [diff] [blame] | 127 | if (!increaseGranularity(Chunks)) { |
| 128 | outs() << "\nAlready at minimum size. Cannot reduce anymore.\n"; |
| 129 | return; |
| 130 | } |
| 131 | |
| 132 | do { |
| 133 | UninterestingChunks = {}; |
| 134 | for (int I = Chunks.size() - 1; I >= 0; --I) { |
| 135 | std::vector<Chunk> CurrentChunks; |
| 136 | |
| 137 | for (auto C : Chunks) |
| 138 | if (!UninterestingChunks.count(C) && C != Chunks[I]) |
| 139 | CurrentChunks.push_back(C); |
| 140 | |
| 141 | if (CurrentChunks.empty()) |
| 142 | continue; |
| 143 | |
| 144 | // Clone module before hacking it up.. |
| 145 | std::unique_ptr<Module> Clone = CloneModule(*Test.getProgram()); |
| 146 | // Generate Module with only Targets inside Current Chunks |
| 147 | ExtractChunksFromModule(CurrentChunks, Clone.get()); |
| 148 | // Write Module to tmp file |
| 149 | SmallString<128> CurrentFilepath = |
| 150 | createTmpFile(Clone.get(), Test.getTmpDir()); |
| 151 | |
| 152 | outs() << "Testing with: "; |
| 153 | printChunks(CurrentChunks, /*Oneline=*/true); |
| 154 | outs() << " | " << sys::path::filename(CurrentFilepath); |
| 155 | |
| 156 | // Current Chunks aren't interesting |
| 157 | if (!Test.run(CurrentFilepath)) { |
| 158 | outs() << "\n"; |
| 159 | continue; |
| 160 | } |
| 161 | |
| 162 | UninterestingChunks.insert(Chunks[I]); |
| 163 | Test.setReducedFilepath(CurrentFilepath); |
| 164 | ReducedProgram = std::move(Clone); |
| 165 | outs() << " **** SUCCESS | lines: " << getLines(CurrentFilepath) << "\n"; |
| 166 | } |
| 167 | // Delete uninteresting chunks |
| 168 | erase_if(Chunks, [&UninterestingChunks](const Chunk &C) { |
| 169 | return UninterestingChunks.count(C); |
| 170 | }); |
| 171 | |
| 172 | } while (!UninterestingChunks.empty() || increaseGranularity(Chunks)); |
| 173 | |
| 174 | // If we reduced the testcase replace it |
| 175 | if (ReducedProgram) |
| 176 | Test.setProgram(std::move(ReducedProgram)); |
| 177 | outs() << "Couldn't increase anymore.\n"; |
| 178 | } |