Debian Codegraph @

clang/test/CodeGen/ptrauth-function-type-discriminator-cast.c

raw
1 // RUN: %clang_cc1 %s -fptrauth-function-pointer-type-discrimination -triple arm64e-apple-ios13 -fptrauth-calls -fptrauth-intrinsics \
2 // RUN: -disable-llvm-passes -emit-llvm -o- | FileCheck %s --check-prefixes=CHECK,TYPE
3
4 // RUN: %clang_cc1 %s -fptrauth-function-pointer-type-discrimination -triple aarch64-linux-gnu -fptrauth-calls -fptrauth-intrinsics \
5 // RUN: -disable-llvm-passes -emit-llvm -o- | FileCheck %s --check-prefixes=CHECK,TYPE
6
7 // RUN: %clang_cc1 %s -triple arm64e-apple-ios13 -fptrauth-calls -fptrauth-intrinsics \
8 // RUN: -disable-llvm-passes -emit-llvm -o- | FileCheck %s --check-prefixes=CHECK,ZERO
9
10 // RUN: %clang_cc1 %s -triple aarch64-linux-gnu -fptrauth-calls -fptrauth-intrinsics \
11 // RUN: -disable-llvm-passes -emit-llvm -o- | FileCheck %s --check-prefixes=CHECK,ZERO
12
13 // RUN: %clang_cc1 %s -fptrauth-function-pointer-type-discrimination -triple arm64e-apple-ios13 -fptrauth-calls -fptrauth-intrinsics \
14 // RUN: -disable-llvm-passes -emit-llvm -xc++ -o- | FileCheck %s --check-prefixes=CHECK,CHECKCXX,TYPE,TYPECXX
15
16 // RUN: %clang_cc1 %s -fptrauth-function-pointer-type-discrimination -triple aarch64-linux-gnu -fptrauth-calls -fptrauth-intrinsics \
17 // RUN: -disable-llvm-passes -emit-llvm -xc++ -o- | FileCheck %s --check-prefixes=CHECK,CHECKCXX,TYPE,TYPECXX
18
19 #ifdef __cplusplus
20 extern "C" {
21 #endif
22
23 void f(void);
24 void f2(int);
25 void (*fptr)(void);
26 void *opaque;
27 unsigned long uintptr;
28
29 #ifdef __cplusplus
30 struct ptr_member {
31 void (*fptr_)(int) = 0;
32 };
33 ptr_member pm;
34 void (*test_member)() = (void (*)())pm.fptr_;
35
36 // CHECKCXX-LABEL: define{{.*}} internal void @__cxx_global_var_init
37 // TYPECXX: call i64 @llvm.ptrauth.resign(i64 {{.*}}, i32 0, i64 2712, i32 0, i64 18983)
38 #endif
39
40
41 // CHECK-LABEL: define{{.*}} void @test_cast_to_opaque
42 void test_cast_to_opaque() {
43 opaque = (void *)f;
44
45 // TYPE: [[RESIGN_VAL:%.*]] = call i64 @llvm.ptrauth.resign(i64 ptrtoint (ptr ptrauth (ptr @f, i32 0, i64 18983) to i64), i32 0, i64 18983, i32 0, i64 0)
46 // TYPE: [[RESIGN_PTR:%.*]] = inttoptr i64 [[RESIGN_VAL]] to ptr
47 // ZERO-NOT: @llvm.ptrauth.resign
48 }
49
50 // CHECK-LABEL: define{{.*}} void @test_cast_from_opaque
51 void test_cast_from_opaque() {
52 fptr = (void (*)(void))opaque;
53
54 // TYPE: [[LOAD:%.*]] = load ptr, ptr @opaque
55 // TYPE: [[CMP:%.*]] = icmp ne ptr [[LOAD]], null
56 // TYPE: br i1 [[CMP]], label %[[RESIGN_LAB:.*]], label
57
58 // TYPE: [[RESIGN_LAB]]:
59 // TYPE: [[INT:%.*]] = ptrtoint ptr [[LOAD]] to i64
60 // TYPE: [[RESIGN_INT:%.*]] = call i64 @llvm.ptrauth.resign(i64 [[INT]], i32 0, i64 0, i32 0, i64 18983)
61
62 // ZERO-NOT: @llvm.ptrauth.resign
63 }
64
65 // CHECK-LABEL: define{{.*}} void @test_cast_to_intptr
66 void test_cast_to_intptr() {
67 uintptr = (unsigned long)fptr;
68
69 // TYPE: [[ENTRY:.*]]:
70 // TYPE: [[LOAD:%.*]] = load ptr, ptr @fptr
71 // TYPE: [[CMP:%.*]] = icmp ne ptr [[LOAD]], null
72 // TYPE: br i1 [[CMP]], label %[[RESIGN_LAB:.*]], label %[[RESIGN_CONT:.*]]
73
74 // TYPE: [[RESIGN_LAB]]:
75 // TYPE: [[INT:%.*]] = ptrtoint ptr [[LOAD]] to i64
76 // TYPE: [[RESIGN_INT:%.*]] = call i64 @llvm.ptrauth.resign(i64 [[INT]], i32 0, i64 18983, i32 0, i64 0)
77 // TYPE: [[RESIGN:%.*]] = inttoptr i64 [[RESIGN_INT]] to ptr
78 // TYPE: br label %[[RESIGN_CONT]]
79
80 // TYPE: [[RESIGN_CONT]]:
81 // TYPE: phi ptr [ null, %[[ENTRY]] ], [ [[RESIGN]], %[[RESIGN_LAB]] ]
82
83 // ZERO-NOT: @llvm.ptrauth.resign
84 }
85
86 // CHECK-LABEL: define{{.*}} void @test_function_to_function_cast
87 void test_function_to_function_cast() {
88 void (*fptr2)(int) = (void (*)(int))fptr;
89 // TYPE: call i64 @llvm.ptrauth.resign(i64 {{.*}}, i32 0, i64 18983, i32 0, i64 2712)
90 // ZERO-NOT: @llvm.ptrauth.resign
91 }
92
93 // CHECK-LABEL: define{{.*}} void @test_call_lvalue_cast
94 void test_call_lvalue_cast() {
95 (*(void (*)(int))f)(42);
96
97 // TYPE: entry:
98 // TYPE-NEXT: [[RESIGN:%.*]] = call i64 @llvm.ptrauth.resign(i64 ptrtoint (ptr ptrauth (ptr @f, i32 0, i64 18983) to i64), i32 0, i64 18983, i32 0, i64 2712)
99 // TYPE-NEXT: [[RESIGN_INT:%.*]] = inttoptr i64 [[RESIGN]] to ptr
100 // TYPE-NEXT: call void [[RESIGN_INT]](i32 noundef 42) [ "ptrauth"(i32 0, i64 2712) ]
101 // ZERO-NOT: @llvm.ptrauth.resign
102 // ZERO: call void ptrauth (ptr @f, i32 0)(i32 noundef 42) [ "ptrauth"(i32 0, i64 0) ]
103 }
104
105
106 #ifdef __cplusplus
107 }
108 #endif