Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 //===- NaClBitcodeMunge.h - Bitcode Munger ----------------------*- C++ -*-===// | 1 //===- NaClBitcodeMunge.h - Bitcode Munger ----------------------*- C++ -*-===// |
| 2 // | 2 // |
| 3 // The LLVM Compiler Infrastructure | 3 // The LLVM Compiler Infrastructure |
| 4 // | 4 // |
| 5 // This file is distributed under the University of Illinois Open Source | 5 // This file is distributed under the University of Illinois Open Source |
| 6 // License. See LICENSE.TXT for details. | 6 // License. See LICENSE.TXT for details. |
| 7 // | 7 // |
| 8 //===----------------------------------------------------------------------===// | 8 //===----------------------------------------------------------------------===// |
| 9 // | 9 // |
| 10 // Test harness for generating a PNaCl bitcode memory buffer from | 10 // Test harness for generating a PNaCl bitcode memory buffer from |
| 11 // an array, and parse/objdump/compress the resulting contents. | 11 // an array, and parse/objdump/compress the resulting contents. |
| 12 // | 12 // |
| 13 // Generates a bitcode memory buffer from an array containing 1 or | 13 // Generates a bitcode memory buffer from an array containing 1 or |
| 14 // more PNaCl records. Used to test errors in PNaCl bitcode. | 14 // more PNaCl records. Used to test errors in PNaCl bitcode. |
| 15 // | 15 // |
| 16 // Bitcode records are modeleled using arrays using the format | 16 // Bitcode records are modeled using arrays using the format |
| 17 // specified in NaClBitcodeMungeUtils.h. | 17 // specified in NaClBitcodeMungeUtils.h. |
| 18 // | 18 // |
| 19 // Note: Since the header record doesn't have any abbreviation indices | 19 // Note: Since the header record doesn't have any abbreviation indices |
| 20 // associated with it, one can use any value. The value will simply be | 20 // associated with it, one can use any value. The value will simply be |
| 21 // ignored. | 21 // ignored. |
| 22 // | 22 // |
| 23 // In addition to specifying the sequence of records, one can also | 23 // In addition to specifying the sequence of records, one can also |
| 24 // define a sequence of edits to be applied to the original sequence | 24 // define a sequence of edits to be applied to the original sequence |
| 25 // of records. This allows the same record sequence to be used in | 25 // of records. This allows the same record sequence to be used in |
| 26 // multiple tests. Again, see NaClBitcodeMungeUtils.h for the | 26 // multiple tests. Again, see NaClBitcodeMungeUtils.h for the |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 40 | 40 |
| 41 #include "llvm/ADT/SmallVector.h" | 41 #include "llvm/ADT/SmallVector.h" |
| 42 #include "llvm/Bitcode/NaCl/NaClBitcodeMungeUtils.h" | 42 #include "llvm/Bitcode/NaCl/NaClBitcodeMungeUtils.h" |
| 43 #include "llvm/Support/MemoryBuffer.h" | 43 #include "llvm/Support/MemoryBuffer.h" |
| 44 #include "llvm/Support/raw_ostream.h" | 44 #include "llvm/Support/raw_ostream.h" |
| 45 | 45 |
| 46 #include <string> | 46 #include <string> |
| 47 | 47 |
| 48 namespace llvm { | 48 namespace llvm { |
| 49 | 49 |
| 50 class NaClBitstreamWriter; | |
| 51 class NaClBitCodeAbbrev; | 50 class NaClBitCodeAbbrev; |
| 52 | 51 |
| 53 /// Base class to run tests on munged bitcode files. | 52 /// Base class to run tests on munged bitcode files. |
| 54 class NaClBitcodeMunger { | 53 class NaClBitcodeMunger { |
| 55 public: | 54 public: |
| 56 // TODO(kschimpf) Replace uses in subzero with corresponding | 55 // TODO(kschimpf) Replace uses in subzero with corresponding |
| 57 // NaClMungedBitcode::EditAction values, so that following constants | 56 // NaClMungedBitcode::EditAction values, so that following constants |
| 58 // can be removed. | 57 // can be removed. |
| 59 | 58 |
| 60 /// The types of editing actions that can be applied. | 59 /// The types of editing actions that can be applied. |
| 61 typedef NaClMungedBitcode::EditAction EditAction; | 60 typedef NaClMungedBitcode::EditAction EditAction; |
| 62 static const EditAction AddBefore = NaClMungedBitcode::AddBefore; | 61 static const EditAction AddBefore = NaClMungedBitcode::AddBefore; |
| 63 static const EditAction AddAfter = NaClMungedBitcode::AddAfter; | 62 static const EditAction AddAfter = NaClMungedBitcode::AddAfter; |
| 64 static const EditAction Remove = NaClMungedBitcode::Remove; | 63 static const EditAction Remove = NaClMungedBitcode::Remove; |
| 65 static const EditAction Replace = NaClMungedBitcode::Replace; | 64 static const EditAction Replace = NaClMungedBitcode::Replace; |
| 66 | 65 |
| 66 /// The write flags to use when writing bitcode. | |
| 67 NaClMungedBitcode::WriteFlags WriteFlags; | |
|
jvoung (off chromium)
2015/05/06 22:08:09
nit: Seems a bit weird for this field to be public
Karl
2015/05/07 20:09:18
Moved to protected and added methods to set. Made
| |
| 68 | |
| 67 /// Creates a bitcode munger, based on the given array of values. | 69 /// Creates a bitcode munger, based on the given array of values. |
| 68 NaClBitcodeMunger(const uint64_t Records[], size_t RecordsSize, | 70 NaClBitcodeMunger(const uint64_t Records[], size_t RecordsSize, |
| 69 uint64_t RecordTerminator) | 71 uint64_t RecordTerminator) |
| 70 : MungedBitcode(Records, RecordsSize, RecordTerminator), | 72 : MungedBitcode(Records, RecordsSize, RecordTerminator), |
| 71 RecordTerminator(RecordTerminator), WriteBlockID(-1), SetBID(-1), | 73 RecordTerminator(RecordTerminator), |
| 72 DumpResults("Error: No previous dump results!\n"), | 74 DumpResults("Error: No previous dump results!\n"), |
| 73 DumpStream(nullptr), Writer(nullptr), FoundErrors(false), | 75 DumpStream(nullptr), FoundErrors(false) {} |
| 74 FatalBuffer(), FatalStream(FatalBuffer) {} | |
| 75 | 76 |
| 76 /// Creates MungedInput and DumpStream for running tests, based on | 77 /// Creates MungedInput and DumpStream for running tests, based on |
| 77 /// given Munges. | 78 /// given Munges. |
| 78 void setupTest( | 79 void setupTest( |
| 79 const char *TestName, const uint64_t Munges[], size_t MungesSize, | 80 const char *TestName, const uint64_t Munges[], size_t MungesSize, |
| 80 bool AddHeader); | 81 bool AddHeader); |
| 81 | 82 |
| 82 /// Cleans up state after a test. | 83 /// Cleans up state after a test. |
| 83 void cleanupTest(); | 84 void cleanupTest(); |
| 84 | 85 |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 97 /// getTestResults(). | 98 /// getTestResults(). |
| 98 std::string getLinesWithPrefix(const std::string &Prefix) const { | 99 std::string getLinesWithPrefix(const std::string &Prefix) const { |
| 99 return getLinesWithTextMatch(Prefix, true); | 100 return getLinesWithTextMatch(Prefix, true); |
| 100 } | 101 } |
| 101 | 102 |
| 102 protected: | 103 protected: |
| 103 // The bitcode records being munged. | 104 // The bitcode records being munged. |
| 104 NaClMungedBitcode MungedBitcode; | 105 NaClMungedBitcode MungedBitcode; |
| 105 // The value used as record terminator. | 106 // The value used as record terminator. |
| 106 uint64_t RecordTerminator; | 107 uint64_t RecordTerminator; |
| 107 // The block ID associated with the block being written. | |
| 108 int WriteBlockID; | |
| 109 // The SetBID for the blockinfo block. | |
| 110 int SetBID; | |
| 111 // The results buffer of the last dump. | 108 // The results buffer of the last dump. |
| 112 std::string DumpResults; | 109 std::string DumpResults; |
| 113 // The memory buffer containing the munged input. | 110 // The memory buffer containing the munged input. |
| 114 std::unique_ptr<MemoryBuffer> MungedInput; | 111 std::unique_ptr<MemoryBuffer> MungedInput; |
| 115 // The stream containing errors and the objdump of the generated bitcode file. | 112 // The stream containing errors and the objdump of the generated bitcode file. |
| 116 raw_ostream *DumpStream; | 113 raw_ostream *DumpStream; |
| 117 // The bitstream writer to use to generate the bitcode file. | |
| 118 NaClBitstreamWriter *Writer; | |
| 119 // True if any errors were reported. | 114 // True if any errors were reported. |
| 120 bool FoundErrors; | 115 bool FoundErrors; |
| 121 // The buffer to hold the generated fatal message. | |
| 122 std::string FatalBuffer; | |
| 123 // The stream to write the fatal message to. | |
| 124 raw_string_ostream FatalStream; | |
| 125 // The stack of maximum abbreviation indices allowed by block enter record. | |
| 126 SmallVector<uint64_t, 3> AbbrevIndexLimitStack; | |
| 127 // The buffer for the contents of the munged input. | 116 // The buffer for the contents of the munged input. |
| 128 SmallVector<char, 1024> MungedInputBuffer; | 117 SmallVector<char, 1024> MungedInputBuffer; |
| 129 | 118 |
| 130 // Records that an error occurred, and returns stream to print error | 119 // Records that an error occurred, and returns stream to print error |
| 131 // message to. | 120 // message to. |
| 132 raw_ostream &Error() { | 121 raw_ostream &Error() { |
| 133 FoundErrors = true; | 122 FoundErrors = true; |
| 134 return *DumpStream << "Error: "; | 123 return *DumpStream << "Error: "; |
| 135 } | 124 } |
| 136 | 125 |
| 137 // Returns stream to print fatal error message to. | |
| 138 // Note: Once the fatal error message has been dumped to the stream, | |
| 139 // one must call ReportFatalError to display the error and terminate | |
| 140 // execution. | |
| 141 raw_ostream &Fatal() { | |
| 142 return FatalStream << "Fatal: "; | |
| 143 } | |
| 144 | |
| 145 // Displays the fatal error message and exits the program. | |
| 146 void ReportFatalError() { | |
| 147 report_fatal_error(FatalStream.str()); | |
| 148 } | |
| 149 | |
| 150 // Returns the lines containing the given Substring, from the string | 126 // Returns the lines containing the given Substring, from the string |
| 151 // getTestResults(). If MustBePrefix, then Substring must match at | 127 // getTestResults(). If MustBePrefix, then Substring must match at |
| 152 // the beginning of the line. | 128 // the beginning of the line. |
| 153 std::string getLinesWithTextMatch(const std::string &Substring, | 129 std::string getLinesWithTextMatch(const std::string &Substring, |
| 154 bool MustBePrefix = false) const; | 130 bool MustBePrefix = false) const; |
| 155 | |
| 156 // Writes out munged bitcode records. | |
| 157 void writeMungedBitcode(const NaClMungedBitcode &Bitcode, bool AddHeader); | |
| 158 | |
| 159 // Emits the given record to the bitcode file. | |
| 160 void emitRecord(const NaClBitcodeAbbrevRecord &Record); | |
| 161 | |
| 162 // Converts the abbreviation record to the corresponding abbreviation. | |
| 163 NaClBitCodeAbbrev *buildAbbrev(const NaClBitcodeAbbrevRecord &Record); | |
| 164 }; | 131 }; |
| 165 | 132 |
| 166 /// Class to run tests for function llvm::NaClObjDump. | 133 /// Class to run tests for function llvm::NaClObjDump. |
| 167 class NaClObjDumpMunger : public NaClBitcodeMunger { | 134 class NaClObjDumpMunger : public NaClBitcodeMunger { |
| 168 public: | 135 public: |
| 169 | 136 |
| 170 /// Creates a bitcode munger, based on the given array of values. | 137 /// Creates a bitcode munger, based on the given array of values. |
| 171 NaClObjDumpMunger(const uint64_t Records[], size_t RecordsSize, | 138 NaClObjDumpMunger(const uint64_t Records[], size_t RecordsSize, |
| 172 uint64_t RecordTerminator) | 139 uint64_t RecordTerminator) |
| 173 : NaClBitcodeMunger(Records, RecordsSize, RecordTerminator), | 140 : NaClBitcodeMunger(Records, RecordsSize, RecordTerminator), |
| (...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 284 | 251 |
| 285 bool runTest(const char* TestName) { | 252 bool runTest(const char* TestName) { |
| 286 uint64_t NoMunges[] = {0}; | 253 uint64_t NoMunges[] = {0}; |
| 287 return runTest(TestName, NoMunges, 0); | 254 return runTest(TestName, NoMunges, 0); |
| 288 } | 255 } |
| 289 }; | 256 }; |
| 290 | 257 |
| 291 } // end namespace llvm. | 258 } // end namespace llvm. |
| 292 | 259 |
| 293 #endif // LLVM_BITCODE_NACL_NACLBITCODEMUNGE_H | 260 #endif // LLVM_BITCODE_NACL_NACLBITCODEMUNGE_H |
| OLD | NEW |