about summary refs log tree commit diff
diff options
context:
space:
mode:
author许杰友 Jieyou Xu (Joe) <39484203+jieyouxu@users.noreply.github.com>2025-01-20 19:14:51 +0800
committerJieyou Xu <jieyouxu@outlook.com>2025-01-31 04:30:51 +0000
commit9734ebb9be2ad760385555e36bb0d065e726d6f5 (patch)
treea21249ad86550e35227be859c159a117ad5ccc42
parent0bc1b4f4f6ace5b5e2a7676e498343cfeafab136 (diff)
downloadrust-9734ebb9be2ad760385555e36bb0d065e726d6f5.tar.gz
rust-9734ebb9be2ad760385555e36bb0d065e726d6f5.zip
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 <binarycat@envs.net>
-rw-r--r--src/tools/tidy/src/allowed_run_make_makefiles.txt1
-rw-r--r--tests/run-make/symbol-mangling-hashed/Makefile48
-rw-r--r--tests/run-make/symbol-mangling-hashed/b_bin.rs9
-rw-r--r--tests/run-make/symbol-mangling-hashed/b_dylib.rs9
-rw-r--r--tests/run-make/symbol-mangling-hashed/default_bin.rs9
-rw-r--r--tests/run-make/symbol-mangling-hashed/default_dylib.rs9
-rw-r--r--tests/run-make/symbol-mangling-hashed/hashed_dylib.rs (renamed from tests/run-make/symbol-mangling-hashed/a_dylib.rs)2
-rw-r--r--tests/run-make/symbol-mangling-hashed/hashed_rlib.rs (renamed from tests/run-make/symbol-mangling-hashed/a_rlib.rs)2
-rw-r--r--tests/run-make/symbol-mangling-hashed/rmake.rs108
9 files changed, 128 insertions, 69 deletions
diff --git a/src/tools/tidy/src/allowed_run_make_makefiles.txt b/src/tools/tidy/src/allowed_run_make_makefiles.txt
index e75d3dc2147..45b40b17ea3 100644
--- a/src/tools/tidy/src/allowed_run_make_makefiles.txt
+++ b/src/tools/tidy/src/allowed_run_make_makefiles.txt
@@ -1,2 +1 @@
 run-make/split-debuginfo/Makefile
-run-make/symbol-mangling-hashed/Makefile
diff --git a/tests/run-make/symbol-mangling-hashed/Makefile b/tests/run-make/symbol-mangling-hashed/Makefile
deleted file mode 100644
index c95036ead95..00000000000
--- a/tests/run-make/symbol-mangling-hashed/Makefile
+++ /dev/null
@@ -1,48 +0,0 @@
-include ../tools.mk
-
-# ignore-cross-compile
-# only-linux
-# only-x86_64
-
-NM=nm -D
-RLIB_NAME=liba_rlib.rlib
-DYLIB_NAME=liba_dylib.so
-SO_NAME=libb_dylib.so
-BIN_NAME=b_bin
-
-ifeq ($(UNAME),Darwin)
-NM=nm -gU
-RLIB_NAME=liba_rlib.rlib
-DYLIB_NAME=liba_dylib.dylib
-SO_NAME=libb_dylib.dylib
-BIN_NAME=b_bin
-endif
-
-ifdef IS_WINDOWS
-NM=nm -g
-RLIB_NAME=liba_rlib.dll.a
-DYLIB_NAME=liba_dylib.dll
-SO_NAME=libb_dylib.dll
-BIN_NAME=b_bin.exe
-endif
-
-all:
-	$(RUSTC) -C prefer-dynamic -Z unstable-options -C symbol-mangling-version=hashed -C metadata=foo a_dylib.rs
-	$(RUSTC) -C prefer-dynamic -Z unstable-options -C symbol-mangling-version=hashed -C metadata=bar a_rlib.rs
-	$(RUSTC) -C prefer-dynamic -L $(TMPDIR) b_dylib.rs
-	$(RUSTC) -C prefer-dynamic -L $(TMPDIR) b_bin.rs
-
-    # Check hashed symbol name
-
-	[ "$$($(NM) $(TMPDIR)/$(DYLIB_NAME) | grep -c hello)" -eq "0" ]
-	[ "$$($(NM) $(TMPDIR)/$(DYLIB_NAME) | grep _RNxC7a_dylib | grep -c ' T ')" -eq "2" ]
-
-	[ "$$($(NM) $(TMPDIR)/$(SO_NAME) | grep b_dylib | grep -c hello)" -eq "1" ]
-	[ "$$($(NM) $(TMPDIR)/$(SO_NAME) | grep _RNxC6a_rlib | grep -c ' T ')" -eq "2" ]
-	[ "$$($(NM) $(TMPDIR)/$(SO_NAME) | grep _RNxC7a_dylib | grep -c ' U ')" -eq "1" ]
-
-	[ "$$($(NM) $(TMPDIR)/$(BIN_NAME) | grep _RNxC6a_rlib | grep -c ' U ')" -eq "1" ]
-	[ "$$($(NM) $(TMPDIR)/$(BIN_NAME) | grep _RNxC7a_dylib | grep -c ' U ')" -eq "1" ]
-	[ "$$($(NM) $(TMPDIR)/$(BIN_NAME) | grep b_dylib | grep hello | grep -c ' U ')" -eq "1" ]
-
-	$(call RUN,$(BIN_NAME))
diff --git a/tests/run-make/symbol-mangling-hashed/b_bin.rs b/tests/run-make/symbol-mangling-hashed/b_bin.rs
deleted file mode 100644
index 8ee7fecda62..00000000000
--- a/tests/run-make/symbol-mangling-hashed/b_bin.rs
+++ /dev/null
@@ -1,9 +0,0 @@
-extern crate a_dylib;
-extern crate a_rlib;
-extern crate b_dylib;
-
-fn main() {
-    a_rlib::hello();
-    a_dylib::hello();
-    b_dylib::hello();
-}
diff --git a/tests/run-make/symbol-mangling-hashed/b_dylib.rs b/tests/run-make/symbol-mangling-hashed/b_dylib.rs
deleted file mode 100644
index 3252c9c75c2..00000000000
--- a/tests/run-make/symbol-mangling-hashed/b_dylib.rs
+++ /dev/null
@@ -1,9 +0,0 @@
-#![crate_type = "dylib"]
-
-extern crate a_dylib;
-extern crate a_rlib;
-
-pub fn hello() {
-    a_rlib::hello();
-    a_dylib::hello();
-}
diff --git a/tests/run-make/symbol-mangling-hashed/default_bin.rs b/tests/run-make/symbol-mangling-hashed/default_bin.rs
new file mode 100644
index 00000000000..387596705c3
--- /dev/null
+++ b/tests/run-make/symbol-mangling-hashed/default_bin.rs
@@ -0,0 +1,9 @@
+extern crate default_dylib;
+extern crate hashed_dylib;
+extern crate hashed_rlib;
+
+fn main() {
+    hashed_rlib::hrhello();
+    hashed_dylib::hdhello();
+    default_dylib::ddhello();
+}
diff --git a/tests/run-make/symbol-mangling-hashed/default_dylib.rs b/tests/run-make/symbol-mangling-hashed/default_dylib.rs
new file mode 100644
index 00000000000..986d1c7b74d
--- /dev/null
+++ b/tests/run-make/symbol-mangling-hashed/default_dylib.rs
@@ -0,0 +1,9 @@
+#![crate_type = "dylib"]
+
+extern crate hashed_dylib;
+extern crate hashed_rlib;
+
+pub fn ddhello() {
+    hashed_rlib::hrhello();
+    hashed_dylib::hdhello();
+}
diff --git a/tests/run-make/symbol-mangling-hashed/a_dylib.rs b/tests/run-make/symbol-mangling-hashed/hashed_dylib.rs
index 49d65b72cac..fbb7cba43e0 100644
--- a/tests/run-make/symbol-mangling-hashed/a_dylib.rs
+++ b/tests/run-make/symbol-mangling-hashed/hashed_dylib.rs
@@ -1,4 +1,4 @@
 #![crate_type = "dylib"]
-pub fn hello() {
+pub fn hdhello() {
     println!("hello dylib");
 }
diff --git a/tests/run-make/symbol-mangling-hashed/a_rlib.rs b/tests/run-make/symbol-mangling-hashed/hashed_rlib.rs
index 71e44ccc200..048e67784d2 100644
--- a/tests/run-make/symbol-mangling-hashed/a_rlib.rs
+++ b/tests/run-make/symbol-mangling-hashed/hashed_rlib.rs
@@ -1,5 +1,5 @@
 #![crate_type = "rlib"]
 
-pub fn hello() {
+pub fn hrhello() {
     println!("hello rlib");
 }
diff --git a/tests/run-make/symbol-mangling-hashed/rmake.rs b/tests/run-make/symbol-mangling-hashed/rmake.rs
new file mode 100644
index 00000000000..136e6b9fa3a
--- /dev/null
+++ b/tests/run-make/symbol-mangling-hashed/rmake.rs
@@ -0,0 +1,108 @@
+// ignore-tidy-linelength
+//! Basic smoke test for the unstable option `-C symbol_mangling_version=hashed` which aims to
+//! replace full symbol mangling names based on hash digests to shorten symbol name lengths in
+//! dylibs for space savings.
+//!
+//! # References
+//!
+//! - MCP #705: Provide option to shorten symbol names by replacing them with a digest:
+//!   <https://github.com/rust-lang/compiler-team/issues/705>.
+//! - Implementation PR: <https://github.com/rust-lang/rust/pull/118636>.
+//! - PE format: <https://learn.microsoft.com/en-us/windows/win32/debug/pe-format>.
+
+//@ ignore-cross-compile
+
+#![deny(warnings)]
+
+use run_make_support::symbols::exported_dynamic_symbol_names;
+use run_make_support::{bin_name, cwd, dynamic_lib_name, is_darwin, object, rfs, run, rustc};
+
+macro_rules! adjust_symbol_prefix {
+    ($name:literal) => {
+        if is_darwin() { concat!("_", $name) } else { $name }
+    };
+}
+
+fn main() {
+    rustc()
+        .input("hashed_dylib.rs")
+        .prefer_dynamic()
+        .arg("-Zunstable-options")
+        .symbol_mangling_version("hashed")
+        .metadata("foo")
+        .run();
+
+    rustc()
+        .input("hashed_rlib.rs")
+        .prefer_dynamic()
+        .arg("-Zunstable-options")
+        .symbol_mangling_version("hashed")
+        .metadata("bar")
+        .run();
+
+    rustc().input("default_dylib.rs").library_search_path(cwd()).prefer_dynamic().run();
+    rustc().input("default_bin.rs").library_search_path(cwd()).prefer_dynamic().run();
+
+    {
+        // Check hashed symbol name
+
+        let dylib_filename = dynamic_lib_name("hashed_dylib");
+        println!("checking dylib `{dylib_filename}`");
+
+        let dylib_blob = rfs::read(&dylib_filename);
+        let dylib_file = object::File::parse(&*dylib_blob)
+            .unwrap_or_else(|e| panic!("failed to parse `{dylib_filename}`: {e}"));
+
+        let dynamic_symbols = exported_dynamic_symbol_names(&dylib_file);
+
+        if dynamic_symbols.iter().filter(|sym| sym.contains("hdhello")).count() != 0 {
+            eprintln!("exported dynamic symbols: {:#?}", dynamic_symbols);
+            panic!("expected no occurrence of `hdhello`");
+        }
+
+        let expected_prefix = adjust_symbol_prefix!("_RNxC12hashed_dylib");
+        if dynamic_symbols.iter().filter(|sym| sym.starts_with(expected_prefix)).count() != 2 {
+            eprintln!("exported dynamic symbols: {:#?}", dynamic_symbols);
+            panic!("expected two dynamic symbols starting with `{expected_prefix}`");
+        }
+    }
+
+    {
+        let dylib_filename = dynamic_lib_name("default_dylib");
+        println!("checking so `{dylib_filename}`");
+
+        let dylib_blob = rfs::read(&dylib_filename);
+        let dylib_file = object::File::parse(&*dylib_blob)
+            .unwrap_or_else(|e| panic!("failed to parse `{dylib_filename}`: {e}"));
+
+        let dynamic_symbols = exported_dynamic_symbol_names(&dylib_file);
+
+        if dynamic_symbols
+            .iter()
+            .filter(|sym| sym.contains("default_dylib") && sym.contains("ddhello"))
+            .count()
+            != 1
+        {
+            eprintln!("exported dynamic symbols: {:#?}", dynamic_symbols);
+            panic!("expected one occurrence of mangled `ddhello`");
+        }
+
+        let expected_rlib_prefix = adjust_symbol_prefix!("_RNxC11hashed_rlib");
+        if dynamic_symbols.iter().filter(|sym| sym.starts_with(expected_rlib_prefix)).count() != 2 {
+            eprintln!("exported dynamic symbols: {:#?}", dynamic_symbols);
+            panic!("expected two exported symbols starting with `{expected_rlib_prefix}`");
+        }
+
+        let expected_dylib_prefix = adjust_symbol_prefix!("_RNxC12hashed_dylib");
+        if dynamic_symbols.iter().any(|sym| sym.starts_with("_RNxC12hashed_dylib")) {
+            eprintln!("exported dynamic symbols: {:#?}", dynamic_symbols);
+            panic!("did not expect any symbols starting with `{expected_dylib_prefix}`");
+        }
+    }
+
+    // Check that the final binary can be run.
+    {
+        let bin_filename = bin_name("default_bin");
+        run(&bin_filename);
+    }
+}