Skip to content

Commit 0d59f60

Browse files
ojedaintel-lab-lkp
authored andcommitted
x86/rust: support RETPOLINE
Support the `RETPOLINE` speculation mitigation by enabling the target features that Clang does. The existing target feature being enabled was a leftover from our old `rust` branch, and it is not enough: the target feature `retpoline-external-thunk` only implies `retpoline-indirect-calls`, but not `retpoline-indirect-branches` (see LLVM's `X86.td`), unlike Clang's flag of the same name `-mretpoline-external-thunk` which does imply both (see Clang's `lib/Driver/ToolChains/Arch/X86.cpp`). Without this, `objtool` would complain if enabled for individual object files (like it is planned in the future), e.g. rust/core.o: warning: objtool: _R...escape_default+0x13: indirect jump found in RETPOLINE build In addition, change the comment to note that LLVM is the one disabling jump tables when retpoline is enabled, thus we do not need to use `-Zno-jump-tables` for Rust here -- see commit c58f2166ab39 ("Introduce the "retpoline" x86 mitigation technique ...") [1]: The goal is simple: avoid generating code which contains an indirect branch that could have its prediction poisoned by an attacker. In many cases, the compiler can simply use directed conditional branches and a small search tree. LLVM already has support for lowering switches in this way and the first step of this patch is to disable jump-table lowering of switches and introduce a pass to rewrite explicit indirectbr sequences into a switch over integers. As well as a live example at [2]. Cc: Daniel Borkmann <[email protected]> Link: llvm/llvm-project@c58f216 [1] Link: https://godbolt.org/z/esT8xnaxj [2] Signed-off-by: Miguel Ojeda <[email protected]>
1 parent 5872080 commit 0d59f60

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

arch/x86/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ ifdef CONFIG_RETPOLINE
195195
KBUILD_CFLAGS += $(RETPOLINE_CFLAGS)
196196
# Additionally, avoid generating expensive indirect jumps which
197197
# are subject to retpolines for small number of switch cases.
198-
# clang turns off jump table generation by default when under
198+
# LLVM turns off jump table generation by default when under
199199
# retpoline builds, however, gcc does not for x86. This has
200200
# only been fixed starting from gcc stable version 8.4.0 and
201201
# onwards, but not for older ones. See gcc bug #86952.

scripts/generate_rust_target.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,14 @@ fn main() {
156156
);
157157
let mut features = "-3dnow,-3dnowa,-mmx,+soft-float".to_string();
158158
if cfg.has("RETPOLINE") {
159+
// The kernel uses `-mretpoline-external-thunk` (for Clang), which Clang maps to the
160+
// target feature of the same name plus the other two target features in
161+
// `clang/lib/Driver/ToolChains/Arch/X86.cpp`. These should be eventually enabled via
162+
// `-Ctarget-feature` when `rustc` starts recognizing them (or via a new dedicated
163+
// flag); see https://github.com/rust-lang/rust/issues/116852.
159164
features += ",+retpoline-external-thunk";
165+
features += ",+retpoline-indirect-branches";
166+
features += ",+retpoline-indirect-calls";
160167
}
161168
ts.push("features", features);
162169
ts.push("llvm-target", "x86_64-linux-gnu");

0 commit comments

Comments
 (0)