1 // Tests for the cfi-vcall feature:
2 // RUN: %clang_cc1 -flto -triple x86_64-unknown-linux -fvisibility hidden -fsanitize=cfi-vcall -fsanitize-trap=cfi-vcall -emit-llvm -o - %s | FileCheck --check-prefix=CFI --check-prefix=CFI-NVT --check-prefix=ITANIUM --check-prefix=TT-ITANIUM --check-prefix=NDIAG %s
3 // RUN: %clang_cc1 -flto -triple x86_64-unknown-linux -fvisibility hidden -fsanitize=cfi-vcall -emit-llvm -o - %s | FileCheck --check-prefix=CFI --check-prefix=CFI-NVT --check-prefix=ITANIUM --check-prefix=TT-ITANIUM --check-prefix=ITANIUM-DIAG --check-prefix=DIAG --check-prefix=DIAG-ABORT %s
4 // RUN: %clang_cc1 -flto -triple x86_64-unknown-linux -fvisibility hidden -fsanitize=cfi-vcall -fsanitize-recover=cfi-vcall -emit-llvm -o - %s | FileCheck --check-prefix=CFI --check-prefix=CFI-NVT --check-prefix=ITANIUM --check-prefix=TT-ITANIUM --check-prefix=ITANIUM-DIAG --check-prefix=DIAG --check-prefix=DIAG-RECOVER %s
5 // RUN: %clang_cc1 -flto -triple x86_64-pc-windows-msvc -fsanitize=cfi-vcall -fsanitize-trap=cfi-vcall -emit-llvm -o - %s | FileCheck --check-prefix=CFI --check-prefix=CFI-NVT --check-prefix=MS --check-prefix=TT-MS --check-prefix=NDIAG %s
6
7 // Tests for the whole-program-vtables feature:
8 // RUN: %clang_cc1 -flto -triple x86_64-unknown-linux -fvisibility hidden -fwhole-program-vtables -emit-llvm -o - %s | FileCheck --check-prefix=VTABLE-OPT --check-prefix=ITANIUM --check-prefix=TT-ITANIUM %s
9 // RUN: %clang_cc1 -flto -triple x86_64-pc-windows-msvc -fwhole-program-vtables -emit-llvm -o - %s | FileCheck --check-prefix=VTABLE-OPT --check-prefix=MS --check-prefix=TT-MS %s
10
11 // Tests for cfi + whole-program-vtables:
12 // RUN: %clang_cc1 -flto -triple x86_64-unknown-linux -fvisibility hidden -fsanitize=cfi-vcall -fsanitize-trap=cfi-vcall -fwhole-program-vtables -emit-llvm -o - %s | FileCheck --check-prefix=CFI --check-prefix=CFI-VT --check-prefix=ITANIUM --check-prefix=TC-ITANIUM %s
13 // RUN: %clang_cc1 -flto -triple x86_64-pc-windows-msvc -fsanitize=cfi-vcall -fsanitize-trap=cfi-vcall -fwhole-program-vtables -emit-llvm -o - %s | FileCheck --check-prefix=CFI --check-prefix=CFI-VT --check-prefix=MS --check-prefix=TC-MS %s
14
15 // ITANIUM: @_ZTV1A = {{[^!]*}}, !type [[A16:![0-9]+]]
16 // ITANIUM-DIAG-SAME: !type [[ALL16:![0-9]+]]
17
18 // ITANIUM: @_ZTV1B = {{[^!]*}}, !type [[A32:![0-9]+]]
19 // ITANIUM-DIAG-SAME: !type [[ALL32:![0-9]+]]
20 // ITANIUM-SAME: !type [[B32:![0-9]+]]
21 // ITANIUM-DIAG-SAME: !type [[ALL32]]
22
23 // ITANIUM: @_ZTV1C = {{[^!]*}}, !type [[A32]]
24 // ITANIUM-DIAG-SAME: !type [[ALL32]]
25 // ITANIUM-SAME: !type [[C32:![0-9]+]]
26 // ITANIUM-DIAG-SAME: !type [[ALL32]]
27
28 // DIAG: @[[SRC:.*]] = private unnamed_addr constant [{{.*}} x i8] c"{{.*}}type-metadata.cpp\00", align 1
29 // DIAG: @[[TYPE:.*]] = private unnamed_addr constant { i16, i16, [4 x i8] } { i16 -1, i16 0, [4 x i8] c"'A'\00" }
30 // DIAG: @[[BADTYPESTATIC:.*]] = private unnamed_addr global { i8, { [{{.*}} x i8]*, i32, i32 }, { i16, i16, [4 x i8] }* } { i8 0, { [{{.*}} x i8]*, i32, i32 } { [{{.*}} x i8]* @[[SRC]], i32 123, i32 3 }, { i16, i16, [4 x i8] }* @[[TYPE]] }
31
32 // ITANIUM: @_ZTVN12_GLOBAL__N_11DE = {{[^!]*}}, !type [[A32]]
33 // ITANIUM-DIAG-SAME: !type [[ALL32]]
34 // ITANIUM-SAME: !type [[B32]]
35 // ITANIUM-DIAG-SAME: !type [[ALL32]]
36 // ITANIUM-SAME: !type [[C88:![0-9]+]]
37 // ITANIUM-DIAG-SAME: !type [[ALL88:![0-9]+]]
38 // ITANIUM-SAME: !type [[D32:![0-9]+]]
39 // ITANIUM-DIAG-SAME: !type [[ALL32]]
40
41 // ITANIUM: @_ZTCN12_GLOBAL__N_11DE0_1B = {{[^!]*}}, !type [[A32]]
42 // ITANIUM-DIAG-SAME: !type [[ALL32]]
43 // ITANIUM-SAME: !type [[B32]]
44 // ITANIUM-DIAG-SAME: !type [[ALL32]]
45
46 // ITANIUM: @_ZTCN12_GLOBAL__N_11DE8_1C = {{[^!]*}}, !type [[A64:![0-9]+]]
47 // ITANIUM-DIAG-SAME: !type [[ALL64:![0-9]+]]
48 // ITANIUM-SAME: !type [[C32]]
49 // ITANIUM-DIAG-SAME: !type [[ALL32]]
50
51 // ITANIUM: @_ZTVZ3foovE2FA = {{[^!]*}}, !type [[A16]]
52 // ITANIUM-DIAG-SAME: !type [[ALL16]]
53 // ITANIUM-SAME: !type [[FA16:![0-9]+]]
54 // ITANIUM-DIAG-SAME: !type [[ALL16]]
55
56 // MS: comdat($"\01??_7A@@6B@"), !type [[A8:![0-9]+]]
57 // MS: comdat($"\01??_7B@@6B0@@"), !type [[B8:![0-9]+]]
58 // MS: comdat($"\01??_7B@@6BA@@@"), !type [[A8]]
59 // MS: comdat($"\01??_7C@@6B@"), !type [[A8]]
60 // MS: comdat($"\01??_7D@?A@@6BB@@@"), !type [[B8]], !type [[D8:![0-9]+]]
61 // MS: comdat($"\01??_7D@?A@@6BA@@@"), !type [[A8]]
62 // MS: comdat($"\01??_7FA@?1??foo@@YAXXZ@6B@"), !type [[A8]], !type [[FA8:![0-9]+]]
63
64 struct A {
65 A();
66 virtual void f();
67 };
68
69 struct B : virtual A {
70 B();
71 virtual void g();
72 virtual void h();
73 };
74
75 struct C : virtual A {
76 C();
77 };
78
79 namespace {
80
81 struct D : B, C {
82 D();
83 virtual void f();
84 virtual void h();
85 };
86
87 }
88
A()89 A::A() {}
B()90 B::B() {}
C()91 C::C() {}
D()92 D::D() {}
93
f()94 void A::f() {
95 }
96
g()97 void B::g() {
98 }
99
f()100 void D::f() {
101 }
102
h()103 void D::h() {
104 }
105
106 // ITANIUM: define hidden void @_Z2afP1A
107 // MS: define void @"\01?af@@YAXPEAUA@@@Z"
af(A * a)108 void af(A *a) {
109 // TT-ITANIUM: [[P:%[^ ]*]] = call i1 @llvm.type.test(i8* [[VT:%[^ ]*]], metadata !"_ZTS1A")
110 // TT-MS: [[P:%[^ ]*]] = call i1 @llvm.type.test(i8* [[VT:%[^ ]*]], metadata !"?AUA@@")
111 // TC-ITANIUM: [[PAIR:%[^ ]*]] = call { i8*, i1 } @llvm.type.checked.load(i8* {{%[^ ]*}}, i32 0, metadata !"_ZTS1A")
112 // TC-MS: [[PAIR:%[^ ]*]] = call { i8*, i1 } @llvm.type.checked.load(i8* {{%[^ ]*}}, i32 0, metadata !"?AUA@@")
113 // CFI-VT: [[P:%[^ ]*]] = extractvalue { i8*, i1 } [[PAIR]], 1
114 // DIAG-NEXT: [[VTVALID0:%[^ ]*]] = call i1 @llvm.type.test(i8* [[VT]], metadata !"all-vtables")
115 // VTABLE-OPT: call void @llvm.assume(i1 [[P]])
116 // CFI-NEXT: br i1 [[P]], label %[[CONTBB:[^ ,]*]], label %[[TRAPBB:[^ ,]*]]
117 // CFI-NEXT: {{^$}}
118
119 // CFI: [[TRAPBB]]
120 // NDIAG-NEXT: call void @llvm.trap()
121 // NDIAG-NEXT: unreachable
122 // DIAG-NEXT: [[VTINT:%[^ ]*]] = ptrtoint i8* [[VT]] to i64
123 // DIAG-NEXT: [[VTVALID:%[^ ]*]] = zext i1 [[VTVALID0]] to i64
124 // DIAG-ABORT-NEXT: call void @__ubsan_handle_cfi_check_fail_abort(i8* getelementptr inbounds ({{.*}} @[[BADTYPESTATIC]], i32 0, i32 0), i64 [[VTINT]], i64 [[VTVALID]])
125 // DIAG-ABORT-NEXT: unreachable
126 // DIAG-RECOVER-NEXT: call void @__ubsan_handle_cfi_check_fail(i8* getelementptr inbounds ({{.*}} @[[BADTYPESTATIC]], i32 0, i32 0), i64 [[VTINT]], i64 [[VTVALID]])
127 // DIAG-RECOVER-NEXT: br label %[[CONTBB]]
128
129 // CFI: [[CONTBB]]
130 // CFI-NVT: [[PTR:%[^ ]*]] = load
131 // CFI-VT: [[PTRI8:%[^ ]*]] = extractvalue { i8*, i1 } [[PAIR]], 0
132 // CFI-VT: [[PTR:%[^ ]*]] = bitcast i8* [[PTRI8]] to
133 // CFI: call void [[PTR]]
134 #line 123
135 a->f();
136 }
137
138 // ITANIUM: define internal void @_Z3df1PN12_GLOBAL__N_11DE
139 // MS: define internal void @"\01?df1@@YAXPEAUD@?A@@@Z"
df1(D * d)140 void df1(D *d) {
141 // TT-ITANIUM: {{%[^ ]*}} = call i1 @llvm.type.test(i8* {{%[^ ]*}}, metadata ![[DTYPE:[0-9]+]])
142 // TT-MS: {{%[^ ]*}} = call i1 @llvm.type.test(i8* {{%[^ ]*}}, metadata !"?AUA@@")
143 // TC-ITANIUM: {{%[^ ]*}} = call { i8*, i1 } @llvm.type.checked.load(i8* {{%[^ ]*}}, i32 0, metadata ![[DTYPE:[0-9]+]])
144 // TC-MS: {{%[^ ]*}} = call { i8*, i1 } @llvm.type.checked.load(i8* {{%[^ ]*}}, i32 0, metadata !"?AUA@@")
145 d->f();
146 }
147
148 // ITANIUM: define internal void @_Z3dg1PN12_GLOBAL__N_11DE
149 // MS: define internal void @"\01?dg1@@YAXPEAUD@?A@@@Z"
dg1(D * d)150 void dg1(D *d) {
151 // TT-ITANIUM: {{%[^ ]*}} = call i1 @llvm.type.test(i8* {{%[^ ]*}}, metadata !"_ZTS1B")
152 // TT-MS: {{%[^ ]*}} = call i1 @llvm.type.test(i8* {{%[^ ]*}}, metadata !"?AUB@@")
153 // TC-ITANIUM: {{%[^ ]*}} = call { i8*, i1 } @llvm.type.checked.load(i8* {{%[^ ]*}}, i32 8, metadata !"_ZTS1B")
154 // TC-MS: {{%[^ ]*}} = call { i8*, i1 } @llvm.type.checked.load(i8* {{%[^ ]*}}, i32 0, metadata !"?AUB@@")
155 d->g();
156 }
157
158 // ITANIUM: define internal void @_Z3dh1PN12_GLOBAL__N_11DE
159 // MS: define internal void @"\01?dh1@@YAXPEAUD@?A@@@Z"
dh1(D * d)160 void dh1(D *d) {
161 // TT-ITANIUM: {{%[^ ]*}} = call i1 @llvm.type.test(i8* {{%[^ ]*}}, metadata ![[DTYPE]])
162 // TT-MS: {{%[^ ]*}} = call i1 @llvm.type.test(i8* {{%[^ ]*}}, metadata ![[DTYPE:[0-9]+]])
163 // TC-ITANIUM: {{%[^ ]*}} = call { i8*, i1 } @llvm.type.checked.load(i8* {{%[^ ]*}}, i32 16, metadata ![[DTYPE]])
164 // TC-MS: {{%[^ ]*}} = call { i8*, i1 } @llvm.type.checked.load(i8* {{%[^ ]*}}, i32 8, metadata ![[DTYPE:[0-9]+]])
165 d->h();
166 }
167
168 // ITANIUM: define internal void @_Z3df2PN12_GLOBAL__N_11DE
169 // MS: define internal void @"\01?df2@@YAXPEAUD@?A@@@Z"
170 __attribute__((no_sanitize("cfi")))
df2(D * d)171 void df2(D *d) {
172 // CFI-NVT-NOT: call i1 @llvm.type.test
173 // CFI-VT: [[P:%[^ ]*]] = call i1 @llvm.type.test
174 // CFI-VT: call void @llvm.assume(i1 [[P]])
175 d->f();
176 }
177
178 // ITANIUM: define internal void @_Z3df3PN12_GLOBAL__N_11DE
179 // MS: define internal void @"\01?df3@@YAXPEAUD@?A@@@Z"
180 __attribute__((no_sanitize("address"))) __attribute__((no_sanitize("cfi-vcall")))
df3(D * d)181 void df3(D *d) {
182 // CFI-NVT-NOT: call i1 @llvm.type.test
183 // CFI-VT: [[P:%[^ ]*]] = call i1 @llvm.type.test
184 // CFI-VT: call void @llvm.assume(i1 [[P]])
185 d->f();
186 }
187
188 D d;
189
foo()190 void foo() {
191 df1(&d);
192 dg1(&d);
193 dh1(&d);
194 df2(&d);
195 df3(&d);
196
197 struct FA : A {
198 void f() {}
199 } fa;
200 af(&fa);
201 }
202
203 namespace test2 {
204
205 struct A {
206 virtual void m_fn1();
207 };
208 struct B {
209 virtual void m_fn2();
210 };
211 struct C : B, A {};
212 struct D : C {
213 void m_fn1();
214 };
215
216 // ITANIUM: define hidden void @_ZN5test21fEPNS_1DE
217 // MS: define void @"\01?f@test2@@YAXPEAUD@1@@Z"
f(D * d)218 void f(D *d) {
219 // TT-ITANIUM: {{%[^ ]*}} = call i1 @llvm.type.test(i8* {{%[^ ]*}}, metadata !"_ZTSN5test21DE")
220 // TT-MS: {{%[^ ]*}} = call i1 @llvm.type.test(i8* {{%[^ ]*}}, metadata !"?AUA@test2@@")
221 // TC-ITANIUM: {{%[^ ]*}} = call { i8*, i1 } @llvm.type.checked.load(i8* {{%[^ ]*}}, i32 8, metadata !"_ZTSN5test21DE")
222 // TC-MS: {{%[^ ]*}} = call { i8*, i1 } @llvm.type.checked.load(i8* {{%[^ ]*}}, i32 0, metadata !"?AUA@test2@@")
223 d->m_fn1();
224 }
225
226 }
227
228 // ITANIUM: [[A16]] = !{i64 16, !"_ZTS1A"}
229 // ITANIUM-DIAG: [[ALL16]] = !{i64 16, !"all-vtables"}
230 // ITANIUM: [[A32]] = !{i64 32, !"_ZTS1A"}
231 // ITANIUM-DIAG: [[ALL32]] = !{i64 32, !"all-vtables"}
232 // ITANIUM: [[B32]] = !{i64 32, !"_ZTS1B"}
233 // ITANIUM: [[C32]] = !{i64 32, !"_ZTS1C"}
234 // ITANIUM: [[C88]] = !{i64 88, !"_ZTS1C"}
235 // ITANIUM-DIAG: [[ALL88]] = !{i64 88, !"all-vtables"}
236 // ITANIUM: [[D32]] = !{i64 32, [[D_ID:![0-9]+]]}
237 // ITANIUM: [[D_ID]] = distinct !{}
238 // ITANIUM: [[A64]] = !{i64 64, !"_ZTS1A"}
239 // ITANIUM-DIAG: [[ALL64]] = !{i64 64, !"all-vtables"}
240 // ITANIUM: [[FA16]] = !{i64 16, [[FA_ID:![0-9]+]]}
241 // ITANIUM: [[FA_ID]] = distinct !{}
242
243 // MS: [[A8]] = !{i64 8, !"?AUA@@"}
244 // MS: [[B8]] = !{i64 8, !"?AUB@@"}
245 // MS: [[D8]] = !{i64 8, [[D_ID:![0-9]+]]}
246 // MS: [[D_ID]] = distinct !{}
247 // MS: [[FA8]] = !{i64 8, [[FA_ID:![0-9]+]]}
248 // MS: [[FA_ID]] = distinct !{}
249