Skip to content

Commit 82af950

Browse files
Joao Moreiraphoebewang
authored andcommitted
[X86] Enable ibt-seal optimization when LTO is used in Kernel
Intel's CET/IBT requires every indirect branch target to be an ENDBR instruction. Because of that, the compiler needs to correctly emit these instruction on function's prologues. Because this is a security feature, it is desirable that only actual indirect-branch-targeted functions are emitted with ENDBRs. While it is possible to identify address-taken functions through LTO, minimizing these ENDBR instructions remains a hard task for user-space binaries because exported functions may end being reachable through PLT entries, that will use an indirect branch for such. Because this cannot be determined during compilation-time, the compiler currently emits ENDBRs to every non-local-linkage function. Despite the challenge presented for user-space, the kernel landscape is different as no PLTs are used. With the intent of providing the most fit ENDBR emission for the kernel, kernel developers proposed an optimization named "ibt-seal" which replaces the ENDBRs for NOPs directly in the binary. The discussion of this feature can be seen in [1]. This diff brings the enablement of the flag -mibt-seal, which in combination with LTO enforces a different policy for ENDBR placement in when the code-model is set to "kernel". In this scenario, the compiler will only emit ENDBRs to address taken functions, ignoring non-address taken functions that are don't have local linkage. A comparison between an LTO-compiled kernel binaries without and with the -mibt-seal feature enabled shows that when -mibt-seal was used, the number of ENDBRs in the vmlinux.o binary patched by objtool decreased from 44383 to 33192, and that the number of superfluous ENDBR instructions nopped-out decreased from 11730 to 540. The 540 missed superfluous ENDBRs need to be investigated further, but hypotheses are: assembly code not being taken care of by the compiler, kernel exported symbols mechanisms creating bogus address taken situations or even these being removed due to other binary optimizations like kernel's static_calls. For now, I assume that the large drop in the number of ENDBR instructions already justifies the feature being merged. [1] - https://lkml.org/lkml/2021/11/22/591 Reviewed By: xiangzhangllvm Differential Revision: https://reviews.llvm.org/D116070
1 parent d93a11c commit 82af950

File tree

9 files changed

+107
-11
lines changed

9 files changed

+107
-11
lines changed

clang/include/clang/Basic/CodeGenOptions.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,8 @@ CODEGENOPT(CFProtectionReturn , 1, 0) ///< if -fcf-protection is
107107
///< set to full or return.
108108
CODEGENOPT(CFProtectionBranch , 1, 0) ///< if -fcf-protection is
109109
///< set to full or branch.
110+
CODEGENOPT(IBTSeal, 1, 0) ///< set to optimize CFProtectionBranch.
111+
110112
CODEGENOPT(XRayInstrumentFunctions , 1, 0) ///< Set when -fxray-instrument is
111113
///< enabled.
112114
CODEGENOPT(StackSizeSection , 1, 0) ///< Set when -fstack-size-section is enabled.

clang/include/clang/Driver/Options.td

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1927,6 +1927,8 @@ def fcf_protection_EQ : Joined<["-"], "fcf-protection=">, Flags<[CoreOption, CC1
19271927
def fcf_protection : Flag<["-"], "fcf-protection">, Group<f_Group>, Flags<[CoreOption, CC1Option]>,
19281928
Alias<fcf_protection_EQ>, AliasArgs<["full"]>,
19291929
HelpText<"Enable cf-protection in 'full' mode">;
1930+
def mibt_seal : Flag<["-"], "mibt-seal">, Group<m_Group>, Flags<[CoreOption, CC1Option]>,
1931+
HelpText<"Optimize fcf-protection=branch/full (requires LTO).">;
19301932

19311933
defm xray_instrument : BoolFOption<"xray-instrument",
19321934
LangOpts<"XRayInstrument">, DefaultFalse,

clang/lib/CodeGen/CodeGenModule.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -712,6 +712,9 @@ void CodeGenModule::Release() {
712712
1);
713713
}
714714

715+
if (CodeGenOpts.IBTSeal)
716+
getModule().addModuleFlag(llvm::Module::Override, "ibt-seal", 1);
717+
715718
// Add module metadata for return address signing (ignoring
716719
// non-leaf/all) and stack tagging. These are actually turned on by function
717720
// attributes, but we use module metadata to emit build attributes. This is

clang/lib/Driver/ToolChains/Clang.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6166,6 +6166,9 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
61666166
Args.MakeArgString(Twine("-fcf-protection=") + A->getValue()));
61676167
}
61686168

6169+
if (IsUsingLTO)
6170+
Args.AddLastArg(CmdArgs, options::OPT_mibt_seal);
6171+
61696172
// Forward -f options with positive and negative forms; we translate these by
61706173
// hand. Do not propagate PGO options to the GPU-side compilations as the
61716174
// profile info is for the host-side compilation only.

clang/lib/Frontend/CompilerInvocation.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1814,6 +1814,9 @@ bool CompilerInvocation::ParseCodeGenArgs(CodeGenOptions &Opts, ArgList &Args,
18141814
Diags.Report(diag::err_drv_invalid_value) << A->getAsString(Args) << Name;
18151815
}
18161816

1817+
if (Opts.PrepareForLTO && Args.hasArg(OPT_mibt_seal))
1818+
Opts.IBTSeal = 1;
1819+
18171820
for (auto *A :
18181821
Args.filtered(OPT_mlink_bitcode_file, OPT_mlink_builtin_bitcode)) {
18191822
CodeGenOptions::BitcodeFileToLink F;

llvm/lib/Target/X86/X86IndirectBranchTracking.cpp

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -95,14 +95,45 @@ static bool IsCallReturnTwice(llvm::MachineOperand &MOp) {
9595
return Attrs.hasFnAttr(Attribute::ReturnsTwice);
9696
}
9797

98+
// Checks if function should have an ENDBR in its prologue
99+
static bool needsPrologueENDBR(MachineFunction &MF, const Module *M) {
100+
Function &F = MF.getFunction();
101+
102+
if (F.doesNoCfCheck())
103+
return false;
104+
105+
const X86TargetMachine *TM =
106+
static_cast<const X86TargetMachine *>(&MF.getTarget());
107+
Metadata *IBTSeal = M->getModuleFlag("ibt-seal");
108+
109+
switch (TM->getCodeModel()) {
110+
// Large code model functions always reachable through indirect calls.
111+
case CodeModel::Large:
112+
return true;
113+
// Only address taken functions in LTO'ed kernel are reachable indirectly.
114+
// IBTSeal implies LTO, thus only check if function is address taken.
115+
case CodeModel::Kernel:
116+
// Check if ibt-seal was enabled (implies LTO is being used).
117+
if (IBTSeal) {
118+
return F.hasAddressTaken();
119+
}
120+
// if !IBTSeal, fall into default case.
121+
LLVM_FALLTHROUGH;
122+
// Address taken or externally linked functions may be reachable.
123+
default:
124+
return (F.hasAddressTaken() || !F.hasLocalLinkage());
125+
}
126+
}
127+
98128
bool X86IndirectBranchTrackingPass::runOnMachineFunction(MachineFunction &MF) {
99129
const X86Subtarget &SubTarget = MF.getSubtarget<X86Subtarget>();
100130

131+
const Module *M = MF.getMMI().getModule();
101132
// Check that the cf-protection-branch is enabled.
102-
Metadata *isCFProtectionSupported =
103-
MF.getMMI().getModule()->getModuleFlag("cf-protection-branch");
104-
// NB: We need to enable IBT in jitted code if JIT compiler is CET
105-
// enabled.
133+
Metadata *isCFProtectionSupported = M->getModuleFlag("cf-protection-branch");
134+
135+
// NB: We need to enable IBT in jitted code if JIT compiler is CET
136+
// enabled.
106137
const X86TargetMachine *TM =
107138
static_cast<const X86TargetMachine *>(&MF.getTarget());
108139
#ifdef __CET__
@@ -119,13 +150,8 @@ bool X86IndirectBranchTrackingPass::runOnMachineFunction(MachineFunction &MF) {
119150
TII = SubTarget.getInstrInfo();
120151
EndbrOpcode = SubTarget.is64Bit() ? X86::ENDBR64 : X86::ENDBR32;
121152

122-
// Large code model, non-internal function or function whose address
123-
// was taken, can be accessed through indirect calls. Mark the first
124-
// BB with ENDBR instruction unless nocf_check attribute is used.
125-
if ((TM->getCodeModel() == CodeModel::Large ||
126-
MF.getFunction().hasAddressTaken() ||
127-
!MF.getFunction().hasLocalLinkage()) &&
128-
!MF.getFunction().doesNoCfCheck()) {
153+
// If function is reachable indirectly, mark the first BB with ENDBR.
154+
if (needsPrologueENDBR(MF, M)) {
129155
auto MBB = MF.begin();
130156
Changed |= addENDBR(*MBB, MBB->begin());
131157
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
; RUN: llc < %s -O2 -mtriple=x86_64-unknown-linux-gnu -x86-indirect-branch-tracking --code-model=kernel | FileCheck %s --check-prefix=CHECK-KERNEL-IBTSEAL
2+
3+
; CHECK-KERNEL-IBTSEAL: foo:
4+
; CHECK-KERNEL-IBTSEAL: endbr
5+
; CHECK-KERNEL-IBTSEAL: bar:
6+
; CHECK-KERNEL-IBTSEAL-NOT: endbr
7+
8+
target triple = "x86_64-unknown-linux-gnu"
9+
10+
define dso_local void @foo() {
11+
ret void
12+
}
13+
14+
define dso_local i8* @bar() {
15+
ret i8* bitcast (void ()* @foo to i8*)
16+
}
17+
18+
!llvm.module.flags = !{!1}
19+
!1 = !{i32 4, !"ibt-seal", i32 1}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
; RUN: llc < %s -O2 -mtriple=x86_64-unknown-linux-gnu -x86-indirect-branch-tracking --code-model=large | FileCheck %s --check-prefix=CHECK-LARGE-IBTSEAL
2+
3+
; CHECK-LARGE-IBTSEAL: foo:
4+
; CHECK-LARGE-IBTSEAL: endbr
5+
; CHECK-LARGE-IBTSEAL: bar:
6+
; CHECK-LARGE-IBTSEAL: endbr
7+
8+
target triple = "x86_64-unknown-linux-gnu"
9+
10+
define dso_local void @foo() {
11+
ret void
12+
}
13+
14+
define dso_local i8* @bar() {
15+
ret i8* bitcast (void ()* @foo to i8*)
16+
}
17+
18+
!llvm.module.flags = !{!1}
19+
!1 = !{i32 4, !"ibt-seal", i32 1}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
; RUN: llc < %s -O2 -mtriple=x86_64-unknown-linux-gnu -x86-indirect-branch-tracking --code-model=small | FileCheck %s --check-prefix=CHECK-SMALL-IBTSEAL
2+
3+
; CHECK-SMALL-IBTSEAL: foo:
4+
; CHECK-SMALL-IBTSEAL: endbr
5+
; CHECK-SMALL-IBTSEAL: bar:
6+
; CHECK-SMALL-IBTSEAL: endbr
7+
8+
target triple = "x86_64-unknown-linux-gnu"
9+
10+
define dso_local void @foo() {
11+
ret void
12+
}
13+
14+
define dso_local i8* @bar() {
15+
ret i8* bitcast (void ()* @foo to i8*)
16+
}
17+
18+
!llvm.module.flags = !{!1}
19+
!1 = !{i32 4, !"ibt-seal", i32 1}

0 commit comments

Comments
 (0)