Skip to content

Commit 9734ebb

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 0bc1b4f commit 9734ebb

File tree

9 files changed

+128
-69
lines changed

9 files changed

+128
-69
lines changed

Diff for: src/tools/tidy/src/allowed_run_make_makefiles.txt

-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
11
run-make/split-debuginfo/Makefile
2-
run-make/symbol-mangling-hashed/Makefile

Diff for: tests/run-make/symbol-mangling-hashed/Makefile

-48
This file was deleted.

Diff for: tests/run-make/symbol-mangling-hashed/b_bin.rs

-9
This file was deleted.

Diff for: tests/run-make/symbol-mangling-hashed/b_dylib.rs

-9
This file was deleted.

Diff for: tests/run-make/symbol-mangling-hashed/default_bin.rs

+9
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::hrhello();
7+
hashed_dylib::hdhello();
8+
default_dylib::ddhello();
9+
}
+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 ddhello() {
7+
hashed_rlib::hrhello();
8+
hashed_dylib::hdhello();
9+
}
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
#![crate_type = "dylib"]
2-
pub fn hello() {
2+
pub fn hdhello() {
33
println!("hello dylib");
44
}
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#![crate_type = "rlib"]
22

3-
pub fn hello() {
3+
pub fn hrhello() {
44
println!("hello rlib");
55
}

Diff for: tests/run-make/symbol-mangling-hashed/rmake.rs

+108
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
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+
macro_rules! adjust_symbol_prefix {
21+
($name:literal) => {
22+
if is_darwin() { concat!("_", $name) } else { $name }
23+
};
24+
}
25+
26+
fn main() {
27+
rustc()
28+
.input("hashed_dylib.rs")
29+
.prefer_dynamic()
30+
.arg("-Zunstable-options")
31+
.symbol_mangling_version("hashed")
32+
.metadata("foo")
33+
.run();
34+
35+
rustc()
36+
.input("hashed_rlib.rs")
37+
.prefer_dynamic()
38+
.arg("-Zunstable-options")
39+
.symbol_mangling_version("hashed")
40+
.metadata("bar")
41+
.run();
42+
43+
rustc().input("default_dylib.rs").library_search_path(cwd()).prefer_dynamic().run();
44+
rustc().input("default_bin.rs").library_search_path(cwd()).prefer_dynamic().run();
45+
46+
{
47+
// Check hashed symbol name
48+
49+
let dylib_filename = dynamic_lib_name("hashed_dylib");
50+
println!("checking dylib `{dylib_filename}`");
51+
52+
let dylib_blob = rfs::read(&dylib_filename);
53+
let dylib_file = object::File::parse(&*dylib_blob)
54+
.unwrap_or_else(|e| panic!("failed to parse `{dylib_filename}`: {e}"));
55+
56+
let dynamic_symbols = exported_dynamic_symbol_names(&dylib_file);
57+
58+
if dynamic_symbols.iter().filter(|sym| sym.contains("hdhello")).count() != 0 {
59+
eprintln!("exported dynamic symbols: {:#?}", dynamic_symbols);
60+
panic!("expected no occurrence of `hdhello`");
61+
}
62+
63+
let expected_prefix = adjust_symbol_prefix!("_RNxC12hashed_dylib");
64+
if dynamic_symbols.iter().filter(|sym| sym.starts_with(expected_prefix)).count() != 2 {
65+
eprintln!("exported dynamic symbols: {:#?}", dynamic_symbols);
66+
panic!("expected two dynamic symbols starting with `{expected_prefix}`");
67+
}
68+
}
69+
70+
{
71+
let dylib_filename = dynamic_lib_name("default_dylib");
72+
println!("checking so `{dylib_filename}`");
73+
74+
let dylib_blob = rfs::read(&dylib_filename);
75+
let dylib_file = object::File::parse(&*dylib_blob)
76+
.unwrap_or_else(|e| panic!("failed to parse `{dylib_filename}`: {e}"));
77+
78+
let dynamic_symbols = exported_dynamic_symbol_names(&dylib_file);
79+
80+
if dynamic_symbols
81+
.iter()
82+
.filter(|sym| sym.contains("default_dylib") && sym.contains("ddhello"))
83+
.count()
84+
!= 1
85+
{
86+
eprintln!("exported dynamic symbols: {:#?}", dynamic_symbols);
87+
panic!("expected one occurrence of mangled `ddhello`");
88+
}
89+
90+
let expected_rlib_prefix = adjust_symbol_prefix!("_RNxC11hashed_rlib");
91+
if dynamic_symbols.iter().filter(|sym| sym.starts_with(expected_rlib_prefix)).count() != 2 {
92+
eprintln!("exported dynamic symbols: {:#?}", dynamic_symbols);
93+
panic!("expected two exported symbols starting with `{expected_rlib_prefix}`");
94+
}
95+
96+
let expected_dylib_prefix = adjust_symbol_prefix!("_RNxC12hashed_dylib");
97+
if dynamic_symbols.iter().any(|sym| sym.starts_with("_RNxC12hashed_dylib")) {
98+
eprintln!("exported dynamic symbols: {:#?}", dynamic_symbols);
99+
panic!("did not expect any symbols starting with `{expected_dylib_prefix}`");
100+
}
101+
}
102+
103+
// Check that the final binary can be run.
104+
{
105+
let bin_filename = bin_name("default_bin");
106+
run(&bin_filename);
107+
}
108+
}

0 commit comments

Comments
 (0)