Skip to content

Commit 383ca61

Browse files
author
Ariel Ben-Yehuda
committed
address review comments
1 parent 3463551 commit 383ca61

4 files changed

Lines changed: 162 additions & 148 deletions

File tree

compiler/rustc_metadata/messages.ftl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,8 @@ metadata_missing_native_library =
155155
156156
metadata_mitigation_less_strict_in_dependency =
157157
your program uses the crate `{$extern_crate}`, that is not protected by `{$mitigation_name}{$mitigation_level}`
158-
.note = Recompile that crate with the mitigation enabled, or use `-Z allow-partial-mitigations={$mitigation_name}` to allow creating an artifact that has the mitigation only partially enabled
159-
.help = It is possible to disable `-Z allow-partial-mitigations={$mitigation_name}` via `-Z allow-partial-mitigations=!{$mitigation_name}`
158+
.note = recompile `{$extern_crate}` with the mitigation enabled, or use `-Z allow-partial-mitigations={$mitigation_name}` to allow creating an artifact that has the mitigation only partially enabled
159+
.help = it is possible to disable `-Z allow-partial-mitigations={$mitigation_name}` via `-Z allow-partial-mitigations=!{$mitigation_name}`
160160
161161
metadata_multiple_candidates =
162162
multiple candidates for `{$flavor}` dependency `{$crate_name}` found

compiler/rustc_session/src/config.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ use rustc_target::spec::{
3535
use tracing::debug;
3636

3737
pub use crate::config::cfg::{Cfg, CheckCfg, ExpectedValues};
38+
use crate::config::enforced_mitigations::EnforcedMitigationKind;
3839
use crate::config::native_libs::parse_native_libs;
3940
pub use crate::config::print_request::{PrintKind, PrintRequest};
4041
use crate::errors::FileWriteFail;

compiler/rustc_session/src/options.rs

Lines changed: 6 additions & 146 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::collections::BTreeMap;
22
use std::num::{IntErrorKind, NonZero};
33
use std::path::PathBuf;
4-
use std::str::{self, FromStr};
4+
use std::str;
55

66
use rustc_abi::Align;
77
use rustc_data_structures::fx::FxIndexMap;
@@ -19,6 +19,7 @@ use rustc_target::spec::{
1919
TargetTuple, TlsModel,
2020
};
2121

22+
use crate::config::enforced_mitigations::MitigationEnablement;
2223
use crate::config::*;
2324
use crate::search_paths::SearchPath;
2425
use crate::utils::NativeLib;
@@ -83,150 +84,7 @@ pub struct TargetModifier {
8384
pub value_name: String,
8485
}
8586

86-
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Encodable, Decodable)]
87-
pub enum EnforcedMitigationLevel {
88-
// Enabled(false) should be the bottom of the Ord hierarchy
89-
Enabled(bool),
90-
StackProtector(StackProtector),
91-
}
92-
93-
impl EnforcedMitigationLevel {
94-
pub fn level_str(&self) -> &'static str {
95-
match self {
96-
EnforcedMitigationLevel::StackProtector(StackProtector::All) => "=all",
97-
EnforcedMitigationLevel::StackProtector(StackProtector::Basic) => "=basic",
98-
EnforcedMitigationLevel::StackProtector(StackProtector::Strong) => "=strong",
99-
// currently `=disabled` should not appear
100-
EnforcedMitigationLevel::Enabled(false) => "=disabled",
101-
EnforcedMitigationLevel::StackProtector(StackProtector::None)
102-
| EnforcedMitigationLevel::Enabled(true) => "",
103-
}
104-
}
105-
}
106-
107-
impl std::fmt::Display for EnforcedMitigationLevel {
108-
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
109-
match self {
110-
EnforcedMitigationLevel::StackProtector(StackProtector::All) => {
111-
write!(f, "all")
112-
}
113-
EnforcedMitigationLevel::StackProtector(StackProtector::Basic) => {
114-
write!(f, "basic")
115-
}
116-
EnforcedMitigationLevel::StackProtector(StackProtector::Strong) => {
117-
write!(f, "strong")
118-
}
119-
EnforcedMitigationLevel::Enabled(true) => {
120-
write!(f, "enabled")
121-
}
122-
EnforcedMitigationLevel::StackProtector(StackProtector::None)
123-
| EnforcedMitigationLevel::Enabled(false) => {
124-
write!(f, "disabled")
125-
}
126-
}
127-
}
128-
}
129-
130-
impl From<bool> for EnforcedMitigationLevel {
131-
fn from(value: bool) -> Self {
132-
EnforcedMitigationLevel::Enabled(value)
133-
}
134-
}
135-
136-
impl From<StackProtector> for EnforcedMitigationLevel {
137-
fn from(value: StackProtector) -> Self {
138-
EnforcedMitigationLevel::StackProtector(value)
139-
}
140-
}
141-
142-
pub struct EnforcedMitigationKindParseError;
143-
144-
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, Encodable, Decodable)]
145-
pub struct MitigationEnablement {
146-
pub kind: EnforcedMitigationKind,
147-
pub enabled: bool,
148-
}
149-
150-
macro_rules! intersperse {
151-
($sep:expr, ($first:expr $(, $rest:expr)* $(,)?)) => {
152-
concat!($first $(, $sep, $rest)*)
153-
};
154-
}
155-
156-
macro_rules! enforced_mitigations {
157-
([$self:ident] enum $kind:ident {$(($name:ident, $text:expr, $since:ident, $code:expr)),*}) => {
158-
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, Encodable, Decodable)]
159-
pub enum EnforcedMitigationKind {
160-
$($name),*
161-
}
162-
163-
impl std::fmt::Display for EnforcedMitigationKind {
164-
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
165-
match self {
166-
$(EnforcedMitigationKind::$name => write!(f, $text)),*
167-
}
168-
}
169-
}
170-
171-
impl EnforcedMitigationKind {
172-
const KINDS: &'static str = concat!("comma-separated list of mitigation kinds (available: ",
173-
intersperse!(", ", ($(concat!("`", $text, "`")),*)), ")");
174-
}
175-
176-
impl FromStr for EnforcedMitigationKind {
177-
type Err = EnforcedMitigationKindParseError;
178-
179-
fn from_str(v: &str) -> Result<EnforcedMitigationKind, EnforcedMitigationKindParseError> {
180-
match v {
181-
$($text => Ok(EnforcedMitigationKind::$name)),*
182-
,
183-
_ => Err(EnforcedMitigationKindParseError),
184-
}
185-
}
186-
}
187-
188-
#[allow(unused)]
189-
impl EnforcedMitigationKind {
190-
pub fn enforced_since(&self) -> Edition {
191-
match self {
192-
// Should change the enforced-since edition of StackProtector to 2015
193-
// (all editions) when `-C stack-protector` is stabilized.
194-
$(EnforcedMitigationKind::$name => Edition::$since),*
195-
}
196-
}
197-
}
198-
199-
impl Session {
200-
pub fn gather_enabled_enforced_mitigations(&$self) -> Vec<EnforcedMitigation> {
201-
let mut mitigations = [
202-
$(
203-
EnforcedMitigation {
204-
kind: EnforcedMitigationKind::$name,
205-
level: From::from($code),
206-
}
207-
),*
208-
];
209-
mitigations.sort();
210-
mitigations.into_iter().collect()
211-
}
212-
}
213-
}
214-
}
215-
216-
enforced_mitigations! {
217-
[self]
218-
enum EnforcedMitigationKind {
219-
(StackProtector, "stack-protector", EditionFuture, self.stack_protector()),
220-
(ControlFlowGuard, "control-flow-guard", EditionFuture, self.opts.cg.control_flow_guard == CFGuard::Checks)
221-
}
222-
}
223-
224-
/// Enforced mitigations, see [RFC 3855](https://github.com/rust-lang/rfcs/pull/3855)
225-
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Encodable, Decodable)]
226-
pub struct EnforcedMitigation {
227-
pub kind: EnforcedMitigationKind,
228-
pub level: EnforcedMitigationLevel,
229-
}
87+
pub mod enforced_mitigations;
23088

23189
mod target_modifier_consistency_check {
23290
use super::*;
@@ -1025,13 +883,15 @@ mod desc {
1025883
pub(crate) const parse_mir_include_spans: &str =
1026884
"either a boolean (`yes`, `no`, `on`, `off`, etc), or `nll` (default: `nll`)";
1027885
pub(crate) const parse_align: &str = "a number that is a power of 2 between 1 and 2^29";
1028-
pub(crate) const parse_allow_partial_mitigations: &str = super::EnforcedMitigationKind::KINDS;
886+
pub(crate) const parse_allow_partial_mitigations: &str =
887+
super::enforced_mitigations::EnforcedMitigationKind::KINDS;
1029888
}
1030889

1031890
pub mod parse {
1032891
use std::str::FromStr;
1033892

1034893
pub(crate) use super::*;
894+
use crate::config::enforced_mitigations::MitigationEnablement;
1035895
pub(crate) const MAX_THREADS_CAP: usize = 256;
1036896

1037897
/// This is for boolean options that don't take a value, and are true simply
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
use std::str::FromStr;
2+
3+
use rustc_macros::{Decodable, Encodable};
4+
use rustc_span::edition::Edition;
5+
use rustc_target::spec::StackProtector;
6+
7+
use crate::Session;
8+
use crate::options::CFGuard;
9+
10+
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Encodable, Decodable)]
11+
pub enum EnforcedMitigationLevel {
12+
// Enabled(false) should be the bottom of the Ord hierarchy
13+
Enabled(bool),
14+
StackProtector(StackProtector),
15+
}
16+
17+
impl EnforcedMitigationLevel {
18+
pub fn level_str(&self) -> &'static str {
19+
match self {
20+
EnforcedMitigationLevel::StackProtector(StackProtector::All) => "=all",
21+
EnforcedMitigationLevel::StackProtector(StackProtector::Basic) => "=basic",
22+
EnforcedMitigationLevel::StackProtector(StackProtector::Strong) => "=strong",
23+
// currently `=disabled` should not appear
24+
EnforcedMitigationLevel::Enabled(false) => "=disabled",
25+
EnforcedMitigationLevel::StackProtector(StackProtector::None)
26+
| EnforcedMitigationLevel::Enabled(true) => "",
27+
}
28+
}
29+
}
30+
31+
impl std::fmt::Display for EnforcedMitigationLevel {
32+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
33+
match self {
34+
EnforcedMitigationLevel::StackProtector(StackProtector::All) => {
35+
write!(f, "all")
36+
}
37+
EnforcedMitigationLevel::StackProtector(StackProtector::Basic) => {
38+
write!(f, "basic")
39+
}
40+
EnforcedMitigationLevel::StackProtector(StackProtector::Strong) => {
41+
write!(f, "strong")
42+
}
43+
EnforcedMitigationLevel::Enabled(true) => {
44+
write!(f, "enabled")
45+
}
46+
EnforcedMitigationLevel::StackProtector(StackProtector::None)
47+
| EnforcedMitigationLevel::Enabled(false) => {
48+
write!(f, "disabled")
49+
}
50+
}
51+
}
52+
}
53+
54+
impl From<bool> for EnforcedMitigationLevel {
55+
fn from(value: bool) -> Self {
56+
EnforcedMitigationLevel::Enabled(value)
57+
}
58+
}
59+
60+
impl From<StackProtector> for EnforcedMitigationLevel {
61+
fn from(value: StackProtector) -> Self {
62+
EnforcedMitigationLevel::StackProtector(value)
63+
}
64+
}
65+
66+
pub struct EnforcedMitigationKindParseError;
67+
68+
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, Encodable, Decodable)]
69+
pub struct MitigationEnablement {
70+
pub kind: EnforcedMitigationKind,
71+
pub enabled: bool,
72+
}
73+
74+
macro_rules! intersperse {
75+
($sep:expr, ($first:expr $(, $rest:expr)* $(,)?)) => {
76+
concat!($first $(, $sep, $rest)*)
77+
};
78+
}
79+
80+
macro_rules! enforced_mitigations {
81+
([$self:ident] enum $kind:ident {$(($name:ident, $text:expr, $since:ident, $code:expr)),*}) => {
82+
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, Encodable, Decodable)]
83+
pub enum EnforcedMitigationKind {
84+
$($name),*
85+
}
86+
87+
impl std::fmt::Display for EnforcedMitigationKind {
88+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
89+
match self {
90+
$(EnforcedMitigationKind::$name => write!(f, $text)),*
91+
}
92+
}
93+
}
94+
95+
impl EnforcedMitigationKind {
96+
pub(crate) const KINDS: &'static str = concat!("comma-separated list of mitigation kinds (available: ",
97+
intersperse!(", ", ($(concat!("`", $text, "`")),*)), ")");
98+
}
99+
100+
impl FromStr for EnforcedMitigationKind {
101+
type Err = EnforcedMitigationKindParseError;
102+
103+
fn from_str(v: &str) -> Result<EnforcedMitigationKind, EnforcedMitigationKindParseError> {
104+
match v {
105+
$($text => Ok(EnforcedMitigationKind::$name)),*
106+
,
107+
_ => Err(EnforcedMitigationKindParseError),
108+
}
109+
}
110+
}
111+
112+
#[allow(unused)]
113+
impl EnforcedMitigationKind {
114+
pub fn enforced_since(&self) -> Edition {
115+
match self {
116+
// Should change the enforced-since edition of StackProtector to 2015
117+
// (all editions) when `-C stack-protector` is stabilized.
118+
$(EnforcedMitigationKind::$name => Edition::$since),*
119+
}
120+
}
121+
}
122+
123+
impl Session {
124+
pub fn gather_enabled_enforced_mitigations(&$self) -> Vec<EnforcedMitigation> {
125+
let mut mitigations = [
126+
$(
127+
EnforcedMitigation {
128+
kind: EnforcedMitigationKind::$name,
129+
level: From::from($code),
130+
}
131+
),*
132+
];
133+
mitigations.sort();
134+
mitigations.into_iter().collect()
135+
}
136+
}
137+
}
138+
}
139+
140+
enforced_mitigations! {
141+
[self]
142+
enum EnforcedMitigationKind {
143+
(StackProtector, "stack-protector", EditionFuture, self.stack_protector()),
144+
(ControlFlowGuard, "control-flow-guard", EditionFuture, self.opts.cg.control_flow_guard == CFGuard::Checks)
145+
}
146+
}
147+
148+
/// Enforced mitigations, see [RFC 3855](https://github.com/rust-lang/rfcs/pull/3855)
149+
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Encodable, Decodable)]
150+
pub struct EnforcedMitigation {
151+
pub kind: EnforcedMitigationKind,
152+
pub level: EnforcedMitigationLevel,
153+
}

0 commit comments

Comments
 (0)