[Verifier] Disallow illegal ptr<->int casts in ConstantExprs

This should have been a part of rL277085, but I hadn't considered this
case.

llvm-svn: 277413
This commit is contained in:
Sanjoy Das 2016-08-02 02:55:57 +00:00
parent 42327a32b2
commit e1129ee64a
2 changed files with 33 additions and 7 deletions

View File

@ -1641,12 +1641,23 @@ void Verifier::visitConstantExprsRecursively(const Constant *EntryC) {
}
void Verifier::visitConstantExpr(const ConstantExpr *CE) {
if (CE->getOpcode() != Instruction::BitCast)
return;
if (CE->getOpcode() == Instruction::BitCast)
Assert(CastInst::castIsValid(Instruction::BitCast, CE->getOperand(0),
CE->getType()),
"Invalid bitcast", CE);
Assert(CastInst::castIsValid(Instruction::BitCast, CE->getOperand(0),
CE->getType()),
"Invalid bitcast", CE);
if (CE->getOpcode() == Instruction::IntToPtr ||
CE->getOpcode() == Instruction::PtrToInt) {
auto *PtrTy = CE->getOpcode() == Instruction::IntToPtr
? CE->getType()
: CE->getOperand(0)->getType();
StringRef Msg = CE->getOpcode() == Instruction::IntToPtr
? "inttoptr not supported for non-integral pointers"
: "ptrtoint not supported for non-integral pointers";
Assert(
!DL.isNonIntegralPointerType(cast<PointerType>(PtrTy->getScalarType())),
Msg);
}
}
bool Verifier::verifyAttributeCount(AttributeSet Attrs, unsigned Params) {
@ -3694,9 +3705,12 @@ void Verifier::visitInstruction(Instruction &I) {
(i + 3 == e && isa<InvokeInst>(I)),
"Cannot take the address of an inline asm!", &I);
} else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(I.getOperand(i))) {
if (CE->getType()->isPtrOrPtrVectorTy()) {
if (CE->getType()->isPtrOrPtrVectorTy() ||
!DL.getNonIntegralAddressSpaces().empty()) {
// If we have a ConstantExpr pointer, we need to see if it came from an
// illegal bitcast (inttoptr <constant int> )
// illegal bitcast. If the datalayout string specifies non-integral
// address spaces then we also need to check for illegal ptrtoint and
// inttoptr expressions.
visitConstantExprsRecursively(CE);
}
}

View File

@ -43,3 +43,15 @@ define i64 @f_6(i8 addrspace(6)* %ptr) {
%val = ptrtoint i8 addrspace(6)* %ptr to i64
ret i64 %val
}
define i8 addrspace(4)* @f_7() {
; CHECK: inttoptr not supported for non-integral pointers
ret i8 addrspace(4)* inttoptr (i64 50 to i8 addrspace(4)*)
}
@global0 = addrspace(4) constant i8 42
define i64 @f_8() {
; CHECK: ptrtoint not supported for non-integral pointers
ret i64 ptrtoint (i8 addrspace(4)* @global0 to i64)
}