george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 1 | //===- FuzzerMerge.h - merging corpa ----------------------------*- C++ -* ===// |
| 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 | // The task: |
| 11 | // Take the existing corpus (possibly empty) and merge new inputs into |
| 12 | // it so that only inputs with new coverage ('features') are added. |
| 13 | // The process should tolerate the crashes, OOMs, leaks, etc. |
| 14 | // |
| 15 | // Algorithm: |
| 16 | // The outter process collects the set of files and writes their names |
| 17 | // into a temporary "control" file, then repeatedly launches the inner |
| 18 | // process until all inputs are processed. |
| 19 | // The outer process does not actually execute the target code. |
| 20 | // |
| 21 | // The inner process reads the control file and sees a) list of all the inputs |
| 22 | // and b) the last processed input. Then it starts processing the inputs one |
| 23 | // by one. Before processing every input it writes one line to control file: |
| 24 | // STARTED INPUT_ID INPUT_SIZE |
| 25 | // After processing an input it write another line: |
| 26 | // DONE INPUT_ID Feature1 Feature2 Feature3 ... |
| 27 | // If a crash happens while processing an input the last line in the control |
| 28 | // file will be "STARTED INPUT_ID" and so the next process will know |
| 29 | // where to resume. |
| 30 | // |
| 31 | // Once all inputs are processed by the innner process(es) the outer process |
| 32 | // reads the control files and does the merge based entirely on the contents |
| 33 | // of control file. |
| 34 | // It uses a single pass greedy algorithm choosing first the smallest inputs |
| 35 | // within the same size the inputs that have more new features. |
| 36 | // |
| 37 | //===----------------------------------------------------------------------===// |
| 38 | |
| 39 | #ifndef LLVM_FUZZER_MERGE_H |
| 40 | #define LLVM_FUZZER_MERGE_H |
| 41 | |
| 42 | #include "FuzzerDefs.h" |
| 43 | |
| 44 | #include <istream> |
| 45 | #include <ostream> |
| 46 | #include <set> |
| 47 | #include <vector> |
| 48 | |
| 49 | namespace fuzzer { |
| 50 | |
| 51 | struct MergeFileInfo { |
| 52 | std::string Name; |
| 53 | size_t Size = 0; |
george.karpenkov | fbfa45c | 2017-08-27 23:20:09 +0000 | [diff] [blame] | 54 | Vector<uint32_t> Features; |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 55 | }; |
| 56 | |
| 57 | struct Merger { |
george.karpenkov | fbfa45c | 2017-08-27 23:20:09 +0000 | [diff] [blame] | 58 | Vector<MergeFileInfo> Files; |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 59 | size_t NumFilesInFirstCorpus = 0; |
| 60 | size_t FirstNotProcessedFile = 0; |
| 61 | std::string LastFailure; |
| 62 | |
| 63 | bool Parse(std::istream &IS, bool ParseCoverage); |
| 64 | bool Parse(const std::string &Str, bool ParseCoverage); |
| 65 | void ParseOrExit(std::istream &IS, bool ParseCoverage); |
george.karpenkov | fbfa45c | 2017-08-27 23:20:09 +0000 | [diff] [blame] | 66 | size_t Merge(const Set<uint32_t> &InitialFeatures, |
kcc | 4b5aa12 | 2019-02-09 00:16:21 +0000 | [diff] [blame] | 67 | Set<uint32_t> *NewFeatures, |
george.karpenkov | fbfa45c | 2017-08-27 23:20:09 +0000 | [diff] [blame] | 68 | Vector<std::string> *NewFiles); |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 69 | size_t ApproximateMemoryConsumption() const; |
george.karpenkov | fbfa45c | 2017-08-27 23:20:09 +0000 | [diff] [blame] | 70 | Set<uint32_t> AllFeatures() const; |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 71 | }; |
| 72 | |
kcc | 4b5aa12 | 2019-02-09 00:16:21 +0000 | [diff] [blame] | 73 | void CrashResistantMerge(const Vector<std::string> &Args, |
| 74 | const Vector<SizedFile> &OldCorpus, |
| 75 | const Vector<SizedFile> &NewCorpus, |
| 76 | Vector<std::string> *NewFiles, |
| 77 | const Set<uint32_t> &InitialFeatures, |
| 78 | Set<uint32_t> *NewFeatures, |
kcc | bfb5975 | 2019-02-12 03:12:40 +0000 | [diff] [blame] | 79 | const std::string &CFPath, |
| 80 | bool Verbose); |
kcc | a381586 | 2019-02-08 21:27:23 +0000 | [diff] [blame] | 81 | |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 82 | } // namespace fuzzer |
| 83 | |
| 84 | #endif // LLVM_FUZZER_MERGE_H |