[PATCH] D30285: [ubsan] Don't check alignment if the alignment is 1

Vedant Kumar via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Feb 22 18:30:44 PST 2017


vsk created this revision.

If a pointer is 1-byte aligned, there's no use in checking its
alignment. Somewhat surprisingly, ubsan can spend a significant amount
of time doing just that!

This loosely depends on https://reviews.llvm.org/D30283.

Testing: check-clang, check-ubsan, and a stage2 ubsan build.

I also compiled X86FastISel.cpp with -fsanitize=alignment using
patched/unpatched clangs based on r295686 with https://reviews.llvm.org/D30283 applied. Here are
the number of alignment checks emitted:

| Setup              | # of alignment checks |
| unpatched + https://reviews.llvm.org/D30283, -O0 | 14307                 |
| patched + https://reviews.llvm.org/D30283, -O0   | 12515                 |


https://reviews.llvm.org/D30285

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


Index: test/CodeGenCXX/ubsan-type-checks.cpp
===================================================================
--- test/CodeGenCXX/ubsan-type-checks.cpp
+++ test/CodeGenCXX/ubsan-type-checks.cpp
@@ -5,16 +5,32 @@
 struct A {
   // COMMON-LABEL: define linkonce_odr void @_ZN1A10do_nothingEv
   void do_nothing() {
-    // ALIGN: ptrtoint %struct.A* %{{.*}} to i64, !nosanitize
-    // ALIGN: and i64 %{{.*}}, 0, !nosanitize
+    // ALIGN-NOT: ptrtoint %struct.A* %{{.*}} to i64, !nosanitize
  
     // NULL: icmp ne %struct.A* %{{.*}}, null, !nosanitize
  
     // OBJSIZE-NOT: call i64 @llvm.objectsize
   }
 };
 
+struct B {
+  int x;
+
+  // COMMON-LABEL: define linkonce_odr void @_ZN1B10do_nothingEv
+  void do_nothing() {
+    // ALIGN: ptrtoint %struct.B* %{{.*}} to i64, !nosanitize
+    // ALIGN: and i64 %{{.*}}, 3, !nosanitize
+
+    // NULL: icmp ne %struct.B* %{{.*}}, null, !nosanitize
+
+    // OBJSIZE-NOT: call i64 @llvm.objectsize
+  }
+};
+
 void force_irgen() {
   A a;
   a.do_nothing();
+
+  B b;
+  b.do_nothing();
 }
Index: test/CodeGenCXX/ubsan-suppress-checks.cpp
===================================================================
--- test/CodeGenCXX/ubsan-suppress-checks.cpp
+++ test/CodeGenCXX/ubsan-suppress-checks.cpp
@@ -124,7 +124,7 @@
     // CHECK: call void @__ubsan_handle_type_mismatch
     //
     // Check the result of the conversion before using it.
-    // CHECK: call void @__ubsan_handle_type_mismatch
+    // NULL: call void @__ubsan_handle_type_mismatch
     //
     // CHECK-NOT: call void @__ubsan_handle_type_mismatch
     B b;
Index: lib/CodeGen/CGExpr.cpp
===================================================================
--- lib/CodeGen/CGExpr.cpp
+++ lib/CodeGen/CGExpr.cpp
@@ -597,7 +597,7 @@
       AlignVal = getContext().getTypeAlignInChars(Ty).getQuantity();
 
     // The glvalue must be suitably aligned.
-    if (AlignVal) {
+    if (AlignVal > 1) {
       llvm::Value *Align =
           Builder.CreateAnd(Builder.CreatePtrToInt(Ptr, IntPtrTy),
                             llvm::ConstantInt::get(IntPtrTy, AlignVal - 1));


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D30285.89458.patch
Type: text/x-patch
Size: 2091 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20170223/44a429de/attachment.bin>


More information about the cfe-commits mailing list