Skip to content

Commit 2aa5888

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 72f939c commit 2aa5888

File tree

9 files changed

+148
-67
lines changed

9 files changed

+148
-67
lines changed
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)