[cfe-commits] r167219 - in /cfe/trunk: lib/CodeGen/CGExpr.cpp test/CodeGen/catch-undef-behavior.c

Richard Smith richard-llvm at metafoo.co.uk
Thu Nov 1 00:22:08 PDT 2012


Author: rsmith
Date: Thu Nov  1 02:22:08 2012
New Revision: 167219

URL: http://llvm.org/viewvc/llvm-project?rev=167219&view=rev
Log:
-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.

Modified:
    cfe/trunk/lib/CodeGen/CGExpr.cpp
    cfe/trunk/test/CodeGen/catch-undef-behavior.c

Modified: cfe/trunk/lib/CodeGen/CGExpr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGExpr.cpp?rev=167219&r1=167218&r2=167219&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/CGExpr.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGExpr.cpp Thu Nov  1 02:22:08 2012
@@ -479,15 +479,17 @@
   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 @@
     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;
   }

Modified: cfe/trunk/test/CodeGen/catch-undef-behavior.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGen/catch-undef-behavior.c?rev=167219&r1=167218&r2=167219&view=diff
==============================================================================
--- cfe/trunk/test/CodeGen/catch-undef-behavior.c (original)
+++ cfe/trunk/test/CodeGen/catch-undef-behavior.c Thu Nov  1 02:22:08 2012
@@ -21,14 +21,18 @@
 // CHECK: @foo
 void foo() {
   union { int i; } u;
-  // CHECK:      %[[SIZE:.*]] = call i64 @llvm.objectsize.i64({{.*}} %[[PTR:.*]], i1 false)
+  // CHECK:      %[[CHECK0:.*]] = icmp ne {{.*}}* %[[PTR:.*]], null
+
+  // CHECK:      %[[I8PTR:.*]] = bitcast i32* %[[PTR]] to i8*
+  // CHECK-NEXT: %[[SIZE:.*]] = call i64 @llvm.objectsize.i64(i8* %[[I8PTR]], i1 false)
   // CHECK-NEXT: %[[CHECK1:.*]] = icmp uge i64 %[[SIZE]], 4
+  // CHECK-NEXT: %[[CHECK01:.*]] = and i1 %[[CHECK0]], %[[CHECK1]]
 
-  // CHECK:      %[[PTRTOINT:.*]] = ptrtoint {{.*}} %[[PTR]] to i64
+  // CHECK:      %[[PTRTOINT:.*]] = ptrtoint {{.*}}* %[[PTR]] to i64
   // CHECK-NEXT: %[[MISALIGN:.*]] = and i64 %[[PTRTOINT]], 3
   // CHECK-NEXT: %[[CHECK2:.*]] = icmp eq i64 %[[MISALIGN]], 0
 
-  // CHECK:      %[[OK:.*]] = and i1 %[[CHECK1]], %[[CHECK2]]
+  // CHECK:      %[[OK:.*]] = and i1 %[[CHECK01]], %[[CHECK2]]
   // CHECK-NEXT: br i1 %[[OK]]
 
   // CHECK:      %[[ARG:.*]] = ptrtoint {{.*}} %[[PTR]] to i64
@@ -52,6 +56,12 @@
   return *a;
 }
 
+// CHECK: @addr_space
+int addr_space(int __attribute__((address_space(256))) *a) {
+  // CHECK-NOT: __ubsan
+  return *a;
+}
+
 // CHECK: @lsh_overflow
 int lsh_overflow(int a, int b) {
   // CHECK:      %[[INBOUNDS:.*]] = icmp ule i32 %[[RHS:.*]], 31





More information about the cfe-commits mailing list