Skip to content

Commit edf0248

Browse files
tests: port symbol-mangling-hashed to rmake.rs
- Use `object` based test logic instead of processing `nm` human-readable textual output. - Try to expand test coverage to not be limited to only linux + x86_64. Co-authored-by: binarycat <[email protected]>
1 parent 5060b8b commit edf0248

File tree

3 files changed

+123
-49
lines changed

3 files changed

+123
-49
lines changed
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,123 @@
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, 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+
let expected_prefix = if is_darwin() { "__RNxC7a_dylib" } else { "_RNxC7a_dylib" };
58+
if dynamic_symbols.iter().filter(|sym| sym.starts_with(expected_prefix)).count() != 2 {
59+
eprintln!("exported dynamic symbols: {:#?}", dynamic_symbols);
60+
panic!("expected two dynamic symbols starting with `{expected_prefix}`");
61+
}
62+
}
63+
64+
{
65+
let so_filename = dynamic_lib_name("b_dylib");
66+
println!("checking so `{so_filename}`");
67+
68+
let so_blob = rfs::read(&so_filename);
69+
let so_file = object::File::parse(&*so_blob)
70+
.unwrap_or_else(|e| panic!("failed to parse `{so_filename}`: {e}"));
71+
72+
let dynamic_symbols = exported_dynamic_symbol_names(&so_file);
73+
74+
if dynamic_symbols
75+
.iter()
76+
.filter(|sym| sym.contains("b_dylib") && sym.contains("hello"))
77+
.count()
78+
!= 1
79+
{
80+
eprintln!("exported dynamic symbols: {:#?}", dynamic_symbols);
81+
panic!("expected one occurrence of mangled `hello`");
82+
}
83+
84+
let expected_rlib_prefix = if is_darwin() { "__RNxC6a_rlib" } else { "_RNxC6a_rlib" };
85+
if dynamic_symbols.iter().filter(|sym| sym.starts_with(expected_rlib_prefix)).count() != 2 {
86+
eprintln!("exported dynamic symbols: {:#?}", dynamic_symbols);
87+
panic!("expected two exported symbols starting with `{expected_rlib_prefix}`");
88+
}
89+
90+
let expected_dylib_prefix = if is_darwin() { "__RNxC7a_dylib" } else { "_RNxC7a_dylib" };
91+
if dynamic_symbols.iter().any(|sym| sym.starts_with("_RNxC7a_dylib")) {
92+
eprintln!("exported dynamic symbols: {:#?}", dynamic_symbols);
93+
panic!("did not expect any symbols starting with `{expected_dylib_prefix}`");
94+
}
95+
}
96+
97+
{
98+
let bin_filename = bin_name("b_bin");
99+
println!("checking bin `{bin_filename}`");
100+
101+
let bin_blob = rfs::read(&bin_filename);
102+
let bin_file = object::File::parse(&*bin_blob)
103+
.unwrap_or_else(|e| panic!("failed to parse `{bin_filename}`: {e}"));
104+
105+
let dynamic_symbols = exported_dynamic_symbol_names(&bin_file);
106+
107+
let expected_rlib_prefix = if is_darwin() { "__RNxC6a_rlib" } else { "_RNxC6a_rlib" };
108+
let expected_dylib_prefix = if is_darwin() { "__RNxC7a_dylib" } else { "_RNxC7a_dylib" };
109+
if dynamic_symbols.iter().any(|sym| {
110+
sym.starts_with(expected_rlib_prefix)
111+
|| sym.starts_with(expected_dylib_prefix)
112+
|| (sym.contains("b_dylib") && sym.contains("hello"))
113+
}) {
114+
eprintln!("dynamic symbols: {:#?}", dynamic_symbols);
115+
panic!(
116+
"did not expect any symbols to: \
117+
(1) start with `{expected_rlib_prefix}` or \
118+
(2) start with `{expected_dylib_prefix}` or \
119+
(3) to be of the form `*b_dylib*hello*`"
120+
);
121+
}
122+
}
123+
}

0 commit comments

Comments
 (0)