From 6ac86cf119640b71328a3530f2906a86487696c9 Mon Sep 17 00:00:00 2001 From: Karthik Bhat Date: Thu, 27 Mar 2014 12:08:23 +0000 Subject: [PATCH] All new elements except the last one initialized to NULL. Ideally, once parsing is complete, all elements should be non-NULL. To safe-guard BitcodeReader, this patch adds null check for all access to these list. Patch by Dinesh Dwivedi! git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@204920 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Bitcode/Reader/BitcodeReader.cpp | 14 +-- test/Bitcode/pr18704.ll | 158 +++++++++++++++++++++++++++ test/Bitcode/pr18704.ll.bc | Bin 0 -> 880 bytes 3 files changed, 165 insertions(+), 7 deletions(-) create mode 100644 test/Bitcode/pr18704.ll create mode 100644 test/Bitcode/pr18704.ll.bc diff --git a/lib/Bitcode/Reader/BitcodeReader.cpp b/lib/Bitcode/Reader/BitcodeReader.cpp index ab0a1249f2d..f712d9db641 100644 --- a/lib/Bitcode/Reader/BitcodeReader.cpp +++ b/lib/Bitcode/Reader/BitcodeReader.cpp @@ -960,7 +960,7 @@ error_code BitcodeReader::ParseValueSymbolTable() { if (ConvertToString(Record, 1, ValueName)) return Error(InvalidRecord); unsigned ValueID = Record[0]; - if (ValueID >= ValueList.size()) + if (ValueID >= ValueList.size() || !ValueList[ValueID]) return Error(InvalidRecord); Value *V = ValueList[ValueID]; @@ -1027,7 +1027,7 @@ error_code BitcodeReader::ParseMetadata() { unsigned Size = Record.size(); NamedMDNode *NMD = TheModule->getOrInsertNamedMetadata(Name); for (unsigned i = 0; i != Size; ++i) { - MDNode *MD = dyn_cast(MDValueList.getValueFwdRef(Record[i])); + MDNode *MD = dyn_cast_or_null(MDValueList.getValueFwdRef(Record[i])); if (MD == 0) return Error(InvalidRecord); NMD->addOperand(MD); @@ -1109,7 +1109,7 @@ error_code BitcodeReader::ResolveGlobalAndAliasInits() { // Not ready to resolve this yet, it requires something later in the file. GlobalInits.push_back(GlobalInitWorklist.back()); } else { - if (Constant *C = dyn_cast(ValueList[ValID])) + if (Constant *C = dyn_cast_or_null(ValueList[ValID])) GlobalInitWorklist.back().first->setInitializer(C); else return Error(ExpectedConstant); @@ -1122,7 +1122,7 @@ error_code BitcodeReader::ResolveGlobalAndAliasInits() { if (ValID >= ValueList.size()) { AliasInits.push_back(AliasInitWorklist.back()); } else { - if (Constant *C = dyn_cast(ValueList[ValID])) + if (Constant *C = dyn_cast_or_null(ValueList[ValID])) AliasInitWorklist.back().first->setAliasee(C); else return Error(ExpectedConstant); @@ -1135,7 +1135,7 @@ error_code BitcodeReader::ResolveGlobalAndAliasInits() { if (ValID >= ValueList.size()) { FunctionPrefixes.push_back(FunctionPrefixWorklist.back()); } else { - if (Constant *C = dyn_cast(ValueList[ValID])) + if (Constant *C = dyn_cast_or_null(ValueList[ValID])) FunctionPrefixWorklist.back().first->setPrefixData(C); else return Error(ExpectedConstant); @@ -1195,7 +1195,7 @@ error_code BitcodeReader::ParseConstants() { case bitc::CST_CODE_SETTYPE: // SETTYPE: [typeid] if (Record.empty()) return Error(InvalidRecord); - if (Record[0] >= TypeList.size()) + if (Record[0] >= TypeList.size() || !TypeList[Record[0]]) return Error(InvalidRecord); CurTy = TypeList[Record[0]]; continue; // Skip the ValueList manipulation. @@ -3039,7 +3039,7 @@ OutOfRecordLoop: if (A->getParent() == 0) { // We found at least one unresolved value. Nuke them all to avoid leaks. for (unsigned i = ModuleValueListSize, e = ValueList.size(); i != e; ++i){ - if ((A = dyn_cast(ValueList[i])) && A->getParent() == 0) { + if ((A = dyn_cast_or_null(ValueList[i])) && A->getParent() == 0) { A->replaceAllUsesWith(UndefValue::get(A->getType())); delete A; } diff --git a/test/Bitcode/pr18704.ll b/test/Bitcode/pr18704.ll new file mode 100644 index 00000000000..f05fe53259f --- /dev/null +++ b/test/Bitcode/pr18704.ll @@ -0,0 +1,158 @@ +; RUN: not llvm-dis < %s.bc 2>&1 | FileCheck %s + +; CHECK: llvm-dis{{(\.EXE|\.exe)?}}: Never resolved value found in function + +; pr18704.ll.bc has an instruction referring to invalid type. +; The test checks that LLVM reports the error and doesn't access freed memory +; in doing so. + +; +; +; +; +; +; +; +; +; +; +; +; +; +; +; +; +; +; +; +; +; +; +; +; +; +; +; +; +; +; +; +; +; +; +; +; +; +; +; +; +; +; +; +; +; +; +; +; +; +; +; +; +; +; +; +; +; +; +; +; +; +; +; +; +; +; +; +; +; +; +; +; +; +; +; +; +; +; +; +; +; +; +; +; +; +; +; +; +; +; +; +; +; +; +; +; +; +; +; +; +; +; +; +; +; +; +; +; +; +; +; +; +; +; +; +; +; +; +; +; +; +; +; +; +; +; +; +; +; +; +; +; +; +; +; +; +; +; +; +; +; +; +; +; +; +; +; +; +; +; diff --git a/test/Bitcode/pr18704.ll.bc b/test/Bitcode/pr18704.ll.bc new file mode 100644 index 0000000000000000000000000000000000000000..dbfcf37b82abc2f90c4487b4caddd72edf0d613d GIT binary patch literal 880 zcmZ8fT}V@57=E{%?VN2VJ=4?NNaf7Aur*|yDgNWOv+DRm%%8ySHW5+qVoM2E7oO>H z{xoAw6E8PeV9|wP7hOb58{&mYtczMjSV_=Dkr2^EVBa>q>4Wcko{#tap7;G8UMH9Q zXdnSH0HB^^K#H&kZ9HgPbnIen3W{xrN<`8mC-mjmoqmD7By~pB&IHBM^@OfX#kwka zRm5OSbql24aHA66^bGA`)`6uF2xJ?qLC4-ZL8^l%_tF4$_jAS*~7u5q1iAU>qM^^^;$r@Hx2AYuV*m!m^gV@$i@rzfIr)e#Ym+Z~0_5$7g&Dj(YYp0A&ajTU1H1S$$-K zbYId?zi?;~mCR)qRh-uSrZl-w2WTs-Y?;Et_lPy21|7L!x$7ZFzhrNdny8foYu#zu zuyE9YqL=47U6^Gh^0ze?-SO#Cz3v5%3eR1&4lATlO)sjAjE7>(Vad8ILz@nUV?PO6x@#8m6rOxSM)Od$s*MglZJoWVF0Y-mg==Y#!1h*P`a( zD%l0|gD3!}vHdwdZY98q6hTXtJTtLiRLEh(;dgw{$fPjoMMZ_r1WVv(2C`$2uZ0L& pUIw5L;c1PNo|o^R^gI^SOiAI9{VDTfRw99|NKZZWzXo~^{{T%4+E@Sp literal 0 HcmV?d00001