4 files changed +61
-0
lines changed Original file line number Diff line number Diff line change @@ -17,3 +17,10 @@ pub fn env_var_os(name: &str) -> OsString {
17
17
None => panic ! ( "failed to retrieve environment variable {name:?}" ) ,
18
18
}
19
19
}
20
+
21
+ /// Check if `NO_DEBUG_ASSERTIONS` is set (usually this may be set in CI jobs).
22
+ #[ track_caller]
23
+ #[ must_use]
24
+ pub fn no_debug_assertions ( ) -> bool {
25
+ std:: env:: var_os ( "NO_DEBUG_ASSERTIONS" ) . is_some ( )
26
+ }
Original file line number Diff line number Diff line change @@ -21,6 +21,7 @@ pub mod run;
21
21
pub mod scoped_run;
22
22
pub mod string;
23
23
pub mod targets;
24
+ pub mod symbols;
24
25
25
26
// Internally we call our fs-related support module as `fs`, but re-export its content as `rfs`
26
27
// to tests to avoid colliding with commonly used `use std::fs;`.
Original file line number Diff line number Diff line change
1
+ use object:: { self , Object , ObjectSymbol , SymbolIterator } ;
2
+ use std:: fs;
3
+ use std:: path:: Path ;
4
+
5
+ /// iterate through the symbols in an object file.
6
+ ///
7
+ /// uses a callback because SymbolIterator does not own its data
8
+ pub fn with_symbol_iter < P , F , R > ( path : P , func : F ) -> R
9
+ where
10
+ P : AsRef < Path > ,
11
+ //I: Iterator + 'a,
12
+ F : FnOnce ( & mut SymbolIterator < ' _ , ' _ > ) -> R ,
13
+ {
14
+ let raw_bytes = fs:: read ( path) . expect ( "unable to read file" ) ;
15
+ let f = object:: File :: parse ( raw_bytes. as_slice ( ) ) . expect ( "unable to parse file" ) ;
16
+ let mut iter = f. symbols ( ) ;
17
+ func ( & mut iter)
18
+ }
19
+
20
+ pub fn any_symbol_contains ( path : impl AsRef < Path > , substrings : & [ & str ] ) -> bool {
21
+ with_symbol_iter ( path, |syms| {
22
+ for sym in syms {
23
+ for substring in substrings {
24
+ if sym
25
+ . name_bytes ( )
26
+ . unwrap ( )
27
+ . windows ( substring. len ( ) )
28
+ . any ( |x| x == substring. as_bytes ( ) )
29
+ {
30
+ eprintln ! ( "{:?} contains {}" , sym, substring) ;
31
+ return true ;
32
+ }
33
+ }
34
+ }
35
+ false
36
+ } )
37
+ }
Original file line number Diff line number Diff line change
1
+ //@ ignore-windows
2
+ //@ ignore-cross-compile
3
+
4
+ use run_make_support:: { env:: no_debug_assertions, rustc, symbols:: any_symbol_contains} ;
5
+
6
+ fn main ( ) {
7
+ rustc ( ) . input ( "main.rs" ) . run ( ) ;
8
+ // panic machinery identifiers, these should not appear in the final binary
9
+ let mut panic_syms = vec ! [ "panic_bounds_check" , "Debug" ] ;
10
+ if no_debug_assertions ( ) {
11
+ // if debug assertions are allowed, we need to allow these,
12
+ // otherwise, add them to the list of symbols to deny.
13
+ panic_syms. extend_from_slice ( & [ "panicking" , "panic_fmt" , "pad_integral" , "Display" ] ) ;
14
+ }
15
+ assert ! ( !any_symbol_contains( "main" , & panic_syms) ) ;
16
+ }
0 commit comments