Clang Project

clang_source_code/test/CodeGen/cfi-icall-cross-dso.c
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
42// ITANIUM-DIAG: declare void @__cfi_slowpath_diag(
43// ITANIUM-TRAP: declare void @__cfi_slowpath(
44// MS-DIAG: declare dso_local void @__cfi_slowpath_diag(
45// MS-TRAP: declare dso_local void @__cfi_slowpath(
46
47void caller(void (*f)()) {
48  f();
49}
50
51// Check that we emit both string and hash based type entries for static void g(),
52// and don't emit them for the declaration of h().
53
54// CHECK: define internal void @g({{.*}} !type [[TVOID:![0-9]+]] !type [[TVOID_GENERALIZED:![0-9]+]] !type [[TVOID_ID:![0-9]+]]
55static void g(void) {}
56
57// CHECK: declare {{(dso_local )?}}void @h({{[^!]*$}}
58void h(void);
59
60typedef void (*Fn)(void);
61Fn g1() {
62  return &g;
63}
64Fn h1() {
65  return &h;
66}
67
68// CHECK: define {{(dso_local )?}}void @bar({{.*}} !type [[TNOPROTO:![0-9]+]] !type [[TNOPROTO_GENERALIZED:![0-9]+]] !type [[TNOPROTO_ID:![0-9]+]]
69// ITANIUM: define available_externally void @foo({{[^!]*$}}
70// MS: define linkonce_odr dso_local void @foo({{.*}} !type [[TNOPROTO]] !type [[TNOPROTO_GENERALIZED:![0-9]+]] !type [[TNOPROTO_ID]]
71inline void foo() {}
72void bar() { foo(); }
73
74// ITANIUM: define weak void @__cfi_check
75// MS: define weak dso_local void @__cfi_check
76
77// CHECK: !{i32 4, !"Cross-DSO CFI", i32 1}
78
79// Check that the type entries are correct.
80
81// ITANIUM: [[TVOID]] = !{i64 0, !"_ZTSFvvE"}
82// ITANIUM: [[TVOID_GENERALIZED]] = !{i64 0, !"_ZTSFvvE.generalized"}
83// ITANIUM: [[TVOID_ID]] = !{i64 0, i64 9080559750644022485}
84// ITANIUM: [[TNOPROTO]] = !{i64 0, !"_ZTSFvE"}
85// ITANIUM: [[TNOPROTO_GENERALIZED]] = !{i64 0, !"_ZTSFvE.generalized"}
86// ITANIUM: [[TNOPROTO_ID]] = !{i64 0, i64 6588678392271548388}
87
88// MS: [[TVOID]] = !{i64 0, !"?6AXXZ"}
89// MS: [[TVOID_GENERALIZED]] = !{i64 0, !"?6AXXZ.generalized"}
90// MS: [[TVOID_ID]] = !{i64 0, i64 5113650790573562461}
91// MS: [[TNOPROTO]] = !{i64 0, !"?6AX@Z"}
92// MS: [[TNOPROTO_GENERALIZED]] = !{i64 0, !"?6AX@Z.generalized"}
93// MS: [[TNOPROTO_ID]] = !{i64 0, i64 4195979634929632483}
94