xref: /llvm-project/llvm/test/CodeGen/ARM/cfguard-checks.ll (revision bed1c7f061aa12417aa081e334afdba45767b938)
1; RUN: llc < %s -mtriple=arm-pc-windows-msvc | FileCheck %s -check-prefixes=CHECK,MSVC
2; RUN: llc < %s -mtriple=arm-w64-windows-gnu | FileCheck %s -check-prefixes=CHECK,MINGW
3; Control Flow Guard is currently only available on Windows
4
5; Test that Control Flow Guard checks are correctly added when required.
6
7
8declare i32 @target_func()
9
10
11; Test that Control Flow Guard checks are not added on calls with the "guard_nocf" attribute.
12define i32 @func_guard_nocf() #0 {
13entry:
14  %func_ptr = alloca ptr, align 8
15  store ptr @target_func, ptr %func_ptr, align 8
16  %0 = load ptr, ptr %func_ptr, align 8
17  %1 = call arm_aapcs_vfpcc i32 %0() #1
18  ret i32 %1
19
20  ; CHECK-LABEL: func_guard_nocf
21  ; CHECK:       movw r0, :lower16:target_func
22	; CHECK:       movt r0, :upper16:target_func
23  ; CHECK-NOT:   __guard_check_icall_fptr
24	; CHECK:       blx r0
25}
26attributes #0 = { "target-cpu"="cortex-a9" "target-features"="+armv7-a,+dsp,+fp16,+neon,+strict-align,+thumb-mode,+vfp3"}
27attributes #1 = { "guard_nocf" }
28
29
30; Test that Control Flow Guard checks are added even at -O0.
31define i32 @func_optnone_cf() #2 {
32entry:
33  %func_ptr = alloca ptr, align 8
34  store ptr @target_func, ptr %func_ptr, align 8
35  %0 = load ptr, ptr %func_ptr, align 8
36  %1 = call i32 %0()
37  ret i32 %1
38
39  ; The call to __guard_check_icall_fptr should come immediately before the call to the target function.
40  ; CHECK-LABEL: func_optnone_cf
41	; CHECK:       movw r0, :lower16:target_func
42	; CHECK:       movt r0, :upper16:target_func
43	; CHECK:       str r0, [sp]
44	; CHECK:       ldr r4, [sp]
45	; CHECK:       movw r0, :lower16:__guard_check_icall_fptr
46	; CHECK:       movt r0, :upper16:__guard_check_icall_fptr
47	; CHECK:       ldr r1, [r0]
48	; CHECK:       mov r0, r4
49	; CHECK:       blx r1
50	; CHECK-NEXT:  blx r4
51}
52attributes #2 = { noinline optnone "target-cpu"="cortex-a9" "target-features"="+armv7-a,+dsp,+fp16,+neon,+strict-align,+thumb-mode,+vfp3"}
53
54
55; Test that Control Flow Guard checks are correctly added in optimized code (common case).
56define i32 @func_cf() #0 {
57entry:
58  %func_ptr = alloca ptr, align 8
59  store ptr @target_func, ptr %func_ptr, align 8
60  %0 = load ptr, ptr %func_ptr, align 8
61  %1 = call i32 %0()
62  ret i32 %1
63
64  ; The call to __guard_check_icall_fptr should come immediately before the call to the target function.
65  ; CHECK-LABEL: func_cf
66  ; CHECK:       movw r0, :lower16:__guard_check_icall_fptr
67	; CHECK:       movt r0, :upper16:__guard_check_icall_fptr
68	; CHECK:       ldr r1, [r0]
69  ; CHECK:       movw r4, :lower16:target_func
70	; CHECK:       movt r4, :upper16:target_func
71	; CHECK:       mov r0, r4
72	; CHECK:       blx r1
73	; CHECK-NEXT:  blx r4
74}
75
76
77; Test that Control Flow Guard checks are correctly added on invoke instructions.
78define i32 @func_cf_invoke() #0 personality ptr @h {
79entry:
80  %0 = alloca i32, align 4
81  %func_ptr = alloca ptr, align 8
82  store ptr @target_func, ptr %func_ptr, align 8
83  %1 = load ptr, ptr %func_ptr, align 8
84  %2 = invoke i32 %1()
85          to label %invoke.cont unwind label %lpad
86invoke.cont:                                      ; preds = %entry
87  ret i32 %2
88
89lpad:                                             ; preds = %entry
90  %tmp = landingpad { ptr, i32 }
91          catch ptr null
92  ret i32 -1
93
94  ; The call to __guard_check_icall_fptr should come immediately before the call to the target function.
95  ; CHECK-LABEL: func_cf_invoke
96  ; CHECK:       movw r0, :lower16:__guard_check_icall_fptr
97	; CHECK:       movt r0, :upper16:__guard_check_icall_fptr
98	; CHECK:       ldr r1, [r0]
99  ; CHECK:       movw r4, :lower16:target_func
100	; CHECK:       movt r4, :upper16:target_func
101	; CHECK:       mov r0, r4
102	; CHECK:       blx r1
103  ; MSVC-NEXT:   $Mtmp0:
104  ; MINGW-NEXT:  .Ltmp0:
105	; CHECK-NEXT:  blx r4
106  ; CHECK:       @ %common.ret
107  ; CHECK:       @ %lpad
108}
109
110declare void @h()
111
112
113; Test that longjmp targets have public labels and are included in the .gljmp section.
114%struct._SETJMP_FLOAT128 = type { [2 x i64] }
115@buf1 = internal global [16 x %struct._SETJMP_FLOAT128] zeroinitializer, align 16
116
117define i32 @func_cf_setjmp() #0 {
118  %1 = alloca i32, align 4
119  %2 = alloca i32, align 4
120  store i32 0, ptr %1, align 4
121  store i32 -1, ptr %2, align 4
122  %3 = call ptr @llvm.frameaddress(i32 0)
123  %4 = call i32 @_setjmp(ptr @buf1, ptr %3) #3
124
125  ; CHECK-LABEL: func_cf_setjmp
126  ; CHECK:       bl _setjmp
127  ; CHECK-NEXT:  $cfgsj_func_cf_setjmp0:
128
129  %5 = call ptr @llvm.frameaddress(i32 0)
130  %6 = call i32 @_setjmp(ptr @buf1, ptr %5) #3
131
132  ; CHECK:       bl _setjmp
133  ; CHECK-NEXT:  $cfgsj_func_cf_setjmp1:
134
135  store i32 1, ptr %2, align 4
136  %7 = load i32, ptr %2, align 4
137  ret i32 %7
138
139  ; CHECK:       .section .gljmp$y,"dr"
140  ; CHECK-NEXT:  .symidx $cfgsj_func_cf_setjmp0
141  ; CHECK-NEXT:  .symidx $cfgsj_func_cf_setjmp1
142}
143
144declare ptr @llvm.frameaddress(i32)
145
146; Function Attrs: returns_twice
147declare dso_local i32 @_setjmp(ptr, ptr) #3
148
149attributes #3 = { returns_twice }
150
151
152!llvm.module.flags = !{!0}
153!0 = !{i32 2, !"cfguard", i32 2}
154