Skip to content

Commit a6f8d63

Browse files
committed
-Zharden-sls flag (target modifier) added to enable mitigation against straight line speculation (SLS)
1 parent f8f6997 commit a6f8d63

File tree

9 files changed

+118
-5
lines changed

9 files changed

+118
-5
lines changed

compiler/rustc_codegen_gcc/src/gcc_util.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use smallvec::{SmallVec, smallvec};
66

77
fn gcc_features_by_flags(sess: &Session, features: &mut Vec<String>) {
88
target_features::retpoline_features_by_flags(sess, features);
9+
target_features::sls_features_by_flags(sess, features);
910
// FIXME: LLVM also sets +reserve-x18 here under some conditions.
1011
}
1112

compiler/rustc_codegen_llvm/src/llvm_util.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -633,6 +633,7 @@ pub(crate) fn target_cpu(sess: &Session) -> &str {
633633
/// The target features for compiler flags other than `-Ctarget-features`.
634634
fn llvm_features_by_flags(sess: &Session, features: &mut Vec<String>) {
635635
target_features::retpoline_features_by_flags(sess, features);
636+
target_features::sls_features_by_flags(sess, features);
636637

637638
// -Zfixed-x18
638639
if sess.opts.unstable_opts.fixed_x18 {

compiler/rustc_codegen_ssa/src/target_features.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use rustc_middle::middle::codegen_fn_attrs::TargetFeature;
77
use rustc_middle::query::Providers;
88
use rustc_middle::ty::TyCtxt;
99
use rustc_session::Session;
10+
use rustc_session::config::HardenSls;
1011
use rustc_session::lint::builtin::AARCH64_SOFTFLOAT_NEON;
1112
use rustc_session::parse::feature_err;
1213
use rustc_span::{Span, Symbol, sym};
@@ -400,6 +401,18 @@ pub fn retpoline_features_by_flags(sess: &Session, features: &mut Vec<String>) {
400401
}
401402
}
402403

404+
pub fn sls_features_by_flags(sess: &Session, features: &mut Vec<String>) {
405+
match &sess.opts.unstable_opts.harden_sls {
406+
HardenSls::None => (),
407+
HardenSls::All => {
408+
features.push("+harden-sls-ijmp".into());
409+
features.push("+harden-sls-ret".into());
410+
}
411+
HardenSls::Return => features.push("+harden-sls-ret".into()),
412+
HardenSls::IndirectJmp => features.push("+harden-sls-ijmp".into()),
413+
}
414+
}
415+
403416
pub(crate) fn provide(providers: &mut Providers) {
404417
*providers = Providers {
405418
rust_target_features: |tcx, cnum| {

compiler/rustc_session/src/config.rs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3172,11 +3172,11 @@ pub(crate) mod dep_tracking {
31723172
use super::{
31733173
AutoDiff, BranchProtection, CFGuard, CFProtection, CollapseMacroDebuginfo, CoverageOptions,
31743174
CrateType, DebugInfo, DebugInfoCompression, ErrorOutputType, FmtDebug, FunctionReturn,
3175-
InliningThreshold, InstrumentCoverage, InstrumentXRay, LinkerPluginLto, LocationDetail,
3176-
LtoCli, MirStripDebugInfo, NextSolverConfig, OomStrategy, OptLevel, OutFileName,
3177-
OutputType, OutputTypes, PatchableFunctionEntry, Polonius, RemapPathScopeComponents,
3178-
ResolveDocLinks, SourceFileHashAlgorithm, SplitDwarfKind, SwitchWithOptPath,
3179-
SymbolManglingVersion, WasiExecModel,
3175+
HardenSls, InliningThreshold, InstrumentCoverage, InstrumentXRay, LinkerPluginLto,
3176+
LocationDetail, LtoCli, MirStripDebugInfo, NextSolverConfig, OomStrategy, OptLevel,
3177+
OutFileName, OutputType, OutputTypes, PatchableFunctionEntry, Polonius,
3178+
RemapPathScopeComponents, ResolveDocLinks, SourceFileHashAlgorithm, SplitDwarfKind,
3179+
SwitchWithOptPath, SymbolManglingVersion, WasiExecModel,
31803180
};
31813181
use crate::lint;
31823182
use crate::utils::NativeLib;
@@ -3278,6 +3278,7 @@ pub(crate) mod dep_tracking {
32783278
Polonius,
32793279
InliningThreshold,
32803280
FunctionReturn,
3281+
HardenSls,
32813282
Align,
32823283
);
32833284

@@ -3532,6 +3533,16 @@ pub enum FunctionReturn {
35323533
ThunkExtern,
35333534
}
35343535

3536+
/// The different settings that the `-Zharden-sls` flag can have.
3537+
#[derive(Clone, Copy, PartialEq, Hash, Debug, Default)]
3538+
pub enum HardenSls {
3539+
#[default]
3540+
None,
3541+
All,
3542+
Return,
3543+
IndirectJmp,
3544+
}
3545+
35353546
/// Whether extra span comments are included when dumping MIR, via the `-Z mir-include-spans` flag.
35363547
/// By default, only enabled in the NLL MIR dumps, and disabled in all other passes.
35373548
#[derive(Clone, Copy, Default, PartialEq, Debug)]

compiler/rustc_session/src/options.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -808,6 +808,7 @@ mod desc {
808808
"either a boolean (`yes`, `no`, `on`, `off`, etc), or a non-negative number";
809809
pub(crate) const parse_llvm_module_flag: &str = "<key>:<type>:<value>:<behavior>. Type must currently be `u32`. Behavior should be one of (`error`, `warning`, `require`, `override`, `append`, `appendunique`, `max`, `min`)";
810810
pub(crate) const parse_function_return: &str = "`keep` or `thunk-extern`";
811+
pub(crate) const parse_harden_sls: &str = "`none`, `all`, `return` or `indirect-jmp`";
811812
pub(crate) const parse_wasm_c_abi: &str = "`spec`";
812813
pub(crate) const parse_mir_include_spans: &str =
813814
"either a boolean (`yes`, `no`, `on`, `off`, etc), or `nll` (default: `nll`)";
@@ -1917,6 +1918,17 @@ pub mod parse {
19171918
true
19181919
}
19191920

1921+
pub(crate) fn parse_harden_sls(slot: &mut HardenSls, v: Option<&str>) -> bool {
1922+
match v {
1923+
Some("none") => *slot = HardenSls::None,
1924+
Some("all") => *slot = HardenSls::All,
1925+
Some("return") => *slot = HardenSls::Return,
1926+
Some("indirect-jmp") => *slot = HardenSls::IndirectJmp,
1927+
_ => return false,
1928+
}
1929+
true
1930+
}
1931+
19201932
pub(crate) fn parse_wasm_c_abi(_slot: &mut (), v: Option<&str>) -> bool {
19211933
v == Some("spec")
19221934
}
@@ -2254,6 +2266,9 @@ options! {
22542266
graphviz_font: String = ("Courier, monospace".to_string(), parse_string, [UNTRACKED],
22552267
"use the given `fontname` in graphviz output; can be overridden by setting \
22562268
environment variable `RUSTC_GRAPHVIZ_FONT` (default: `Courier, monospace`)"),
2269+
harden_sls: HardenSls = (HardenSls::None, parse_harden_sls, [TRACKED TARGET_MODIFIER],
2270+
"flag to mitigate against straight line speculation (SLS) [none|all|return|indirect-jmp] \
2271+
(default: none)"),
22572272
has_thread_local: Option<bool> = (None, parse_opt_bool, [TRACKED],
22582273
"explicitly enable the `cfg(target_thread_local)` directive"),
22592274
hint_mostly_unused: bool = (false, parse_bool, [TRACKED],

compiler/rustc_target/src/target_features.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,16 @@ static X86_FEATURES: &[(&str, Stability, ImpliedFeatures)] = &[
433433
("fma", Stable, &["avx"]),
434434
("fxsr", Stable, &[]),
435435
("gfni", Stable, &["sse2"]),
436+
(
437+
"harden-sls-ijmp",
438+
Stability::Forbidden { reason: "use `harden-sls` compiler flag instead" },
439+
&[],
440+
),
441+
(
442+
"harden-sls-ret",
443+
Stability::Forbidden { reason: "use `harden-sls` compiler flag instead" },
444+
&[],
445+
),
436446
("kl", Stable, &["sse2"]),
437447
("lahfsahf", Unstable(sym::lahfsahf_target_feature), &[]),
438448
("lzcnt", Stable, &[]),

tests/codegen/harden-sls.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// ignore-tidy-linelength
2+
// Test that the `harden-sls-ijmp`, `harden-sls-ret` target features is (not) emitted when
3+
// the `harden-sls=[none|all|return|indirect-jmp]` flag is (not) set.
4+
5+
//@ add-core-stubs
6+
//@ revisions: none all return indirect_jmp
7+
//@ needs-llvm-components: x86
8+
//@ compile-flags: --target x86_64-unknown-linux-gnu
9+
//@ [none] compile-flags: -Zharden-sls=none
10+
//@ [all] compile-flags: -Zharden-sls=all
11+
//@ [return] compile-flags: -Zharden-sls=return
12+
//@ [indirect_jmp] compile-flags: -Zharden-sls=indirect-jmp
13+
14+
#![crate_type = "lib"]
15+
#![feature(no_core)]
16+
#![no_core]
17+
18+
extern crate minicore;
19+
use minicore::*;
20+
21+
#[no_mangle]
22+
pub fn foo() {
23+
// CHECK: @foo() unnamed_addr #0
24+
25+
// none-NOT: attributes #0 = { {{.*}}"target-features"="{{[^"]*}}+harden-sls-ijmp{{.*}} }
26+
// none-NOT: attributes #0 = { {{.*}}"target-features"="{{[^"]*}}+harden-sls-ret{{.*}} }
27+
28+
// all: attributes #0 = { {{.*}}"target-features"="{{[^"]*}}+harden-sls-ijmp,+harden-sls-ret{{.*}} }
29+
30+
// return-NOT: attributes #0 = { {{.*}}"target-features"="{{[^"]*}}+harden-sls-ijmp{{.*}} }
31+
// return: attributes #0 = { {{.*}}"target-features"="{{[^"]*}}+harden-sls-ret{{.*}} }
32+
33+
// indirect_jmp-NOT: attributes #0 = { {{.*}}"target-features"="{{[^"]*}}+harden-sls-ret{{.*}} }
34+
// indirect_jmp: attributes #0 = { {{.*}}"target-features"="{{[^"]*}}+harden-sls-ijmp{{.*}} }
35+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
warning: target feature `harden-sls-ijmp` cannot be enabled with `-Ctarget-feature`: use `harden-sls` compiler flag instead
2+
|
3+
= note: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
4+
= note: for more information, see issue #116344 <https://github.com/rust-lang/rust/issues/116344>
5+
6+
warning: target feature `harden-sls-ret` cannot be enabled with `-Ctarget-feature`: use `harden-sls` compiler flag instead
7+
|
8+
= note: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
9+
= note: for more information, see issue #116344 <https://github.com/rust-lang/rust/issues/116344>
10+
11+
warning: 2 warnings emitted
12+
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//@ add-core-stubs
2+
//@ revisions: by_flag by_feature
3+
//@ compile-flags: --target=x86_64-unknown-linux-gnu --crate-type=lib
4+
//@ needs-llvm-components: x86
5+
//@ [by_flag]compile-flags: -Zharden-sls=all
6+
//@ [by_feature]compile-flags: -Ctarget-feature=+harden-sls-ijmp,+harden-sls-ret
7+
//@ [by_flag]build-pass
8+
// For now this is just a warning.
9+
//@ [by_feature]build-pass
10+
#![feature(no_core, lang_items)]
11+
#![no_std]
12+
#![no_core]
13+
14+
//[by_feature]~? WARN target feature `harden-sls-ijmp` cannot be enabled with `-Ctarget-feature`: use `harden-sls` compiler flag instead
15+
//[by_feature]~? WARN target feature `harden-sls-ret` cannot be enabled with `-Ctarget-feature`: use `harden-sls` compiler flag instead

0 commit comments

Comments
 (0)