OLD | NEW |
(Empty) | |
| 1 //===- NaClFuzz.h - Fuzz PNaCl bitcode records ------------------*- C++ -*-===// |
| 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 // This file defines a basic fuzzer for a list of PNaCl bitcode records. |
| 11 // |
| 12 // *** WARNING *** The implementation of the fuzzer uses a random |
| 13 // number generator. As a result, this code is not thread safe. |
| 14 // |
| 15 //===----------------------------------------------------------------------===// |
| 16 |
| 17 #ifndef LLVM_BITCODE_NACL_NACLFUZZ_H |
| 18 #define LLVM_BITCODE_NACL_NACLFUZZ_H |
| 19 |
| 20 #include "llvm/ADT/SmallVector.h" |
| 21 #include "llvm/Bitcode/NaCl/NaClBitcodeMungeUtils.h" |
| 22 #include "llvm/Bitcode/NaCl/NaClRandNumGen.h" |
| 23 |
| 24 #include <random> |
| 25 |
| 26 namespace naclfuzz { |
| 27 |
| 28 using namespace llvm; |
| 29 |
| 30 /// \brief Fuzzes a list of editable bitcode records. |
| 31 class RecordFuzzer { |
| 32 RecordFuzzer(const RecordFuzzer&) = delete; |
| 33 void operator=(const RecordFuzzer&) = delete; |
| 34 public: |
| 35 typedef NaClMungedBitcode::iterator iterator; |
| 36 |
| 37 /// \brief The set of possible fuzzing actions. |
| 38 enum EditAction { |
| 39 /// \brief Inserts a new record into the list of bitcode records. |
| 40 InsertRecord, |
| 41 /// \brief Mutate contents of an existing bitcode record. |
| 42 MutateRecord, |
| 43 /// \brief Removes an existing record from the list of bitcode |
| 44 /// records. |
| 45 RemoveRecord, |
| 46 /// \brief Replaces an existing record with a new bitcode record. |
| 47 ReplaceRecord, |
| 48 /// \brief Swaps two records in the bitcode record list. |
| 49 SwapRecord |
| 50 }; |
| 51 |
| 52 virtual ~RecordFuzzer(); |
| 53 |
| 54 /// \brief Generates a random mutation of the bitcode, using the |
| 55 /// provided random number generator. Percentage (a value between 0 |
| 56 /// and 1 defined by Count/Base) is used to define the number of |
| 57 /// fuzzing actions applied to the bitcode. Returns true if fuzzing |
| 58 /// succeeded. |
| 59 /// |
| 60 /// May be called an arbitrary number of times. Results are left in |
| 61 /// the munged bitcode records passed into static method |
| 62 /// createSimpleRecordFuzzer. |
| 63 virtual bool fuzz(unsigned Count, unsigned Base=100) = 0; |
| 64 |
| 65 /// \brief Shows how many times each record was edited in the |
| 66 /// corresponding (input) bitcode, over all calls to fuzz. |
| 67 virtual void showRecordDistribution(raw_ostream &Out) const = 0; |
| 68 |
| 69 /// \brief Shows how many times each type of edit action was applied |
| 70 /// to the corresponding bitcode, over all calls to fuzz. |
| 71 virtual void showEditDistribution(raw_ostream &Out) const = 0; |
| 72 |
| 73 // Creates an instance of a fuzzer for the given bitcode. |
| 74 static RecordFuzzer |
| 75 *createSimpleRecordFuzzer(NaClMungedBitcode &Bitcode, |
| 76 RandomNumberGenerator &RandGenerator); |
| 77 |
| 78 /// Returns printable name for the edit action. |
| 79 static const char *actionName(EditAction Action); |
| 80 |
| 81 protected: |
| 82 RecordFuzzer(NaClMungedBitcode &Bitcode, RandomNumberGenerator &Generator); |
| 83 |
| 84 // Holds the bitcode being munged. |
| 85 NaClMungedBitcode &Bitcode; |
| 86 |
| 87 // Hold the random number generator. |
| 88 RandomNumberGenerator &Generator; |
| 89 |
| 90 // Erases the last fuzzing result from the munged bitcode records |
| 91 // in Bitcode. |
| 92 virtual void clear(); |
| 93 }; |
| 94 |
| 95 } // end of namespace naclfuzz |
| 96 |
| 97 #endif // LLVM_BITCODE_NACL_NACLFUZZ_H |
OLD | NEW |