1 // RUN: %clang_cc1 -std=c++11 -triple x86_64-unknown-linux -emit-llvm -fsanitize=null %s -o - | FileCheck %s --check-prefix=CHECK-NULL --check-prefix=ITANIUM
2 // RUN: %clang_cc1 -std=c++11 -triple x86_64-windows -emit-llvm -fsanitize=null %s -o - | FileCheck %s --check-prefix=CHECK-NULL --check-prefix=MSABI
3 // RUN: %clang_cc1 -std=c++11 -triple x86_64-unknown-linux -emit-llvm -fsanitize=null,vptr %s -o - | FileCheck %s --check-prefix=CHECK-VPTR --check-prefix=ITANIUM
4 // RUN: %clang_cc1 -std=c++11 -triple x86_64-windows -emit-llvm -fsanitize=null,vptr %s -o - | FileCheck %s --check-prefix=CHECK-VPTR --check-prefix=MSABI  --check-prefix=CHECK-VPTR-MS
5 struct T {
~TT6   virtual ~T() {}
vT7   virtual int v() { return 1; }
8 };
9 
10 struct U : T {
11   ~U();
vU12   virtual int v() { return 2; }
13 };
14 
~U()15 U::~U() {}
16 
17 // CHECK-VPTR-MS: @__ubsan_vptr_type_cache = external dso_local
18 
19 // ITANIUM: define{{.*}} i32 @_Z5get_vP1T
20 // MSABI: define dso_local i32 @"?get_v
get_v(T * t)21 int get_v(T* t) {
22   // First, we check that vtable is not loaded before a type check.
23   // CHECK-NULL-NOT: load {{.*}} (%struct.T*{{.*}})**, {{.*}} (%struct.T*{{.*}})***
24   // CHECK-NULL: [[UBSAN_CMP_RES:%[0-9]+]] = icmp ne %struct.T* %{{[_a-z0-9]+}}, null
25   // CHECK-NULL-NEXT: br i1 [[UBSAN_CMP_RES]], label %{{.*}}, label %{{.*}}
26   // CHECK-NULL: call void @__ubsan_handle_type_mismatch_v1_abort
27   // Second, we check that vtable is actually loaded once the type check is done.
28   // CHECK-NULL: load {{.*}} (%struct.T*{{.*}})**, {{.*}} (%struct.T*{{.*}})***
29   return t->v();
30 }
31 
32 // ITANIUM: define{{.*}} void @_Z9delete_itP1T
33 // MSABI: define dso_local void @"?delete_it
delete_it(T * t)34 void delete_it(T *t) {
35   // First, we check that vtable is not loaded before a type check.
36   // CHECK-VPTR-NOT: load {{.*}} (%struct.T*{{.*}})**, {{.*}} (%struct.T*{{.*}})***
37   // CHECK-VPTR: br i1 {{.*}} label %{{.*}}
38   // CHECK-VPTR: call void @__ubsan_handle_dynamic_type_cache_miss_abort
39   // Second, we check that vtable is actually loaded once the type check is done.
40   // CHECK-VPTR: load {{.*}} (%struct.T*{{.*}})**, {{.*}} (%struct.T*{{.*}})***
41   delete t;
42 }
43 
44 // ITANIUM: define{{.*}} %struct.U* @_Z7dyncastP1T
45 // MSABI: define dso_local %struct.U* @"?dyncast
dyncast(T * t)46 U* dyncast(T *t) {
47   // First, we check that dynamic_cast is not called before a type check.
48   // CHECK-VPTR-NOT: call i8* @__{{dynamic_cast|RTDynamicCast}}
49   // CHECK-VPTR: br i1 {{.*}} label %{{.*}}
50   // CHECK-VPTR: call void @__ubsan_handle_dynamic_type_cache_miss_abort
51   // Second, we check that dynamic_cast is actually called once the type check is done.
52   // CHECK-VPTR: call i8* @__{{dynamic_cast|RTDynamicCast}}
53   return dynamic_cast<U*>(t);
54 }
55