[PATCH] D64262: Bitstream reader: Fix undefined behavior seen after rL364464

Bjorn Pettersson via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Fri Jul 5 13:09:14 PDT 2019


bjope created this revision.
bjope added a reviewer: jfb.
Herald added subscribers: kadircet, dexonsmith, ilya-biryukov.
Herald added a project: clang.

After rL364464 <https://reviews.llvm.org/rL364464> the following tests started to fail when
running the clang-doc tests with an ubsan instrumented
build of clang-doc:

  Clang Tools :: clang-doc/single-file-public.cpp
  Extra Tools Unit Tests :: clang-doc/./ClangDocTests/BitcodeTest.emitEnumInfoBitcode
  Extra Tools Unit Tests :: clang-doc/./ClangDocTests/BitcodeTest.emitMethodInfoBitcode
  Extra Tools Unit Tests :: clang-doc/./ClangDocTests/BitcodeTest.emitRecordInfoBitcode
  Extra Tools Unit Tests :: clang-doc/./ClangDocTests/SerializeTest.emitInfoWithCommentBitcode

We need to check that the read value is in range for being
casted to the llvm::bitc::FixedAbbrevIDs enum, before the
cast in ClangDocBitcodeReader::skipUntilRecordOrBlock.

SerializedDiagnosticReader::skipUntilRecordOrBlock was updated
in the same way.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D64262

Files:
  clang-tools-extra/clang-doc/BitcodeReader.cpp
  clang/lib/Frontend/SerializedDiagnosticReader.cpp


Index: clang/lib/Frontend/SerializedDiagnosticReader.cpp
===================================================================
--- clang/lib/Frontend/SerializedDiagnosticReader.cpp
+++ clang/lib/Frontend/SerializedDiagnosticReader.cpp
@@ -124,7 +124,12 @@
     else
       return llvm::errorToErrorCode(Res.takeError());
 
-    switch ((llvm::bitc::FixedAbbrevIDs)Code) {
+    if (Code >= static_cast<unsigned>(llvm::bitc::FIRST_APPLICATION_ABBREV)) {
+      // We found a record.
+      BlockOrRecordID = Code;
+      return Cursor::Record;
+    }
+    switch (static_cast<llvm::bitc::FixedAbbrevIDs>(Code)) {
     case llvm::bitc::ENTER_SUBBLOCK:
       if (Expected<unsigned> Res = Stream.ReadSubBlockID())
         BlockOrRecordID = Res.get();
@@ -145,10 +150,8 @@
     case llvm::bitc::UNABBREV_RECORD:
       return SDError::UnsupportedConstruct;
 
-    default:
-      // We found a record.
-      BlockOrRecordID = Code;
-      return Cursor::Record;
+    case llvm::bitc::FIRST_APPLICATION_ABBREV:
+      llvm_unreachable("Unexpected abbrev id.");
     }
   }
 
Index: clang-tools-extra/clang-doc/BitcodeReader.cpp
===================================================================
--- clang-tools-extra/clang-doc/BitcodeReader.cpp
+++ clang-tools-extra/clang-doc/BitcodeReader.cpp
@@ -615,10 +615,12 @@
       return Cursor::BadBlock;
     }
 
-    // FIXME check that the enum is in range.
-    auto Code = static_cast<llvm::bitc::FixedAbbrevIDs>(MaybeCode.get());
-
-    switch (Code) {
+    unsigned Code = MaybeCode.get();
+    if (Code >= static_cast<unsigned>(llvm::bitc::FIRST_APPLICATION_ABBREV)) {
+      BlockOrRecordID = Code;
+      return Cursor::Record;
+    }
+    switch (static_cast<llvm::bitc::FixedAbbrevIDs>(Code)) {
     case llvm::bitc::ENTER_SUBBLOCK:
       if (Expected<unsigned> MaybeID = Stream.ReadSubBlockID())
         BlockOrRecordID = MaybeID.get();
@@ -639,9 +641,8 @@
       continue;
     case llvm::bitc::UNABBREV_RECORD:
       return Cursor::BadBlock;
-    default:
-      BlockOrRecordID = Code;
-      return Cursor::Record;
+    case llvm::bitc::FIRST_APPLICATION_ABBREV:
+      llvm_unreachable("Unexpected abbrev id.");
     }
   }
   llvm_unreachable("Premature stream end.");


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D64262.208222.patch
Type: text/x-patch
Size: 2230 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20190705/f27be98c/attachment.bin>


More information about the cfe-commits mailing list