From 9a1030a4d6febc9133de25dce7cda1bae513e8cf Mon Sep 17 00:00:00 2001 From: Filipe Cabecinhas Date: Tue, 14 Apr 2015 14:07:15 +0000 Subject: [PATCH] Error out of ParseBitcodeInto(Module*) if we haven't read a Module 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 | 8 ++++++-- test/Bitcode/Inputs/invalid-no-proper-module.bc | Bin 0 -> 612 bytes test/Bitcode/invalid.test | 5 +++++ 3 files changed, 11 insertions(+), 2 deletions(-) create mode 100644 test/Bitcode/Inputs/invalid-no-proper-module.bc diff --git a/lib/Bitcode/Reader/BitcodeReader.cpp b/lib/Bitcode/Reader/BitcodeReader.cpp index 149642b1b3e..bd4d70b25aa 100644 --- a/lib/Bitcode/Reader/BitcodeReader.cpp +++ b/lib/Bitcode/Reader/BitcodeReader.cpp @@ -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 index 0000000000000000000000000000000000000000..6d5a291db6317efd82a5530e9bc8421c4396d8d1 GIT binary patch literal 612 zcmZ>AK5%a#4+FzA1_lQ1CItp2AT0#M?2XD39Z#@$vI!VjPGW4~QDxE)AYDKpu?XlY5I7)^ z3Sls`X)r@r1}>5iMuXxhsN9o81_nVO+nA$4=Ey^tG!aJI4rhy&!#z$7?3ESl#WM|beFh<8N~RdFtI4ef~;alRA6A>2hzeUj;se~D#RRUd&uG@V1A$gC=Sxk z9w2a5Br#1PW~;1#pyJgw2bY$VXeL3yOSxPwE*=&@6%aGHfi%br##ssp42K&T*noU& udZE4t+Y8gs literal 0 HcmV?d00001 diff --git a/test/Bitcode/invalid.test b/test/Bitcode/invalid.test index 0eacb9dbbb1..9cab227ab19 100644 --- a/test/Bitcode/invalid.test +++ b/test/Bitcode/invalid.test @@ -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 -- 2.34.1