george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 1 | //===- FuzzerMerge.cpp - merging corpora ----------------------------------===// |
| 2 | // |
chandlerc | 4028449 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // Merging corpora. |
| 9 | //===----------------------------------------------------------------------===// |
| 10 | |
morehouse | a80f645 | 2017-12-04 19:25:59 +0000 | [diff] [blame] | 11 | #include "FuzzerCommand.h" |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 12 | #include "FuzzerMerge.h" |
| 13 | #include "FuzzerIO.h" |
| 14 | #include "FuzzerInternal.h" |
| 15 | #include "FuzzerTracePC.h" |
| 16 | #include "FuzzerUtil.h" |
| 17 | |
| 18 | #include <fstream> |
| 19 | #include <iterator> |
| 20 | #include <set> |
| 21 | #include <sstream> |
dor1s | d6908fb | 2019-09-11 14:11:08 +0000 | [diff] [blame] | 22 | #include <unordered_set> |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 23 | |
| 24 | namespace fuzzer { |
| 25 | |
| 26 | bool Merger::Parse(const std::string &Str, bool ParseCoverage) { |
| 27 | std::istringstream SS(Str); |
| 28 | return Parse(SS, ParseCoverage); |
| 29 | } |
| 30 | |
| 31 | void Merger::ParseOrExit(std::istream &IS, bool ParseCoverage) { |
| 32 | if (!Parse(IS, ParseCoverage)) { |
| 33 | Printf("MERGE: failed to parse the control file (unexpected error)\n"); |
| 34 | exit(1); |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | // The control file example: |
| 39 | // |
| 40 | // 3 # The number of inputs |
| 41 | // 1 # The number of inputs in the first corpus, <= the previous number |
| 42 | // file0 |
| 43 | // file1 |
| 44 | // file2 # One file name per line. |
| 45 | // STARTED 0 123 # FileID, file size |
kcc | 001e5f7 | 2019-02-14 23:12:33 +0000 | [diff] [blame] | 46 | // FT 0 1 4 6 8 # FileID COV1 COV2 ... |
| 47 | // COV 0 7 8 9 # FileID COV1 COV1 |
| 48 | // STARTED 1 456 # If FT is missing, the input crashed while processing. |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 49 | // STARTED 2 567 |
kcc | 001e5f7 | 2019-02-14 23:12:33 +0000 | [diff] [blame] | 50 | // FT 2 8 9 |
| 51 | // COV 2 11 12 |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 52 | bool Merger::Parse(std::istream &IS, bool ParseCoverage) { |
| 53 | LastFailure.clear(); |
| 54 | std::string Line; |
| 55 | |
| 56 | // Parse NumFiles. |
| 57 | if (!std::getline(IS, Line, '\n')) return false; |
| 58 | std::istringstream L1(Line); |
| 59 | size_t NumFiles = 0; |
| 60 | L1 >> NumFiles; |
| 61 | if (NumFiles == 0 || NumFiles > 10000000) return false; |
| 62 | |
| 63 | // Parse NumFilesInFirstCorpus. |
| 64 | if (!std::getline(IS, Line, '\n')) return false; |
| 65 | std::istringstream L2(Line); |
| 66 | NumFilesInFirstCorpus = NumFiles + 1; |
| 67 | L2 >> NumFilesInFirstCorpus; |
| 68 | if (NumFilesInFirstCorpus > NumFiles) return false; |
| 69 | |
| 70 | // Parse file names. |
| 71 | Files.resize(NumFiles); |
| 72 | for (size_t i = 0; i < NumFiles; i++) |
| 73 | if (!std::getline(IS, Files[i].Name, '\n')) |
| 74 | return false; |
| 75 | |
kcc | 001e5f7 | 2019-02-14 23:12:33 +0000 | [diff] [blame] | 76 | // Parse STARTED, FT, and COV lines. |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 77 | size_t ExpectedStartMarker = 0; |
| 78 | const size_t kInvalidStartMarker = -1; |
| 79 | size_t LastSeenStartMarker = kInvalidStartMarker; |
george.karpenkov | fbfa45c | 2017-08-27 23:20:09 +0000 | [diff] [blame] | 80 | Vector<uint32_t> TmpFeatures; |
kcc | 98a8624 | 2019-02-15 00:08:16 +0000 | [diff] [blame] | 81 | Set<uint32_t> PCs; |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 82 | while (std::getline(IS, Line, '\n')) { |
| 83 | std::istringstream ISS1(Line); |
| 84 | std::string Marker; |
| 85 | size_t N; |
| 86 | ISS1 >> Marker; |
| 87 | ISS1 >> N; |
| 88 | if (Marker == "STARTED") { |
| 89 | // STARTED FILE_ID FILE_SIZE |
| 90 | if (ExpectedStartMarker != N) |
| 91 | return false; |
| 92 | ISS1 >> Files[ExpectedStartMarker].Size; |
| 93 | LastSeenStartMarker = ExpectedStartMarker; |
| 94 | assert(ExpectedStartMarker < Files.size()); |
| 95 | ExpectedStartMarker++; |
kcc | 001e5f7 | 2019-02-14 23:12:33 +0000 | [diff] [blame] | 96 | } else if (Marker == "FT") { |
| 97 | // FT FILE_ID COV1 COV2 COV3 ... |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 98 | size_t CurrentFileIdx = N; |
| 99 | if (CurrentFileIdx != LastSeenStartMarker) |
| 100 | return false; |
| 101 | LastSeenStartMarker = kInvalidStartMarker; |
| 102 | if (ParseCoverage) { |
| 103 | TmpFeatures.clear(); // use a vector from outer scope to avoid resizes. |
kcc | 9c0ed93 | 2019-02-15 01:22:00 +0000 | [diff] [blame] | 104 | while (ISS1 >> N) |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 105 | TmpFeatures.push_back(N); |
mgrang | 2c1f00d | 2018-03-20 01:17:18 +0000 | [diff] [blame] | 106 | std::sort(TmpFeatures.begin(), TmpFeatures.end()); |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 107 | Files[CurrentFileIdx].Features = TmpFeatures; |
| 108 | } |
kcc | 001e5f7 | 2019-02-14 23:12:33 +0000 | [diff] [blame] | 109 | } else if (Marker == "COV") { |
kcc | 98a8624 | 2019-02-15 00:08:16 +0000 | [diff] [blame] | 110 | size_t CurrentFileIdx = N; |
kcc | 001e5f7 | 2019-02-14 23:12:33 +0000 | [diff] [blame] | 111 | if (ParseCoverage) |
kcc | 9c0ed93 | 2019-02-15 01:22:00 +0000 | [diff] [blame] | 112 | while (ISS1 >> N) |
kcc | 001e5f7 | 2019-02-14 23:12:33 +0000 | [diff] [blame] | 113 | if (PCs.insert(N).second) |
kcc | 98a8624 | 2019-02-15 00:08:16 +0000 | [diff] [blame] | 114 | Files[CurrentFileIdx].Cov.push_back(N); |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 115 | } else { |
| 116 | return false; |
| 117 | } |
| 118 | } |
| 119 | if (LastSeenStartMarker != kInvalidStartMarker) |
| 120 | LastFailure = Files[LastSeenStartMarker].Name; |
| 121 | |
| 122 | FirstNotProcessedFile = ExpectedStartMarker; |
| 123 | return true; |
| 124 | } |
| 125 | |
| 126 | size_t Merger::ApproximateMemoryConsumption() const { |
| 127 | size_t Res = 0; |
| 128 | for (const auto &F: Files) |
| 129 | Res += sizeof(F) + F.Features.size() * sizeof(F.Features[0]); |
| 130 | return Res; |
| 131 | } |
| 132 | |
kcc | 243006d | 2019-02-12 00:12:33 +0000 | [diff] [blame] | 133 | // Decides which files need to be merged (add those to NewFiles). |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 134 | // Returns the number of new features added. |
kcc | 95a78ad | 2019-02-15 00:15:13 +0000 | [diff] [blame] | 135 | size_t Merger::Merge(const Set<uint32_t> &InitialFeatures, |
| 136 | Set<uint32_t> *NewFeatures, |
| 137 | const Set<uint32_t> &InitialCov, Set<uint32_t> *NewCov, |
| 138 | Vector<std::string> *NewFiles) { |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 139 | NewFiles->clear(); |
| 140 | assert(NumFilesInFirstCorpus <= Files.size()); |
kcc | 243006d | 2019-02-12 00:12:33 +0000 | [diff] [blame] | 141 | Set<uint32_t> AllFeatures = InitialFeatures; |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 142 | |
| 143 | // What features are in the initial corpus? |
| 144 | for (size_t i = 0; i < NumFilesInFirstCorpus; i++) { |
| 145 | auto &Cur = Files[i].Features; |
kcc | 243006d | 2019-02-12 00:12:33 +0000 | [diff] [blame] | 146 | AllFeatures.insert(Cur.begin(), Cur.end()); |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 147 | } |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 148 | // Remove all features that we already know from all other inputs. |
| 149 | for (size_t i = NumFilesInFirstCorpus; i < Files.size(); i++) { |
| 150 | auto &Cur = Files[i].Features; |
george.karpenkov | fbfa45c | 2017-08-27 23:20:09 +0000 | [diff] [blame] | 151 | Vector<uint32_t> Tmp; |
kcc | 243006d | 2019-02-12 00:12:33 +0000 | [diff] [blame] | 152 | std::set_difference(Cur.begin(), Cur.end(), AllFeatures.begin(), |
| 153 | AllFeatures.end(), std::inserter(Tmp, Tmp.begin())); |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 154 | Cur.swap(Tmp); |
| 155 | } |
| 156 | |
| 157 | // Sort. Give preference to |
| 158 | // * smaller files |
| 159 | // * files with more features. |
mgrang | 2c1f00d | 2018-03-20 01:17:18 +0000 | [diff] [blame] | 160 | std::sort(Files.begin() + NumFilesInFirstCorpus, Files.end(), |
| 161 | [&](const MergeFileInfo &a, const MergeFileInfo &b) -> bool { |
| 162 | if (a.Size != b.Size) |
| 163 | return a.Size < b.Size; |
| 164 | return a.Features.size() > b.Features.size(); |
| 165 | }); |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 166 | |
| 167 | // One greedy pass: add the file's features to AllFeatures. |
| 168 | // If new features were added, add this file to NewFiles. |
| 169 | for (size_t i = NumFilesInFirstCorpus; i < Files.size(); i++) { |
| 170 | auto &Cur = Files[i].Features; |
| 171 | // Printf("%s -> sz %zd ft %zd\n", Files[i].Name.c_str(), |
| 172 | // Files[i].Size, Cur.size()); |
kcc | 243006d | 2019-02-12 00:12:33 +0000 | [diff] [blame] | 173 | bool FoundNewFeatures = false; |
| 174 | for (auto Fe: Cur) { |
| 175 | if (AllFeatures.insert(Fe).second) { |
| 176 | FoundNewFeatures = true; |
| 177 | NewFeatures->insert(Fe); |
| 178 | } |
| 179 | } |
| 180 | if (FoundNewFeatures) |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 181 | NewFiles->push_back(Files[i].Name); |
kcc | 98a8624 | 2019-02-15 00:08:16 +0000 | [diff] [blame] | 182 | for (auto Cov : Files[i].Cov) |
| 183 | if (InitialCov.find(Cov) == InitialCov.end()) |
| 184 | NewCov->insert(Cov); |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 185 | } |
kcc | 95a78ad | 2019-02-15 00:15:13 +0000 | [diff] [blame] | 186 | return NewFeatures->size(); |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 187 | } |
| 188 | |
george.karpenkov | fbfa45c | 2017-08-27 23:20:09 +0000 | [diff] [blame] | 189 | Set<uint32_t> Merger::AllFeatures() const { |
| 190 | Set<uint32_t> S; |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 191 | for (auto &File : Files) |
| 192 | S.insert(File.Features.begin(), File.Features.end()); |
| 193 | return S; |
| 194 | } |
| 195 | |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 196 | // Inner process. May crash if the target crashes. |
| 197 | void Fuzzer::CrashResistantMergeInternalStep(const std::string &CFPath) { |
| 198 | Printf("MERGE-INNER: using the control file '%s'\n", CFPath.c_str()); |
| 199 | Merger M; |
| 200 | std::ifstream IF(CFPath); |
| 201 | M.ParseOrExit(IF, false); |
| 202 | IF.close(); |
| 203 | if (!M.LastFailure.empty()) |
| 204 | Printf("MERGE-INNER: '%s' caused a failure at the previous merge step\n", |
| 205 | M.LastFailure.c_str()); |
| 206 | |
| 207 | Printf("MERGE-INNER: %zd total files;" |
| 208 | " %zd processed earlier; will process %zd files now\n", |
| 209 | M.Files.size(), M.FirstNotProcessedFile, |
| 210 | M.Files.size() - M.FirstNotProcessedFile); |
| 211 | |
| 212 | std::ofstream OF(CFPath, std::ofstream::out | std::ofstream::app); |
kcc | b966c9e | 2017-09-15 22:02:26 +0000 | [diff] [blame] | 213 | Set<size_t> AllFeatures; |
dor1s | 9431573 | 2019-08-12 20:21:27 +0000 | [diff] [blame] | 214 | auto PrintStatsWrapper = [this, &AllFeatures](const char* Where) { |
| 215 | this->PrintStats(Where, "\n", 0, AllFeatures.size()); |
| 216 | }; |
kcc | 001e5f7 | 2019-02-14 23:12:33 +0000 | [diff] [blame] | 217 | Set<const TracePC::PCTableEntry *> AllPCs; |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 218 | for (size_t i = M.FirstNotProcessedFile; i < M.Files.size(); i++) { |
kcc | a381586 | 2019-02-08 21:27:23 +0000 | [diff] [blame] | 219 | Fuzzer::MaybeExitGracefully(); |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 220 | auto U = FileToVector(M.Files[i].Name); |
| 221 | if (U.size() > MaxInputLen) { |
| 222 | U.resize(MaxInputLen); |
| 223 | U.shrink_to_fit(); |
| 224 | } |
dor1s | d6908fb | 2019-09-11 14:11:08 +0000 | [diff] [blame] | 225 | |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 226 | // Write the pre-run marker. |
kcc | 9c0ed93 | 2019-02-15 01:22:00 +0000 | [diff] [blame] | 227 | OF << "STARTED " << i << " " << U.size() << "\n"; |
morehouse | a80f645 | 2017-12-04 19:25:59 +0000 | [diff] [blame] | 228 | OF.flush(); // Flush is important since Command::Execute may crash. |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 229 | // Run. |
| 230 | TPC.ResetMaps(); |
| 231 | ExecuteCallback(U.data(), U.size()); |
kcc | b966c9e | 2017-09-15 22:02:26 +0000 | [diff] [blame] | 232 | // Collect coverage. We are iterating over the files in this order: |
| 233 | // * First, files in the initial corpus ordered by size, smallest first. |
| 234 | // * Then, all other files, smallest first. |
| 235 | // So it makes no sense to record all features for all files, instead we |
| 236 | // only record features that were not seen before. |
| 237 | Set<size_t> UniqFeatures; |
kcc | c924e38 | 2017-09-15 22:10:36 +0000 | [diff] [blame] | 238 | TPC.CollectFeatures([&](size_t Feature) { |
kcc | b966c9e | 2017-09-15 22:02:26 +0000 | [diff] [blame] | 239 | if (AllFeatures.insert(Feature).second) |
| 240 | UniqFeatures.insert(Feature); |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 241 | }); |
kcc | 001e5f7 | 2019-02-14 23:12:33 +0000 | [diff] [blame] | 242 | TPC.UpdateObservedPCs(); |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 243 | // Show stats. |
| 244 | if (!(TotalNumberOfRuns & (TotalNumberOfRuns - 1))) |
dor1s | 9431573 | 2019-08-12 20:21:27 +0000 | [diff] [blame] | 245 | PrintStatsWrapper("pulse "); |
dor1s | 544be6a | 2019-08-09 18:20:53 +0000 | [diff] [blame] | 246 | if (TotalNumberOfRuns == M.NumFilesInFirstCorpus) |
dor1s | 9431573 | 2019-08-12 20:21:27 +0000 | [diff] [blame] | 247 | PrintStatsWrapper("LOADED"); |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 248 | // Write the post-run marker and the coverage. |
kcc | 001e5f7 | 2019-02-14 23:12:33 +0000 | [diff] [blame] | 249 | OF << "FT " << i; |
kcc | b966c9e | 2017-09-15 22:02:26 +0000 | [diff] [blame] | 250 | for (size_t F : UniqFeatures) |
kcc | 9c0ed93 | 2019-02-15 01:22:00 +0000 | [diff] [blame] | 251 | OF << " " << F; |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 252 | OF << "\n"; |
kcc | 9c0ed93 | 2019-02-15 01:22:00 +0000 | [diff] [blame] | 253 | OF << "COV " << i; |
kcc | 001e5f7 | 2019-02-14 23:12:33 +0000 | [diff] [blame] | 254 | TPC.ForEachObservedPC([&](const TracePC::PCTableEntry *TE) { |
| 255 | if (AllPCs.insert(TE).second) |
| 256 | OF << " " << TPC.PCTableEntryIdx(TE); |
| 257 | }); |
| 258 | OF << "\n"; |
kcc | a00e807 | 2017-11-09 21:30:33 +0000 | [diff] [blame] | 259 | OF.flush(); |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 260 | } |
dor1s | 9431573 | 2019-08-12 20:21:27 +0000 | [diff] [blame] | 261 | PrintStatsWrapper("DONE "); |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 262 | } |
| 263 | |
dor1s | d6908fb | 2019-09-11 14:11:08 +0000 | [diff] [blame] | 264 | static size_t WriteNewControlFile(const std::string &CFPath, |
| 265 | const Vector<SizedFile> &OldCorpus, |
| 266 | const Vector<SizedFile> &NewCorpus, |
| 267 | const Vector<MergeFileInfo> &KnownFiles) { |
| 268 | std::unordered_set<std::string> FilesToSkip; |
| 269 | for (auto &SF: KnownFiles) |
| 270 | FilesToSkip.insert(SF.Name); |
| 271 | |
| 272 | Vector<std::string> FilesToUse; |
| 273 | auto MaybeUseFile = [=, &FilesToUse](std::string Name) { |
| 274 | if (FilesToSkip.find(Name) == FilesToSkip.end()) |
| 275 | FilesToUse.push_back(Name); |
| 276 | }; |
| 277 | for (auto &SF: OldCorpus) |
| 278 | MaybeUseFile(SF.File); |
| 279 | auto FilesToUseFromOldCorpus = FilesToUse.size(); |
| 280 | for (auto &SF: NewCorpus) |
| 281 | MaybeUseFile(SF.File); |
| 282 | |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 283 | RemoveFile(CFPath); |
| 284 | std::ofstream ControlFile(CFPath); |
dor1s | d6908fb | 2019-09-11 14:11:08 +0000 | [diff] [blame] | 285 | ControlFile << FilesToUse.size() << "\n"; |
| 286 | ControlFile << FilesToUseFromOldCorpus << "\n"; |
| 287 | for (auto &FN: FilesToUse) |
| 288 | ControlFile << FN << "\n"; |
| 289 | |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 290 | if (!ControlFile) { |
| 291 | Printf("MERGE-OUTER: failed to write to the control file: %s\n", |
| 292 | CFPath.c_str()); |
| 293 | exit(1); |
| 294 | } |
dor1s | d6908fb | 2019-09-11 14:11:08 +0000 | [diff] [blame] | 295 | |
| 296 | return FilesToUse.size(); |
kcc | 99a71a1 | 2017-11-09 05:49:28 +0000 | [diff] [blame] | 297 | } |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 298 | |
kcc | d1449be | 2019-02-08 22:59:03 +0000 | [diff] [blame] | 299 | // Outer process. Does not call the target code and thus should not fail. |
kcc | 4b5aa12 | 2019-02-09 00:16:21 +0000 | [diff] [blame] | 300 | void CrashResistantMerge(const Vector<std::string> &Args, |
kcc | bfb5975 | 2019-02-12 03:12:40 +0000 | [diff] [blame] | 301 | const Vector<SizedFile> &OldCorpus, |
| 302 | const Vector<SizedFile> &NewCorpus, |
| 303 | Vector<std::string> *NewFiles, |
| 304 | const Set<uint32_t> &InitialFeatures, |
kcc | 98a8624 | 2019-02-15 00:08:16 +0000 | [diff] [blame] | 305 | Set<uint32_t> *NewFeatures, |
| 306 | const Set<uint32_t> &InitialCov, |
| 307 | Set<uint32_t> *NewCov, |
| 308 | const std::string &CFPath, |
kcc | bfb5975 | 2019-02-12 03:12:40 +0000 | [diff] [blame] | 309 | bool V /*Verbose*/) { |
kcc | 64bcb92 | 2019-02-13 04:04:45 +0000 | [diff] [blame] | 310 | if (NewCorpus.empty() && OldCorpus.empty()) return; // Nothing to merge. |
kcc | 99a71a1 | 2017-11-09 05:49:28 +0000 | [diff] [blame] | 311 | size_t NumAttempts = 0; |
dor1s | d6908fb | 2019-09-11 14:11:08 +0000 | [diff] [blame] | 312 | Vector<MergeFileInfo> KnownFiles; |
kcc | a381586 | 2019-02-08 21:27:23 +0000 | [diff] [blame] | 313 | if (FileSize(CFPath)) { |
kcc | bfb5975 | 2019-02-12 03:12:40 +0000 | [diff] [blame] | 314 | VPrintf(V, "MERGE-OUTER: non-empty control file provided: '%s'\n", |
kcc | a381586 | 2019-02-08 21:27:23 +0000 | [diff] [blame] | 315 | CFPath.c_str()); |
kcc | 99a71a1 | 2017-11-09 05:49:28 +0000 | [diff] [blame] | 316 | Merger M; |
kcc | a381586 | 2019-02-08 21:27:23 +0000 | [diff] [blame] | 317 | std::ifstream IF(CFPath); |
dor1s | d6908fb | 2019-09-11 14:11:08 +0000 | [diff] [blame] | 318 | if (M.Parse(IF, /*ParseCoverage=*/true)) { |
kcc | bfb5975 | 2019-02-12 03:12:40 +0000 | [diff] [blame] | 319 | VPrintf(V, "MERGE-OUTER: control file ok, %zd files total," |
kcc | 99a71a1 | 2017-11-09 05:49:28 +0000 | [diff] [blame] | 320 | " first not processed file %zd\n", |
| 321 | M.Files.size(), M.FirstNotProcessedFile); |
| 322 | if (!M.LastFailure.empty()) |
kcc | bfb5975 | 2019-02-12 03:12:40 +0000 | [diff] [blame] | 323 | VPrintf(V, "MERGE-OUTER: '%s' will be skipped as unlucky " |
kcc | 99a71a1 | 2017-11-09 05:49:28 +0000 | [diff] [blame] | 324 | "(merge has stumbled on it the last time)\n", |
| 325 | M.LastFailure.c_str()); |
| 326 | if (M.FirstNotProcessedFile >= M.Files.size()) { |
dor1s | d6908fb | 2019-09-11 14:11:08 +0000 | [diff] [blame] | 327 | // Merge has already been completed with the given merge control file. |
| 328 | if (M.Files.size() == OldCorpus.size() + NewCorpus.size()) { |
| 329 | VPrintf( |
| 330 | V, |
| 331 | "MERGE-OUTER: nothing to do, merge has been completed before\n"); |
| 332 | exit(0); |
| 333 | } |
kcc | 99a71a1 | 2017-11-09 05:49:28 +0000 | [diff] [blame] | 334 | |
dor1s | d6908fb | 2019-09-11 14:11:08 +0000 | [diff] [blame] | 335 | // Number of input files likely changed, start merge from scratch, but |
| 336 | // reuse coverage information from the given merge control file. |
| 337 | VPrintf( |
| 338 | V, |
| 339 | "MERGE-OUTER: starting merge from scratch, but reusing coverage " |
| 340 | "information from the given control file\n"); |
| 341 | KnownFiles = M.Files; |
| 342 | } else { |
| 343 | // There is a merge in progress, continue. |
| 344 | NumAttempts = M.Files.size() - M.FirstNotProcessedFile; |
| 345 | } |
kcc | 99a71a1 | 2017-11-09 05:49:28 +0000 | [diff] [blame] | 346 | } else { |
kcc | bfb5975 | 2019-02-12 03:12:40 +0000 | [diff] [blame] | 347 | VPrintf(V, "MERGE-OUTER: bad control file, will overwrite it\n"); |
kcc | 99a71a1 | 2017-11-09 05:49:28 +0000 | [diff] [blame] | 348 | } |
| 349 | } |
| 350 | |
| 351 | if (!NumAttempts) { |
| 352 | // The supplied control file is empty or bad, create a fresh one. |
dor1s | d6908fb | 2019-09-11 14:11:08 +0000 | [diff] [blame] | 353 | VPrintf(V, "MERGE-OUTER: " |
| 354 | "%zd files, %zd in the initial corpus, %zd processed earlier\n", |
| 355 | OldCorpus.size() + NewCorpus.size(), OldCorpus.size(), |
| 356 | KnownFiles.size()); |
| 357 | NumAttempts = WriteNewControlFile(CFPath, OldCorpus, NewCorpus, KnownFiles); |
kcc | 99a71a1 | 2017-11-09 05:49:28 +0000 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | // Execute the inner process until it passes. |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 361 | // Every inner process should execute at least one input. |
morehouse | a80f645 | 2017-12-04 19:25:59 +0000 | [diff] [blame] | 362 | Command BaseCmd(Args); |
| 363 | BaseCmd.removeFlag("merge"); |
kcc | a381586 | 2019-02-08 21:27:23 +0000 | [diff] [blame] | 364 | BaseCmd.removeFlag("fork"); |
kcc | d701d9e | 2019-05-23 00:22:46 +0000 | [diff] [blame] | 365 | BaseCmd.removeFlag("collect_data_flow"); |
kcc | 99a71a1 | 2017-11-09 05:49:28 +0000 | [diff] [blame] | 366 | for (size_t Attempt = 1; Attempt <= NumAttempts; Attempt++) { |
kcc | a381586 | 2019-02-08 21:27:23 +0000 | [diff] [blame] | 367 | Fuzzer::MaybeExitGracefully(); |
kcc | bfb5975 | 2019-02-12 03:12:40 +0000 | [diff] [blame] | 368 | VPrintf(V, "MERGE-OUTER: attempt %zd\n", Attempt); |
morehouse | a80f645 | 2017-12-04 19:25:59 +0000 | [diff] [blame] | 369 | Command Cmd(BaseCmd); |
| 370 | Cmd.addFlag("merge_control_file", CFPath); |
| 371 | Cmd.addFlag("merge_inner", "1"); |
kcc | bfb5975 | 2019-02-12 03:12:40 +0000 | [diff] [blame] | 372 | if (!V) { |
metzman | e847d8a | 2019-02-27 19:27:16 +0000 | [diff] [blame] | 373 | Cmd.setOutputFile(getDevNull()); |
kcc | bfb5975 | 2019-02-12 03:12:40 +0000 | [diff] [blame] | 374 | Cmd.combineOutAndErr(); |
| 375 | } |
morehouse | a80f645 | 2017-12-04 19:25:59 +0000 | [diff] [blame] | 376 | auto ExitCode = ExecuteCommand(Cmd); |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 377 | if (!ExitCode) { |
kcc | bfb5975 | 2019-02-12 03:12:40 +0000 | [diff] [blame] | 378 | VPrintf(V, "MERGE-OUTER: succesfull in %zd attempt(s)\n", Attempt); |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 379 | break; |
| 380 | } |
| 381 | } |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 382 | // Read the control file and do the merge. |
| 383 | Merger M; |
| 384 | std::ifstream IF(CFPath); |
| 385 | IF.seekg(0, IF.end); |
kcc | bfb5975 | 2019-02-12 03:12:40 +0000 | [diff] [blame] | 386 | VPrintf(V, "MERGE-OUTER: the control file has %zd bytes\n", |
| 387 | (size_t)IF.tellg()); |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 388 | IF.seekg(0, IF.beg); |
| 389 | M.ParseOrExit(IF, true); |
| 390 | IF.close(); |
kcc | bfb5975 | 2019-02-12 03:12:40 +0000 | [diff] [blame] | 391 | VPrintf(V, |
| 392 | "MERGE-OUTER: consumed %zdMb (%zdMb rss) to parse the control file\n", |
| 393 | M.ApproximateMemoryConsumption() >> 20, GetPeakRSSMb()); |
dor1s | d6908fb | 2019-09-11 14:11:08 +0000 | [diff] [blame] | 394 | |
| 395 | M.Files.insert(M.Files.end(), KnownFiles.begin(), KnownFiles.end()); |
kcc | 98a8624 | 2019-02-15 00:08:16 +0000 | [diff] [blame] | 396 | M.Merge(InitialFeatures, NewFeatures, InitialCov, NewCov, NewFiles); |
| 397 | VPrintf(V, "MERGE-OUTER: %zd new files with %zd new features added; " |
| 398 | "%zd new coverage edges\n", |
| 399 | NewFiles->size(), NewFeatures->size(), NewCov->size()); |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 400 | } |
| 401 | |
| 402 | } // namespace fuzzer |