Skip to content

Commit 897a145

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 897a145

File tree

3 files changed

+116
-49
lines changed

3 files changed

+116
-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,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)