From e1129ee64a97d7e647bdff6e9e3e3ce9eabe8555 Mon Sep 17 00:00:00 2001 From: Sanjoy Das Date: Tue, 2 Aug 2016 02:55:57 +0000 Subject: [PATCH] [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 --- llvm/lib/IR/Verifier.cpp | 28 +++++++++++++++------ llvm/test/Verifier/non-integral-pointers.ll | 12 +++++++++ 2 files changed, 33 insertions(+), 7 deletions(-) diff --git a/llvm/lib/IR/Verifier.cpp b/llvm/lib/IR/Verifier.cpp index 5b7f9a930fbc..ec4eb793426c 100644 --- a/llvm/lib/IR/Verifier.cpp +++ b/llvm/lib/IR/Verifier.cpp @@ -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(PtrTy->getScalarType())), + Msg); + } } bool Verifier::verifyAttributeCount(AttributeSet Attrs, unsigned Params) { @@ -3694,9 +3705,12 @@ void Verifier::visitInstruction(Instruction &I) { (i + 3 == e && isa(I)), "Cannot take the address of an inline asm!", &I); } else if (ConstantExpr *CE = dyn_cast(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 ) + // 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); } } diff --git a/llvm/test/Verifier/non-integral-pointers.ll b/llvm/test/Verifier/non-integral-pointers.ll index 221dd3505224..02eda34bf615 100644 --- a/llvm/test/Verifier/non-integral-pointers.ll +++ b/llvm/test/Verifier/non-integral-pointers.ll @@ -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) +}