11use std:: collections:: BTreeMap ;
22use std:: num:: { IntErrorKind , NonZero } ;
33use std:: path:: PathBuf ;
4- use std:: str:: { self , FromStr } ;
4+ use std:: str;
55
66use rustc_abi:: Align ;
77use 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 ;
2223use crate :: config:: * ;
2324use crate :: search_paths:: SearchPath ;
2425use 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
23189mod 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
1031890pub 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
0 commit comments