Auto merge of #99944 - bjorn3:hide_proc_macro_symbols, r=eddyb

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.

Fixes https://github.com/rust-lang/rust/issues/99909
Fixes #59998

cc `@eddyb`
This commit is contained in:
bors 2022-08-01 03:58:52 +00:00
commit 25bb1c13bd
15 changed files with 41 additions and 41 deletions

View File

@ -656,9 +656,7 @@ impl<'a> Linker for GccLinker<'a> {
return;
}
if crate_type == CrateType::ProcMacro {
return;
}
// 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" });

View File

@ -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,
},
));
}

View File

@ -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(#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" ]
# 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(#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" ]
# 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" ]

View File

@ -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();
}

View File

@ -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;

View File

@ -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!();
| ^^^^^^^^^^^^^^^^

View File

@ -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;

View File

@ -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!();
| ^^^^^^^^^^^^^^^^

View File

@ -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;

View File

@ -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!();
| ^^^^^^^^^^^^^^^^^^^^

View File

@ -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

View File

@ -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!();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

View File

@ -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;

View File

@ -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)]
| ^^^^^