Error out of ParseBitcodeInto(Module*) if we haven't read a Module
authorFilipe Cabecinhas <me@filcab.net>
Tue, 14 Apr 2015 14:07:15 +0000 (14:07 +0000)
committerFilipe Cabecinhas <me@filcab.net>
Tue, 14 Apr 2015 14:07:15 +0000 (14:07 +0000)
Summary:
Without this check the following case failed:

Skip a SubBlock which is not a MODULE_BLOCK_ID nor a BLOCKINFO_BLOCK_ID
Got to end of file

TheModule would still be == nullptr, and we would subsequentially fail
when materializing the Module (assert at the start of
BitcodeReader::MaterializeModule).

Bug found with AFL.

Reviewers: dexonsmith, rafael

Subscribers: llvm-commits

Differential Revision: http://reviews.llvm.org/D9014

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@234887 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Bitcode/Reader/BitcodeReader.cpp
test/Bitcode/Inputs/invalid-no-proper-module.bc [new file with mode: 0644]
test/Bitcode/invalid.test

index 149642b1b3e315669875ab7d1d3d97e15c460ad6..bd4d70b25aadcba4790cefcfcf9fa4337e40dc90 100644 (file)
@@ -3063,8 +3063,12 @@ std::error_code BitcodeReader::ParseBitcodeInto(Module *M,
   // We expect a number of well-defined blocks, though we don't necessarily
   // need to understand them all.
   while (1) {
-    if (Stream.AtEndOfStream())
-      return std::error_code();
+    if (Stream.AtEndOfStream()) {
+      if (TheModule)
+        return std::error_code();
+      // We didn't really read a proper Module.
+      return Error("Malformed IR file");
+    }
 
     BitstreamEntry Entry =
       Stream.advance(BitstreamCursor::AF_DontAutoprocessAbbrevs);
diff --git a/test/Bitcode/Inputs/invalid-no-proper-module.bc b/test/Bitcode/Inputs/invalid-no-proper-module.bc
new file mode 100644 (file)
index 0000000..6d5a291
Binary files /dev/null and b/test/Bitcode/Inputs/invalid-no-proper-module.bc differ
index 0eacb9dbbb1e8dc7855ec17f686e536e45726a0f..9cab227ab1985492601ab963ae61c13347b30796 100644 (file)
@@ -50,3 +50,8 @@ EXTRACT-IDXS: EXTRACTVAL: Invalid type
 INSERT-ARRAY: INSERTVAL: Invalid array index
 INSERT-STRUCT: INSERTVAL: Invalid struct index
 INSERT-IDXS: INSERTVAL: Invalid type
+
+RUN: not llvm-dis -disable-output %p/Inputs/invalid-no-proper-module.bc 2>&1 | \
+RUN:   FileCheck --check-prefix=NO-MODULE %s
+
+NO-MODULE: Malformed IR file