Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 //===- NaClBitcodeMungeWriter.cpp - Write munged bitcode --------*- C++ -*-===// | 1 //===- NaClBitcodeMungeWriter.cpp - Write munged bitcode --------*- 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 // Implements method NaClMungedBitcode.write(), which writes out a munged | 10 // Implements method NaClMungedBitcode.write(), which writes out a munged |
| (...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 111 | 111 |
| 112 // Marks that an abbreviation definition is being omitted (i.e. not | 112 // Marks that an abbreviation definition is being omitted (i.e. not |
| 113 // written) for the current block. | 113 // written) for the current block. |
| 114 void markCurrentBlockWithOmittedAbbreviations() { | 114 void markCurrentBlockWithOmittedAbbreviations() { |
| 115 assert(!ScopeStack.empty()); | 115 assert(!ScopeStack.empty()); |
| 116 ScopeStack.back().OmittedAbbreviations = true; | 116 ScopeStack.back().OmittedAbbreviations = true; |
| 117 if (getCurWriteBlockID() == naclbitc::BLOCKINFO_BLOCK_ID) | 117 if (getCurWriteBlockID() == naclbitc::BLOCKINFO_BLOCK_ID) |
| 118 BlocksWithOmittedAbbrevs.insert(SetBID); | 118 BlocksWithOmittedAbbrevs.insert(SetBID); |
| 119 } | 119 } |
| 120 | 120 |
| 121 // Returns true if abbreviation operand is legal. If not, generates | |
|
jvoung (off chromium)
2015/05/27 20:09:37
Maybe "logs" instead of "generates"? It's not retu
Karl
2015/05/28 20:50:31
Ok. Updated the comment to better reflect this.
| |
| 122 // a recoverable error and returns false. | |
| 123 bool verifyAbbrevOp(NaClBitCodeAbbrevOp::Encoding Encoding, uint64_t Value, | |
| 124 const NaClBitcodeAbbrevRecord &Record); | |
| 125 | |
| 121 // Converts the abbreviation record to the corresponding abbreviation. | 126 // Converts the abbreviation record to the corresponding abbreviation. |
| 122 // Returns nullptr if unable to build abbreviation. | 127 // Returns nullptr if unable to build abbreviation. |
| 123 NaClBitCodeAbbrev *buildAbbrev(const NaClBitcodeAbbrevRecord &Record); | 128 NaClBitCodeAbbrev *buildAbbrev(const NaClBitcodeAbbrevRecord &Record); |
| 124 | 129 |
| 125 // Emits the given record to the bitcode file. Returns true if | 130 // Emits the given record to the bitcode file. Returns true if |
| 126 // successful. | 131 // successful. |
| 127 bool LLVM_ATTRIBUTE_UNUSED_RESULT | 132 bool LLVM_ATTRIBUTE_UNUSED_RESULT |
| 128 emitRecord(NaClBitstreamWriter &Writer, | 133 emitRecord(NaClBitstreamWriter &Writer, |
| 129 const NaClBitcodeAbbrevRecord &Record); | 134 const NaClBitcodeAbbrevRecord &Record); |
| 130 | 135 |
| (...skipping 327 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 458 break; | 463 break; |
| 459 } | 464 } |
| 460 return true; | 465 return true; |
| 461 } | 466 } |
| 462 | 467 |
| 463 static NaClBitCodeAbbrev *deleteAbbrev(NaClBitCodeAbbrev *Abbrev) { | 468 static NaClBitCodeAbbrev *deleteAbbrev(NaClBitCodeAbbrev *Abbrev) { |
| 464 Abbrev->dropRef(); | 469 Abbrev->dropRef(); |
| 465 return nullptr; | 470 return nullptr; |
| 466 } | 471 } |
| 467 | 472 |
| 473 bool WriteState::verifyAbbrevOp(NaClBitCodeAbbrevOp::Encoding Encoding, | |
| 474 uint64_t Value, | |
| 475 const NaClBitcodeAbbrevRecord &Record) { | |
| 476 if (NaClBitCodeAbbrevOp::isValid(Encoding, Value)) | |
| 477 return true; | |
| 478 RecoverableError() << "Invalid abbreviation " | |
| 479 << NaClBitCodeAbbrevOp::getEncodingName(Encoding) | |
| 480 << "(" << static_cast<int64_t>(Value) | |
| 481 << ") in: " << Record << "\n"; | |
| 482 return false; | |
| 483 } | |
| 484 | |
| 468 NaClBitCodeAbbrev *WriteState::buildAbbrev( | 485 NaClBitCodeAbbrev *WriteState::buildAbbrev( |
| 469 const NaClBitcodeAbbrevRecord &Record) { | 486 const NaClBitcodeAbbrevRecord &Record) { |
| 470 // Note: Recover by removing abbreviation. | 487 // Note: Recover by removing abbreviation. |
| 471 NaClBitCodeAbbrev *Abbrev = new NaClBitCodeAbbrev(); | 488 NaClBitCodeAbbrev *Abbrev = new NaClBitCodeAbbrev(); |
| 472 size_t Index = 0; | 489 size_t Index = 0; |
| 473 size_t NumValues = Record.Values.size(); | 490 size_t NumValues = Record.Values.size(); |
| 474 if (NumValues == 0) { | 491 if (NumValues == 0) { |
| 475 RecoverableError() << "Empty abbreviation record not allowed: " | 492 RecoverableError() << "Empty abbreviation record not allowed: " |
| 476 << Record << "\n"; | 493 << Record << "\n"; |
| 477 return deleteAbbrev(Abbrev); | 494 return deleteAbbrev(Abbrev); |
| 478 } | 495 } |
| 479 size_t NumAbbreviations = Record.Values[Index++]; | 496 size_t NumAbbreviations = Record.Values[Index++]; |
|
jvoung (off chromium)
2015/05/27 20:09:37
Sorry a few random cleanup suggestions as I'm re-r
Karl
2015/05/28 20:50:31
Good point. Changing.
| |
| 480 if (NumAbbreviations == 0) { | 497 if (NumAbbreviations == 0) { |
| 481 RecoverableError() << "Abbreviation must contain at least one operator: " | 498 RecoverableError() << "Abbreviation must contain at least one operator: " |
| 482 << Record << "\n"; | 499 << Record << "\n"; |
| 483 return deleteAbbrev(Abbrev); | 500 return deleteAbbrev(Abbrev); |
| 484 } | 501 } |
| 485 for (size_t Count = 0; Count < NumAbbreviations; ++Count) { | 502 for (size_t Count = 0; Count < NumAbbreviations; ++Count) { |
| 486 if (Index >= NumValues) { | 503 if (Index >= NumValues) { |
| 487 RecoverableError() | 504 RecoverableError() |
| 488 << "Malformed abbreviation found. Expects " << NumAbbreviations | 505 << "Malformed abbreviation found. Expects " << NumAbbreviations |
| 489 << " operands but found " << Count << ": " << Record << "\n"; | 506 << " operands but found " << Count << ": " << Record << "\n"; |
| 490 return deleteAbbrev(Abbrev); | 507 return deleteAbbrev(Abbrev); |
| 491 } | 508 } |
| 492 switch (Record.Values[Index++]) { | 509 switch (Record.Values[Index++]) { |
|
jvoung (off chromium)
2015/05/27 20:09:37
Maybe stash Record.Values[Index++] in a named vari
Karl
2015/05/28 20:50:31
Done.
| |
| 493 case 1: | 510 case 1: { |
| 494 if (Index >= NumValues) { | 511 if (Index >= NumValues) { |
| 495 RecoverableError() << "Malformed literal abbreviation: " | 512 RecoverableError() << "Malformed literal abbreviation: " |
| 496 << Record << "\n"; | 513 << Record << "\n"; |
| 497 return deleteAbbrev(Abbrev); | 514 return deleteAbbrev(Abbrev); |
| 498 } | 515 } |
| 499 Abbrev->Add(NaClBitCodeAbbrevOp(Record.Values[Index++])); | 516 uint64_t Value = Record.Values[Index++]; |
| 517 if (!verifyAbbrevOp(NaClBitCodeAbbrevOp::Literal, Value, Record)) | |
| 518 return deleteAbbrev(Abbrev); | |
| 519 Abbrev->Add(NaClBitCodeAbbrevOp(Value)); | |
| 500 break; | 520 break; |
| 521 } | |
| 501 case 0: { | 522 case 0: { |
| 502 if (Index >= NumValues) { | 523 if (Index >= NumValues) { |
| 503 RecoverableError() << "Malformed abbreviation found: " | 524 RecoverableError() << "Malformed abbreviation found: " |
| 504 << Record << "\n"; | 525 << Record << "\n"; |
| 505 return deleteAbbrev(Abbrev); | 526 return deleteAbbrev(Abbrev); |
| 506 } | 527 } |
| 507 unsigned Kind = Record.Values[Index++]; | 528 unsigned Kind = Record.Values[Index++]; |
| 508 switch (Kind) { | 529 switch (Kind) { |
| 509 case NaClBitCodeAbbrevOp::Fixed: | 530 case NaClBitCodeAbbrevOp::Fixed: { |
| 510 if (Index >= NumValues) { | 531 if (Index >= NumValues) { |
| 511 RecoverableError() << "Malformed fixed abbreviation found: " | 532 RecoverableError() << "Malformed fixed abbreviation found: " |
| 512 << Record << "\n"; | 533 << Record << "\n"; |
| 513 return deleteAbbrev(Abbrev); | 534 return deleteAbbrev(Abbrev); |
| 514 } | 535 } |
| 515 Abbrev->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Fixed, | 536 uint64_t Value = Record.Values[Index++]; |
| 516 Record.Values[Index++])); | 537 if (!verifyAbbrevOp(NaClBitCodeAbbrevOp::Fixed, Value, Record)) |
| 538 return deleteAbbrev(Abbrev); | |
| 539 Abbrev->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Fixed, Value)); | |
| 517 break; | 540 break; |
| 518 case NaClBitCodeAbbrevOp::VBR: | 541 } |
| 542 case NaClBitCodeAbbrevOp::VBR: { | |
| 519 if (Index >= NumValues) { | 543 if (Index >= NumValues) { |
| 520 RecoverableError() << "Malformed vbr abbreviation found: " | 544 RecoverableError() << "Malformed vbr abbreviation found: " |
| 521 << Record << "\n"; | 545 << Record << "\n"; |
| 522 return deleteAbbrev(Abbrev); | 546 return deleteAbbrev(Abbrev); |
| 523 } | 547 } |
| 524 Abbrev->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::VBR, | 548 uint64_t Value = Record.Values[Index++]; |
| 525 Record.Values[Index++])); | 549 if (!verifyAbbrevOp(NaClBitCodeAbbrevOp::VBR, Value, Record)) |
| 550 return deleteAbbrev(Abbrev); | |
| 551 Abbrev->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::VBR, Value)); | |
| 526 break; | 552 break; |
| 553 } | |
| 527 case NaClBitCodeAbbrevOp::Array: | 554 case NaClBitCodeAbbrevOp::Array: |
| 528 if (Index >= NumValues) { | 555 if (Index >= NumValues) { |
|
jvoung (off chromium)
2015/05/27 20:09:37
fwiw, in the other cases that have this check agai
Karl
2015/05/28 20:50:31
I agree. Fixing.
Also, since getting values and c
| |
| 529 RecoverableError() << "Malformed array abbreviation found: " | 556 RecoverableError() << "Malformed array abbreviation found: " |
| 530 << Record << "\n"; | 557 << Record << "\n"; |
| 531 return deleteAbbrev(Abbrev); | 558 return deleteAbbrev(Abbrev); |
| 532 } | 559 } |
| 560 if (Count + 2 != NumAbbreviations) { | |
| 561 RecoverableError() << "Array abbreviation must be second to last: " | |
| 562 << Record << "\n"; | |
| 563 return deleteAbbrev(Abbrev); | |
| 564 } | |
| 533 Abbrev->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Array)); | 565 Abbrev->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Array)); |
| 534 break; | 566 break; |
| 535 case NaClBitCodeAbbrevOp::Char6: | 567 case NaClBitCodeAbbrevOp::Char6: |
| 536 Abbrev->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Char6)); | 568 Abbrev->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Char6)); |
| 537 break; | 569 break; |
| 538 default: | 570 default: |
| 539 RecoverableError() << "Unknown abbreviation kind " << Kind | 571 RecoverableError() << "Unknown abbreviation kind " << Kind |
| 540 << ": " << Record << "\n"; | 572 << ": " << Record << "\n"; |
| 541 return deleteAbbrev(Abbrev); | 573 return deleteAbbrev(Abbrev); |
| 542 } | 574 } |
| 543 break; | 575 break; |
| 544 } | 576 } |
| 545 default: | 577 default: |
| 546 RecoverableError() << "Error: Bad abbreviation operand encoding " | 578 RecoverableError() << "Bad abbreviation operand encoding " |
| 547 << Record.Values[Index-1] << ": " << Record << "\n"; | 579 << Record.Values[Index-1] << ": " << Record << "\n"; |
| 548 return deleteAbbrev(Abbrev); | 580 return deleteAbbrev(Abbrev); |
| 549 } | 581 } |
| 550 } | 582 } |
| 583 if (Index != NumValues) { | |
| 584 RecoverableError() << "Error: Too many values for number of operands (" | |
| 585 << NumAbbreviations << "): " | |
| 586 << Record << "\n"; | |
| 587 return deleteAbbrev(Abbrev); | |
| 588 } | |
| 589 if (!Abbrev->isValid()) { | |
| 590 raw_ostream &Out = RecoverableError(); | |
| 591 Abbrev->Print(Out << "Abbreviation "); | |
| 592 Out << " not valid: " << Record << "\n"; | |
| 593 return deleteAbbrev(Abbrev); | |
| 594 } | |
| 551 return Abbrev; | 595 return Abbrev; |
| 552 } | 596 } |
| 553 | 597 |
| 554 } // end of anonymous namespace. | 598 } // end of anonymous namespace. |
| 555 | 599 |
| 556 NaClMungedBitcode::WriteResults NaClMungedBitcode::writeMaybeRepair( | 600 NaClMungedBitcode::WriteResults NaClMungedBitcode::writeMaybeRepair( |
| 557 SmallVectorImpl<char> &Buffer, bool AddHeader, | 601 SmallVectorImpl<char> &Buffer, bool AddHeader, |
| 558 const WriteFlags &Flags) const { | 602 const WriteFlags &Flags) const { |
| 559 NaClBitstreamWriter Writer(Buffer); | 603 NaClBitstreamWriter Writer(Buffer); |
| 560 WriteState State(Flags); | 604 WriteState State(Flags); |
| 561 if (AddHeader) { | 605 if (AddHeader) { |
| 562 NaClWriteHeader(Writer, true); | 606 NaClWriteHeader(Writer, true); |
| 563 } | 607 } |
| 564 for (const NaClBitcodeAbbrevRecord &Record : *this) { | 608 for (const NaClBitcodeAbbrevRecord &Record : *this) { |
| 565 if (!State.emitRecord(Writer, Record)) | 609 if (!State.emitRecord(Writer, Record)) |
| 566 break; | 610 break; |
| 567 } | 611 } |
| 568 bool RecoverSilently = | 612 bool RecoverSilently = |
| 569 State.Results.NumErrors > 0 && !Flags.getTryToRecover(); | 613 State.Results.NumErrors > 0 && !Flags.getTryToRecover(); |
| 570 return State.finish(Writer, RecoverSilently); | 614 return State.finish(Writer, RecoverSilently); |
| 571 } | 615 } |
| OLD | NEW |