CodeGen: Only emit CFI unrelated cast checks for bit casts.

We were previously emitting them for no-op casts (e.g. implicit casts
to const).

llvm-svn: 257738
This commit is contained in:
Peter Collingbourne 2016-01-14 02:49:48 +00:00
parent a6b2c4f721
commit 574975efa9
2 changed files with 17 additions and 7 deletions

View File

@ -824,7 +824,8 @@ Address CodeGenFunction::EmitPointerWithAlignment(const Expr *E,
getNaturalPointeeTypeAlignment(E->getType(), Source)); getNaturalPointeeTypeAlignment(E->getType(), Source));
} }
if (SanOpts.has(SanitizerKind::CFIUnrelatedCast)) { if (SanOpts.has(SanitizerKind::CFIUnrelatedCast) &&
CE->getCastKind() == CK_BitCast) {
if (auto PT = E->getType()->getAs<PointerType>()) if (auto PT = E->getType()->getAs<PointerType>())
EmitVTablePtrCheckForCast(PT->getPointeeType(), Addr.getPointer(), EmitVTablePtrCheckForCast(PT->getPointeeType(), Addr.getPointer(),
/*MayBeNull=*/true, /*MayBeNull=*/true,

View File

@ -8,6 +8,7 @@
struct A { struct A {
virtual void f(); virtual void f();
int i() const;
}; };
struct B : A { struct B : A {
@ -103,23 +104,31 @@ void vbrr(char &&r) {
// CHECK-UCAST-LABEL: define void @_Z3vcpPv // CHECK-UCAST-LABEL: define void @_Z3vcpPv
// CHECK-UCAST-STRICT-LABEL: define void @_Z3vcpPv // CHECK-UCAST-STRICT-LABEL: define void @_Z3vcpPv
void vcp(void *p) { void vcp(void *p) {
// CHECK-UCAST: [[P:%[^ ]*]] = call i1 @llvm.bitset.test(i8* {{%[^ ]*}}, metadata !"_ZTS1A") // CHECK-UCAST: call i1 @llvm.bitset.test(i8* {{%[^ ]*}}, metadata !"_ZTS1A")
// CHECK-UCAST-STRICT: [[P:%[^ ]*]] = call i1 @llvm.bitset.test(i8* {{%[^ ]*}}, metadata !"_ZTS1C") // CHECK-UCAST-STRICT: call i1 @llvm.bitset.test(i8* {{%[^ ]*}}, metadata !"_ZTS1C")
static_cast<C*>(p); static_cast<C*>(p);
} }
// CHECK-UCAST-LABEL: define void @_Z3bcpP1B // CHECK-UCAST-LABEL: define void @_Z3bcpP1B
// CHECK-UCAST-STRICT-LABEL: define void @_Z3bcpP1B // CHECK-UCAST-STRICT-LABEL: define void @_Z3bcpP1B
void bcp(B *p) { void bcp(B *p) {
// CHECK-UCAST: [[P:%[^ ]*]] = call i1 @llvm.bitset.test(i8* {{%[^ ]*}}, metadata !"_ZTS1A") // CHECK-UCAST: call i1 @llvm.bitset.test(i8* {{%[^ ]*}}, metadata !"_ZTS1A")
// CHECK-UCAST-STRICT: [[P:%[^ ]*]] = call i1 @llvm.bitset.test(i8* {{%[^ ]*}}, metadata !"_ZTS1C") // CHECK-UCAST-STRICT: call i1 @llvm.bitset.test(i8* {{%[^ ]*}}, metadata !"_ZTS1C")
(C *)p; (C *)p;
} }
// CHECK-UCAST-LABEL: define void @_Z8bcp_callP1B // CHECK-UCAST-LABEL: define void @_Z8bcp_callP1B
// CHECK-UCAST-STRICT-LABEL: define void @_Z8bcp_callP1B // CHECK-UCAST-STRICT-LABEL: define void @_Z8bcp_callP1B
void bcp_call(B *p) { void bcp_call(B *p) {
// CHECK-UCAST: [[P:%[^ ]*]] = call i1 @llvm.bitset.test(i8* {{%[^ ]*}}, metadata !"_ZTS1A") // CHECK-UCAST: call i1 @llvm.bitset.test(i8* {{%[^ ]*}}, metadata !"_ZTS1A")
// CHECK-UCAST-STRICT: [[P:%[^ ]*]] = call i1 @llvm.bitset.test(i8* {{%[^ ]*}}, metadata !"_ZTS1C") // CHECK-UCAST-STRICT: call i1 @llvm.bitset.test(i8* {{%[^ ]*}}, metadata !"_ZTS1C")
((C *)p)->f(); ((C *)p)->f();
} }
// CHECK-UCAST-LABEL: define i32 @_Z6a_callP1A
// CHECK-UCAST-STRICT-LABEL: define i32 @_Z6a_callP1A
int a_call(A *a) {
// CHECK-UCAST-NOT: @llvm.bitset.test
// CHECK-UCAST-STRICT-NOT: @llvm.bitset.test
return a->i();
}