Skip to content

Commit 821c2d7

Browse files
committed
-Zharden-sls flag (target modifier) added to enable mitigation against straight line speculation (SLS)
1 parent 945d0f1 commit 821c2d7

File tree

6 files changed

+118
-5
lines changed

6 files changed

+118
-5
lines changed

Diff for: compiler/rustc_session/src/config.rs

+16-5
Original file line numberDiff line numberDiff line change
@@ -2944,11 +2944,11 @@ pub(crate) mod dep_tracking {
29442944
use super::{
29452945
AutoDiff, BranchProtection, CFGuard, CFProtection, CollapseMacroDebuginfo, CoverageOptions,
29462946
CrateType, DebugInfo, DebugInfoCompression, ErrorOutputType, FmtDebug, FunctionReturn,
2947-
InliningThreshold, InstrumentCoverage, InstrumentXRay, LinkerPluginLto, LocationDetail,
2948-
LtoCli, MirStripDebugInfo, NextSolverConfig, OomStrategy, OptLevel, OutFileName,
2949-
OutputType, OutputTypes, PatchableFunctionEntry, Polonius, RemapPathScopeComponents,
2950-
ResolveDocLinks, SourceFileHashAlgorithm, SplitDwarfKind, SwitchWithOptPath,
2951-
SymbolManglingVersion, WasiExecModel,
2947+
HardenSls, InliningThreshold, InstrumentCoverage, InstrumentXRay, LinkerPluginLto,
2948+
LocationDetail, LtoCli, MirStripDebugInfo, NextSolverConfig, OomStrategy, OptLevel,
2949+
OutFileName, OutputType, OutputTypes, PatchableFunctionEntry, Polonius,
2950+
RemapPathScopeComponents, ResolveDocLinks, SourceFileHashAlgorithm, SplitDwarfKind,
2951+
SwitchWithOptPath, SymbolManglingVersion, WasiExecModel,
29522952
};
29532953
use crate::lint;
29542954
use crate::utils::NativeLib;
@@ -3049,6 +3049,7 @@ pub(crate) mod dep_tracking {
30493049
Polonius,
30503050
InliningThreshold,
30513051
FunctionReturn,
3052+
HardenSls,
30523053
WasmCAbi,
30533054
Align,
30543055
);
@@ -3304,6 +3305,16 @@ pub enum FunctionReturn {
33043305
ThunkExtern,
33053306
}
33063307

3308+
/// The different settings that the `-Zharden-sls` flag can have.
3309+
#[derive(Clone, Copy, PartialEq, Hash, Debug, Default)]
3310+
pub enum HardenSls {
3311+
#[default]
3312+
None,
3313+
All,
3314+
Return,
3315+
IndirectJmp,
3316+
}
3317+
33073318
/// Whether extra span comments are included when dumping MIR, via the `-Z mir-include-spans` flag.
33083319
/// By default, only enabled in the NLL MIR dumps, and disabled in all other passes.
33093320
#[derive(Clone, Copy, Default, PartialEq, Debug)]

Diff for: compiler/rustc_session/src/options.rs

+27
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,7 @@ macro_rules! top_level_options {
290290
pub fn target_feature_flag_enabled(&self, flag: &str) -> bool {
291291
match flag {
292292
"x86-retpoline" => self.unstable_opts.x86_retpoline,
293+
"harden-sls" => !matches!(self.unstable_opts.harden_sls, HardenSls::None),
293294
_ => false,
294295
}
295296
}
@@ -307,6 +308,17 @@ macro_rules! top_level_options {
307308
+retpoline-indirect-calls"
308309
);
309310
}
311+
if let Some(features) = match unstable_opts.harden_sls {
312+
HardenSls::None => None,
313+
HardenSls::All => Some("+harden-sls-ijmp,+harden-sls-ret"),
314+
HardenSls::Return => Some("+harden-sls-ret"),
315+
HardenSls::IndirectJmp => Some("+harden-sls-ijmp"),
316+
} {
317+
if !cg.target_feature.is_empty() {
318+
cg.target_feature.push(',');
319+
}
320+
cg.target_feature.push_str(features);
321+
}
310322
}
311323
}
312324
);
@@ -812,6 +824,7 @@ mod desc {
812824
"either a boolean (`yes`, `no`, `on`, `off`, etc), or a non-negative number";
813825
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`)";
814826
pub(crate) const parse_function_return: &str = "`keep` or `thunk-extern`";
827+
pub(crate) const parse_harden_sls: &str = "`none`, `all`, `return` or `indirect-jmp`";
815828
pub(crate) const parse_wasm_c_abi: &str = "`legacy` or `spec`";
816829
pub(crate) const parse_mir_include_spans: &str =
817830
"either a boolean (`yes`, `no`, `on`, `off`, etc), or `nll` (default: `nll`)";
@@ -1908,6 +1921,17 @@ pub mod parse {
19081921
true
19091922
}
19101923

1924+
pub(crate) fn parse_harden_sls(slot: &mut HardenSls, v: Option<&str>) -> bool {
1925+
match v {
1926+
Some("none") => *slot = HardenSls::None,
1927+
Some("all") => *slot = HardenSls::All,
1928+
Some("return") => *slot = HardenSls::Return,
1929+
Some("indirect-jmp") => *slot = HardenSls::IndirectJmp,
1930+
_ => return false,
1931+
}
1932+
true
1933+
}
1934+
19111935
pub(crate) fn parse_wasm_c_abi(slot: &mut WasmCAbi, v: Option<&str>) -> bool {
19121936
match v {
19131937
Some("spec") => *slot = WasmCAbi::Spec,
@@ -2236,6 +2260,9 @@ options! {
22362260
graphviz_font: String = ("Courier, monospace".to_string(), parse_string, [UNTRACKED],
22372261
"use the given `fontname` in graphviz output; can be overridden by setting \
22382262
environment variable `RUSTC_GRAPHVIZ_FONT` (default: `Courier, monospace`)"),
2263+
harden_sls: HardenSls = (HardenSls::None, parse_harden_sls, [TRACKED TARGET_MODIFIER],
2264+
"flag to mitigate against straight line speculation (SLS) [none|all|return|indirect-jmp] \
2265+
(default: none)"),
22392266
has_thread_local: Option<bool> = (None, parse_opt_bool, [TRACKED],
22402267
"explicitly enable the `cfg(target_thread_local)` directive"),
22412268
human_readable_cgu_names: bool = (false, parse_bool, [TRACKED],

Diff for: compiler/rustc_target/src/target_features.rs

+16
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,22 @@ const X86_FEATURES: &[(&str, Stability, ImpliedFeatures)] = &[
421421
("fma", Stable, &["avx"]),
422422
("fxsr", Stable, &[]),
423423
("gfni", Unstable(sym::avx512_target_feature), &["sse2"]),
424+
(
425+
"harden-sls-ijmp",
426+
Stability::EnabledByTargetModifierFlag {
427+
reason: "use `harden-sls` target modifier flag instead",
428+
flag: "harden-sls",
429+
},
430+
&[],
431+
),
432+
(
433+
"harden-sls-ret",
434+
Stability::EnabledByTargetModifierFlag {
435+
reason: "use `harden-sls` target modifier flag instead",
436+
flag: "harden-sls",
437+
},
438+
&[],
439+
),
424440
("lahfsahf", Unstable(sym::lahfsahf_target_feature), &[]),
425441
("lzcnt", Stable, &[]),
426442
("movbe", Stable, &[]),

Diff for: tests/codegen/harden-sls.rs

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Test that the `harden-sls-ijmp`, `harden-sls-ret` target features is (not) emitted when
2+
// the `harden-sls=[none|all|return|indirect-jmp]` flag is (not) set.
3+
4+
//@ revisions: none all return indirect_jmp
5+
//@ needs-llvm-components: x86
6+
//@ compile-flags: --target x86_64-unknown-linux-gnu
7+
//@ [none] compile-flags: -Zharden-sls=none
8+
//@ [all] compile-flags: -Zharden-sls=all
9+
//@ [return] compile-flags: -Zharden-sls=return
10+
//@ [indirect_jmp] compile-flags: -Zharden-sls=indirect-jmp
11+
12+
#![crate_type = "lib"]
13+
#![feature(no_core, lang_items)]
14+
#![no_core]
15+
16+
#[lang = "sized"]
17+
trait Sized {}
18+
19+
#[no_mangle]
20+
pub fn foo() {
21+
// CHECK: @foo() unnamed_addr #0
22+
23+
// none-NOT: attributes #0 = { {{.*}}"target-features"="{{[^"]*}}+harden-sls-ijmp{{.*}} }
24+
// none-NOT: attributes #0 = { {{.*}}"target-features"="{{[^"]*}}+harden-sls-ret{{.*}} }
25+
26+
// all: attributes #0 = { {{.*}}"target-features"="{{[^"]*}}+harden-sls-ijmp,+harden-sls-ret{{.*}} }
27+
28+
// return-NOT: attributes #0 = { {{.*}}"target-features"="{{[^"]*}}+harden-sls-ijmp{{.*}} }
29+
// return: attributes #0 = { {{.*}}"target-features"="{{[^"]*}}+harden-sls-ret{{.*}} }
30+
31+
// indirect_jmp-NOT: attributes #0 = { {{.*}}"target-features"="{{[^"]*}}+harden-sls-ret{{.*}} }
32+
// indirect_jmp: attributes #0 = { {{.*}}"target-features"="{{[^"]*}}+harden-sls-ijmp{{.*}} }
33+
}
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` target modifier 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` target modifier 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+
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//@ revisions: by_flag by_feature
2+
//@ compile-flags: --target=x86_64-unknown-linux-gnu --crate-type=lib
3+
//@ needs-llvm-components: x86
4+
//@ [by_flag]compile-flags: -Zharden-sls=all
5+
//@ [by_feature]compile-flags: -Ctarget-feature=+harden-sls-ijmp,+harden-sls-ret
6+
//@ [by_flag]build-pass
7+
// For now this is just a warning.
8+
//@ [by_feature]build-pass
9+
#![feature(no_core, lang_items)]
10+
#![no_std]
11+
#![no_core]
12+
13+
#[lang = "sized"]
14+
pub trait Sized {}

0 commit comments

Comments
 (0)