Skip to content

Commit b4a52f4

Browse files
committed
[X86][KCFI] Do not emit a type prefix for nocf_check functions
With indirect branch protection, the `nocf_check` attribute prevents a function from being called indirectly by omitting the ENDBR instruction from the beginning of the function body. As KCFI type prefixes are only needed for indirectly callable functions, don't emit the unnecessary prefix for `nocf_check` functions.
1 parent 6ab2b87 commit b4a52f4

File tree

2 files changed

+42
-2
lines changed

2 files changed

+42
-2
lines changed

llvm/lib/Target/X86/X86AsmPrinter.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,8 +170,9 @@ void X86AsmPrinter::emitKCFITypeId(const MachineFunction &MF) {
170170
Type = mdconst::extract<ConstantInt>(MD->getOperand(0));
171171

172172
// If we don't have a type to emit, just emit padding if needed to maintain
173-
// the same alignment for all functions.
174-
if (!Type) {
173+
// the same alignment for all functions. Also skip `nocf_check` functions as
174+
// they are not indirectly callable due to a missing ENDBR.
175+
if (!Type || F.doesNoCfCheck()) {
175176
EmitKCFITypePadding(MF, /*HasType=*/false);
176177
return;
177178
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
; RUN: llc -mtriple=x86_64-unknown-unknown -x86-indirect-branch-tracking < %s | FileCheck %s
2+
3+
; CHECK-LABEL: __cfi_cf_check_func:
4+
; CHECK: movl $12345678, %eax
5+
define void @cf_check_func() !kcfi_type !2 {
6+
; CHECK-LABEL: cf_check_func:
7+
; CHECK: endbr64
8+
; CHECK: retq
9+
entry:
10+
ret void
11+
}
12+
13+
; CHECK-NOT: __cfi_notype_cf_check_func:
14+
; CHECK-NOT: movl
15+
define void @notype_cf_check_func() {
16+
; CHECK-LABEL: notype_cf_check_func:
17+
; CHECK: endbr64
18+
; CHECK: retq
19+
entry:
20+
ret void
21+
}
22+
23+
; CHECK-NOT: __cfi_nocf_check_func:
24+
; CHECK-NOT: movl
25+
define void @nocf_check_func() #0 !kcfi_type !2 {
26+
; CHECK-LABEL: nocf_check_func:
27+
; CHECK-NOT: endbr64
28+
; CHECK: retq
29+
entry:
30+
ret void
31+
}
32+
33+
attributes #0 = { nocf_check }
34+
35+
!llvm.module.flags = !{!0, !1}
36+
37+
!0 = !{i32 8, !"cf-protection-branch", i32 1}
38+
!1 = !{i32 4, !"kcfi", i32 1}
39+
!2 = !{i32 12345678}

0 commit comments

Comments
 (0)