Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(48)

Side by Side Diff: include/llvm/Bitcode/NaCl/NaClBitcodeMunge.h

Issue 1139673004: Harden writer of munged bitcode for fuzzing (Closed) Base URL: https://chromium.googlesource.com/native_client/pnacl-llvm.git@master
Patch Set: Fix issues in last patch. Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | include/llvm/Bitcode/NaCl/NaClBitstreamWriter.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
66 } 66 }
67 67
68 /// Sets death test flag. When true, output will be redirected to 68 /// Sets death test flag. When true, output will be redirected to
69 /// the errs() (rather than buffered) so that the test can be 69 /// the errs() (rather than buffered) so that the test can be
70 /// debugged. 70 /// debugged.
71 void setRunAsDeathTest(bool NewValue) { 71 void setRunAsDeathTest(bool NewValue) {
72 RunAsDeathTest = NewValue; 72 RunAsDeathTest = NewValue;
73 } 73 }
74 74
75 /// Creates MungedInput and DumpStream for running tests, based on 75 /// Creates MungedInput and DumpStream for running tests, based on
76 /// given Munges. 76 /// given Munges. Returns true if able to set up test.
77 void setupTest( 77 bool setupTest(
78 const char *TestName, const uint64_t Munges[], size_t MungesSize, 78 const char *TestName, const uint64_t Munges[], size_t MungesSize,
79 bool AddHeader); 79 bool AddHeader);
80 80
81 /// Cleans up state after a test. 81 /// Cleans up state after a test. Returns true if no errors found.
82 void cleanupTest(); 82 bool cleanupTest();
83 83
84 /// Returns the resulting string generated by the corresponding test. 84 /// Returns the resulting string generated by the corresponding test.
85 const std::string &getTestResults() const { 85 const std::string &getTestResults() const {
86 return DumpResults; 86 return DumpResults;
87 } 87 }
88 88
89 /// Returns the lines containing the given Substring, from the 89 /// Returns the lines containing the given Substring, from the
90 /// string getTestResults(). 90 /// string getTestResults().
91 std::string getLinesWithSubstring(const std::string &Substring) const { 91 std::string getLinesWithSubstring(const std::string &Substring) const {
92 return getLinesWithTextMatch(Substring, false); 92 return getLinesWithTextMatch(Substring, false);
(...skipping 10 matching lines...) Expand all
103 void setTryToRecoverOnWrite(bool NewValue) { 103 void setTryToRecoverOnWrite(bool NewValue) {
104 WriteFlags.setTryToRecover(NewValue); 104 WriteFlags.setTryToRecover(NewValue);
105 } 105 }
106 106
107 /// When NewValue, write bad abbreviation index into bitcode when 107 /// When NewValue, write bad abbreviation index into bitcode when
108 /// writing during next test. 108 /// writing during next test.
109 void setWriteBadAbbrevIndex(bool NewValue) { 109 void setWriteBadAbbrevIndex(bool NewValue) {
110 WriteFlags.setWriteBadAbbrevIndex(NewValue); 110 WriteFlags.setWriteBadAbbrevIndex(NewValue);
111 } 111 }
112 112
113 /// Get access to munged bitcodes.
114 NaClMungedBitcode &getMungedBitcode() {
115 return MungedBitcode;
116 }
117
118 /// Apply given munges to the munged bitcode.
119 void munge(const uint64_t Munges[], size_t MungesSize) {
120 MungedBitcode.munge(Munges, MungesSize, RecordTerminator);
121 }
122
113 protected: 123 protected:
114 // The bitcode records being munged. 124 // The bitcode records being munged.
115 NaClMungedBitcode MungedBitcode; 125 NaClMungedBitcode MungedBitcode;
116 // The value used as record terminator. 126 // The value used as record terminator.
117 uint64_t RecordTerminator; 127 uint64_t RecordTerminator;
118 // The results buffer of the last dump. 128 // The results buffer of the last dump.
119 std::string DumpResults; 129 std::string DumpResults;
120 // The memory buffer containing the munged input. 130 // The memory buffer containing the munged input.
121 std::unique_ptr<MemoryBuffer> MungedInput; 131 std::unique_ptr<MemoryBuffer> MungedInput;
122 // The stream containing errors and the objdump of the generated bitcode file. 132 // The stream containing errors and the objdump of the generated bitcode file.
(...skipping 21 matching lines...) Expand all
144 bool MustBePrefix = false) const; 154 bool MustBePrefix = false) const;
145 155
146 // Returns the log stream to use. When running death tests, redirect output 156 // Returns the log stream to use. When running death tests, redirect output
147 // to the error stream (rather than buffering in DumpStream), so that 157 // to the error stream (rather than buffering in DumpStream), so that
148 // the output can be seen in gtest death tests. 158 // the output can be seen in gtest death tests.
149 raw_ostream &getDumpStream() const { 159 raw_ostream &getDumpStream() const {
150 return RunAsDeathTest ? errs() : *DumpStream; 160 return RunAsDeathTest ? errs() : *DumpStream;
151 } 161 }
152 }; 162 };
153 163
164 /// Class to run tests writing munged bitcode.
165 class NaClWriteMunger : public NaClBitcodeMunger {
166 public:
167 NaClWriteMunger(const uint64_t Records[], size_t RecordsSize,
168 uint64_t RecordTerminator)
169 : NaClBitcodeMunger(Records, RecordsSize, RecordTerminator) {}
170
171 /// Writes munged bitcode and puts error messages into DumpResults.
172 /// Returns true if successful.
173 bool runTest(const char* TestName, const uint64_t Munges[],
174 size_t MungesSize);
175
176 /// Same as above, but without test name.
177 bool runTest(const uint64_t Munges[], size_t MungesSize) {
178 return runTest("Test", Munges, MungesSize);
179 }
180
181 /// Same as above, but without any edits.
182 bool runTest(const char* TestName) {
183 uint64_t NoMunges[] = {0};
184 return runTest(TestName, NoMunges, 0);
185 }
186
187 /// Same as above, but without test name.
188 bool runTest() {
189 return runTest("Test");
190 }
191 };
192
154 /// Class to run tests for function llvm::NaClObjDump. 193 /// Class to run tests for function llvm::NaClObjDump.
155 class NaClObjDumpMunger : public NaClBitcodeMunger { 194 class NaClObjDumpMunger : public NaClBitcodeMunger {
156 public: 195 public:
157 196
158 /// Creates a bitcode munger, based on the given array of values. 197 /// Creates a bitcode munger, based on the given array of values.
159 NaClObjDumpMunger(const uint64_t Records[], size_t RecordsSize, 198 NaClObjDumpMunger(const uint64_t Records[], size_t RecordsSize,
160 uint64_t RecordTerminator) 199 uint64_t RecordTerminator)
161 : NaClBitcodeMunger(Records, RecordsSize, RecordTerminator) {} 200 : NaClBitcodeMunger(Records, RecordsSize, RecordTerminator) {}
162 201
163 /// Runs function NaClObjDump on the sequence of records associated 202 /// Runs function NaClObjDump on the sequence of records associated
(...skipping 11 matching lines...) Expand all
175 return runTestWithFlags(TestName, NoMunges, 0, AddHeader, NoRecords, 214 return runTestWithFlags(TestName, NoMunges, 0, AddHeader, NoRecords,
176 NoAssembly); 215 NoAssembly);
177 } 216 }
178 217
179 /// Same as above except it runs function NaClObjDump with flags 218 /// Same as above except it runs function NaClObjDump with flags
180 /// NoRecords and NoAssembly set to false, and AddHeader set to true. 219 /// NoRecords and NoAssembly set to false, and AddHeader set to true.
181 bool runTest(const char *TestName) { 220 bool runTest(const char *TestName) {
182 return runTestWithFlags(TestName, true, false, false); 221 return runTestWithFlags(TestName, true, false, false);
183 } 222 }
184 223
185 /// Same as runTest, but only print out assembly and errors. 224 /// Same as above but without test name.
225 bool runTest() {
226 return runTest("Test");
227 }
228
229 /// Same as above, but only print out assembly and errors.
186 bool runTestForAssembly(const char *TestName) { 230 bool runTestForAssembly(const char *TestName) {
187 return runTestWithFlags(TestName, true, true, false); 231 return runTestWithFlags(TestName, true, true, false);
188 } 232 }
189 233
190 /// Same as runTest, but only generate error messages. 234 /// Same as above, but only generate error messages.
191 bool runTestForErrors(const char *TestName) { 235 bool runTestForErrors(const char *TestName) {
192 return runTestWithFlags(TestName, true, true, true); 236 return runTestWithFlags(TestName, true, true, true);
193 } 237 }
194 238
195 /// Runs function llvm::NaClObjDump on the sequence of records 239 /// Runs function llvm::NaClObjDump on the sequence of records
196 /// associated with the instance. Array Munges contains the sequence 240 /// associated with the instance. Array Munges contains the sequence
197 /// of edits to apply to the sequence of records when generating the 241 /// of edits to apply to the sequence of records when generating the
198 /// bitsequence in a memory buffer. This generated bitsequence is 242 /// bitsequence in a memory buffer. This generated bitsequence is
199 /// then passed to NaClObjDump. TestName is the name associated 243 /// then passed to NaClObjDump. TestName is the name associated
200 /// with the memory buffer. Arguments NoRecords and NoAssembly are 244 /// with the memory buffer. Arguments NoRecords and NoAssembly are
201 /// passed to NaClObjDump. Returns true if test succeeds without 245 /// passed to NaClObjDump. Returns true if test succeeds without
202 /// errors. 246 /// errors.
203 bool runTestWithFlags(const char* TestName, const uint64_t Munges[], 247 bool runTestWithFlags(const char* TestName, const uint64_t Munges[],
204 size_t MungesSize, bool AddHeader, 248 size_t MungesSize, bool AddHeader,
205 bool NoRecords, bool NoAssembly); 249 bool NoRecords, bool NoAssembly);
206 250
207 /// Same as above except it runs function NaClObjDump with flags 251 /// Same as above except it runs function NaClObjDump with flags
208 /// NoRecords and NoAssembly set to false, and AddHeader set to 252 /// NoRecords and NoAssembly set to false, and AddHeader set to
209 /// true. 253 /// true.
210 bool runTest(const char* TestName, const uint64_t Munges[], 254 bool runTest(const char* TestName, const uint64_t Munges[],
211 size_t MungesSize) { 255 size_t MungesSize) {
212 return runTestWithFlags(TestName, Munges, MungesSize, true, false, false); 256 return runTestWithFlags(TestName, Munges, MungesSize, true, false, false);
213 } 257 }
214 258
259 bool runTest(const uint64_t Munges[], size_t MungesSize) {
260 return runTest("Test", Munges, MungesSize);
261 }
262
215 bool runTestForAssembly(const char* TestName, const uint64_t Munges[], 263 bool runTestForAssembly(const char* TestName, const uint64_t Munges[],
216 size_t MungesSize) { 264 size_t MungesSize) {
217 return runTestWithFlags(TestName, Munges, MungesSize, true, true, false); 265 return runTestWithFlags(TestName, Munges, MungesSize, true, true, false);
218 } 266 }
219 267
220 bool runTestForErrors(const char* TestName, const uint64_t Munges[], 268 bool runTestForErrors(const char* TestName, const uint64_t Munges[],
221 size_t MungesSize) { 269 size_t MungesSize) {
222 return runTestWithFlags(TestName, Munges, MungesSize, true, true, true); 270 return runTestWithFlags(TestName, Munges, MungesSize, true, true, true);
223 } 271 }
224 }; 272 };
225 273
226 // Class to run tests for function NaClParseBitcodeFile. 274 // Class to run tests for function NaClParseBitcodeFile.
227 class NaClParseBitcodeMunger : public NaClBitcodeMunger { 275 class NaClParseBitcodeMunger : public NaClBitcodeMunger {
228 public: 276 public:
229 NaClParseBitcodeMunger(const uint64_t Records[], size_t RecordsSize, 277 NaClParseBitcodeMunger(const uint64_t Records[], size_t RecordsSize,
230 uint64_t RecordTerminator) 278 uint64_t RecordTerminator)
231 : NaClBitcodeMunger(Records, RecordsSize, RecordTerminator) {} 279 : NaClBitcodeMunger(Records, RecordsSize, RecordTerminator) {}
232 280
233 /// Runs function llvm::NaClParseBitcodeFile, and puts error messages 281 /// Runs function llvm::NaClParseBitcodeFile, and puts error messages
234 /// into DumpResults. Returns true if parse is successful. 282 /// into DumpResults. Returns true if parse is successful.
235 bool runTest(const char* TestName, const uint64_t Munges[], 283 bool runTest(const char* TestName, const uint64_t Munges[],
236 size_t MungesSize, bool VerboseErrors); 284 size_t MungesSize, bool VerboseErrors);
237 285
286 /// Same as above, but without test name.
287 bool runTest(const uint64_t Munges[], size_t MungesSize, bool VerboseErrors) {
288 return runTest("Test", Munges, MungesSize, VerboseErrors);
289 }
290
238 // Same as above, but without any edits. 291 // Same as above, but without any edits.
239 bool runTest(const char* TestName, bool VerboseErrors) { 292 bool runTest(const char* TestName, bool VerboseErrors) {
240 uint64_t NoMunges[] = {0}; 293 uint64_t NoMunges[] = {0};
241 return runTest(TestName, NoMunges, 0, VerboseErrors); 294 return runTest(TestName, NoMunges, 0, VerboseErrors);
242 } 295 }
243 }; 296 };
244 297
245 // Class to run tests for NaClBitcodeCompressor.compress(). 298 // Class to run tests for NaClBitcodeCompressor.compress().
246 class NaClCompressMunger : public NaClBitcodeMunger { 299 class NaClCompressMunger : public NaClBitcodeMunger {
247 public: 300 public:
248 NaClCompressMunger(const uint64_t Records[], size_t RecordsSize, 301 NaClCompressMunger(const uint64_t Records[], size_t RecordsSize,
249 uint64_t RecordTerminator) 302 uint64_t RecordTerminator)
250 : NaClBitcodeMunger(Records, RecordsSize, RecordTerminator) {} 303 : NaClBitcodeMunger(Records, RecordsSize, RecordTerminator) {}
251 304
252 bool runTest(const char* TestName, const uint64_t Munges[], 305 bool runTest(const char* TestName, const uint64_t Munges[],
253 size_t MungesSize); 306 size_t MungesSize);
254 307
255 bool runTest(const char* TestName) { 308 bool runTest(const char* TestName) {
256 uint64_t NoMunges[] = {0}; 309 uint64_t NoMunges[] = {0};
257 return runTest(TestName, NoMunges, 0); 310 return runTest(TestName, NoMunges, 0);
258 } 311 }
259 }; 312 };
260 313
261 } // end namespace llvm. 314 } // end namespace llvm.
262 315
263 #endif // LLVM_BITCODE_NACL_NACLBITCODEMUNGE_H 316 #endif // LLVM_BITCODE_NACL_NACLBITCODEMUNGE_H
OLDNEW
« no previous file with comments | « no previous file | include/llvm/Bitcode/NaCl/NaClBitstreamWriter.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698