Skip to content

Commit 24830de

Browse files
committed
Make sure fmt-write-bloat doesn't vacuously pass
Previously, the test only checks for the absence of certain panic symbols, but having no symbols was also a possible albeit vacuous way to satisfy this assertion. Fix this by checking we at least observe the `main` symbol which is always expected to be present.
1 parent 3c479ca commit 24830de

File tree

1 file changed

+73
-5
lines changed

1 file changed

+73
-5
lines changed

tests/run-make/fmt-write-bloat/rmake.rs

Lines changed: 73 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,89 @@
1515
//! `NO_DEBUG_ASSERTIONS=1`). If debug assertions are disabled, then we can check for the absence of
1616
//! additional `usize` formatting and padding related symbols.
1717
18+
// ignore-tidy-linelength
19+
1820
//@ ignore-cross-compile
1921

20-
use run_make_support::artifact_names::bin_name;
2122
use run_make_support::env::no_debug_assertions;
22-
use run_make_support::rustc;
2323
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+
}
2435

2536
fn main() {
2637
rustc().input("main.rs").opt().run();
2738
// 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")];
2940
if no_debug_assertions() {
3041
// if debug assertions are allowed, we need to allow these,
3142
// 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));
33102
}
34-
assert!(!any_symbol_contains(bin_name("main"), &panic_syms));
35103
}

0 commit comments

Comments
 (0)