Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / clang / test / CodeGen / kcfi.c
blobf6b2e4b398aa7c3af1d9157cd94323adbc8be2ea
1 // RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -emit-llvm -fsanitize=kcfi -o - %s | FileCheck %s
2 // RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -emit-llvm -fsanitize=kcfi -x c++ -o - %s | FileCheck %s --check-prefixes=CHECK,MEMBER
3 // RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -emit-llvm -fsanitize=kcfi -fpatchable-function-entry-offset=3 -o - %s | FileCheck %s --check-prefixes=CHECK,OFFSET
4 #if !__has_feature(kcfi)
5 #error Missing kcfi?
6 #endif
8 /// Must emit __kcfi_typeid symbols for address-taken function declarations
9 // CHECK: module asm ".weak __kcfi_typeid_[[F4:[a-zA-Z0-9_]+]]"
10 // CHECK: module asm ".set __kcfi_typeid_[[F4]], [[#%d,HASH:]]"
11 /// Must not __kcfi_typeid symbols for non-address-taken declarations
12 // CHECK-NOT: module asm ".weak __kcfi_typeid_{{f6|_Z2f6v}}"
13 typedef int (*fn_t)(void);
15 // CHECK: define dso_local{{.*}} i32 @{{f1|_Z2f1v}}(){{.*}} !kcfi_type ![[#TYPE:]]
16 int f1(void) { return 0; }
18 // CHECK: define dso_local{{.*}} i32 @{{f2|_Z2f2v}}(){{.*}} !kcfi_type ![[#TYPE2:]]
19 unsigned int f2(void) { return 2; }
21 // CHECK-LABEL: define dso_local{{.*}} i32 @{{__call|_Z6__callPFivE}}(ptr{{.*}} %f)
22 int __call(fn_t f) __attribute__((__no_sanitize__("kcfi"))) {
23 // CHECK-NOT: call{{.*}} i32 %{{.}}(){{.*}} [ "kcfi"
24 return f();
27 // CHECK: define dso_local{{.*}} i32 @{{call|_Z4callPFivE}}(ptr{{.*}} %f){{.*}}
28 int call(fn_t f) {
29 // CHECK: call{{.*}} i32 %{{.}}(){{.*}} [ "kcfi"(i32 [[#HASH]]) ]
30 return f();
33 // CHECK-DAG: define internal{{.*}} i32 @{{f3|_ZL2f3v}}(){{.*}} !kcfi_type ![[#TYPE]]
34 static int f3(void) { return 1; }
36 // CHECK-DAG: declare !kcfi_type ![[#TYPE]]{{.*}} i32 @[[F4]]()
37 extern int f4(void);
39 /// Must not emit !kcfi_type for non-address-taken local functions
40 // CHECK: define internal{{.*}} i32 @{{f5|_ZL2f5v}}()
41 // CHECK-NOT: !kcfi_type
42 // CHECK-SAME: {
43 static int f5(void) { return 2; }
45 // CHECK-DAG: declare !kcfi_type ![[#TYPE]]{{.*}} i32 @{{f6|_Z2f6v}}()
46 extern int f6(void);
48 int test(void) {
49 return call(f1) +
50 __call((fn_t)f2) +
51 call(f3) +
52 call(f4) +
53 f5() +
54 f6();
57 #ifdef __cplusplus
58 struct A {
59 // MEMBER-DAG: define{{.*}} void @_ZN1A1fEv(ptr{{.*}} %this){{.*}} !kcfi_type ![[#TYPE3:]]
60 void f() {}
63 void test_member_call(void) {
64 void (A::* p)() = &A::f;
65 // MEMBER-DAG: call void %[[#]](ptr{{.*}} [ "kcfi"(i32 [[#%d,HASH3:]]) ]
66 (A().*p)();
68 #endif
70 // CHECK-DAG: ![[#]] = !{i32 4, !"kcfi", i32 1}
71 // OFFSET-DAG: ![[#]] = !{i32 4, !"kcfi-offset", i32 3}
72 // CHECK-DAG: ![[#TYPE]] = !{i32 [[#HASH]]}
73 // CHECK-DAG: ![[#TYPE2]] = !{i32 [[#%d,HASH2:]]}
74 // MEMBER-DAG: ![[#TYPE3]] = !{i32 [[#HASH3]]}