1 // __cfi_check_fail codegen when not all CFI checkers are enabled.
2 // RUN: %clang_cc1 -triple x86_64-unknown-linux -O0 -fsanitize-cfi-cross-dso \
3 // RUN:     -fsanitize=cfi-vcall \
4 // RUN:     -emit-llvm -o - %s | FileCheck %s
5 
caller(void (* f)())6 void caller(void (*f)()) {
7   f();
8 }
9 
10 // CHECK: define weak_odr hidden void @__cfi_check_fail(i8*, i8*)
11 // CHECK: store i8* %0, i8** %[[ALLOCA0:.*]], align 8
12 // CHECK: store i8* %1, i8** %[[ALLOCA1:.*]], align 8
13 // CHECK: %[[DATA:.*]] = load i8*, i8** %[[ALLOCA0]], align 8
14 // CHECK: %[[ADDR:.*]] = load i8*, i8** %[[ALLOCA1]], align 8
15 // CHECK: %[[ICMP_NOT_NULL:.*]] = icmp ne i8* %[[DATA]], null
16 // CHECK: br i1 %[[ICMP_NOT_NULL]], label %[[CONT0:.*]], label %[[TRAP:.*]],
17 
18 // CHECK: [[TRAP]]:
19 // CHECK-NEXT:   call void @llvm.trap()
20 // CHECK-NEXT:   unreachable
21 
22 // CHECK: [[CONT0]]:
23 // CHECK:   %[[A:.*]] = bitcast i8* %[[DATA]] to { i8, { i8*, i32, i32 }, i8* }*
24 // CHECK:   %[[KINDPTR:.*]] = getelementptr {{.*}} %[[A]], i32 0, i32 0
25 // CHECK:   %[[KIND:.*]] = load i8, i8* %[[KINDPTR]], align 4
26 // CHECK:   %[[VTVALID0:.*]] = call i1 @llvm.type.test(i8* %[[ADDR]], metadata !"all-vtables")
27 // CHECK:   %[[VTVALID:.*]] = zext i1 %[[VTVALID0]] to i64
28 // CHECK:   %[[NOT_0:.*]] = icmp ne i8 %[[KIND]], 0
29 // CHECK:   br i1 %[[NOT_0]], label %[[CONT1:.*]], label %[[HANDLE0:.*]], !prof
30 
31 // CHECK: [[HANDLE0]]:
32 // CHECK:   %[[DATA0:.*]] = ptrtoint i8* %[[DATA]] to i64,
33 // CHECK:   %[[ADDR0:.*]] = ptrtoint i8* %[[ADDR]] to i64,
34 // CHECK:   call void @__ubsan_handle_cfi_check_fail_abort(i64 %[[DATA0]], i64 %[[ADDR0]], i64 %[[VTVALID]])
35 // CHECK:   unreachable
36 
37 // CHECK: [[CONT1]]:
38 // CHECK:   %[[NOT_1:.*]] = icmp ne i8 %[[KIND]], 1
39 // CHECK:   br i1 %[[NOT_1]], label %[[CONT2:.*]], label %[[HANDLE1:.*]], !nosanitize
40 
41 // CHECK: [[HANDLE1]]:
42 // CHECK-NEXT:   call void @llvm.trap()
43 // CHECK-NEXT:   unreachable
44 
45 // CHECK: [[CONT2]]:
46 // CHECK:   %[[NOT_2:.*]] = icmp ne i8 %[[KIND]], 2
47 // CHECK:   br i1 %[[NOT_2]], label %[[CONT3:.*]], label %[[HANDLE2:.*]], !nosanitize
48 
49 // CHECK: [[HANDLE2]]:
50 // CHECK-NEXT:   call void @llvm.trap()
51 // CHECK-NEXT:   unreachable
52 
53 // CHECK: [[CONT3]]:
54 // CHECK:   %[[NOT_3:.*]] = icmp ne i8 %[[KIND]], 3
55 // CHECK:   br i1 %[[NOT_3]], label %[[CONT4:.*]], label %[[HANDLE3:.*]], !nosanitize
56 
57 // CHECK: [[HANDLE3]]:
58 // CHECK-NEXT:   call void @llvm.trap()
59 // CHECK-NEXT:   unreachable
60 
61 // CHECK: [[CONT4]]:
62 // CHECK:   %[[NOT_4:.*]] = icmp ne i8 %[[KIND]], 4
63 // CHECK:   br i1 %[[NOT_4]], label %[[CONT5:.*]], label %[[HANDLE4:.*]], !nosanitize
64 
65 // CHECK: [[HANDLE4]]:
66 // CHECK-NEXT:   call void @llvm.trap()
67 // CHECK-NEXT:   unreachable
68 
69 // CHECK: [[CONT5]]:
70 // CHECK:   ret void
71