diff options
author | Nikita Popov <npopov@redhat.com> | 2024-11-28 11:15:32 +0100 |
---|---|---|
committer | Nikita Popov <npopov@redhat.com> | 2024-11-28 11:21:38 +0100 |
commit | 98204a2e5bb754b0260175e42614c5463807beb5 (patch) | |
tree | 61cf2ade686bf5054a5c413ad1151a0bccd92d10 /llvm/lib/Bitcode/Reader/BitcodeReader.cpp | |
parent | 992b00020fae2d8b0f150a45885768551a867a10 (diff) | |
download | llvm-98204a2e5bb754b0260175e42614c5463807beb5.zip llvm-98204a2e5bb754b0260175e42614c5463807beb5.tar.gz llvm-98204a2e5bb754b0260175e42614c5463807beb5.tar.bz2 |
[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 https://github.com/llvm/llvm-project/issues/117707.
Diffstat (limited to 'llvm/lib/Bitcode/Reader/BitcodeReader.cpp')
-rw-r--r-- | llvm/lib/Bitcode/Reader/BitcodeReader.cpp | 37 |
1 files changed, 32 insertions, 5 deletions
diff --git a/llvm/lib/Bitcode/Reader/BitcodeReader.cpp b/llvm/lib/Bitcode/Reader/BitcodeReader.cpp index 11fbe6e..a585a24 100644 --- a/llvm/lib/Bitcode/Reader/BitcodeReader.cpp +++ b/llvm/lib/Bitcode/Reader/BitcodeReader.cpp @@ -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(), |