Skip to content

Commit

Permalink
[ubsan] Don't check alignment if the alignment is 1
Browse files Browse the repository at this point in the history
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 D30283.

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

Differential Revision: https://reviews.llvm.org/D30285

llvm-svn: 300371
  • Loading branch information
vedantk committed Apr 14, 2017
1 parent ffd7c88 commit 116aebc
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
2 changes: 1 addition & 1 deletion clang/lib/CodeGen/CGExpr.cpp
Expand Up @@ -599,7 +599,7 @@ void CodeGenFunction::EmitTypeCheck(TypeCheckKind TCK, SourceLocation Loc,
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));
Expand Down
2 changes: 1 addition & 1 deletion clang/test/CodeGenCXX/ubsan-suppress-checks.cpp
Expand Up @@ -133,7 +133,7 @@ struct B {
// 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;
Expand Down
20 changes: 18 additions & 2 deletions clang/test/CodeGenCXX/ubsan-type-checks.cpp
Expand Up @@ -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();
}

0 comments on commit 116aebc

Please sign in to comment.