Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 06b5854

Browse files
committedJan 20, 2025
Auto merge of rust-lang#135768 - jieyouxu:migrate-symbol-mangling-hashed, r=<try>
tests: Port `symbol-mangling-hashed` to rmake.rs Part of rust-lang#121876. This PR supersedes rust-lang#128567 and is co-authored with `@lolbinarycat.` ### Summary This PR ports `tests/run-make/symbol-mangling-hashed` to rmake.rs. Notable differences when compared to the Makefile version includes: - It's no longer limited to linux + x86_64 only. In particular, this now is exercised on darwin and windows (esp. msvc) too. - The test uses `object` crate to be more precise in the filtering, and avoids relying on parsing the human-readable `nm` output for *some* `nm` in the given environment (which isn't really a thing on msvc anyway, and `llvm-nm` doesn't handle msvc dylibs AFAICT). - Dump the symbols satisfying various criteria on test failure to make it hopefully less of a pain to debug if it ever fails in CI. ### Review advice - Best reviewed commit-by-commit. - I'm not *super* sure about the msvc logic, would benefit from a MSVC (PE/COFF) expert taking a look. --- try-job: x86_64-msvc try-job: i686-msvc-1 try-job: i686-mingw try-job: x86_64-mingw-1 try-job: x86_64-apple-1 try-job: aarch64-apple try-job: test-various
2 parents b5741a3 + 897a145 commit 06b5854

File tree

7 files changed

+161
-57
lines changed

7 files changed

+161
-57
lines changed
 

‎src/tools/run-make-support/src/external_deps/rustc.rs

+12
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,18 @@ impl Rustc {
215215
self
216216
}
217217

218+
/// Specify option of `-C symbol-mangling-version`.
219+
pub fn symbol_mangling_version(&mut self, option: &str) -> &mut Self {
220+
self.cmd.arg(format!("-Csymbol-mangling-version={option}"));
221+
self
222+
}
223+
224+
/// Specify `-C prefer-dynamic`.
225+
pub fn prefer_dynamic(&mut self) -> &mut Self {
226+
self.cmd.arg(format!("-Cprefer-dynamic"));
227+
self
228+
}
229+
218230
/// Specify error format to use
219231
pub fn error_format(&mut self, format: &str) -> &mut Self {
220232
self.cmd.arg(format!("--error-format={format}"));

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

+7-3
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,9 @@ pub use wasmparser;
4747
// tidy-alphabetical-end
4848

4949
// Re-exports of external dependencies.
50-
pub use external_deps::{c_build, c_cxx_compiler, clang, htmldocck, llvm, python, rustc, rustdoc};
50+
pub use external_deps::{
51+
cargo, c_build, c_cxx_compiler, clang, htmldocck, llvm, python, rustc, rustdoc
52+
};
5153

5254
// These rely on external dependencies.
5355
pub use c_cxx_compiler::{Cc, Gcc, cc, cxx, extra_c_flags, extra_cxx_flags, gcc};
@@ -79,7 +81,10 @@ pub use env::{env_var, env_var_os, set_current_dir};
7981
pub use run::{cmd, run, run_fail, run_with_args};
8082

8183
/// Helpers for checking target information.
82-
pub use targets::{is_aix, is_darwin, is_msvc, is_windows, llvm_components_contain, target, uname, apple_os};
84+
pub use targets::{
85+
apple_os, is_aix, is_darwin, is_msvc, is_windows, is_windows_gnu, llvm_components_contain,
86+
target, uname,
87+
};
8388

8489
/// Helpers for building names of output artifacts that are potentially target-specific.
8590
pub use artifact_names::{
@@ -104,4 +109,3 @@ pub use assertion_helpers::{
104109
pub use string::{
105110
count_regex_matches_in_files_with_extension, invalid_utf8_contains, invalid_utf8_not_contains,
106111
};
107-
use crate::external_deps::cargo;

‎src/tools/run-make-support/src/symbols.rs

+20-5
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,32 @@ use std::path::Path;
22

33
use object::{self, Object, ObjectSymbol, SymbolIterator};
44

5-
/// Iterate through the symbols in an object file.
6-
///
7-
/// Uses a callback because `SymbolIterator` does not own its data.
5+
/// Given an [`object::File`], find the exported dynamic symbol names via
6+
/// [`object::Object::exports`]. This does **not** impose any filters on the specific dynamic
7+
/// symbols, e.g. if they are global or local, if they are defined or not, and in which section the
8+
/// dynamic symbols reside in.
9+
#[track_caller]
10+
pub fn exported_dynamic_symbol_names<'file>(file: &'file object::File<'file>) -> Vec<&'file str> {
11+
file.exports()
12+
.unwrap()
13+
.into_iter()
14+
.filter_map(|sym| std::str::from_utf8(sym.name()).ok())
15+
.collect()
16+
}
17+
18+
/// Iterate through the symbols in an object file. See [`object::Object::symbols`].
819
///
920
/// Panics if `path` is not a valid object file readable by the current user.
21+
#[track_caller]
1022
pub fn with_symbol_iter<P, F, R>(path: P, func: F) -> R
1123
where
1224
P: AsRef<Path>,
1325
F: FnOnce(&mut SymbolIterator<'_, '_>) -> R,
1426
{
15-
let raw_bytes = crate::fs::read(path);
16-
let f = object::File::parse(raw_bytes.as_slice()).expect("unable to parse file");
27+
let path = path.as_ref();
28+
let blob = crate::fs::read(path);
29+
let f = object::File::parse(&*blob)
30+
.unwrap_or_else(|e| panic!("failed to parse `{}`: {e}", path.display()));
1731
let mut iter = f.symbols();
1832
func(&mut iter)
1933
}
@@ -24,6 +38,7 @@ where
2438
/// `path` contain a substring listed in `substrings`.
2539
///
2640
/// Panics if `path` is not a valid object file readable by the current user.
41+
#[track_caller]
2742
pub fn any_symbol_contains(path: impl AsRef<Path>, substrings: &[&str]) -> bool {
2843
with_symbol_iter(path, |syms| {
2944
for sym in syms {

‎src/tools/run-make-support/src/targets.rs

+6
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ pub fn is_msvc() -> bool {
2222
target().contains("msvc")
2323
}
2424

25+
/// Check if target is windows-gnu.
26+
#[must_use]
27+
pub fn is_windows_gnu() -> bool {
28+
target().ends_with("windows-gnu")
29+
}
30+
2531
/// Check if target uses macOS.
2632
#[must_use]
2733
pub fn is_darwin() -> bool {
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
run-make/cat-and-grep-sanity-check/Makefile
22
run-make/jobserver-error/Makefile
33
run-make/split-debuginfo/Makefile
4-
run-make/symbol-mangling-hashed/Makefile
54
run-make/translation/Makefile

‎tests/run-make/symbol-mangling-hashed/Makefile

-48
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
// ignore-tidy-linelength
2+
//! Basic smoke test for the unstable option `-C symbol_mangling_version=hashed` which aims to
3+
//! replace full symbol mangling names based on hash digests to shorten symbol name lengths in
4+
//! dylibs for space savings.
5+
//!
6+
//! # References
7+
//!
8+
//! - MCP #705: Provide option to shorten symbol names by replacing them with a digest:
9+
//! <https://github.com/rust-lang/compiler-team/issues/705>.
10+
//! - Implementation PR: <https://github.com/rust-lang/rust/pull/118636>.
11+
//! - PE format: <https://learn.microsoft.com/en-us/windows/win32/debug/pe-format>.
12+
13+
//@ ignore-cross-compile
14+
15+
#![deny(warnings)]
16+
17+
use run_make_support::symbols::exported_dynamic_symbol_names;
18+
use run_make_support::{bin_name, cwd, dynamic_lib_name, object, rfs, rustc};
19+
20+
fn main() {
21+
rustc()
22+
.input("a_dylib.rs")
23+
.prefer_dynamic()
24+
.arg("-Zunstable-options")
25+
.symbol_mangling_version("hashed")
26+
.metadata("foo")
27+
.run();
28+
29+
rustc()
30+
.input("a_rlib.rs")
31+
.prefer_dynamic()
32+
.arg("-Zunstable-options")
33+
.symbol_mangling_version("hashed")
34+
.metadata("bar")
35+
.run();
36+
37+
rustc().input("b_dylib.rs").library_search_path(cwd()).prefer_dynamic().run();
38+
rustc().input("b_bin.rs").library_search_path(cwd()).prefer_dynamic().run();
39+
40+
// Check hashed symbol name
41+
42+
{
43+
let dylib_filename = dynamic_lib_name("a_dylib");
44+
println!("checking dylib `{dylib_filename}`");
45+
46+
let dylib_blob = rfs::read(&dylib_filename);
47+
let dylib_file = object::File::parse(&*dylib_blob)
48+
.unwrap_or_else(|e| panic!("failed to parse `{dylib_filename}`: {e}"));
49+
50+
let dynamic_symbols = exported_dynamic_symbol_names(&dylib_file);
51+
52+
if dynamic_symbols.iter().filter(|sym| sym.contains("hello")).count() != 0 {
53+
eprintln!("exported dynamic symbols: {:#?}", dynamic_symbols);
54+
panic!("expected no occurrence of `hello`");
55+
}
56+
57+
if dynamic_symbols.iter().filter(|sym| sym.starts_with("_RNxC7a_dylib")).count() != 2 {
58+
eprintln!("exported dynamic symbols: {:#?}", dynamic_symbols);
59+
panic!("expected two dynamic symbols starting with `_RNxC7a_dylib`");
60+
}
61+
}
62+
63+
{
64+
let so_filename = dynamic_lib_name("b_dylib");
65+
println!("checking so `{so_filename}`");
66+
67+
let so_blob = rfs::read(&so_filename);
68+
let so_file = object::File::parse(&*so_blob)
69+
.unwrap_or_else(|e| panic!("failed to parse `{so_filename}`: {e}"));
70+
71+
let dynamic_symbols = exported_dynamic_symbol_names(&so_file);
72+
73+
if dynamic_symbols
74+
.iter()
75+
.filter(|sym| sym.contains("b_dylib") && sym.contains("hello"))
76+
.count()
77+
!= 1
78+
{
79+
eprintln!("exported dynamic symbols: {:#?}", dynamic_symbols);
80+
panic!("expected one occurrence of mangled `hello`");
81+
}
82+
83+
if dynamic_symbols.iter().filter(|sym| sym.starts_with("_RNxC6a_rlib")).count() != 2 {
84+
eprintln!("exported dynamic symbols: {:#?}", dynamic_symbols);
85+
panic!("expected two exported symbols starting with `_RNxC6a_rlib`");
86+
}
87+
88+
if dynamic_symbols.iter().any(|sym| sym.starts_with("_RNxC7a_dylib")) {
89+
eprintln!("exported dynamic symbols: {:#?}", dynamic_symbols);
90+
panic!("did not expect any symbols starting with `_RNxC7a_dylib`");
91+
}
92+
}
93+
94+
{
95+
let bin_filename = bin_name("b_bin");
96+
println!("checking bin `{bin_filename}`");
97+
98+
let bin_blob = rfs::read(&bin_filename);
99+
let bin_file = object::File::parse(&*bin_blob)
100+
.unwrap_or_else(|e| panic!("failed to parse `{bin_filename}`: {e}"));
101+
102+
let dynamic_symbols = exported_dynamic_symbol_names(&bin_file);
103+
104+
if dynamic_symbols.iter().any(|sym| {
105+
sym.starts_with("_RNxC6a_rlib")
106+
|| sym.starts_with("_RNxC7a_dylib")
107+
|| (sym.contains("b_dylib") && sym.contains("hello"))
108+
}) {
109+
eprintln!("dynamic symbols: {:#?}", dynamic_symbols);
110+
panic!(
111+
"did not expect any symbols to (1) start with `_RNxC6a_rlib` or (2) start with \
112+
`_RNxC7a_dylib` or (3) to be of the form `*b_dylib*hello*`"
113+
);
114+
}
115+
}
116+
}

0 commit comments

Comments
 (0)
Failed to load comments.