Skip to content

Commit 01da05b

Browse files
committed
[X86][test] Add tests for -fpatchable-function-entry=N,M (where M>0) and its interaction with -fcf-protection=branch
Reviewed By: nickdesaulniers Differential Revision: https://reviews.llvm.org/D73071
1 parent 22467e2 commit 01da05b

File tree

2 files changed

+81
-0
lines changed

2 files changed

+81
-0
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
; RUN: llc -mtriple=x86_64 %s -o - | FileCheck --check-prefixes=CHECK %s
2+
3+
;; -fpatchable-function-entry=0 -fcf-protection=branch
4+
define void @f0() "patchable-function-entry"="0" "branch-target-enforcement" {
5+
; CHECK-LABEL: f0:
6+
; CHECK-NEXT: .Lfunc_begin0:
7+
; CHECK: # %bb.0:
8+
; CHECK-NEXT: endbr64
9+
; CHECK-NEXT: retq
10+
; CHECK-NOT: .section __patchable_function_entries
11+
ret void
12+
}
13+
14+
;; -fpatchable-function-entry=1 -fcf-protection=branch
15+
define void @f1() "patchable-function-entry"="1" {
16+
; CHECK-LABEL: f1:
17+
; CHECK-NEXT: .Lfunc_begin1:
18+
; CHECK: endbr64
19+
; CHECK-NEXT: nop
20+
; CHECK-NEXT: retq
21+
; CHECK: .section __patchable_function_entries,"awo",@progbits,f1,unique,0
22+
; CHECK-NEXT: .p2align 3
23+
; CHECK-NEXT: .quad .Lfunc_begin1
24+
ret void
25+
}
26+
27+
;; -fpatchable-function-entry=2,1 -fcf-protection=branch
28+
define void @f2_1() "patchable-function-entry"="1" "patchable-function-prefix"="1" {
29+
; CHECK-LABEL: .type f2_1,@function
30+
; CHECK-NEXT: .Ltmp0:
31+
; CHECK-NEXT: nop
32+
; CHECK-NEXT: f2_1:
33+
; CHECK-NEXT: .Lfunc_begin2:
34+
; CHECK: # %bb.0:
35+
; CHECK-NEXT: endbr64
36+
; CHECK-NEXT: nop
37+
; CHECK-NEXT: retq
38+
; CHECK: .Lfunc_end2:
39+
; CHECK-NEXT: .size f2_1, .Lfunc_end2-f2_1
40+
; CHECK: .section __patchable_function_entries,"awo",@progbits,f1,unique,0
41+
; CHECK-NEXT: .p2align 3
42+
; CHECK-NEXT: .quad .Ltmp0
43+
ret void
44+
}
45+
46+
!llvm.module.flags = !{!0}
47+
48+
!0 = !{i32 4, !"cf-protection-branch", i32 1}

llvm/test/CodeGen/X86/patchable-function-entry.ll

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,13 @@ define void @f1() "patchable-function-entry"="1" {
2424
ret void
2525
}
2626

27+
;; Without -function-sections, f2 is in the same text section as f1.
28+
;; They share the __patchable_function_entries section.
29+
;; With -function-sections, f1 and f2 are in different text sections.
30+
;; Use separate __patchable_function_entries.
2731
define void @f2() "patchable-function-entry"="2" {
2832
; CHECK-LABEL: f2:
33+
; CHECK-NEXT: .Lfunc_begin2:
2934
; 32-COUNT-2: nop
3035
; 64: xchgw %ax, %ax
3136
; CHECK-NEXT: ret
@@ -41,6 +46,7 @@ define void @f2() "patchable-function-entry"="2" {
4146
$f3 = comdat any
4247
define void @f3() "patchable-function-entry"="3" comdat {
4348
; CHECK-LABEL: f3:
49+
; CHECK-NEXT: .Lfunc_begin3:
4450
; 32-COUNT-3: nop
4551
; 64: nopl (%rax)
4652
; CHECK: ret
@@ -56,6 +62,7 @@ define void @f3() "patchable-function-entry"="3" comdat {
5662
$f5 = comdat any
5763
define void @f5() "patchable-function-entry"="5" comdat {
5864
; CHECK-LABEL: f5:
65+
; CHECK-NEXT: .Lfunc_begin4:
5966
; 32-COUNT-5: nop
6067
; 64: nopl 8(%rax,%rax)
6168
; CHECK-NEXT: ret
@@ -67,3 +74,29 @@ define void @f5() "patchable-function-entry"="5" comdat {
6774
; 64-NEXT: .quad .Lfunc_begin4
6875
ret void
6976
}
77+
78+
;; -fpatchable-function-entry=3,2
79+
;; "patchable-function-prefix" emits data before the function entry label.
80+
;; We emit 1-byte NOPs before the function entry, so that with a partial patch,
81+
;; the remaining instructions do not need to be modified.
82+
define void @f3_2() "patchable-function-entry"="1" "patchable-function-prefix"="2" {
83+
; CHECK-LABEL: .type f3_2,@function
84+
; CHECK-NEXT: .Ltmp0: # @f3_2
85+
; CHECK-NEXT: nop
86+
; CHECK-NEXT: nop
87+
; CHECK-NEXT: f3_2:
88+
; CHECK: # %bb.0:
89+
; CHECK-NEXT: nop
90+
; CHECK-NEXT: ret
91+
;; .size does not include the prefix.
92+
; CHECK: .Lfunc_end5:
93+
; CHECK-NEXT: .size f3_2, .Lfunc_end5-f3_2
94+
; NOFSECT .section __patchable_function_entries,"awo",@progbits,f0,unique,0
95+
; FSECT: .section __patchable_function_entries,"awo",@progbits,f3_2,unique,4
96+
; 32: .p2align 2
97+
; 32-NEXT: .long .Ltmp0
98+
; 64: .p2align 3
99+
; 64-NEXT: .quad .Ltmp0
100+
%frame = alloca i8, i32 16
101+
ret void
102+
}

0 commit comments

Comments
 (0)