Skip to content

Commit 99b405a

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

18 files changed

Lines changed: 237 additions & 10 deletions

File tree

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
@@ -643,6 +643,7 @@ fn llvm_features_by_flags(sess: &Session, features: &mut Vec<String>) {
643643
}
644644

645645
target_features::retpoline_features_by_flags(sess, features);
646+
target_features::sls_features_by_flags(sess, features);
646647

647648
// -Zfixed-x18
648649
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, 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};
@@ -455,6 +456,18 @@ pub fn retpoline_features_by_flags(sess: &Session, features: &mut Vec<String>) {
455456
}
456457
}
457458

459+
pub fn sls_features_by_flags(sess: &Session, features: &mut Vec<String>) {
460+
match &sess.opts.unstable_opts.harden_sls {
461+
HardenSls::None => (),
462+
HardenSls::All => {
463+
features.push("+harden-sls-ijmp".into());
464+
features.push("+harden-sls-ret".into());
465+
}
466+
HardenSls::Return => features.push("+harden-sls-ret".into()),
467+
HardenSls::IndirectJmp => features.push("+harden-sls-ijmp".into()),
468+
}
469+
}
470+
458471
pub(crate) fn provide(providers: &mut Providers) {
459472
*providers = Providers {
460473
rust_target_features: |tcx, cnum| {

compiler/rustc_session/src/config.rs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3074,10 +3074,11 @@ pub(crate) mod dep_tracking {
30743074
use super::{
30753075
AnnotateMoves, AutoDiff, BranchProtection, CFGuard, CFProtection, CoverageOptions,
30763076
CrateType, DebugInfo, DebugInfoCompression, ErrorOutputType, FmtDebug, FunctionReturn,
3077-
InliningThreshold, InstrumentCoverage, InstrumentXRay, LinkerPluginLto, LocationDetail,
3078-
LtoCli, MirStripDebugInfo, NextSolverConfig, Offload, OptLevel, OutFileName, OutputType,
3079-
OutputTypes, PatchableFunctionEntry, Polonius, ResolveDocLinks, SourceFileHashAlgorithm,
3080-
SplitDwarfKind, SwitchWithOptPath, SymbolManglingVersion, WasiExecModel,
3077+
HardenSls, InliningThreshold, InstrumentCoverage, InstrumentXRay, LinkerPluginLto,
3078+
LocationDetail, LtoCli, MirStripDebugInfo, NextSolverConfig, Offload, OptLevel,
3079+
OutFileName, OutputType, OutputTypes, PatchableFunctionEntry, Polonius, ResolveDocLinks,
3080+
SourceFileHashAlgorithm, SplitDwarfKind, SwitchWithOptPath, SymbolManglingVersion,
3081+
WasiExecModel,
30813082
};
30823083
use crate::lint;
30833084
use crate::utils::NativeLib;
@@ -3180,6 +3181,7 @@ pub(crate) mod dep_tracking {
31803181
Polonius,
31813182
InliningThreshold,
31823183
FunctionReturn,
3184+
HardenSls,
31833185
Align,
31843186
);
31853187

@@ -3394,6 +3396,16 @@ pub enum FunctionReturn {
33943396
ThunkExtern,
33953397
}
33963398

3399+
/// The different settings that the `-Zharden-sls` flag can have.
3400+
#[derive(Clone, Copy, PartialEq, Hash, Debug, Default)]
3401+
pub enum HardenSls {
3402+
#[default]
3403+
None,
3404+
All,
3405+
Return,
3406+
IndirectJmp,
3407+
}
3408+
33973409
/// Whether extra span comments are included when dumping MIR, via the `-Z mir-include-spans` flag.
33983410
/// By default, only enabled in the NLL MIR dumps, and disabled in all other passes.
33993411
#[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
@@ -878,6 +878,7 @@ mod desc {
878878
"either a boolean (`yes`, `no`, `on`, `off`, etc), or a non-negative number";
879879
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`)";
880880
pub(crate) const parse_function_return: &str = "`keep` or `thunk-extern`";
881+
pub(crate) const parse_harden_sls: &str = "`none`, `all`, `return` or `indirect-jmp`";
881882
pub(crate) const parse_wasm_c_abi: &str = "`spec`";
882883
pub(crate) const parse_mir_include_spans: &str =
883884
"either a boolean (`yes`, `no`, `on`, `off`, etc), or `nll` (default: `nll`)";
@@ -2029,6 +2030,17 @@ pub mod parse {
20292030
true
20302031
}
20312032

2033+
pub(crate) fn parse_harden_sls(slot: &mut HardenSls, v: Option<&str>) -> bool {
2034+
match v {
2035+
Some("none") => *slot = HardenSls::None,
2036+
Some("all") => *slot = HardenSls::All,
2037+
Some("return") => *slot = HardenSls::Return,
2038+
Some("indirect-jmp") => *slot = HardenSls::IndirectJmp,
2039+
_ => return false,
2040+
}
2041+
true
2042+
}
2043+
20322044
pub(crate) fn parse_wasm_c_abi(_slot: &mut (), v: Option<&str>) -> bool {
20332045
v == Some("spec")
20342046
}
@@ -2374,6 +2386,9 @@ options! {
23742386
graphviz_font: String = ("Courier, monospace".to_string(), parse_string, [UNTRACKED],
23752387
"use the given `fontname` in graphviz output; can be overridden by setting \
23762388
environment variable `RUSTC_GRAPHVIZ_FONT` (default: `Courier, monospace`)"),
2389+
harden_sls: HardenSls = (HardenSls::None, parse_harden_sls, [TRACKED TARGET_MODIFIER],
2390+
"flag to mitigate against straight line speculation (SLS) [none|all|return|indirect-jmp] \
2391+
(default: none)"),
23772392
has_thread_local: Option<bool> = (None, parse_opt_bool, [TRACKED],
23782393
"explicitly enable the `cfg(target_thread_local)` directive"),
23792394
help: bool = (false, parse_no_value, [UNTRACKED], "Print unstable compiler options"),

compiler/rustc_target/src/target_features.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,16 @@ static X86_FEATURES: &[(&str, Stability, ImpliedFeatures)] = &[
453453
("fma", Stable, &["avx"]),
454454
("fxsr", Stable, &[]),
455455
("gfni", Stable, &["sse2"]),
456+
(
457+
"harden-sls-ijmp",
458+
Stability::Forbidden { reason: "use `harden-sls` compiler flag instead", hard_error: true },
459+
&[],
460+
),
461+
(
462+
"harden-sls-ret",
463+
Stability::Forbidden { reason: "use `harden-sls` compiler flag instead", hard_error: true },
464+
&[],
465+
),
456466
("kl", Stable, &["sse2"]),
457467
("lahfsahf", Unstable(sym::lahfsahf_target_feature), &[]),
458468
("lzcnt", Stable, &[]),

src/doc/rustc-dev-guide/src/tests/directives.md

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -241,12 +241,14 @@ See also [Debuginfo tests](compiletest.md#debuginfo-tests) for directives for ig
241241

242242
| Directive | Explanation | Supported test suites | Possible values |
243243
|---------------------|----------------------------------------------------------------------------------------------|--------------------------------------------|--------------------------------------------------------------------------------------------|
244-
| `compile-flags` | Flags passed to `rustc` when building the test or aux file | All except for `run-make`/`run-make-cargo` | Any valid `rustc` flags, e.g. `-Awarnings -Dfoo`. Cannot be `-Cincremental` or `--edition` |
245-
| `edition` | The edition used to build the test | All except for `run-make`/`run-make-cargo` | Any valid `--edition` value |
246-
| `rustc-env` | Env var to set when running `rustc` | All except for `run-make`/`run-make-cargo` | `<KEY>=<VALUE>` |
247-
| `unset-rustc-env` | Env var to unset when running `rustc` | All except for `run-make`/`run-make-cargo` | Any env var name |
248-
| `incremental` | Proper incremental support for tests outside of incremental test suite | `ui`, `crashes` | N/A |
249-
| `no-prefer-dynamic` | Don't use `-C prefer-dynamic`, don't build as a dylib via a `--crate-type=dylib` preset flag | `ui`, `crashes` | N/A |
244+
| `compile-flags` | Flags passed to `rustc` when building the test or aux file | All except for `run-make`/`run-make-cargo` | Any valid `rustc` flags, e.g. `-Awarnings -Dfoo`. Cannot be `-Cincremental` or `--edition` |
245+
| `minicore-compile-flags` | Additional flags passed to `rustc` when building minicore | All except for `run-make`/`run-make-cargo` | Any valid `rustc` flags, e.g. `-Awarnings -Dfoo`. Cannot be `-Cincremental` or `--edition` |
246+
| `non-aux-compile-flags` | Additional flags passed to `rustc` when building the test (not for auxiliary builds) | All except for `run-make`/`run-make-cargo` | Any valid `rustc` flags, e.g. `-Awarnings -Dfoo`. Cannot be `-Cincremental` or `--edition` |
247+
| `edition` | The edition used to build the test | All except for `run-make`/`run-make-cargo` | Any valid `--edition` value |
248+
| `rustc-env` | Env var to set when running `rustc` | All except for `run-make`/`run-make-cargo` | `<KEY>=<VALUE>` |
249+
| `unset-rustc-env` | Env var to unset when running `rustc` | All except for `run-make`/`run-make-cargo` | Any env var name |
250+
| `incremental` | Proper incremental support for tests outside of incremental test suite | `ui`, `crashes` | N/A |
251+
| `no-prefer-dynamic` | Don't use `-C prefer-dynamic`, don't build as a dylib via a `--crate-type=dylib` preset flag | `ui`, `crashes` | N/A |
250252

251253
<div class="warning">
252254

src/doc/rustc-dev-guide/src/tests/minicore.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ The `minicore` items must be kept up to date with `core`.
4444
For consistent diagnostic output between using `core` and `minicore`, any `diagnostic`
4545
attributes (e.g. `on_unimplemented`) should be replicated exactly in `minicore`.
4646

47+
## Specific compile flags
48+
`compile-flags` is used both for auxiliary builds (including minicore) and main test build.
49+
`minicore-compile-flags` directive may be used to provide compile flags for minicore build only.
50+
`non-aux-compile-flags` directive may be used to provide compile flags for main test only.
51+
4752
## Example codegen test that uses `minicore`
4853

4954
```rust,no_run

src/tools/compiletest/src/directives.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,8 @@ pub(crate) struct TestProps {
206206
pub add_minicore: bool,
207207
/// Add these flags to the build of `minicore`.
208208
pub minicore_compile_flags: Vec<String>,
209+
/// Add these flags to the non-auxiliary build.
210+
pub non_aux_compile_flags: Vec<String>,
209211
/// Whether line annotations are required for the given error kind.
210212
pub dont_require_annotations: HashSet<ErrorKind>,
211213
/// Whether pretty printers should be disabled in gdb.
@@ -259,6 +261,7 @@ mod directives {
259261
pub const NO_AUTO_CHECK_CFG: &'static str = "no-auto-check-cfg";
260262
pub const ADD_MINICORE: &'static str = "add-minicore";
261263
pub const MINICORE_COMPILE_FLAGS: &'static str = "minicore-compile-flags";
264+
pub const NON_AUX_COMPILE_FLAGS: &'static str = "non-aux-compile-flags";
262265
pub const DISABLE_GDB_PRETTY_PRINTERS: &'static str = "disable-gdb-pretty-printers";
263266
pub const COMPARE_OUTPUT_BY_LINES: &'static str = "compare-output-by-lines";
264267
}
@@ -316,6 +319,7 @@ impl TestProps {
316319
no_auto_check_cfg: false,
317320
add_minicore: false,
318321
minicore_compile_flags: vec![],
322+
non_aux_compile_flags: vec![],
319323
dont_require_annotations: Default::default(),
320324
disable_gdb_pretty_printers: false,
321325
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
@@ -196,6 +196,7 @@ pub(crate) const KNOWN_DIRECTIVE_NAMES: &[&str] = &[
196196
"needs-xray",
197197
"no-auto-check-cfg",
198198
"no-prefer-dynamic",
199+
"non-aux-compile-flags",
199200
"normalize-stderr",
200201
"normalize-stderr-32bit",
201202
"normalize-stderr-64bit",

0 commit comments

Comments
 (0)