[PATCH] D37544: [ubsan] Skip alignment checks which are folded away

Phabricator via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon Oct 2 18:29:24 PDT 2017


This revision was automatically updated to reflect the committed changes.
Closed by commit rL314752: [ubsan] Skip alignment checks which are folded away (authored by vedantk).

Changed prior to commit:
  https://reviews.llvm.org/D37544?vs=116374&id=117461#toc

Repository:
  rL LLVM

https://reviews.llvm.org/D37544

Files:
  cfe/trunk/lib/CodeGen/CGExpr.cpp
  cfe/trunk/test/CodeGenCXX/ubsan-suppress-checks.cpp


Index: cfe/trunk/lib/CodeGen/CGExpr.cpp
===================================================================
--- cfe/trunk/lib/CodeGen/CGExpr.cpp
+++ cfe/trunk/lib/CodeGen/CGExpr.cpp
@@ -618,6 +618,7 @@
   auto PtrToAlloca =
       dyn_cast<llvm::AllocaInst>(Ptr->stripPointerCastsNoFollowAliases());
 
+  llvm::Value *True = llvm::ConstantInt::getTrue(getLLVMContext());
   llvm::Value *IsNonNull = nullptr;
   bool IsGuaranteedNonNull =
       SkippedChecks.has(SanitizerKind::Null) || PtrToAlloca;
@@ -629,8 +630,7 @@
 
     // The IR builder can constant-fold the null check if the pointer points to
     // a constant.
-    IsGuaranteedNonNull =
-        IsNonNull == llvm::ConstantInt::getTrue(getLLVMContext());
+    IsGuaranteedNonNull = IsNonNull == True;
 
     // Skip the null check if the pointer is known to be non-null.
     if (!IsGuaranteedNonNull) {
@@ -684,7 +684,8 @@
           PtrAsInt, llvm::ConstantInt::get(IntPtrTy, AlignVal - 1));
       llvm::Value *Aligned =
           Builder.CreateICmpEQ(Align, llvm::ConstantInt::get(IntPtrTy, 0));
-      Checks.push_back(std::make_pair(Aligned, SanitizerKind::Alignment));
+      if (Aligned != True)
+        Checks.push_back(std::make_pair(Aligned, SanitizerKind::Alignment));
     }
   }
 
Index: cfe/trunk/test/CodeGenCXX/ubsan-suppress-checks.cpp
===================================================================
--- cfe/trunk/test/CodeGenCXX/ubsan-suppress-checks.cpp
+++ cfe/trunk/test/CodeGenCXX/ubsan-suppress-checks.cpp
@@ -17,6 +17,17 @@
   // CHECK: ret void
 }
 
+// CHECK-LABEL: define void @_Z31use_us16_aligned_array_elementsv
+void use_us16_aligned_array_elements() {
+  static const unsigned short Arr[] = {0, 1, 2};
+  auto use_array = [](const unsigned short(&X)[3]) -> void {};
+  use_array(Arr);
+
+  // CHECK-NOT: br i1 true
+  // ALIGN-NOT: call void @__ubsan_handle_type_mismatch
+  // CHECK: ret void
+}
+
 struct A {
   int foo;
 
@@ -229,4 +240,5 @@
   d->load_member_3();
 
   load_non_null_pointers();
+  use_us16_aligned_array_elements();
 }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D37544.117461.patch
Type: text/x-patch
Size: 2042 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20171003/4af4d1cb/attachment.bin>


More information about the cfe-commits mailing list