[llvm] r239845 - Improve handling of end of file in the bitcode reader.

Rafael Espindola rafael.espindola at gmail.com
Tue Jun 16 13:03:40 PDT 2015


Author: rafael
Date: Tue Jun 16 15:03:39 2015
New Revision: 239845

URL: http://llvm.org/viewvc/llvm-project?rev=239845&view=rev
Log:
Improve handling of end of file in the bitcode reader.

Before this patch the bitcode reader would read a module from a file
that contained in order:

* Any number of non MODULE_BLOCK sub blocks.
* One MODULE_BLOCK
* Any number of non MODULE_BLOCK sub blocks.
* 4 '\n' characters to handle OS X's ranlib.

Since we support lazy reading of modules, any information that is relevant
for the module has to be in the MODULE_BLOCK or before it. We don't gain
anything from checking what is after.

This patch then changes the reader to stop once the MODULE_BLOCK has been
successfully parsed.

This avoids the ugly special case for .bc files in an archive and makes it
easier to embed bitcode files.

Added:
    llvm/trunk/test/Bitcode/Inputs/padding-garbage.bc
Modified:
    llvm/trunk/lib/Bitcode/Reader/BitcodeReader.cpp
    llvm/trunk/test/Bitcode/padding.test

Modified: llvm/trunk/lib/Bitcode/Reader/BitcodeReader.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Bitcode/Reader/BitcodeReader.cpp?rev=239845&r1=239844&r2=239845&view=diff
==============================================================================
--- llvm/trunk/lib/Bitcode/Reader/BitcodeReader.cpp (original)
+++ llvm/trunk/lib/Bitcode/Reader/BitcodeReader.cpp Tue Jun 16 15:03:39 2015
@@ -3079,7 +3079,7 @@ std::error_code BitcodeReader::parseModu
 
 std::error_code BitcodeReader::parseBitcodeInto(Module *M,
                                                 bool ShouldLazyLoadMetadata) {
-  TheModule = nullptr;
+  TheModule = M;
 
   if (std::error_code EC = initStream())
     return EC;
@@ -3097,8 +3097,6 @@ std::error_code BitcodeReader::parseBitc
   // need to understand them all.
   while (1) {
     if (Stream.AtEndOfStream()) {
-      if (TheModule)
-        return std::error_code();
       // We didn't really read a proper Module.
       return error("Malformed IR file");
     }
@@ -3106,47 +3104,14 @@ std::error_code BitcodeReader::parseBitc
     BitstreamEntry Entry =
       Stream.advance(BitstreamCursor::AF_DontAutoprocessAbbrevs);
 
-    switch (Entry.Kind) {
-    case BitstreamEntry::Error:
+    if (Entry.Kind != BitstreamEntry::SubBlock)
       return error("Malformed block");
-    case BitstreamEntry::EndBlock:
-      return std::error_code();
 
-    case BitstreamEntry::SubBlock:
-      switch (Entry.ID) {
-      case bitc::BLOCKINFO_BLOCK_ID:
-        if (Stream.ReadBlockInfoBlock())
-          return error("Malformed block");
-        break;
-      case bitc::MODULE_BLOCK_ID:
-        // Reject multiple MODULE_BLOCK's in a single bitstream.
-        if (TheModule)
-          return error("Invalid multiple blocks");
-        TheModule = M;
-        if (std::error_code EC = parseModule(false, ShouldLazyLoadMetadata))
-          return EC;
-        if (Streamer)
-          return std::error_code();
-        break;
-      default:
-        if (Stream.SkipBlock())
-          return error("Invalid record");
-        break;
-      }
-      continue;
-    case BitstreamEntry::Record:
-      // There should be no records in the top-level of blocks.
-
-      // The ranlib in Xcode 4 will align archive members by appending newlines
-      // to the end of them. If this file size is a multiple of 4 but not 8, we
-      // have to read and ignore these final 4 bytes :-(
-      if (Stream.getAbbrevIDWidth() == 2 && Entry.ID == 2 &&
-          Stream.Read(6) == 2 && Stream.Read(24) == 0xa0a0a &&
-          Stream.AtEndOfStream())
-        return std::error_code();
+    if (Entry.ID == bitc::MODULE_BLOCK_ID)
+      return parseModule(false, ShouldLazyLoadMetadata);
 
+    if (Stream.SkipBlock())
       return error("Invalid record");
-    }
   }
 }
 

Added: llvm/trunk/test/Bitcode/Inputs/padding-garbage.bc
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Bitcode/Inputs/padding-garbage.bc?rev=239845&view=auto
==============================================================================
Binary files llvm/trunk/test/Bitcode/Inputs/padding-garbage.bc (added) and llvm/trunk/test/Bitcode/Inputs/padding-garbage.bc Tue Jun 16 15:03:39 2015 differ

Modified: llvm/trunk/test/Bitcode/padding.test
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Bitcode/padding.test?rev=239845&r1=239844&r2=239845&view=diff
==============================================================================
--- llvm/trunk/test/Bitcode/padding.test (original)
+++ llvm/trunk/test/Bitcode/padding.test Tue Jun 16 15:03:39 2015
@@ -1,7 +1,7 @@
 Test that both llvm-dis (uses a data streamer) and opt (no data streamer)
-handle a .bc file padded with '\n' at the end.
+handle a .bc file with any padding.
 
-This files can be produced under a peculiar situation:
+A file padded with '\n' can be produced under a peculiar situation:
 
 * A .bc is produced os OS X, but without a darwin triple, so it has no
   wrapper.
@@ -9,5 +9,10 @@ This files can be produced under a pecul
 * ranlib is ran on that archive. It will pad the members to make them multiple
   of 8 bytes.
 
+and there is no reason to not handle the general case.
+
 RUN: llvm-dis -disable-output %p/Inputs/padding.bc
 RUN: opt -disable-output %p/Inputs/padding.bc
+
+RUN: llvm-dis -disable-output %p/Inputs/padding-garbage.bc
+RUN: opt -disable-output %p/Inputs/padding-garbage.bc





More information about the llvm-commits mailing list