From 3c49fe0cbd86b452ba519ffe98ece4a620caee23 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20W=C3=B6rister?= Date: Mon, 19 Feb 2024 14:03:09 +0100 Subject: [PATCH 1/3] link.exe: don't embed full path to PDB file in binary. --- compiler/rustc_codegen_ssa/src/back/linker.rs | 9 +++++++++ tests/run-make/pdb-alt-path/Makefile | 20 +++++++++++++++++++ tests/run-make/pdb-alt-path/main.rs | 3 +++ 3 files changed, 32 insertions(+) create mode 100644 tests/run-make/pdb-alt-path/Makefile create mode 100644 tests/run-make/pdb-alt-path/main.rs diff --git a/compiler/rustc_codegen_ssa/src/back/linker.rs b/compiler/rustc_codegen_ssa/src/back/linker.rs index b4e054417f31c..65bff08d78131 100644 --- a/compiler/rustc_codegen_ssa/src/back/linker.rs +++ b/compiler/rustc_codegen_ssa/src/back/linker.rs @@ -929,6 +929,15 @@ impl<'a> Linker for MsvcLinker<'a> { // from the CodeView line tables in the object files. self.cmd.arg("/DEBUG"); + // Default to emitting only the file name of the PDB file into + // the binary instead of the full path. Emitting the full path + // may leak private information (such as user names). + // See https://github.com/rust-lang/rust/issues/87825. + // + // This default behavior can be overridden by explicitly passing + // `-Clink-arg=/PDBALTPATH:...` to rustc. + self.cmd.arg("/PDBALTPATH:%_PDB%"); + // This will cause the Microsoft linker to embed .natvis info into the PDB file let natvis_dir_path = self.sess.sysroot.join("lib\\rustlib\\etc"); if let Ok(natvis_dir) = fs::read_dir(&natvis_dir_path) { diff --git a/tests/run-make/pdb-alt-path/Makefile b/tests/run-make/pdb-alt-path/Makefile new file mode 100644 index 0000000000000..d7d435957a385 --- /dev/null +++ b/tests/run-make/pdb-alt-path/Makefile @@ -0,0 +1,20 @@ +include ../tools.mk + +# only-windows-msvc + +all: + # Test that we don't have the full path to the PDB file in the binary + $(RUSTC) main.rs -g --crate-name my_crate_name --crate-type bin + $(CGREP) "my_crate_name.pdb" < $(TMPDIR)/my_crate_name.exe + $(CGREP) -v "\\my_crate_name.pdb" < $(TMPDIR)/my_crate_name.exe + + # Test that backtraces still can find debuginfo by checking that they contain symbol names and + # source locations. + RUST_BACKTRACE="full" $(TMPDIR)/my_crate_name.exe &> $(TMPDIR)/backtrace.txt || exit 0 + $(CGREP) "my_crate_name::main" < $(TMPDIR)/backtrace.txt + $(CGREP) "pdb-alt-path\\main.rs:2" < $(TMPDIR)/backtrace.txt + + # Test that explicitly passed `-Clink-arg=/PDBALTPATH:...` is respected + $(RUSTC) main.rs -g --crate-name my_crate_name --crate-type bin -Clink-arg=/PDBALTPATH:abcdefg.pdb + $(CGREP) "abcdefg.pdb" < $(TMPDIR)/my_crate_name.exe + $(CGREP) -v "my_crate_name.pdb" < $(TMPDIR)/my_crate_name.exe diff --git a/tests/run-make/pdb-alt-path/main.rs b/tests/run-make/pdb-alt-path/main.rs new file mode 100644 index 0000000000000..e95109fd08a3b --- /dev/null +++ b/tests/run-make/pdb-alt-path/main.rs @@ -0,0 +1,3 @@ +fn main() { + panic!("backtrace please"); +} From e1c3a5a7aa25362636751989aec5222f928def91 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20W=C3=B6rister?= Date: Wed, 13 Mar 2024 11:44:50 +0100 Subject: [PATCH 2/3] Force frame pointers in pdb-alt-path test case --- tests/run-make/pdb-alt-path/Makefile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/run-make/pdb-alt-path/Makefile b/tests/run-make/pdb-alt-path/Makefile index d7d435957a385..5795dae97c253 100644 --- a/tests/run-make/pdb-alt-path/Makefile +++ b/tests/run-make/pdb-alt-path/Makefile @@ -4,7 +4,7 @@ include ../tools.mk all: # Test that we don't have the full path to the PDB file in the binary - $(RUSTC) main.rs -g --crate-name my_crate_name --crate-type bin + $(RUSTC) main.rs -g --crate-name my_crate_name --crate-type bin -Cforce-frame-pointers $(CGREP) "my_crate_name.pdb" < $(TMPDIR)/my_crate_name.exe $(CGREP) -v "\\my_crate_name.pdb" < $(TMPDIR)/my_crate_name.exe @@ -15,6 +15,6 @@ all: $(CGREP) "pdb-alt-path\\main.rs:2" < $(TMPDIR)/backtrace.txt # Test that explicitly passed `-Clink-arg=/PDBALTPATH:...` is respected - $(RUSTC) main.rs -g --crate-name my_crate_name --crate-type bin -Clink-arg=/PDBALTPATH:abcdefg.pdb + $(RUSTC) main.rs -g --crate-name my_crate_name --crate-type bin -Clink-arg=/PDBALTPATH:abcdefg.pdb -Cforce-frame-pointers $(CGREP) "abcdefg.pdb" < $(TMPDIR)/my_crate_name.exe $(CGREP) -v "my_crate_name.pdb" < $(TMPDIR)/my_crate_name.exe From 0a094bae28a00117b99262621ff22876796d2885 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20W=C3=B6rister?= Date: Thu, 14 Mar 2024 11:03:15 +0100 Subject: [PATCH 3/3] Make pdb-alt-path test more unwind-friendly for i686-pc-windows-msvc --- tests/run-make/pdb-alt-path/Makefile | 6 +++--- tests/run-make/pdb-alt-path/main.rs | 23 ++++++++++++++++++++++- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/tests/run-make/pdb-alt-path/Makefile b/tests/run-make/pdb-alt-path/Makefile index 5795dae97c253..7a0ae3bf2ef0d 100644 --- a/tests/run-make/pdb-alt-path/Makefile +++ b/tests/run-make/pdb-alt-path/Makefile @@ -10,9 +10,9 @@ all: # Test that backtraces still can find debuginfo by checking that they contain symbol names and # source locations. - RUST_BACKTRACE="full" $(TMPDIR)/my_crate_name.exe &> $(TMPDIR)/backtrace.txt || exit 0 - $(CGREP) "my_crate_name::main" < $(TMPDIR)/backtrace.txt - $(CGREP) "pdb-alt-path\\main.rs:2" < $(TMPDIR)/backtrace.txt + $(TMPDIR)/my_crate_name.exe &> $(TMPDIR)/backtrace.txt + $(CGREP) "my_crate_name::fn_in_backtrace" < $(TMPDIR)/backtrace.txt + $(CGREP) "main.rs:15" < $(TMPDIR)/backtrace.txt # Test that explicitly passed `-Clink-arg=/PDBALTPATH:...` is respected $(RUSTC) main.rs -g --crate-name my_crate_name --crate-type bin -Clink-arg=/PDBALTPATH:abcdefg.pdb -Cforce-frame-pointers diff --git a/tests/run-make/pdb-alt-path/main.rs b/tests/run-make/pdb-alt-path/main.rs index e95109fd08a3b..d38d540fbc257 100644 --- a/tests/run-make/pdb-alt-path/main.rs +++ b/tests/run-make/pdb-alt-path/main.rs @@ -1,3 +1,24 @@ +// The various #[inline(never)] annotations and std::hint::black_box calls are +// an attempt to make unwinding as non-flaky as possible on i686-pc-windows-msvc. + +#[inline(never)] +fn generate_backtrace(x: &u32) { + std::hint::black_box(x); + let bt = std::backtrace::Backtrace::force_capture(); + println!("{}", bt); + std::hint::black_box(x); +} + +#[inline(never)] +fn fn_in_backtrace(x: &u32) { + std::hint::black_box(x); + generate_backtrace(x); + std::hint::black_box(x); +} + fn main() { - panic!("backtrace please"); + let x = &41; + std::hint::black_box(x); + fn_in_backtrace(x); + std::hint::black_box(x); }