X-Git-Url: http://plrg.eecs.uci.edu/git/?a=blobdiff_plain;f=lib%2FBitcode%2FReader%2FBitcodeReader.cpp;h=92af0f8603b62838f4e4a6d49653073b29372b1d;hb=6606ad944bb4e88fc41dca522c02f8f8d54aab2d;hp=56855ebb81b4c171de7c8715967e433481fe384a;hpb=fbc547da8195481feeed745f7179c593db304602;p=oota-llvm.git diff --git a/lib/Bitcode/Reader/BitcodeReader.cpp b/lib/Bitcode/Reader/BitcodeReader.cpp index 56855ebb81b..92af0f8603b 100644 --- a/lib/Bitcode/Reader/BitcodeReader.cpp +++ b/lib/Bitcode/Reader/BitcodeReader.cpp @@ -350,7 +350,7 @@ namespace { /// @brief A class for maintaining the slot number definition /// as a placeholder for the actual definition for forward constants defs. class ConstantPlaceHolder : public ConstantExpr { - void operator=(const ConstantPlaceHolder &) LLVM_DELETED_FUNCTION; + void operator=(const ConstantPlaceHolder &) = delete; public: // allocate space for exactly one operand void *operator new(size_t s) { @@ -555,9 +555,17 @@ Metadata *BitcodeReaderMDValueList::getValueFwdRef(unsigned Idx) { if (Metadata *MD = MDValuePtrs[Idx]) return MD; - // Create and return a placeholder, which will later be RAUW'd. - AnyFwdRefs = true; + // Track forward refs to be resolved later. + if (AnyFwdRefs) { + MinFwdRef = std::min(MinFwdRef, Idx); + MaxFwdRef = std::max(MaxFwdRef, Idx); + } else { + AnyFwdRefs = true; + MinFwdRef = MaxFwdRef = Idx; + } ++NumFwdRefs; + + // Create and return a placeholder, which will later be RAUW'd. Metadata *MD = MDNode::getTemporary(Context, None).release(); MDValuePtrs[Idx].reset(MD); return MD; @@ -573,7 +581,8 @@ void BitcodeReaderMDValueList::tryToResolveCycles() { return; // Resolve any cycles. - for (auto &MD : MDValuePtrs) { + for (unsigned I = MinFwdRef, E = MaxFwdRef + 1; I != E; ++I) { + auto &MD = MDValuePtrs[I]; auto *N = dyn_cast_or_null(MD); if (!N) continue; @@ -581,6 +590,9 @@ void BitcodeReaderMDValueList::tryToResolveCycles() { assert(!N->isTemporary() && "Unexpected forward reference"); N->resolveCycles(); } + + // Make sure we return early again until there's another forward ref. + AnyFwdRefs = false; } Type *BitcodeReader::getTypeByID(unsigned ID) { @@ -782,6 +794,16 @@ static Attribute::AttrKind GetAttrFromCode(uint64_t Code) { } } +std::error_code BitcodeReader::parseAlignmentValue(uint64_t Exponent, + unsigned &Alignment) { + // Note: Alignment in bitcode files is incremented by 1, so that zero + // can be used for default alignment. + if (Exponent > Value::MaxAlignmentExponent + 1) + return Error("Invalid alignment value"); + Alignment = (1 << static_cast(Exponent)) >> 1; + return std::error_code(); +} + std::error_code BitcodeReader::ParseAttrKind(uint64_t Code, Attribute::AttrKind *Kind) { *Kind = GetAttrFromCode(Code); @@ -1395,8 +1417,8 @@ std::error_code BitcodeReader::ParseMetadata() { GET_OR_DISTINCT(MDDerivedType, Record[0], (Context, Record[1], getMDString(Record[2]), getMDOrNull(Record[3]), Record[4], - getMDOrNull(Record[5]), getMD(Record[6]), Record[7], - Record[8], Record[9], Record[10], + getMDOrNull(Record[5]), getMDOrNull(Record[6]), + Record[7], Record[8], Record[9], Record[10], getMDOrNull(Record[11]))), NextMDValueNo++); break; @@ -1442,13 +1464,14 @@ std::error_code BitcodeReader::ParseMetadata() { return Error("Invalid record"); MDValueList.AssignValue( - GET_OR_DISTINCT( - MDCompileUnit, Record[0], - (Context, Record[1], getMD(Record[2]), getMDString(Record[3]), - Record[4], getMDString(Record[5]), Record[6], - getMDString(Record[7]), Record[8], getMDOrNull(Record[9]), - getMDOrNull(Record[10]), getMDOrNull(Record[11]), - getMDOrNull(Record[12]), getMDOrNull(Record[13]))), + GET_OR_DISTINCT(MDCompileUnit, Record[0], + (Context, Record[1], getMDOrNull(Record[2]), + getMDString(Record[3]), Record[4], + getMDString(Record[5]), Record[6], + getMDString(Record[7]), Record[8], + getMDOrNull(Record[9]), getMDOrNull(Record[10]), + getMDOrNull(Record[11]), getMDOrNull(Record[12]), + getMDOrNull(Record[13]))), NextMDValueNo++); break; } @@ -1503,25 +1526,24 @@ std::error_code BitcodeReader::ParseMetadata() { break; } case bitc::METADATA_TEMPLATE_TYPE: { - if (Record.size() != 4) + if (Record.size() != 3) return Error("Invalid record"); - MDValueList.AssignValue( - GET_OR_DISTINCT(MDTemplateTypeParameter, Record[0], - (Context, getMDOrNull(Record[1]), - getMDString(Record[2]), getMDOrNull(Record[3]))), - NextMDValueNo++); + MDValueList.AssignValue(GET_OR_DISTINCT(MDTemplateTypeParameter, + Record[0], + (Context, getMDString(Record[1]), + getMDOrNull(Record[2]))), + NextMDValueNo++); break; } case bitc::METADATA_TEMPLATE_VALUE: { - if (Record.size() != 6) + if (Record.size() != 5) return Error("Invalid record"); MDValueList.AssignValue( GET_OR_DISTINCT(MDTemplateValueParameter, Record[0], - (Context, Record[1], getMDOrNull(Record[2]), - getMDString(Record[3]), getMDOrNull(Record[4]), - getMDOrNull(Record[5]))), + (Context, Record[1], getMDString(Record[2]), + getMDOrNull(Record[3]), getMDOrNull(Record[4]))), NextMDValueNo++); break; } @@ -1539,6 +1561,54 @@ std::error_code BitcodeReader::ParseMetadata() { NextMDValueNo++); break; } + case bitc::METADATA_LOCAL_VAR: { + if (Record.size() != 10) + return Error("Invalid record"); + + MDValueList.AssignValue( + GET_OR_DISTINCT(MDLocalVariable, Record[0], + (Context, Record[1], getMDOrNull(Record[2]), + getMDString(Record[3]), getMDOrNull(Record[4]), + Record[5], getMDOrNull(Record[6]), Record[7], + Record[8], getMDOrNull(Record[9]))), + NextMDValueNo++); + break; + } + case bitc::METADATA_EXPRESSION: { + if (Record.size() < 1) + return Error("Invalid record"); + + MDValueList.AssignValue( + GET_OR_DISTINCT(MDExpression, Record[0], + (Context, makeArrayRef(Record).slice(1))), + NextMDValueNo++); + break; + } + case bitc::METADATA_OBJC_PROPERTY: { + if (Record.size() != 8) + return Error("Invalid record"); + + MDValueList.AssignValue( + GET_OR_DISTINCT(MDObjCProperty, Record[0], + (Context, getMDString(Record[1]), + getMDOrNull(Record[2]), Record[3], + getMDString(Record[4]), getMDString(Record[5]), + Record[6], getMDOrNull(Record[7]))), + NextMDValueNo++); + break; + } + case bitc::METADATA_IMPORTED_ENTITY: { + if (Record.size() != 6) + return Error("Invalid record"); + + MDValueList.AssignValue( + GET_OR_DISTINCT(MDImportedEntity, Record[0], + (Context, Record[1], getMDOrNull(Record[2]), + getMDOrNull(Record[3]), Record[4], + getMDString(Record[5]))), + NextMDValueNo++); + break; + } case bitc::METADATA_STRING: { std::string String(Record.begin(), Record.end()); llvm::UpgradeMDStringConstant(String); @@ -2402,7 +2472,9 @@ std::error_code BitcodeReader::ParseModule(bool Resume) { bool isConstant = Record[1]; uint64_t RawLinkage = Record[3]; GlobalValue::LinkageTypes Linkage = getDecodedLinkage(RawLinkage); - unsigned Alignment = (1 << Record[4]) >> 1; + unsigned Alignment; + if (std::error_code EC = parseAlignmentValue(Record[4], Alignment)) + return EC; std::string Section; if (Record[5]) { if (Record[5]-1 >= SectionTable.size()) @@ -2482,7 +2554,10 @@ std::error_code BitcodeReader::ParseModule(bool Resume) { Func->setLinkage(getDecodedLinkage(RawLinkage)); Func->setAttributes(getAttributes(Record[4])); - Func->setAlignment((1 << Record[5]) >> 1); + unsigned Alignment; + if (std::error_code EC = parseAlignmentValue(Record[5], Alignment)) + return EC; + Func->setAlignment(Alignment); if (Record[6]) { if (Record[6]-1 >= SectionTable.size()) return Error("Invalid ID"); @@ -2987,9 +3062,22 @@ std::error_code BitcodeReader::ParseFunctionBody(Function *F) { InstructionList.push_back(I); break; } - case bitc::FUNC_CODE_INST_INBOUNDS_GEP: - case bitc::FUNC_CODE_INST_GEP: { // GEP: [n x operands] + case bitc::FUNC_CODE_INST_INBOUNDS_GEP_OLD: + case bitc::FUNC_CODE_INST_GEP_OLD: + case bitc::FUNC_CODE_INST_GEP: { // GEP: type, [n x operands] unsigned OpNum = 0; + + Type *Ty; + bool InBounds; + + if (BitCode == bitc::FUNC_CODE_INST_GEP) { + InBounds = Record[OpNum++]; + Ty = getTypeByID(Record[OpNum++]); + } else { + InBounds = BitCode == bitc::FUNC_CODE_INST_INBOUNDS_GEP_OLD; + Ty = nullptr; + } + Value *BasePtr; if (getValueTypePair(Record, OpNum, NextValueNo, BasePtr)) return Error("Invalid record"); @@ -3003,8 +3091,9 @@ std::error_code BitcodeReader::ParseFunctionBody(Function *F) { } I = GetElementPtrInst::Create(BasePtr, GEPIdx); + assert(!Ty || Ty == cast(I)->getSourceElementType()); InstructionList.push_back(I); - if (BitCode == bitc::FUNC_CODE_INST_INBOUNDS_GEP) + if (InBounds) cast(I)->setIsInBounds(true); break; } @@ -3017,12 +3106,27 @@ std::error_code BitcodeReader::ParseFunctionBody(Function *F) { return Error("Invalid record"); SmallVector EXTRACTVALIdx; + Type *CurTy = Agg->getType(); for (unsigned RecSize = Record.size(); OpNum != RecSize; ++OpNum) { + bool IsArray = CurTy->isArrayTy(); + bool IsStruct = CurTy->isStructTy(); uint64_t Index = Record[OpNum]; + + if (!IsStruct && !IsArray) + return Error("EXTRACTVAL: Invalid type"); if ((unsigned)Index != Index) return Error("Invalid value"); + if (IsStruct && Index >= CurTy->subtypes().size()) + return Error("EXTRACTVAL: Invalid struct index"); + if (IsArray && Index >= CurTy->getArrayNumElements()) + return Error("EXTRACTVAL: Invalid array index"); EXTRACTVALIdx.push_back((unsigned)Index); + + if (IsStruct) + CurTy = CurTy->subtypes()[Index]; + else + CurTy = CurTy->subtypes()[0]; } I = ExtractValueInst::Create(Agg, EXTRACTVALIdx); @@ -3041,12 +3145,29 @@ std::error_code BitcodeReader::ParseFunctionBody(Function *F) { return Error("Invalid record"); SmallVector INSERTVALIdx; + Type *CurTy = Agg->getType(); for (unsigned RecSize = Record.size(); OpNum != RecSize; ++OpNum) { + bool IsArray = CurTy->isArrayTy(); + bool IsStruct = CurTy->isStructTy(); uint64_t Index = Record[OpNum]; + + if (!IsStruct && !IsArray) + return Error("INSERTVAL: Invalid type"); + if (!CurTy->isStructTy() && !CurTy->isArrayTy()) + return Error("Invalid type"); if ((unsigned)Index != Index) return Error("Invalid value"); + if (IsStruct && Index >= CurTy->subtypes().size()) + return Error("INSERTVAL: Invalid struct index"); + if (IsArray && Index >= CurTy->getArrayNumElements()) + return Error("INSERTVAL: Invalid array index"); + INSERTVALIdx.push_back((unsigned)Index); + if (IsStruct) + CurTy = CurTy->subtypes()[Index]; + else + CurTy = CurTy->subtypes()[0]; } I = InsertValueInst::Create(Agg, Val, INSERTVALIdx); @@ -3447,12 +3568,17 @@ std::error_code BitcodeReader::ParseFunctionBody(Function *F) { dyn_cast_or_null(getTypeByID(Record[0])); Type *OpTy = getTypeByID(Record[1]); Value *Size = getFnValueByID(Record[2], OpTy); - unsigned AlignRecord = Record[3]; - bool InAlloca = AlignRecord & (1 << 5); - unsigned Align = AlignRecord & ((1 << 5) - 1); + uint64_t AlignRecord = Record[3]; + const uint64_t InAllocaMask = uint64_t(1) << 5; + bool InAlloca = AlignRecord & InAllocaMask; + unsigned Align; + if (std::error_code EC = + parseAlignmentValue(AlignRecord & ~InAllocaMask, Align)) { + return EC; + } if (!Ty || !Size) return Error("Invalid record"); - AllocaInst *AI = new AllocaInst(Ty->getElementType(), Size, (1 << Align) >> 1); + AllocaInst *AI = new AllocaInst(Ty->getElementType(), Size, Align); AI->setUsedWithInAlloca(InAlloca); I = AI; InstructionList.push_back(I); @@ -3462,10 +3588,21 @@ std::error_code BitcodeReader::ParseFunctionBody(Function *F) { unsigned OpNum = 0; Value *Op; if (getValueTypePair(Record, OpNum, NextValueNo, Op) || - OpNum+2 != Record.size()) + (OpNum + 2 != Record.size() && OpNum + 3 != Record.size())) return Error("Invalid record"); - I = new LoadInst(Op, "", Record[OpNum+1], (1 << Record[OpNum]) >> 1); + Type *Ty = nullptr; + if (OpNum + 3 == Record.size()) + Ty = getTypeByID(Record[OpNum++]); + + unsigned Align; + if (std::error_code EC = parseAlignmentValue(Record[OpNum], Align)) + return EC; + I = new LoadInst(Op, "", Record[OpNum+1], Align); + + assert((!Ty || Ty == I->getType()) && + "Explicit type doesn't match pointee type of the first operand"); + InstructionList.push_back(I); break; } @@ -3474,9 +3611,13 @@ std::error_code BitcodeReader::ParseFunctionBody(Function *F) { unsigned OpNum = 0; Value *Op; if (getValueTypePair(Record, OpNum, NextValueNo, Op) || - OpNum+4 != Record.size()) + (OpNum + 4 != Record.size() && OpNum + 5 != Record.size())) return Error("Invalid record"); + Type *Ty = nullptr; + if (OpNum + 5 == Record.size()) + Ty = getTypeByID(Record[OpNum++]); + AtomicOrdering Ordering = GetDecodedOrdering(Record[OpNum+2]); if (Ordering == NotAtomic || Ordering == Release || Ordering == AcquireRelease) @@ -3485,8 +3626,14 @@ std::error_code BitcodeReader::ParseFunctionBody(Function *F) { return Error("Invalid record"); SynchronizationScope SynchScope = GetDecodedSynchScope(Record[OpNum+3]); - I = new LoadInst(Op, "", Record[OpNum+1], (1 << Record[OpNum]) >> 1, - Ordering, SynchScope); + unsigned Align; + if (std::error_code EC = parseAlignmentValue(Record[OpNum], Align)) + return EC; + I = new LoadInst(Op, "", Record[OpNum+1], Align, Ordering, SynchScope); + + assert((!Ty || Ty == I->getType()) && + "Explicit type doesn't match pointee type of the first operand"); + InstructionList.push_back(I); break; } @@ -3498,8 +3645,10 @@ std::error_code BitcodeReader::ParseFunctionBody(Function *F) { cast(Ptr->getType())->getElementType(), Val) || OpNum+2 != Record.size()) return Error("Invalid record"); - - I = new StoreInst(Val, Ptr, Record[OpNum+1], (1 << Record[OpNum]) >> 1); + unsigned Align; + if (std::error_code EC = parseAlignmentValue(Record[OpNum], Align)) + return EC; + I = new StoreInst(Val, Ptr, Record[OpNum+1], Align); InstructionList.push_back(I); break; } @@ -3521,8 +3670,10 @@ std::error_code BitcodeReader::ParseFunctionBody(Function *F) { if (Ordering != NotAtomic && Record[OpNum] == 0) return Error("Invalid record"); - I = new StoreInst(Val, Ptr, Record[OpNum+1], (1 << Record[OpNum]) >> 1, - Ordering, SynchScope); + unsigned Align; + if (std::error_code EC = parseAlignmentValue(Record[OpNum], Align)) + return EC; + I = new StoreInst(Val, Ptr, Record[OpNum+1], Align, Ordering, SynchScope); InstructionList.push_back(I); break; }