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 840317a

Browse files
committedJan 28, 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-1 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 aa6f5ab + 2aa5888 commit 840317a

File tree

13 files changed

+193
-75
lines changed

13 files changed

+193
-75
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,3 +1,2 @@
11
run-make/split-debuginfo/Makefile
2-
run-make/symbol-mangling-hashed/Makefile
32
run-make/translation/Makefile

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

-48
This file was deleted.

‎tests/run-make/symbol-mangling-hashed/b_bin.rs

-9
This file was deleted.

‎tests/run-make/symbol-mangling-hashed/b_dylib.rs

-9
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
extern crate default_dylib;
2+
extern crate hashed_dylib;
3+
extern crate hashed_rlib;
4+
5+
fn main() {
6+
hashed_rlib::hello();
7+
hashed_dylib::hello();
8+
default_dylib::hello();
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#![crate_type = "dylib"]
2+
3+
extern crate hashed_dylib;
4+
extern crate hashed_rlib;
5+
6+
pub fn hello() {
7+
hashed_rlib::hello();
8+
hashed_dylib::hello();
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
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, is_darwin, object, rfs, run, rustc};
19+
20+
fn main() {
21+
rustc()
22+
.input("hashed_dylib.rs")
23+
.prefer_dynamic()
24+
.arg("-Zunstable-options")
25+
.symbol_mangling_version("hashed")
26+
.metadata("foo")
27+
.run();
28+
29+
rustc()
30+
.input("hashed_rlib.rs")
31+
.prefer_dynamic()
32+
.arg("-Zunstable-options")
33+
.symbol_mangling_version("hashed")
34+
.metadata("bar")
35+
.run();
36+
37+
rustc().input("default_dylib.rs").library_search_path(cwd()).prefer_dynamic().run();
38+
rustc().input("default_bin.rs").library_search_path(cwd()).prefer_dynamic().run();
39+
40+
// Check hashed symbol name
41+
42+
{
43+
let dylib_filename = dynamic_lib_name("hashed_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+
let expected_prefix =
58+
if is_darwin() { "__RNxC12hashed_dylib" } else { "_RNxC12hashed_dylib" };
59+
if dynamic_symbols.iter().filter(|sym| sym.starts_with(expected_prefix)).count() != 2 {
60+
eprintln!("exported dynamic symbols: {:#?}", dynamic_symbols);
61+
panic!("expected two dynamic symbols starting with `{expected_prefix}`");
62+
}
63+
}
64+
65+
{
66+
let dylib_filename = dynamic_lib_name("default_dylib");
67+
println!("checking so `{dylib_filename}`");
68+
69+
let dylib_blob = rfs::read(&dylib_filename);
70+
let dylib_file = object::File::parse(&*dylib_blob)
71+
.unwrap_or_else(|e| panic!("failed to parse `{dylib_filename}`: {e}"));
72+
73+
let dynamic_symbols = exported_dynamic_symbol_names(&dylib_file);
74+
75+
if dynamic_symbols
76+
.iter()
77+
.filter(|sym| sym.contains("default_dylib") && sym.contains("hello"))
78+
.count()
79+
!= 1
80+
{
81+
eprintln!("exported dynamic symbols: {:#?}", dynamic_symbols);
82+
panic!("expected one occurrence of mangled `hello`");
83+
}
84+
85+
let expected_rlib_prefix =
86+
if is_darwin() { "__RNxC11hashed_rlib" } else { "_RNxC11hashed_rlib" };
87+
if dynamic_symbols.iter().filter(|sym| sym.starts_with(expected_rlib_prefix)).count() != 2 {
88+
eprintln!("exported dynamic symbols: {:#?}", dynamic_symbols);
89+
panic!("expected two exported symbols starting with `{expected_rlib_prefix}`");
90+
}
91+
92+
let expected_dylib_prefix =
93+
if is_darwin() { "__RNxC12hashed_dylib" } else { "_RNxC12hashed_dylib" };
94+
if dynamic_symbols.iter().any(|sym| sym.starts_with("_RNxC12hashed_dylib")) {
95+
eprintln!("exported dynamic symbols: {:#?}", dynamic_symbols);
96+
panic!("did not expect any symbols starting with `{expected_dylib_prefix}`");
97+
}
98+
}
99+
100+
{
101+
let bin_filename = bin_name("default_bin");
102+
println!("checking bin `{bin_filename}`");
103+
104+
let bin_blob = rfs::read(&bin_filename);
105+
let bin_file = object::File::parse(&*bin_blob)
106+
.unwrap_or_else(|e| panic!("failed to parse `{bin_filename}`: {e}"));
107+
108+
let dynamic_symbols = exported_dynamic_symbol_names(&bin_file);
109+
110+
let expected_rlib_prefix =
111+
if is_darwin() { "__RNxC11hashed_rlib" } else { "_RNxC11hashed_rlib" };
112+
let expected_dylib_prefix =
113+
if is_darwin() { "__RNxC12hashed_dylib" } else { "_RNxC12hashed_dylib" };
114+
if dynamic_symbols.iter().any(|sym| {
115+
sym.starts_with(expected_rlib_prefix)
116+
|| sym.starts_with(expected_dylib_prefix)
117+
|| (sym.contains("default_dylib") && sym.contains("hello"))
118+
}) {
119+
eprintln!("dynamic symbols: {:#?}", dynamic_symbols);
120+
panic!(
121+
"did not expect any symbols to: \
122+
(1) start with `{expected_rlib_prefix}` or \
123+
(2) start with `{expected_dylib_prefix}` or \
124+
(3) to be of the form `*default_dylib*hello*`"
125+
);
126+
}
127+
128+
run(&bin_filename);
129+
}
130+
}

0 commit comments

Comments
 (0)
Failed to load comments.