From 292d67bb69e878104d2cdc1a1f72264e4fc2852a Mon Sep 17 00:00:00 2001 From: Richard Smith Date: Thu, 1 Nov 2012 07:22:08 +0000 Subject: -fcatch-undefined-behavior: Start checking loads and stores for null pointers. We want the diagnostic, and if the load is optimized away, we still want to trap it. Stop checking non-default address spaces; that doesn't work in general. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@167219 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/CodeGen/CGExpr.cpp | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) (limited to 'lib/CodeGen/CGExpr.cpp') diff --git a/lib/CodeGen/CGExpr.cpp b/lib/CodeGen/CGExpr.cpp index fa0449ec57..d59a72df87 100644 --- a/lib/CodeGen/CGExpr.cpp +++ b/lib/CodeGen/CGExpr.cpp @@ -479,15 +479,17 @@ void CodeGenFunction::EmitTypeCheck(TypeCheckKind TCK, SourceLocation Loc, if (!CatchUndefined) return; + // Don't check pointers outside the default address space. The null check + // isn't correct, the object-size check isn't supported by LLVM, and we can't + // communicate the addresses to the runtime handler for the vptr check. + if (Address->getType()->getPointerAddressSpace()) + return; + llvm::Value *Cond = 0; - if (TCK != TCK_Load && TCK != TCK_Store) { - // The glvalue must not be an empty glvalue. Don't bother checking this for - // loads and stores, because we will get a segfault anyway (if the operation - // isn't optimized out). - Cond = Builder.CreateICmpNE( - Address, llvm::Constant::getNullValue(Address->getType())); - } + // The glvalue must not be an empty glvalue. + Cond = Builder.CreateICmpNE( + Address, llvm::Constant::getNullValue(Address->getType())); uint64_t AlignVal = Alignment.getQuantity(); @@ -496,16 +498,14 @@ void CodeGenFunction::EmitTypeCheck(TypeCheckKind TCK, SourceLocation Loc, if (!AlignVal) AlignVal = getContext().getTypeAlignInChars(Ty).getQuantity(); - // This needs to be to the standard address space. - Address = Builder.CreateBitCast(Address, Int8PtrTy); - // The glvalue must refer to a large enough storage region. // FIXME: If -faddress-sanitizer is enabled, insert dynamic instrumentation // to check this. llvm::Value *F = CGM.getIntrinsic(llvm::Intrinsic::objectsize, IntPtrTy); llvm::Value *Min = Builder.getFalse(); + llvm::Value *CastAddr = Builder.CreateBitCast(Address, Int8PtrTy); llvm::Value *LargeEnough = - Builder.CreateICmpUGE(Builder.CreateCall2(F, Address, Min), + Builder.CreateICmpUGE(Builder.CreateCall2(F, CastAddr, Min), llvm::ConstantInt::get(IntPtrTy, Size)); Cond = Cond ? Builder.CreateAnd(Cond, LargeEnough) : LargeEnough; } -- cgit v1.2.3