1// RUN: %clang_cc1 -triple x86_64-unknown-linux -O1 \
2// RUN:   -fsanitize=cfi-icall -fsanitize-cfi-cross-dso \
3// RUN:   -emit-llvm -o - %s | FileCheck \
4// RUN:       --check-prefix=CHECK --check-prefix=CHECK-DIAG \
5// RUN:       --check-prefix=ITANIUM --check-prefix=ITANIUM-DIAG \
6// RUN:       %s
7
8// RUN: %clang_cc1 -triple x86_64-unknown-linux -O1 \
9// RUN:   -fsanitize=cfi-icall -fsanitize-cfi-cross-dso -fsanitize-trap=cfi-icall \
10// RUN:   -emit-llvm -o - %s | FileCheck \
11// RUN:       --check-prefix=CHECK \
12// RUN:       --check-prefix=ITANIUM --check-prefix=ITANIUM-TRAP \
13// RUN:       %s
14
15// RUN: %clang_cc1 -triple x86_64-pc-windows-msvc -O1 \
16// RUN:   -fsanitize=cfi-icall -fsanitize-cfi-cross-dso \
17// RUN:   -emit-llvm -o - %s | FileCheck \
18// RUN:       --check-prefix=CHECK --check-prefix=CHECK-DIAG \
19// RUN:       --check-prefix=MS --check-prefix=MS-DIAG \
20// RUN:       %s
21
22// RUN: %clang_cc1 -triple x86_64-pc-windows-msvc -O1 \
23// RUN:   -fsanitize=cfi-icall -fsanitize-cfi-cross-dso -fsanitize-trap=cfi-icall \
24// RUN:   -emit-llvm -o - %s | FileCheck \
25// RUN:       --check-prefix=CHECK \
26// RUN:       --check-prefix=MS --check-prefix=MS-TRAP \
27// RUN:       %s
28
29// CHECK-DIAG: @[[SRC:.*]] = private unnamed_addr constant {{.*}}cfi-icall-cross-dso.c\00
30// CHECK-DIAG: @[[TYPE:.*]] = private unnamed_addr constant { i16, i16, [{{.*}} x i8] } { i16 -1, i16 0, [{{.*}} x i8] c"'void ()'\00"
31// CHECK-DIAG: @[[DATA:.*]] = private unnamed_addr global {{.*}}@[[SRC]]{{.*}}@[[TYPE]]
32
33
34// ITANIUM: call i1 @llvm.type.test(i8* %{{.*}}, metadata !"_ZTSFvE"), !nosanitize
35// ITANIUM-DIAG: call void @__cfi_slowpath_diag(i64 6588678392271548388, i8* %{{.*}}, {{.*}}@[[DATA]]{{.*}}) {{.*}}, !nosanitize
36// ITANIUM-TRAP: call void @__cfi_slowpath(i64 6588678392271548388, i8* %{{.*}}) {{.*}}, !nosanitize
37
38// MS: call i1 @llvm.type.test(i8* %{{.*}}, metadata !"?6AX@Z"), !nosanitize
39// MS-DIAG: call void @__cfi_slowpath_diag(i64 4195979634929632483, i8* %{{.*}}, {{.*}}@[[DATA]]{{.*}}) {{.*}}, !nosanitize
40// MS-TRAP: call void @__cfi_slowpath(i64 4195979634929632483, i8* %{{.*}}) {{.*}}, !nosanitize
41
42void caller(void (*f)()) {
43  f();
44}
45
46// Check that we emit both string and hash based type entries for static void g(),
47// and don't emit them for the declaration of h().
48
49// CHECK: define internal void @g({{.*}} !type [[TVOID:![0-9]+]] !type [[TVOID_ID:![0-9]+]]
50static void g(void) {}
51
52// CHECK: declare void @h({{[^!]*$}}
53void h(void);
54
55typedef void (*Fn)(void);
56Fn g1() {
57  return &g;
58}
59Fn h1() {
60  return &h;
61}
62
63// CHECK: define void @bar({{.*}} !type [[TNOPROTO:![0-9]+]] !type [[TNOPROTO_ID:![0-9]+]]
64// ITANIUM: define available_externally void @foo({{[^!]*$}}
65// MS: define linkonce_odr void @foo({{.*}} !type [[TNOPROTO]] !type [[TNOPROTO_ID]]
66inline void foo() {}
67void bar() { foo(); }
68
69// CHECK: !{i32 4, !"Cross-DSO CFI", i32 1}
70
71// Check that the type entries are correct.
72
73// ITANIUM: [[TVOID]] = !{i64 0, !"_ZTSFvvE"}
74// ITANIUM: [[TVOID_ID]] = !{i64 0, i64 9080559750644022485}
75// ITANIUM: [[TNOPROTO]] = !{i64 0, !"_ZTSFvE"}
76// ITANIUM: [[TNOPROTO_ID]] = !{i64 0, i64 6588678392271548388}
77
78// MS: [[TVOID]] = !{i64 0, !"?6AXXZ"}
79// MS: [[TVOID_ID]] = !{i64 0, i64 5113650790573562461}
80// MS: [[TNOPROTO]] = !{i64 0, !"?6AX@Z"}
81// MS: [[TNOPROTO_ID]] = !{i64 0, i64 4195979634929632483}
82