From 6098e795a20f03654ab967a3705a495454abb05b Mon Sep 17 00:00:00 2001 From: bjorn3 <17426603+bjorn3@users.noreply.github.com> Date: Sat, 30 Jul 2022 12:16:33 +0000 Subject: [PATCH 1/3] Limit symbols exported from proc macros Only __rustc_proc_macro_decls_*__ and rust_metadata_* need to be exported for proc macros to work. All other symbols only increase binary size and have the potential to conflict with symbols from the host compiler. --- compiler/rustc_codegen_ssa/src/back/linker.rs | 4 +--- .../src/back/symbol_export.rs | 8 ++++--- .../symbol-visibility/Makefile | 21 +++++++++++++++++++ .../symbol-visibility/a_proc_macro.rs | 9 ++++++++ 4 files changed, 36 insertions(+), 6 deletions(-) create mode 100644 src/test/run-make-fulldeps/symbol-visibility/a_proc_macro.rs diff --git a/compiler/rustc_codegen_ssa/src/back/linker.rs b/compiler/rustc_codegen_ssa/src/back/linker.rs index a4bbdefbb87f8..3d77ddc3f84c2 100644 --- a/compiler/rustc_codegen_ssa/src/back/linker.rs +++ b/compiler/rustc_codegen_ssa/src/back/linker.rs @@ -656,9 +656,7 @@ impl<'a> Linker for GccLinker<'a> { return; } - if crate_type == CrateType::ProcMacro { - return; - } + // FIXME hide #[no_mangle] symbols for proc-macros let is_windows = self.sess.target.is_like_windows; let path = tmpdir.join(if is_windows { "list.def" } else { "list" }); diff --git a/compiler/rustc_codegen_ssa/src/back/symbol_export.rs b/compiler/rustc_codegen_ssa/src/back/symbol_export.rs index d1a267f7643f5..e6b6055759d27 100644 --- a/compiler/rustc_codegen_ssa/src/back/symbol_export.rs +++ b/compiler/rustc_codegen_ssa/src/back/symbol_export.rs @@ -257,16 +257,18 @@ fn exported_symbols_provider_local<'tcx>( })); } - if tcx.sess.crate_types().contains(&CrateType::Dylib) { + if tcx.sess.crate_types().contains(&CrateType::Dylib) + || tcx.sess.crate_types().contains(&CrateType::ProcMacro) + { let symbol_name = metadata_symbol_name(tcx); let exported_symbol = ExportedSymbol::NoDefId(SymbolName::new(tcx, &symbol_name)); symbols.push(( exported_symbol, SymbolExportInfo { - level: SymbolExportLevel::Rust, + level: SymbolExportLevel::C, kind: SymbolExportKind::Data, - used: false, + used: true, }, )); } diff --git a/src/test/run-make-fulldeps/symbol-visibility/Makefile b/src/test/run-make-fulldeps/symbol-visibility/Makefile index 4bb35f33ad3e9..b816afa7395a1 100644 --- a/src/test/run-make-fulldeps/symbol-visibility/Makefile +++ b/src/test/run-make-fulldeps/symbol-visibility/Makefile @@ -5,6 +5,7 @@ include ../tools.mk NM=nm -D CDYLIB_NAME=liba_cdylib.so RDYLIB_NAME=liba_rust_dylib.so +PROC_MACRO_NAME=liba_proc_macro.so EXE_NAME=an_executable COMBINED_CDYLIB_NAME=libcombined_rlib_dylib.so @@ -12,6 +13,7 @@ ifeq ($(UNAME),Darwin) NM=nm -gU CDYLIB_NAME=liba_cdylib.dylib RDYLIB_NAME=liba_rust_dylib.dylib +PROC_MACRO_NAME=liba_proc_macro.dylib EXE_NAME=an_executable COMBINED_CDYLIB_NAME=libcombined_rlib_dylib.dylib endif @@ -20,6 +22,7 @@ ifdef IS_WINDOWS NM=nm -g CDYLIB_NAME=liba_cdylib.dll.a RDYLIB_NAME=liba_rust_dylib.dll.a +PROC_MACRO_NAME=liba_proc_macro.dll EXE_NAME=an_executable.exe COMBINED_CDYLIB_NAME=libcombined_rlib_dylib.dll.a endif @@ -31,6 +34,7 @@ all: $(RUSTC) -Zshare-generics=no an_rlib.rs $(RUSTC) -Zshare-generics=no a_cdylib.rs $(RUSTC) -Zshare-generics=no a_rust_dylib.rs + $(RUSTC) -Zshare-generics=no a_proc_macro.rs $(RUSTC) -Zshare-generics=no an_executable.rs $(RUSTC) -Zshare-generics=no a_cdylib.rs --crate-name combined_rlib_dylib --crate-type=rlib,cdylib @@ -54,6 +58,14 @@ all: # Check that a Rust dylib does not export generics if -Zshare-generics=no [ "$$($(NM) $(TMPDIR)/$(RDYLIB_NAME) | grep -v __imp_ | grep -c public_generic_function_from_rlib)" -eq "0" ] + # Check that a proc macro exports its public #[no_mangle] functions + # FIXME avoid exporting #[no_mangle] symbols for proc macros + [ "$$($(NM) $(TMPDIR)/$(CDYLIB_NAME) | grep -v __imp_ | grep -c public_c_function_from_cdylib)" -eq "1" ] + # Check that a proc macro exports the public #[no_mangle] functions of dependencies + [ "$$($(NM) $(TMPDIR)/$(CDYLIB_NAME) | grep -v __imp_ | grep -c public_c_function_from_rlib)" -eq "1" ] + # Check that a proc macro DOES NOT export any public Rust functions + [ "$$($(NM) $(TMPDIR)/$(CDYLIB_NAME) | grep -v __imp_ | grep -c $(RE_ANY_RUST_SYMBOL))" -eq "0" ] + # FIXME(nbdd0121): This is broken in MinGW, see https://github.com/rust-lang/rust/pull/95604#issuecomment-1101564032 ifndef IS_WINDOWS # Check that an executable does not export any dynamic symbols @@ -75,6 +87,7 @@ endif $(RUSTC) -Zshare-generics=yes an_rlib.rs $(RUSTC) -Zshare-generics=yes a_cdylib.rs $(RUSTC) -Zshare-generics=yes a_rust_dylib.rs + $(RUSTC) -Zshare-generics=yes a_proc_macro.rs $(RUSTC) -Zshare-generics=yes an_executable.rs # Check that a cdylib exports its public #[no_mangle] functions @@ -94,6 +107,14 @@ endif [ "$$($(NM) $(TMPDIR)/$(RDYLIB_NAME) | grep -v __imp_ | grep -c public_rust_function_from_rlib)" -eq "1" ] [ "$$($(NM) $(TMPDIR)/$(RDYLIB_NAME) | grep -v __imp_ | grep -c public_generic_function_from_rlib)" -eq "1" ] + # Check that a proc macro exports its public #[no_mangle] functions + # FIXME avoid exporting #[no_mangle] symbols for proc macros + [ "$$($(NM) $(TMPDIR)/$(CDYLIB_NAME) | grep -v __imp_ | grep -c public_c_function_from_cdylib)" -eq "1" ] + # Check that a proc macro exports the public #[no_mangle] functions of dependencies + [ "$$($(NM) $(TMPDIR)/$(CDYLIB_NAME) | grep -v __imp_ | grep -c public_c_function_from_rlib)" -eq "1" ] + # Check that a proc macro DOES NOT export any public Rust functions + [ "$$($(NM) $(TMPDIR)/$(CDYLIB_NAME) | grep -v __imp_ | grep -c $(RE_ANY_RUST_SYMBOL))" -eq "0" ] + ifndef IS_WINDOWS # Check that an executable does not export any dynamic symbols [ "$$($(NM) $(TMPDIR)/$(EXE_NAME) | grep -v __imp_ | grep -c public_c_function_from_rlib)" -eq "0" ] diff --git a/src/test/run-make-fulldeps/symbol-visibility/a_proc_macro.rs b/src/test/run-make-fulldeps/symbol-visibility/a_proc_macro.rs new file mode 100644 index 0000000000000..9fd1a8a671767 --- /dev/null +++ b/src/test/run-make-fulldeps/symbol-visibility/a_proc_macro.rs @@ -0,0 +1,9 @@ +#![crate_type = "proc-macro"] + +extern crate an_rlib; + +// This should not be exported +#[no_mangle] +extern "C" fn public_c_function_from_cdylib() { + an_rlib::public_c_function_from_rlib(); +} From df13721863253f6298264165348a60dfb9783f94 Mon Sep 17 00:00:00 2001 From: bjorn3 <17426603+bjorn3@users.noreply.github.com> Date: Sun, 31 Jul 2022 10:03:44 +0000 Subject: [PATCH 2/3] Remove workarounds for issue 59998 --- .../proc-macro}/auxiliary/proc-macro-panic.rs | 0 src/test/ui/proc-macro/invalid-punct-ident-1.rs | 5 ----- src/test/ui/proc-macro/invalid-punct-ident-1.stderr | 2 +- src/test/ui/proc-macro/invalid-punct-ident-2.rs | 5 ----- src/test/ui/proc-macro/invalid-punct-ident-2.stderr | 2 +- src/test/ui/proc-macro/invalid-punct-ident-3.rs | 5 ----- src/test/ui/proc-macro/invalid-punct-ident-3.stderr | 2 +- .../proc-macro}/issue-76270-panic-in-libproc-macro.rs | 5 ----- .../issue-76270-panic-in-libproc-macro.stderr | 2 +- src/test/ui/proc-macro/load-panic-backtrace.rs | 10 ---------- src/test/ui/proc-macro/load-panic-backtrace.stderr | 2 +- 11 files changed, 5 insertions(+), 35 deletions(-) rename src/test/{ui-fulldeps => ui/proc-macro}/auxiliary/proc-macro-panic.rs (100%) rename src/test/{ui-fulldeps => ui/proc-macro}/issue-76270-panic-in-libproc-macro.rs (68%) rename src/test/{ui-fulldeps => ui/proc-macro}/issue-76270-panic-in-libproc-macro.stderr (80%) diff --git a/src/test/ui-fulldeps/auxiliary/proc-macro-panic.rs b/src/test/ui/proc-macro/auxiliary/proc-macro-panic.rs similarity index 100% rename from src/test/ui-fulldeps/auxiliary/proc-macro-panic.rs rename to src/test/ui/proc-macro/auxiliary/proc-macro-panic.rs diff --git a/src/test/ui/proc-macro/invalid-punct-ident-1.rs b/src/test/ui/proc-macro/invalid-punct-ident-1.rs index fdf3ca2e261b6..814cd77cfe9bc 100644 --- a/src/test/ui/proc-macro/invalid-punct-ident-1.rs +++ b/src/test/ui/proc-macro/invalid-punct-ident-1.rs @@ -1,9 +1,4 @@ // aux-build:invalid-punct-ident.rs -// ignore-stage1 -// only-linux -// -// FIXME: This should be a normal (stage1, all platforms) test in -// src/test/ui/proc-macro once issue #59998 is fixed. #[macro_use] extern crate invalid_punct_ident; diff --git a/src/test/ui/proc-macro/invalid-punct-ident-1.stderr b/src/test/ui/proc-macro/invalid-punct-ident-1.stderr index bb0a48cb16b8d..7babe685bedf5 100644 --- a/src/test/ui/proc-macro/invalid-punct-ident-1.stderr +++ b/src/test/ui/proc-macro/invalid-punct-ident-1.stderr @@ -1,5 +1,5 @@ error: proc macro panicked - --> $DIR/invalid-punct-ident-1.rs:11:1 + --> $DIR/invalid-punct-ident-1.rs:6:1 | LL | invalid_punct!(); | ^^^^^^^^^^^^^^^^ diff --git a/src/test/ui/proc-macro/invalid-punct-ident-2.rs b/src/test/ui/proc-macro/invalid-punct-ident-2.rs index 151f6203439f7..a04dec707fe44 100644 --- a/src/test/ui/proc-macro/invalid-punct-ident-2.rs +++ b/src/test/ui/proc-macro/invalid-punct-ident-2.rs @@ -1,9 +1,4 @@ // aux-build:invalid-punct-ident.rs -// ignore-stage1 -// only-linux -// -// FIXME: This should be a normal (stage1, all platforms) test in -// src/test/ui/proc-macro once issue #59998 is fixed. #[macro_use] extern crate invalid_punct_ident; diff --git a/src/test/ui/proc-macro/invalid-punct-ident-2.stderr b/src/test/ui/proc-macro/invalid-punct-ident-2.stderr index 0bd07bd649e47..01b80768c5748 100644 --- a/src/test/ui/proc-macro/invalid-punct-ident-2.stderr +++ b/src/test/ui/proc-macro/invalid-punct-ident-2.stderr @@ -1,5 +1,5 @@ error: proc macro panicked - --> $DIR/invalid-punct-ident-2.rs:11:1 + --> $DIR/invalid-punct-ident-2.rs:6:1 | LL | invalid_ident!(); | ^^^^^^^^^^^^^^^^ diff --git a/src/test/ui/proc-macro/invalid-punct-ident-3.rs b/src/test/ui/proc-macro/invalid-punct-ident-3.rs index 7c22a56b6fbe9..f0e953608cb54 100644 --- a/src/test/ui/proc-macro/invalid-punct-ident-3.rs +++ b/src/test/ui/proc-macro/invalid-punct-ident-3.rs @@ -1,9 +1,4 @@ // aux-build:invalid-punct-ident.rs -// ignore-stage1 -// only-linux -// -// FIXME: This should be a normal (stage1, all platforms) test in -// src/test/ui/proc-macro once issue #59998 is fixed. #[macro_use] extern crate invalid_punct_ident; diff --git a/src/test/ui/proc-macro/invalid-punct-ident-3.stderr b/src/test/ui/proc-macro/invalid-punct-ident-3.stderr index a0cc5ef6e2d62..899c38158c2d0 100644 --- a/src/test/ui/proc-macro/invalid-punct-ident-3.stderr +++ b/src/test/ui/proc-macro/invalid-punct-ident-3.stderr @@ -1,5 +1,5 @@ error: proc macro panicked - --> $DIR/invalid-punct-ident-3.rs:11:1 + --> $DIR/invalid-punct-ident-3.rs:6:1 | LL | invalid_raw_ident!(); | ^^^^^^^^^^^^^^^^^^^^ diff --git a/src/test/ui-fulldeps/issue-76270-panic-in-libproc-macro.rs b/src/test/ui/proc-macro/issue-76270-panic-in-libproc-macro.rs similarity index 68% rename from src/test/ui-fulldeps/issue-76270-panic-in-libproc-macro.rs rename to src/test/ui/proc-macro/issue-76270-panic-in-libproc-macro.rs index 981abb4e2c4d2..98fa06b6e4574 100644 --- a/src/test/ui-fulldeps/issue-76270-panic-in-libproc-macro.rs +++ b/src/test/ui/proc-macro/issue-76270-panic-in-libproc-macro.rs @@ -1,10 +1,5 @@ // aux-build:proc-macro-panic.rs // edition:2018 -// ignore-stage1 -// only-linux -// -// FIXME: This should be a normal (stage1, all platforms) test in -// src/test/ui/proc-macro once issue #59998 is fixed. // Regression test for issue #76270 // Tests that we don't print an ICE message when a panic diff --git a/src/test/ui-fulldeps/issue-76270-panic-in-libproc-macro.stderr b/src/test/ui/proc-macro/issue-76270-panic-in-libproc-macro.stderr similarity index 80% rename from src/test/ui-fulldeps/issue-76270-panic-in-libproc-macro.stderr rename to src/test/ui/proc-macro/issue-76270-panic-in-libproc-macro.stderr index 5cc292dbb40c5..1dc0f16bf6c40 100644 --- a/src/test/ui-fulldeps/issue-76270-panic-in-libproc-macro.stderr +++ b/src/test/ui/proc-macro/issue-76270-panic-in-libproc-macro.stderr @@ -1,5 +1,5 @@ error: proc macro panicked - --> $DIR/issue-76270-panic-in-libproc-macro.rs:15:1 + --> $DIR/issue-76270-panic-in-libproc-macro.rs:10:1 | LL | proc_macro_panic::panic_in_libproc_macro!(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/src/test/ui/proc-macro/load-panic-backtrace.rs b/src/test/ui/proc-macro/load-panic-backtrace.rs index 4a3ba9aee7453..cd6f70a55758c 100644 --- a/src/test/ui/proc-macro/load-panic-backtrace.rs +++ b/src/test/ui/proc-macro/load-panic-backtrace.rs @@ -1,18 +1,8 @@ // aux-build:test-macros.rs // compile-flags: -Z proc-macro-backtrace // rustc-env:RUST_BACKTRACE=0 - -// FIXME https://github.com/rust-lang/rust/issues/59998 // normalize-stderr-test "thread '.*' panicked " -> "" // normalize-stderr-test "note:.*RUST_BACKTRACE=1.*\n" -> "" -// normalize-stderr-test "\nerror: internal compiler error.*\n\n" -> "" -// normalize-stderr-test "note:.*unexpectedly panicked.*\n\n" -> "" -// normalize-stderr-test "note: we would appreciate a bug report.*\n\n" -> "" -// normalize-stderr-test "note: compiler flags.*\n\n" -> "" -// normalize-stderr-test "note: rustc.*running on.*\n\n" -> "" -// normalize-stderr-test "query stack during panic:\n" -> "" -// normalize-stderr-test "we're just showing a limited slice of the query stack\n" -> "" -// normalize-stderr-test "end of query stack\n" -> "" #[macro_use] extern crate test_macros; diff --git a/src/test/ui/proc-macro/load-panic-backtrace.stderr b/src/test/ui/proc-macro/load-panic-backtrace.stderr index f825047e33168..cef5786d1b8af 100644 --- a/src/test/ui/proc-macro/load-panic-backtrace.stderr +++ b/src/test/ui/proc-macro/load-panic-backtrace.stderr @@ -1,6 +1,6 @@ at 'panic-derive', $DIR/auxiliary/test-macros.rs:43:5 error: proc-macro derive panicked - --> $DIR/load-panic-backtrace.rs:20:10 + --> $DIR/load-panic-backtrace.rs:10:10 | LL | #[derive(Panic)] | ^^^^^ From b87f8a4d9c5b02be27d763bb76d66298f1c01ffe Mon Sep 17 00:00:00 2001 From: bjorn3 <17426603+bjorn3@users.noreply.github.com> Date: Sun, 31 Jul 2022 10:10:58 +0000 Subject: [PATCH 3/3] Add issue reference --- compiler/rustc_codegen_ssa/src/back/linker.rs | 2 +- src/test/run-make-fulldeps/symbol-visibility/Makefile | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/compiler/rustc_codegen_ssa/src/back/linker.rs b/compiler/rustc_codegen_ssa/src/back/linker.rs index 3d77ddc3f84c2..ce51b2e9531fc 100644 --- a/compiler/rustc_codegen_ssa/src/back/linker.rs +++ b/compiler/rustc_codegen_ssa/src/back/linker.rs @@ -656,7 +656,7 @@ impl<'a> Linker for GccLinker<'a> { return; } - // FIXME hide #[no_mangle] symbols for proc-macros + // FIXME(#99978) hide #[no_mangle] symbols for proc-macros let is_windows = self.sess.target.is_like_windows; let path = tmpdir.join(if is_windows { "list.def" } else { "list" }); diff --git a/src/test/run-make-fulldeps/symbol-visibility/Makefile b/src/test/run-make-fulldeps/symbol-visibility/Makefile index b816afa7395a1..17052ef4e6346 100644 --- a/src/test/run-make-fulldeps/symbol-visibility/Makefile +++ b/src/test/run-make-fulldeps/symbol-visibility/Makefile @@ -59,7 +59,7 @@ all: [ "$$($(NM) $(TMPDIR)/$(RDYLIB_NAME) | grep -v __imp_ | grep -c public_generic_function_from_rlib)" -eq "0" ] # Check that a proc macro exports its public #[no_mangle] functions - # FIXME avoid exporting #[no_mangle] symbols for proc macros + # FIXME(#99978) avoid exporting #[no_mangle] symbols for proc macros [ "$$($(NM) $(TMPDIR)/$(CDYLIB_NAME) | grep -v __imp_ | grep -c public_c_function_from_cdylib)" -eq "1" ] # Check that a proc macro exports the public #[no_mangle] functions of dependencies [ "$$($(NM) $(TMPDIR)/$(CDYLIB_NAME) | grep -v __imp_ | grep -c public_c_function_from_rlib)" -eq "1" ] @@ -108,7 +108,7 @@ endif [ "$$($(NM) $(TMPDIR)/$(RDYLIB_NAME) | grep -v __imp_ | grep -c public_generic_function_from_rlib)" -eq "1" ] # Check that a proc macro exports its public #[no_mangle] functions - # FIXME avoid exporting #[no_mangle] symbols for proc macros + # FIXME(#99978) avoid exporting #[no_mangle] symbols for proc macros [ "$$($(NM) $(TMPDIR)/$(CDYLIB_NAME) | grep -v __imp_ | grep -c public_c_function_from_cdylib)" -eq "1" ] # Check that a proc macro exports the public #[no_mangle] functions of dependencies [ "$$($(NM) $(TMPDIR)/$(CDYLIB_NAME) | grep -v __imp_ | grep -c public_c_function_from_rlib)" -eq "1" ]