1 ; RUN: llc < %s -mtriple=aarch64-pc-windows-msvc | FileCheck %s
2 ; Control Flow Guard is currently only available on Windows
4 ; Test that Control Flow Guard checks are correctly added when required.
7 declare i32 @target_func()
10 ; Test that Control Flow Guard checks are not added on calls with the "guard_nocf" attribute.
11 define i32 @func_guard_nocf() {
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
19 ; CHECK-LABEL: func_guard_nocf
20 ; CHECK: adrp x8, target_func
21 ; CHECK: add x8, x8, :lo12:target_func
22 ; CHECK-NOT: __guard_check_icall_fptr
25 attributes #0 = { "guard_nocf" }
28 ; Test that Control Flow Guard checks are added even at -O0.
29 define i32 @func_optnone_cf() #1 {
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
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, target_func
40 ; CHECK: add x8, x8, :lo12:target_func
41 ; CHECK: adrp x9, __guard_check_icall_fptr
42 ; CHECK: add x9, x9, :lo12:__guard_check_icall_fptr
48 attributes #1 = { noinline optnone }
51 ; Test that Control Flow Guard checks are correctly added in optimized code (common case).
52 define i32 @func_cf() {
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
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, :lo12:__guard_check_icall_fptr]
64 ; CHECK: adrp x8, target_func
65 ; CHECK: add x8, x8, :lo12:target_func
72 ; Test that Control Flow Guard checks are correctly added on invoke instructions.
73 define i32 @func_cf_invoke() personality i8* bitcast (void ()* @h to i8*) {
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
80 to label %invoke.cont unwind label %lpad
81 invoke.cont: ; preds = %entry
84 lpad: ; preds = %entry
85 %tmp = landingpad { i8*, i32 }
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, :lo12:__guard_check_icall_fptr]
93 ; CHECK: adrp x8, target_func
94 ; CHECK: add x8, x8, :lo12:target_func
99 ; CHECK: // %common.ret
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
110 define 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
118 ; CHECK-LABEL: func_cf_setjmp
120 ; CHECK-NEXT: $cfgsj_func_cf_setjmp0:
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
126 ; CHECK-NEXT: $cfgsj_func_cf_setjmp1:
128 store i32 1, i32* %2, align 4
129 %7 = load i32, i32* %2, align 4
132 ; CHECK: .section .gljmp$y,"dr"
133 ; CHECK-NEXT: .symidx $cfgsj_func_cf_setjmp0
134 ; CHECK-NEXT: .symidx $cfgsj_func_cf_setjmp1
137 declare i8* @llvm.frameaddress(i32)
139 ; Function Attrs: returns_twice
140 declare dso_local i32 @_setjmp(i8*, i8*) #2
142 attributes #2 = { returns_twice }
143 attributes #3 = { returns_twice }
146 !llvm.module.flags = !{!0}
147 !0 = !{i32 2, !"cfguard", i32 2}