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

Side by Side Diff: unittests/Bitcode/NaClMungeWriteErrorTests.cpp

Issue 1146203003: Fix abbreviation handling in munged bitcode writer. (Closed) Base URL: https://chromium.googlesource.com/native_client/pnacl-llvm.git@master
Patch Set: Don't allow application of abbreviation if index out of range. 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
OLDNEW
1 //===- llvm/unittest/Bitcode/NaClMungeWriteErrorTests.cpp -----------------===// 1 //===- llvm/unittest/Bitcode/NaClMungeWriteErrorTests.cpp -----------------===//
2 // Tests parser for PNaCl bitcode instructions. 2 // Tests parser for PNaCl bitcode instructions.
3 // 3 //
4 // The LLVM Compiler Infrastructure 4 // The LLVM Compiler Infrastructure
5 // 5 //
6 // This file is distributed under the University of Illinois Open Source 6 // This file is distributed under the University of Illinois Open Source
7 // License. See LICENSE.TXT for details. 7 // License. See LICENSE.TXT for details.
8 // 8 //
9 //===----------------------------------------------------------------------===// 9 //===----------------------------------------------------------------------===//
10 10
(...skipping 241 matching lines...) Expand 10 before | Expand all | Expand 10 after
252 "Error \\(Block 17\\)\\: Uses illegal abbreviation index\\: 4\\: \\[2\\]" 252 "Error \\(Block 17\\)\\: Uses illegal abbreviation index\\: 4\\: \\[2\\]"
253 ".*" 253 ".*"
254 // Corresponding error while parsing. 254 // Corresponding error while parsing.
255 "Fatal\\(35\\:0)\\: Invalid abbreviation \\# 4 defined for record" 255 "Fatal\\(35\\:0)\\: Invalid abbreviation \\# 4 defined for record"
256 ".*" 256 ".*"
257 // Output of report_fatal_error. 257 // Output of report_fatal_error.
258 "LLVM ERROR\\: Unable to continue" 258 "LLVM ERROR\\: Unable to continue"
259 ".*"); 259 ".*");
260 } 260 }
261 261
262 // Show that we check that the abbreviation actually applies to the
263 // record associated with that abbreviation. Also shows that we repair
264 // the problem by applying the default abbreviation instead.o
jvoung (off chromium) 2015/05/20 22:40:10 instead.o --> instead.
Karl 2015/05/21 18:22:08 Done.
265 TEST(NaClMungeWriteErrorsTests, TestMismatchedAbbreviation) {
266 // Create edits to:
267 // 1) Expand the number of abbreviation index bits for the block from 2 to 3.
268 // 2) Introduce the incorrect abbreviation for the return instruction.
269 // i.e. [9] instead of [10].
270 // 3) Apply the bad abbreviation to record "ret"
271 const uint64_t FunctionEnterIndex = 7;
272 const uint64_t Edits[] {
273 FunctionEnterIndex, NaClMungedBitcode::Replace,
274 1, naclbitc::BLK_CODE_ENTER, naclbitc::FUNCTION_BLOCK_ID, 3, Terminator,
275 RetVoidIndex, NaClMungedBitcode::AddBefore,
jvoung (off chromium) 2015/05/20 22:40:10 I guess it is kind of cool/weird that you can defi
Karl 2015/05/21 18:22:08 It is.
276 2, naclbitc::BLK_CODE_DEFINE_ABBREV, 1, 1,
277 naclbitc::FUNC_CODE_INST_RET - 1, Terminator,
278 RetVoidIndex, NaClMungedBitcode::Replace,
279 4, naclbitc::FUNC_CODE_INST_RET, Terminator
280 };
281
282 NaClWriteMunger Munger(ARRAY_TERM(BitcodeRecords));
283 Munger.munge(ARRAY(Edits));
284 EXPECT_EQ(
285 " 1: [65535, 8, 2]\n"
286 " 1: [65535, 17, 3]\n"
287 " 3: [1, 2]\n"
288 " 3: [2]\n"
289 " 3: [21, 0, 0]\n"
290 " 0: [65534]\n"
291 " 3: [8, 1, 0, 0, 0]\n"
292 " 1: [65535, 12, 3]\n" // Upped abbreviation index bits to 3
293 " 3: [1, 1]\n"
294 " 2: [65533, 1, 1, 9]\n" // added abbrev 4: [9]
295 " 4: [10]\n" // "ret" with bad abbreviation.
296 " 0: [65534]\n"
297 " 0: [65534]\n",
298 stringify(Munger));
299 Munger.setTryToRecoverOnWrite(true);
300 EXPECT_TRUE(Munger.runTest());
jvoung (off chromium) 2015/05/20 22:40:10 Add a run without recovery too?
Karl 2015/05/21 18:22:08 Done.
301 EXPECT_EQ(
302 "Error (Block 12): Abbreviation doesn't apply to record: 4: [10]\n"
303 " 1: [65535, 8, 2]\n"
304 " 1: [65535, 17, 3]\n"
305 " 3: [1, 2]\n"
306 " 3: [2]\n"
307 " 3: [21, 0, 0]\n"
308 " 0: [65534]\n"
309 " 3: [8, 1, 0, 0, 0]\n"
310 " 1: [65535, 12, 3]\n"
311 " 3: [1, 1]\n"
312 " 2: [65533, 1, 1, 9]\n"
313 " 3: [10]\n" // Implicit repair here.
314 " 0: [65534]\n"
315 " 0: [65534]\n",
316 Munger.getTestResults());
317 }
318
319 // Show that we recognize when an abbreviation definition record is
320 // malformed. Also show that we repair the problem by removing the
321 // definition.
322 TEST(NaClMungeWriteErrorsTests, TestWritingMalformedAbbreviation) {
323 // Create edits to:
324 // 1) Expand the number of abbreviation index bits for the block from 2 to 3.
325 // 2) Leave out the "literal" operand encoding out.
326 const uint64_t FunctionEnterIndex = 7;
327 const uint64_t Edits[] {
328 FunctionEnterIndex, NaClMungedBitcode::Replace, // Set Abbrev bits = 3
329 1, naclbitc::BLK_CODE_ENTER, naclbitc::FUNCTION_BLOCK_ID, 3, Terminator,
330 RetVoidIndex, NaClMungedBitcode::AddBefore, // bad abbreviation!
331 2, naclbitc::BLK_CODE_DEFINE_ABBREV, 1, // 1,
jvoung (off chromium) 2015/05/20 22:40:10 How about expand on why the "// 1,"? E.g., // 1,
Karl 2015/05/21 18:22:08 Done.
332 naclbitc::FUNC_CODE_INST_RET, Terminator,
333 };
334
335 NaClWriteMunger Munger(ARRAY_TERM(BitcodeRecords));
336 Munger.setTryToRecoverOnWrite(true);
337 EXPECT_TRUE(Munger.runTest(ARRAY(Edits)));
338 EXPECT_EQ(
339 "Error (Block 12): Error: Bad abbreviation operand encoding 10: "
340 "2: [65533, 1, 10]\n"
341 " 1: [65535, 8, 2]\n"
342 " 1: [65535, 17, 3]\n"
343 " 3: [1, 2]\n"
344 " 3: [2]\n"
345 " 3: [21, 0, 0]\n"
346 " 0: [65534]\n"
347 " 3: [8, 1, 0, 0, 0]\n"
348 " 1: [65535, 12, 3]\n" // Note: not followed by abbreviation def.
349 " 3: [1, 1]\n"
350 " 3: [10]\n"
351 " 0: [65534]\n"
352 " 0: [65534]\n",
353 Munger.getTestResults());
354 }
355
356 // Show how we deal with additional abbreviations defined for a block,
357 // once a bad abbreviation definition record is found. That is, we
358 // remove all succeeding abbreviations definitions for that block. In
359 // addition, any record refering to a remove abbreviation is changed
360 // to use the default abbreviation.
361 TEST(NaClMungedWriteErrorTests, TestRemovingAbbrevWithMultAbbrevs) {
362 NaClWriteMunger Munger(ARRAY_TERM(BitcodeRecords));
363 const uint64_t FunctionEnterIndex = 7;
364 const uint64_t Edits[] {
365 FunctionEnterIndex, NaClMungedBitcode::Replace, // Set Abbrev bits = 3
366 1, naclbitc::BLK_CODE_ENTER, naclbitc::FUNCTION_BLOCK_ID, 3, Terminator,
367 RetVoidIndex, NaClMungedBitcode::AddBefore, // bad abbreviation!
368 2, naclbitc::BLK_CODE_DEFINE_ABBREV, 1, // 1,
369 naclbitc::FUNC_CODE_INST_RET - 1, Terminator,
370 RetVoidIndex, NaClMungedBitcode::AddBefore, // good abbreviation to ignore.
371 2, naclbitc::BLK_CODE_DEFINE_ABBREV, 1, 1,
372 naclbitc::FUNC_CODE_INST_RET, Terminator,
373 RetVoidIndex, NaClMungedBitcode::Replace, // reference to good abreviation.
374 5, naclbitc::FUNC_CODE_INST_RET, Terminator
375 };
376
377 Munger.setTryToRecoverOnWrite(true);
378 EXPECT_TRUE(Munger.runTest(ARRAY(Edits)));
379 EXPECT_EQ(
380 "Error (Block 12): Error: Bad abbreviation operand encoding 9:"
381 " 2: [65533, 1, 9]\n"
382 "Error (Block 12): Ignoring abbreviation: 2: [65533, 1, 1, 10]\n"
383 "Error (Block 12): Uses illegal abbreviation index: 5: [10]\n"
384 " 1: [65535, 8, 2]\n"
385 " 1: [65535, 17, 3]\n"
386 " 3: [1, 2]\n"
387 " 3: [2]\n"
388 " 3: [21, 0, 0]\n"
389 " 0: [65534]\n"
390 " 3: [8, 1, 0, 0, 0]\n"
391 " 1: [65535, 12, 3]\n"
392 " 3: [1, 1]\n"
393 " 3: [10]\n" // Abbreviation index 5 replaced with default.
394 " 0: [65534]\n"
395 " 0: [65534]\n",
396 Munger.getTestResults());
397 }
398
262 // Show that error recovery works when writing an illegal abbreviation 399 // Show that error recovery works when writing an illegal abbreviation
263 // index. Show success by parsing fixed bitcode. 400 // index. Show success by parsing fixed bitcode.
264 TEST(NaClMungeWriteErrorTests, RecoverWhenParsingBadAbbrevIndex) { 401 TEST(NaClMungeWriteErrorTests, RecoverWhenParsingBadAbbrevIndex) {
265 NaClParseBitcodeMunger Munger(ARRAY_TERM(BitcodeRecords)); 402 NaClParseBitcodeMunger Munger(ARRAY_TERM(BitcodeRecords));
266 Munger.setTryToRecoverOnWrite(true); 403 Munger.setTryToRecoverOnWrite(true);
267 EXPECT_TRUE( 404 EXPECT_TRUE(
268 Munger.runTest(ARRAY(AbbrevIndex4VoidTypeEdit), true)); 405 Munger.runTest(ARRAY(AbbrevIndex4VoidTypeEdit), true));
269 EXPECT_EQ( 406 EXPECT_EQ(
270 "Error (Block 17): Uses illegal abbreviation index: 4: [2]\n" 407 "Error (Block 17): Uses illegal abbreviation index: 4: [2]\n"
271 "Successful parse!\n", 408 "Successful parse!\n",
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after
383 " 3: [10]\n" 520 " 3: [10]\n"
384 " 0: [65534]\n" 521 " 0: [65534]\n"
385 " 0: [65534]\n" 522 " 0: [65534]\n"
386 " 1: [65535, 4294967295, 3]\n" 523 " 1: [65535, 4294967295, 3]\n"
387 " 3: [1, 4]\n" 524 " 3: [1, 4]\n"
388 " 0: [65534]\n", 525 " 0: [65534]\n",
389 Munger.getTestResults()); 526 Munger.getTestResults());
390 } 527 }
391 528
392 } // end of namespace naclmungetest 529 } // end of namespace naclmungetest
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698