Skip to content

Commit c7aa404

Browse files
authored
Rollup merge of rust-lang#128147 - lolbinarycat:fmt-write-bloat-rmake, r=jieyouxu
migrate fmt-write-bloat to rmake try-job: aarch64-apple try-job: x86_64-gnu-llvm-18 try-job: dist-x86_64-linux
2 parents a886938 + ebd6718 commit c7aa404

File tree

6 files changed

+89
-26
lines changed

6 files changed

+89
-26
lines changed

src/tools/run-make-support/src/env.rs

+7
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,10 @@ pub fn env_var_os(name: &str) -> OsString {
1717
None => panic!("failed to retrieve environment variable {name:?}"),
1818
}
1919
}
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+
}

src/tools/run-make-support/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ pub mod run;
2121
pub mod scoped_run;
2222
pub mod string;
2323
pub mod targets;
24+
pub mod symbols;
2425

2526
// Internally we call our fs-related support module as `fs`, but re-export its content as `rfs`
2627
// to tests to avoid colliding with commonly used `use std::fs;`.
+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
use std::path::Path;
2+
3+
use object::{self, Object, ObjectSymbol, SymbolIterator};
4+
5+
/// Iterate through the symbols in an object file.
6+
///
7+
/// Uses a callback because `SymbolIterator` does not own its data.
8+
///
9+
/// Panics if `path` is not a valid object file readable by the current user.
10+
pub fn with_symbol_iter<P, F, R>(path: P, func: F) -> R
11+
where
12+
P: AsRef<Path>,
13+
F: FnOnce(&mut SymbolIterator<'_, '_>) -> R,
14+
{
15+
let raw_bytes = crate::fs::read(path);
16+
let f = object::File::parse(raw_bytes.as_slice()).expect("unable to parse file");
17+
let mut iter = f.symbols();
18+
func(&mut iter)
19+
}
20+
21+
/// Check an object file's symbols for substrings.
22+
///
23+
/// Returns `true` if any of the symbols found in the object file at
24+
/// `path` contain a substring listed in `substrings`.
25+
///
26+
/// Panics if `path` is not a valid object file readable by the current user.
27+
pub fn any_symbol_contains(path: impl AsRef<Path>, substrings: &[&str]) -> bool {
28+
with_symbol_iter(path, |syms| {
29+
for sym in syms {
30+
for substring in substrings {
31+
if sym
32+
.name_bytes()
33+
.unwrap()
34+
.windows(substring.len())
35+
.any(|x| x == substring.as_bytes())
36+
{
37+
eprintln!("{:?} contains {}", sym, substring);
38+
return true;
39+
}
40+
}
41+
}
42+
false
43+
})
44+
}

src/tools/tidy/src/allowed_run_make_makefiles.txt

-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ run-make/dep-info-spaces/Makefile
1010
run-make/dep-info/Makefile
1111
run-make/emit-to-stdout/Makefile
1212
run-make/extern-fn-reachable/Makefile
13-
run-make/fmt-write-bloat/Makefile
1413
run-make/foreign-double-unwind/Makefile
1514
run-make/foreign-exceptions/Makefile
1615
run-make/incr-add-rust-src-component/Makefile

tests/run-make/fmt-write-bloat/Makefile

-25
This file was deleted.
+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
//! Before #78122, writing any `fmt::Arguments` would trigger the inclusion of `usize` formatting
2+
//! and padding code in the resulting binary, because indexing used in `fmt::write` would generate
3+
//! code using `panic_bounds_check`, which prints the index and length.
4+
//!
5+
//! These bounds checks are not necessary, as `fmt::Arguments` never contains any out-of-bounds
6+
//! indexes. The test is a `run-make` test, because it needs to check the result after linking. A
7+
//! codegen or assembly test doesn't check the parts that will be pulled in from `core` by the
8+
//! linker.
9+
//!
10+
//! In this test, we try to check that the `usize` formatting and padding code are not present in
11+
//! the final binary by checking that panic symbols such as `panic_bounds_check` are **not**
12+
//! present.
13+
//!
14+
//! Some CI jobs try to run faster by disabling debug assertions (through setting
15+
//! `NO_DEBUG_ASSERTIONS=1`). If debug assertions are disabled, then we can check for the absence of
16+
//! additional `usize` formatting and padding related symbols.
17+
18+
// Reason: This test is `ignore-windows` because the `no_std` test (using `#[link(name = "c")])`
19+
// doesn't link on windows.
20+
//@ ignore-windows
21+
//@ ignore-cross-compile
22+
23+
use run_make_support::env::no_debug_assertions;
24+
use run_make_support::rustc;
25+
use run_make_support::symbols::any_symbol_contains;
26+
27+
fn main() {
28+
rustc().input("main.rs").opt().run();
29+
// panic machinery identifiers, these should not appear in the final binary
30+
let mut panic_syms = vec!["panic_bounds_check", "Debug"];
31+
if no_debug_assertions() {
32+
// if debug assertions are allowed, we need to allow these,
33+
// otherwise, add them to the list of symbols to deny.
34+
panic_syms.extend_from_slice(&["panicking", "panic_fmt", "pad_integral", "Display"]);
35+
}
36+
assert!(!any_symbol_contains("main", &panic_syms));
37+
}

0 commit comments

Comments
 (0)