Skip to content

Commit 98204a2

Browse files
committed
[Bitcode] Verify types for aggregate initializers
Unfortunately all the nice error messages get lost because we don't forward errors from lazy value materialization. Fixes llvm#117707.
1 parent 992b000 commit 98204a2

File tree

3 files changed

+37
-5
lines changed

3 files changed

+37
-5
lines changed

llvm/lib/Bitcode/Reader/BitcodeReader.cpp

+32-5
Original file line numberDiff line numberDiff line change
@@ -1663,15 +1663,42 @@ Expected<Value *> BitcodeReader::materializeValue(unsigned StartValID,
16631663
C = BlockAddress::get(Fn, BB);
16641664
break;
16651665
}
1666-
case BitcodeConstant::ConstantStructOpcode:
1667-
C = ConstantStruct::get(cast<StructType>(BC->getType()), ConstOps);
1666+
case BitcodeConstant::ConstantStructOpcode: {
1667+
auto *ST = cast<StructType>(BC->getType());
1668+
if (ST->getNumElements() != ConstOps.size())
1669+
return error("Invalid number of elements in struct initializer");
1670+
1671+
for (const auto [Ty, Op] : zip(ST->elements(), ConstOps))
1672+
if (Op->getType() != Ty)
1673+
return error("Incorrect type in struct initializer");
1674+
1675+
C = ConstantStruct::get(ST, ConstOps);
16681676
break;
1669-
case BitcodeConstant::ConstantArrayOpcode:
1670-
C = ConstantArray::get(cast<ArrayType>(BC->getType()), ConstOps);
1677+
}
1678+
case BitcodeConstant::ConstantArrayOpcode: {
1679+
auto *AT = cast<ArrayType>(BC->getType());
1680+
if (AT->getNumElements() != ConstOps.size())
1681+
return error("Invalid number of elements in array initializer");
1682+
1683+
for (Constant *Op : ConstOps)
1684+
if (Op->getType() != AT->getElementType())
1685+
return error("Incorrect type in array initializer");
1686+
1687+
C = ConstantArray::get(AT, ConstOps);
16711688
break;
1672-
case BitcodeConstant::ConstantVectorOpcode:
1689+
}
1690+
case BitcodeConstant::ConstantVectorOpcode: {
1691+
auto *VT = cast<FixedVectorType>(BC->getType());
1692+
if (VT->getNumElements() != ConstOps.size())
1693+
return error("Invalid number of elements in vector initializer");
1694+
1695+
for (Constant *Op : ConstOps)
1696+
if (Op->getType() != VT->getElementType())
1697+
return error("Incorrect type in vector initializer");
1698+
16731699
C = ConstantVector::get(ConstOps);
16741700
break;
1701+
}
16751702
case Instruction::GetElementPtr:
16761703
C = ConstantExpr::getGetElementPtr(
16771704
BC->SrcElemTy, ConstOps[0], ArrayRef(ConstOps).drop_front(),
1.42 KB
Binary file not shown.

llvm/test/Bitcode/invalid.test

+5
Original file line numberDiff line numberDiff line change
@@ -285,3 +285,8 @@ RUN: not llvm-dis -disable-output %p/Inputs/invalid-forward-declare.bc 2>&1 | \
285285
RUN: FileCheck --check-prefix=INVALID-FORWARD-DECLARE %s
286286

287287
INVALID-FORWARD-DECLARE: Assigned value does not match type of forward declaration
288+
289+
RUN: not llvm-dis -disable-output %p/Inputs/invalid-initializer.bc 2>&1 | \
290+
RUN: FileCheck --check-prefix=INVALID-INITIALIZER %s
291+
292+
INVALID-INITIALIZER: Invalid record

0 commit comments

Comments
 (0)