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