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