[llvm] 8a71854 - [Bitcode] Handle invalid abbrev number error more gracefully

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Mon Feb 7 01:35:41 PST 2022


Author: Nikita Popov
Date: 2022-02-07T10:34:34+01:00
New Revision: 8a718541838a0ab1e87f4b8923bb16e640f73de3

URL: https://github.com/llvm/llvm-project/commit/8a718541838a0ab1e87f4b8923bb16e640f73de3
DIFF: https://github.com/llvm/llvm-project/commit/8a718541838a0ab1e87f4b8923bb16e640f73de3.diff

LOG: [Bitcode] Handle invalid abbrev number error more gracefully

Avoid report_fatal_error(), propagate the error upwards instead.

Added: 
    llvm/test/Bitcode/Inputs/invalid-abbrev-number.bc

Modified: 
    llvm/include/llvm/Bitstream/BitstreamReader.h
    llvm/lib/Bitcode/Reader/BitcodeAnalyzer.cpp
    llvm/lib/Bitstream/Reader/BitstreamReader.cpp
    llvm/test/Bitcode/invalid.test

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/Bitstream/BitstreamReader.h b/llvm/include/llvm/Bitstream/BitstreamReader.h
index 37b7c4d73cfff..91955c27364af 100644
--- a/llvm/include/llvm/Bitstream/BitstreamReader.h
+++ b/llvm/include/llvm/Bitstream/BitstreamReader.h
@@ -521,10 +521,11 @@ class BitstreamCursor : SimpleBitstreamCursor {
 
 public:
   /// Return the abbreviation for the specified AbbrevId.
-  const BitCodeAbbrev *getAbbrev(unsigned AbbrevID) {
+  Expected<const BitCodeAbbrev *> getAbbrev(unsigned AbbrevID) {
     unsigned AbbrevNo = AbbrevID - bitc::FIRST_APPLICATION_ABBREV;
     if (AbbrevNo >= CurAbbrevs.size())
-      report_fatal_error("Invalid abbrev number");
+      return createStringError(
+          std::errc::illegal_byte_sequence, "Invalid abbrev number");
     return CurAbbrevs[AbbrevNo].get();
   }
 

diff  --git a/llvm/lib/Bitcode/Reader/BitcodeAnalyzer.cpp b/llvm/lib/Bitcode/Reader/BitcodeAnalyzer.cpp
index ffef352999815..fdb07cf282a93 100644
--- a/llvm/lib/Bitcode/Reader/BitcodeAnalyzer.cpp
+++ b/llvm/lib/Bitcode/Reader/BitcodeAnalyzer.cpp
@@ -864,7 +864,10 @@ Error BitcodeAnalyzer::parseBlock(unsigned BlockID, unsigned IndentLevel,
         O->OS << " codeid=" << Code;
       const BitCodeAbbrev *Abbv = nullptr;
       if (Entry.ID != bitc::UNABBREV_RECORD) {
-        Abbv = Stream.getAbbrev(Entry.ID);
+        Expected<const BitCodeAbbrev *> MaybeAbbv = Stream.getAbbrev(Entry.ID);
+        if (!MaybeAbbv)
+          return MaybeAbbv.takeError();
+        Abbv = MaybeAbbv.get();
         O->OS << " abbrevid=" << Entry.ID;
       }
 

diff  --git a/llvm/lib/Bitstream/Reader/BitstreamReader.cpp b/llvm/lib/Bitstream/Reader/BitstreamReader.cpp
index 3865af1f38897..ffeb506154f90 100644
--- a/llvm/lib/Bitstream/Reader/BitstreamReader.cpp
+++ b/llvm/lib/Bitstream/Reader/BitstreamReader.cpp
@@ -111,7 +111,11 @@ Expected<unsigned> BitstreamCursor::skipRecord(unsigned AbbrevID) {
     return Code;
   }
 
-  const BitCodeAbbrev *Abbv = getAbbrev(AbbrevID);
+  Expected<const BitCodeAbbrev *> MaybeAbbv = getAbbrev(AbbrevID);
+  if (!MaybeAbbv)
+    return MaybeAbbv.takeError();
+
+  const BitCodeAbbrev *Abbv = MaybeAbbv.get();
   const BitCodeAbbrevOp &CodeOp = Abbv->getOperandInfo(0);
   unsigned Code;
   if (CodeOp.isLiteral())
@@ -228,7 +232,10 @@ Expected<unsigned> BitstreamCursor::readRecord(unsigned AbbrevID,
     return Code;
   }
 
-  const BitCodeAbbrev *Abbv = getAbbrev(AbbrevID);
+  Expected<const BitCodeAbbrev *> MaybeAbbv = getAbbrev(AbbrevID);
+  if (!MaybeAbbv)
+    return MaybeAbbv.takeError();
+  const BitCodeAbbrev *Abbv = MaybeAbbv.get();
 
   // Read the record code first.
   assert(Abbv->getNumOperandInfos() != 0 && "no record code in abbreviation?");

diff  --git a/llvm/test/Bitcode/Inputs/invalid-abbrev-number.bc b/llvm/test/Bitcode/Inputs/invalid-abbrev-number.bc
new file mode 100644
index 0000000000000..0bf399d4c7885
--- /dev/null
+++ b/llvm/test/Bitcode/Inputs/invalid-abbrev-number.bc
@@ -0,0 +1 @@
+BCÀÞe---C
\ No newline at end of file

diff  --git a/llvm/test/Bitcode/invalid.test b/llvm/test/Bitcode/invalid.test
index 01c081e46be1b..67546bf36bfbe 100644
--- a/llvm/test/Bitcode/invalid.test
+++ b/llvm/test/Bitcode/invalid.test
@@ -246,3 +246,8 @@ RUN: not llvm-dis -disable-output %p/Inputs/invalid-cmpxchg-ordering-4.bc 2>&1 |
 RUN:   FileCheck --check-prefix=CMPXCHG-ORDERING %s
 
 CMPXCHG-ORDERING: Invalid cmpxchg {{failure|success}} ordering
+
+RUN: not llvm-dis -disable-output %p/Inputs/invalid-abbrev-number.bc 2>&1 | \
+RUN:   FileCheck --check-prefix=INVALID-ABBREV-NUMBER %s
+
+INVALID-ABBREV-NUMBER: Invalid abbrev number


        


More information about the llvm-commits mailing list