|
15 | 15 | //! `NO_DEBUG_ASSERTIONS=1`). If debug assertions are disabled, then we can check for the absence of
|
16 | 16 | //! additional `usize` formatting and padding related symbols.
|
17 | 17 |
|
| 18 | +// ignore-tidy-linelength |
| 19 | + |
18 | 20 | //@ ignore-cross-compile
|
19 | 21 |
|
20 |
| -use run_make_support::artifact_names::bin_name; |
21 | 22 | use run_make_support::env::no_debug_assertions;
|
22 |
| -use run_make_support::rustc; |
23 | 23 | use run_make_support::symbols::any_symbol_contains;
|
| 24 | +use run_make_support::{bin_name, is_darwin, is_windows_msvc, pdb, rustc, target}; |
| 25 | + |
| 26 | +// Not applicable for `extern "C"` symbol decoration handling. |
| 27 | +fn sym(sym_name: &str) -> String { |
| 28 | + if is_darwin() { |
| 29 | + // Symbols are decorated with an underscore prefix on darwin platforms. |
| 30 | + format!("_{sym_name}") |
| 31 | + } else { |
| 32 | + sym_name.to_string() |
| 33 | + } |
| 34 | +} |
24 | 35 |
|
25 | 36 | fn main() {
|
26 | 37 | rustc().input("main.rs").opt().run();
|
27 | 38 | // panic machinery identifiers, these should not appear in the final binary
|
28 |
| - let mut panic_syms = vec!["panic_bounds_check", "Debug"]; |
| 39 | + let mut panic_syms = vec![sym("panic_bounds_check"), sym("Debug")]; |
29 | 40 | if no_debug_assertions() {
|
30 | 41 | // if debug assertions are allowed, we need to allow these,
|
31 | 42 | // otherwise, add them to the list of symbols to deny.
|
32 |
| - panic_syms.extend_from_slice(&["panicking", "panic_fmt", "pad_integral", "Display"]); |
| 43 | + panic_syms.extend_from_slice(&[ |
| 44 | + sym("panicking"), |
| 45 | + sym("panic_fmt"), |
| 46 | + sym("pad_integral"), |
| 47 | + sym("Display"), |
| 48 | + ]); |
| 49 | + } |
| 50 | + |
| 51 | + if is_windows_msvc() { |
| 52 | + use pdb::FallibleIterator; |
| 53 | + |
| 54 | + let file = std::fs::File::open("main.pdb").expect("failed to open `main.pdb`"); |
| 55 | + let mut pdb = pdb::PDB::open(file).expect("failed to parse `main.pdb`"); |
| 56 | + |
| 57 | + let symbol_table = pdb.global_symbols().expect("failed to parse PDB global symbols"); |
| 58 | + let mut symbols = symbol_table.iter(); |
| 59 | + |
| 60 | + let mut found_symbols = vec![]; |
| 61 | + |
| 62 | + while let Some(symbol) = symbols.next().expect("failed to parse symbol") { |
| 63 | + match symbol.parse() { |
| 64 | + Ok(pdb::SymbolData::Public(data)) => { |
| 65 | + found_symbols.push(data.name.to_string()); |
| 66 | + } |
| 67 | + _ => {} |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + // Make sure we at least have the `main` symbol itself, otherwise even no symbols can |
| 72 | + // trivially satisfy the "no panic symbol" assertion. |
| 73 | + let main_sym = if is_darwin() { |
| 74 | + // Symbols are decorated with an underscore prefix on darwin platforms. |
| 75 | + "_main" |
| 76 | + } else if target().contains("i686") && is_windows_msvc() { |
| 77 | + // `extern "C"` i.e. `__cdecl` on `i686` windows-msvc means that the symbol will be |
| 78 | + // decorated with an underscore, but not on `x86_64` windows-msvc. |
| 79 | + // See <https://learn.microsoft.com/en-us/cpp/build/reference/decorated-names?view=msvc-170#FormatC>. |
| 80 | + "_main" |
| 81 | + } else { |
| 82 | + "main" |
| 83 | + }; |
| 84 | + |
| 85 | + assert!(found_symbols.iter().any(|sym| sym == main_sym), "expected `main` symbol"); |
| 86 | + |
| 87 | + for found_symbol in found_symbols { |
| 88 | + for panic_symbol in &panic_syms { |
| 89 | + assert_ne!( |
| 90 | + found_symbol, |
| 91 | + panic_symbol.as_str(), |
| 92 | + "found unexpected panic machinery symbol" |
| 93 | + ); |
| 94 | + } |
| 95 | + } |
| 96 | + } else { |
| 97 | + let panic_syms = panic_syms.iter().map(String::as_str).collect::<Vec<_>>(); |
| 98 | + // Make sure we at least have the `main` symbol itself, otherwise even no symbols can |
| 99 | + // trivially satisfy the "no panic symbol" assertion. |
| 100 | + assert!(any_symbol_contains(bin_name("main"), &["main"])); |
| 101 | + assert!(!any_symbol_contains(bin_name("main"), &panic_syms)); |
33 | 102 | }
|
34 |
| - assert!(!any_symbol_contains(bin_name("main"), &panic_syms)); |
35 | 103 | }
|
0 commit comments