Skip to content

Commit 0e89893

Browse files
committed
-Zharden-sls flag (target modifier) added to enable mitigation against straight line speculation (SLS)
1 parent 69d4d5f commit 0e89893

File tree

20 files changed

+234
-28
lines changed

20 files changed

+234
-28
lines changed

compiler/rustc_codegen_gcc/src/gcc_util.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use rustc_target::spec::Arch;
77

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

compiler/rustc_codegen_llvm/src/llvm_util.rs

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

631632
// -Zfixed-x18
632633
if sess.opts.unstable_opts.fixed_x18 {

compiler/rustc_codegen_ssa/messages.ftl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@ codegen_ssa_field_associated_value_expected = associated value expected for `{$n
7272
7373
codegen_ssa_forbidden_ctarget_feature =
7474
target feature `{$feature}` cannot be {$enabled} with `-Ctarget-feature`: {$reason}
75-
.note = this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
7675
codegen_ssa_forbidden_ctarget_feature_issue = for more information, see issue #116344 <https://github.com/rust-lang/rust/issues/116344>
7776
7877
codegen_ssa_forbidden_target_feature_attr =

compiler/rustc_codegen_ssa/src/errors.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1252,7 +1252,6 @@ pub(crate) struct UnstableCTargetFeature<'a> {
12521252

12531253
#[derive(Diagnostic)]
12541254
#[diag(codegen_ssa_forbidden_ctarget_feature)]
1255-
#[note]
12561255
#[note(codegen_ssa_forbidden_ctarget_feature_issue)]
12571256
pub(crate) struct ForbiddenCTargetFeature<'a> {
12581257
pub feature: &'a str,

compiler/rustc_codegen_ssa/src/target_features.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use rustc_middle::middle::codegen_fn_attrs::{TargetFeature, TargetFeatureKind};
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};
@@ -297,7 +298,7 @@ pub fn cfg_target_feature<'a, const N: usize>(
297298
}
298299
Some((_, stability, _)) => {
299300
if let Err(reason) = stability.toggle_allowed() {
300-
sess.dcx().emit_warn(errors::ForbiddenCTargetFeature {
301+
sess.dcx().emit_err(errors::ForbiddenCTargetFeature {
301302
feature: base_feature,
302303
enabled: if enable { "enabled" } else { "disabled" },
303304
reason,
@@ -415,6 +416,18 @@ pub fn retpoline_features_by_flags(sess: &Session, features: &mut Vec<String>) {
415416
}
416417
}
417418

419+
pub fn sls_features_by_flags(sess: &Session, features: &mut Vec<String>) {
420+
match &sess.opts.unstable_opts.harden_sls {
421+
HardenSls::None => (),
422+
HardenSls::All => {
423+
features.push("+harden-sls-ijmp".into());
424+
features.push("+harden-sls-ret".into());
425+
}
426+
HardenSls::Return => features.push("+harden-sls-ret".into()),
427+
HardenSls::IndirectJmp => features.push("+harden-sls-ijmp".into()),
428+
}
429+
}
430+
418431
pub(crate) fn provide(providers: &mut Providers) {
419432
*providers = Providers {
420433
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
@@ -3295,11 +3295,11 @@ pub(crate) mod dep_tracking {
32953295
use super::{
32963296
AnnotateMoves, AutoDiff, BranchProtection, CFGuard, CFProtection, CollapseMacroDebuginfo,
32973297
CoverageOptions, CrateType, DebugInfo, DebugInfoCompression, ErrorOutputType, FmtDebug,
3298-
FunctionReturn, InliningThreshold, InstrumentCoverage, InstrumentXRay, LinkerPluginLto,
3299-
LocationDetail, LtoCli, MirStripDebugInfo, NextSolverConfig, Offload, OomStrategy,
3300-
OptLevel, OutFileName, OutputType, OutputTypes, PatchableFunctionEntry, Polonius,
3301-
RemapPathScopeComponents, ResolveDocLinks, SourceFileHashAlgorithm, SplitDwarfKind,
3302-
SwitchWithOptPath, SymbolManglingVersion, WasiExecModel,
3298+
FunctionReturn, HardenSls, InliningThreshold, InstrumentCoverage, InstrumentXRay,
3299+
LinkerPluginLto, LocationDetail, LtoCli, MirStripDebugInfo, NextSolverConfig, Offload,
3300+
OomStrategy, OptLevel, OutFileName, OutputType, OutputTypes, PatchableFunctionEntry,
3301+
Polonius, RemapPathScopeComponents, ResolveDocLinks, SourceFileHashAlgorithm,
3302+
SplitDwarfKind, SwitchWithOptPath, SymbolManglingVersion, WasiExecModel,
33033303
};
33043304
use crate::lint;
33053305
use crate::utils::NativeLib;
@@ -3403,6 +3403,7 @@ pub(crate) mod dep_tracking {
34033403
Polonius,
34043404
InliningThreshold,
34053405
FunctionReturn,
3406+
HardenSls,
34063407
Align,
34073408
);
34083409

@@ -3657,6 +3658,16 @@ pub enum FunctionReturn {
36573658
ThunkExtern,
36583659
}
36593660

3661+
/// The different settings that the `-Zharden-sls` flag can have.
3662+
#[derive(Clone, Copy, PartialEq, Hash, Debug, Default)]
3663+
pub enum HardenSls {
3664+
#[default]
3665+
None,
3666+
All,
3667+
Return,
3668+
IndirectJmp,
3669+
}
3670+
36603671
/// Whether extra span comments are included when dumping MIR, via the `-Z mir-include-spans` flag.
36613672
/// By default, only enabled in the NLL MIR dumps, and disabled in all other passes.
36623673
#[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
@@ -876,6 +876,7 @@ mod desc {
876876
"either a boolean (`yes`, `no`, `on`, `off`, etc), or a non-negative number";
877877
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`)";
878878
pub(crate) const parse_function_return: &str = "`keep` or `thunk-extern`";
879+
pub(crate) const parse_harden_sls: &str = "`none`, `all`, `return` or `indirect-jmp`";
879880
pub(crate) const parse_wasm_c_abi: &str = "`spec`";
880881
pub(crate) const parse_mir_include_spans: &str =
881882
"either a boolean (`yes`, `no`, `on`, `off`, etc), or `nll` (default: `nll`)";
@@ -2033,6 +2034,17 @@ pub mod parse {
20332034
true
20342035
}
20352036

2037+
pub(crate) fn parse_harden_sls(slot: &mut HardenSls, v: Option<&str>) -> bool {
2038+
match v {
2039+
Some("none") => *slot = HardenSls::None,
2040+
Some("all") => *slot = HardenSls::All,
2041+
Some("return") => *slot = HardenSls::Return,
2042+
Some("indirect-jmp") => *slot = HardenSls::IndirectJmp,
2043+
_ => return false,
2044+
}
2045+
true
2046+
}
2047+
20362048
pub(crate) fn parse_wasm_c_abi(_slot: &mut (), v: Option<&str>) -> bool {
20372049
v == Some("spec")
20382050
}
@@ -2375,6 +2387,9 @@ options! {
23752387
graphviz_font: String = ("Courier, monospace".to_string(), parse_string, [UNTRACKED],
23762388
"use the given `fontname` in graphviz output; can be overridden by setting \
23772389
environment variable `RUSTC_GRAPHVIZ_FONT` (default: `Courier, monospace`)"),
2390+
harden_sls: HardenSls = (HardenSls::None, parse_harden_sls, [TRACKED TARGET_MODIFIER],
2391+
"flag to mitigate against straight line speculation (SLS) [none|all|return|indirect-jmp] \
2392+
(default: none)"),
23782393
has_thread_local: Option<bool> = (None, parse_opt_bool, [TRACKED],
23792394
"explicitly enable the `cfg(target_thread_local)` directive"),
23802395
higher_ranked_assumptions: 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
@@ -436,6 +436,16 @@ static X86_FEATURES: &[(&str, Stability, ImpliedFeatures)] = &[
436436
("fma", Stable, &["avx"]),
437437
("fxsr", Stable, &[]),
438438
("gfni", Stable, &["sse2"]),
439+
(
440+
"harden-sls-ijmp",
441+
Stability::Forbidden { reason: "use `harden-sls` compiler flag instead" },
442+
&[],
443+
),
444+
(
445+
"harden-sls-ret",
446+
Stability::Forbidden { reason: "use `harden-sls` compiler flag instead" },
447+
&[],
448+
),
439449
("kl", Stable, &["sse2"]),
440450
("lahfsahf", Unstable(sym::lahfsahf_target_feature), &[]),
441451
("lzcnt", Stable, &[]),

src/tools/compiletest/src/directives.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,8 @@ pub(crate) struct TestProps {
197197
pub add_minicore: bool,
198198
/// Add these flags to the build of `minicore`.
199199
pub minicore_compile_flags: Vec<String>,
200+
/// Add these flags to the non-auxiliary build.
201+
pub non_aux_compile_flags: Vec<String>,
200202
/// Whether line annotatins are required for the given error kind.
201203
pub dont_require_annotations: HashSet<ErrorKind>,
202204
/// Whether pretty printers should be disabled in gdb.
@@ -250,6 +252,7 @@ mod directives {
250252
pub const NO_AUTO_CHECK_CFG: &'static str = "no-auto-check-cfg";
251253
pub const ADD_MINICORE: &'static str = "add-minicore";
252254
pub const MINICORE_COMPILE_FLAGS: &'static str = "minicore-compile-flags";
255+
pub const NON_AUX_COMPILE_FLAGS: &'static str = "non-aux-compile-flags";
253256
pub const DISABLE_GDB_PRETTY_PRINTERS: &'static str = "disable-gdb-pretty-printers";
254257
pub const COMPARE_OUTPUT_BY_LINES: &'static str = "compare-output-by-lines";
255258
}
@@ -307,6 +310,7 @@ impl TestProps {
307310
no_auto_check_cfg: false,
308311
add_minicore: false,
309312
minicore_compile_flags: vec![],
313+
non_aux_compile_flags: vec![],
310314
dont_require_annotations: Default::default(),
311315
disable_gdb_pretty_printers: false,
312316
compare_output_by_lines: false,

src/tools/compiletest/src/directives/directive_names.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ pub(crate) const KNOWN_DIRECTIVE_NAMES: &[&str] = &[
187187
"needs-xray",
188188
"no-auto-check-cfg",
189189
"no-prefer-dynamic",
190+
"non-aux-compile-flags",
190191
"normalize-stderr",
191192
"normalize-stderr-32bit",
192193
"normalize-stderr-64bit",

0 commit comments

Comments
 (0)