Skip to content

Commit

Permalink
[Bitcode] Verify types for aggregate initializers
Browse files Browse the repository at this point in the history
Unfortunately all the nice error messages get lost because we
don't forward errors from lazy value materialization.

Fixes #117707.
  • Loading branch information
nikic committed Nov 28, 2024
1 parent 992b000 commit 98204a2
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 5 deletions.
37 changes: 32 additions & 5 deletions llvm/lib/Bitcode/Reader/BitcodeReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1663,15 +1663,42 @@ Expected<Value *> BitcodeReader::materializeValue(unsigned StartValID,
C = BlockAddress::get(Fn, BB);
break;
}
case BitcodeConstant::ConstantStructOpcode:
C = ConstantStruct::get(cast<StructType>(BC->getType()), ConstOps);
case BitcodeConstant::ConstantStructOpcode: {
auto *ST = cast<StructType>(BC->getType());
if (ST->getNumElements() != ConstOps.size())
return error("Invalid number of elements in struct initializer");

for (const auto [Ty, Op] : zip(ST->elements(), ConstOps))
if (Op->getType() != Ty)
return error("Incorrect type in struct initializer");

C = ConstantStruct::get(ST, ConstOps);
break;
case BitcodeConstant::ConstantArrayOpcode:
C = ConstantArray::get(cast<ArrayType>(BC->getType()), ConstOps);
}
case BitcodeConstant::ConstantArrayOpcode: {
auto *AT = cast<ArrayType>(BC->getType());
if (AT->getNumElements() != ConstOps.size())
return error("Invalid number of elements in array initializer");

for (Constant *Op : ConstOps)
if (Op->getType() != AT->getElementType())
return error("Incorrect type in array initializer");

C = ConstantArray::get(AT, ConstOps);
break;
case BitcodeConstant::ConstantVectorOpcode:
}
case BitcodeConstant::ConstantVectorOpcode: {
auto *VT = cast<FixedVectorType>(BC->getType());
if (VT->getNumElements() != ConstOps.size())
return error("Invalid number of elements in vector initializer");

for (Constant *Op : ConstOps)
if (Op->getType() != VT->getElementType())
return error("Incorrect type in vector initializer");

C = ConstantVector::get(ConstOps);
break;
}
case Instruction::GetElementPtr:
C = ConstantExpr::getGetElementPtr(
BC->SrcElemTy, ConstOps[0], ArrayRef(ConstOps).drop_front(),
Expand Down
Binary file added llvm/test/Bitcode/Inputs/invalid-initializer.bc
Binary file not shown.
5 changes: 5 additions & 0 deletions llvm/test/Bitcode/invalid.test
Original file line number Diff line number Diff line change
Expand Up @@ -285,3 +285,8 @@ RUN: not llvm-dis -disable-output %p/Inputs/invalid-forward-declare.bc 2>&1 | \
RUN: FileCheck --check-prefix=INVALID-FORWARD-DECLARE %s

INVALID-FORWARD-DECLARE: Assigned value does not match type of forward declaration

RUN: not llvm-dis -disable-output %p/Inputs/invalid-initializer.bc 2>&1 | \
RUN: FileCheck --check-prefix=INVALID-INITIALIZER %s

INVALID-INITIALIZER: Invalid record

0 comments on commit 98204a2

Please sign in to comment.