Skip to content

Commit e732f69

Browse files
committed
[Bitcode] Report error for missing element type for attr upgrade
Otherwise this is going to crash either the TypeFinder or the Verifier.
1 parent dfeb978 commit e732f69

File tree

3 files changed

+23
-6
lines changed

3 files changed

+23
-6
lines changed

llvm/lib/Bitcode/Reader/BitcodeReader.cpp

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -702,7 +702,7 @@ class BitcodeReader : public BitcodeReaderBase, public GVMaterializer {
702702
/// Upgrades old-style typeless byval/sret/inalloca attributes by adding the
703703
/// corresponding argument's pointee type. Also upgrades intrinsics that now
704704
/// require an elementtype attribute.
705-
void propagateAttributeTypes(CallBase *CB, ArrayRef<unsigned> ArgsTys);
705+
Error propagateAttributeTypes(CallBase *CB, ArrayRef<unsigned> ArgsTys);
706706

707707
/// Converts alignment exponent (i.e. power of two (or zero)) to the
708708
/// corresponding alignment to use. If alignment is too large, returns
@@ -4081,8 +4081,8 @@ Error BitcodeReader::typeCheckLoadStoreInst(Type *ValType, Type *PtrType) {
40814081
return Error::success();
40824082
}
40834083

4084-
void BitcodeReader::propagateAttributeTypes(CallBase *CB,
4085-
ArrayRef<unsigned> ArgTyIDs) {
4084+
Error BitcodeReader::propagateAttributeTypes(CallBase *CB,
4085+
ArrayRef<unsigned> ArgTyIDs) {
40864086
for (unsigned i = 0; i != CB->arg_size(); ++i) {
40874087
for (Attribute::AttrKind Kind : {Attribute::ByVal, Attribute::StructRet,
40884088
Attribute::InAlloca}) {
@@ -4093,6 +4093,9 @@ void BitcodeReader::propagateAttributeTypes(CallBase *CB,
40934093
CB->removeParamAttr(i, Kind);
40944094

40954095
Type *PtrEltTy = getPtrElementTypeByID(ArgTyIDs[i]);
4096+
if (!PtrEltTy)
4097+
return error("Missing element type for typed attribute upgrade");
4098+
40964099
Attribute NewAttr;
40974100
switch (Kind) {
40984101
case Attribute::ByVal:
@@ -4121,6 +4124,8 @@ void BitcodeReader::propagateAttributeTypes(CallBase *CB,
41214124

41224125
if (CI.isIndirect && !CB->getParamElementType(ArgNo)) {
41234126
Type *ElemTy = getPtrElementTypeByID(ArgTyIDs[ArgNo]);
4127+
if (!ElemTy)
4128+
return error("Missing element type for inline asm upgrade");
41244129
CB->addParamAttr(
41254130
ArgNo, Attribute::get(Context, Attribute::ElementType, ElemTy));
41264131
}
@@ -4134,13 +4139,17 @@ void BitcodeReader::propagateAttributeTypes(CallBase *CB,
41344139
case Intrinsic::preserve_struct_access_index:
41354140
if (!CB->getParamElementType(0)) {
41364141
Type *ElTy = getPtrElementTypeByID(ArgTyIDs[0]);
4142+
if (!ElTy)
4143+
return error("Missing element type for elementtype upgrade");
41374144
Attribute NewAttr = Attribute::get(Context, Attribute::ElementType, ElTy);
41384145
CB->addParamAttr(0, NewAttr);
41394146
}
41404147
break;
41414148
default:
41424149
break;
41434150
}
4151+
4152+
return Error::success();
41444153
}
41454154

41464155
/// Lazily parse the specified function body block.
@@ -5067,7 +5076,8 @@ Error BitcodeReader::parseFunctionBody(Function *F) {
50675076
cast<InvokeInst>(I)->setCallingConv(
50685077
static_cast<CallingConv::ID>(CallingConv::MaxID & CCInfo));
50695078
cast<InvokeInst>(I)->setAttributes(PAL);
5070-
propagateAttributeTypes(cast<CallBase>(I), ArgTyIDs);
5079+
if (Error Err = propagateAttributeTypes(cast<CallBase>(I), ArgTyIDs))
5080+
return Err;
50715081

50725082
break;
50735083
}
@@ -5161,7 +5171,8 @@ Error BitcodeReader::parseFunctionBody(Function *F) {
51615171
cast<CallBrInst>(I)->setCallingConv(
51625172
static_cast<CallingConv::ID>((0x7ff & CCInfo) >> bitc::CALL_CCONV));
51635173
cast<CallBrInst>(I)->setAttributes(PAL);
5164-
propagateAttributeTypes(cast<CallBase>(I), ArgTyIDs);
5174+
if (Error Err = propagateAttributeTypes(cast<CallBase>(I), ArgTyIDs))
5175+
return Err;
51655176
break;
51665177
}
51675178
case bitc::FUNC_CODE_INST_UNREACHABLE: // UNREACHABLE
@@ -5773,7 +5784,8 @@ Error BitcodeReader::parseFunctionBody(Function *F) {
57735784
TCK = CallInst::TCK_NoTail;
57745785
cast<CallInst>(I)->setTailCallKind(TCK);
57755786
cast<CallInst>(I)->setAttributes(PAL);
5776-
propagateAttributeTypes(cast<CallBase>(I), ArgTyIDs);
5787+
if (Error Err = propagateAttributeTypes(cast<CallBase>(I), ArgTyIDs))
5788+
return Err;
57775789
if (FMF.any()) {
57785790
if (!isa<FPMathOperator>(I))
57795791
return error("Fast-math-flags specified for call without "
Binary file not shown.

llvm/test/Bitcode/invalid.test

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,3 +291,8 @@ RUN: not llvm-dis -disable-output %p/Inputs/invalid-diimportedentity-record.bc 2
291291
RUN: FileCheck --check-prefix=INVALID-DIIMPORTEDENTITY-RECORD %s
292292

293293
INVALID-DIIMPORTEDENTITY-RECORD: Invalid DIImportedEntity record
294+
295+
RUN: not llvm-dis -disable-output -opaque-pointers %p/Inputs/missing-element-type-for-attribute.bc 2>&1 | \
296+
RUN: FileCheck --check-prefix=MISSING-ELEMENT-TYPE-FOR-ATTRIBUTE %s
297+
298+
MISSING-ELEMENT-TYPE-FOR-ATTRIBUTE: Missing element type for typed attribute upgrade

0 commit comments

Comments
 (0)