Skip to content

Commit 25bb1c1

Browse files
committed
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 #99909 Fixes #59998 cc `@eddyb`
2 parents 6423ab3 + b87f8a4 commit 25bb1c1

15 files changed

+41
-41
lines changed

compiler/rustc_codegen_ssa/src/back/linker.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -656,9 +656,7 @@ impl<'a> Linker for GccLinker<'a> {
656656
return;
657657
}
658658

659-
if crate_type == CrateType::ProcMacro {
660-
return;
661-
}
659+
// FIXME(#99978) hide #[no_mangle] symbols for proc-macros
662660

663661
let is_windows = self.sess.target.is_like_windows;
664662
let path = tmpdir.join(if is_windows { "list.def" } else { "list" });

compiler/rustc_codegen_ssa/src/back/symbol_export.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -257,16 +257,18 @@ fn exported_symbols_provider_local<'tcx>(
257257
}));
258258
}
259259

260-
if tcx.sess.crate_types().contains(&CrateType::Dylib) {
260+
if tcx.sess.crate_types().contains(&CrateType::Dylib)
261+
|| tcx.sess.crate_types().contains(&CrateType::ProcMacro)
262+
{
261263
let symbol_name = metadata_symbol_name(tcx);
262264
let exported_symbol = ExportedSymbol::NoDefId(SymbolName::new(tcx, &symbol_name));
263265

264266
symbols.push((
265267
exported_symbol,
266268
SymbolExportInfo {
267-
level: SymbolExportLevel::Rust,
269+
level: SymbolExportLevel::C,
268270
kind: SymbolExportKind::Data,
269-
used: false,
271+
used: true,
270272
},
271273
));
272274
}

src/test/run-make-fulldeps/symbol-visibility/Makefile

+21
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@ include ../tools.mk
55
NM=nm -D
66
CDYLIB_NAME=liba_cdylib.so
77
RDYLIB_NAME=liba_rust_dylib.so
8+
PROC_MACRO_NAME=liba_proc_macro.so
89
EXE_NAME=an_executable
910
COMBINED_CDYLIB_NAME=libcombined_rlib_dylib.so
1011

1112
ifeq ($(UNAME),Darwin)
1213
NM=nm -gU
1314
CDYLIB_NAME=liba_cdylib.dylib
1415
RDYLIB_NAME=liba_rust_dylib.dylib
16+
PROC_MACRO_NAME=liba_proc_macro.dylib
1517
EXE_NAME=an_executable
1618
COMBINED_CDYLIB_NAME=libcombined_rlib_dylib.dylib
1719
endif
@@ -20,6 +22,7 @@ ifdef IS_WINDOWS
2022
NM=nm -g
2123
CDYLIB_NAME=liba_cdylib.dll.a
2224
RDYLIB_NAME=liba_rust_dylib.dll.a
25+
PROC_MACRO_NAME=liba_proc_macro.dll
2326
EXE_NAME=an_executable.exe
2427
COMBINED_CDYLIB_NAME=libcombined_rlib_dylib.dll.a
2528
endif
@@ -31,6 +34,7 @@ all:
3134
$(RUSTC) -Zshare-generics=no an_rlib.rs
3235
$(RUSTC) -Zshare-generics=no a_cdylib.rs
3336
$(RUSTC) -Zshare-generics=no a_rust_dylib.rs
37+
$(RUSTC) -Zshare-generics=no a_proc_macro.rs
3438
$(RUSTC) -Zshare-generics=no an_executable.rs
3539
$(RUSTC) -Zshare-generics=no a_cdylib.rs --crate-name combined_rlib_dylib --crate-type=rlib,cdylib
3640

@@ -54,6 +58,14 @@ all:
5458
# Check that a Rust dylib does not export generics if -Zshare-generics=no
5559
[ "$$($(NM) $(TMPDIR)/$(RDYLIB_NAME) | grep -v __imp_ | grep -c public_generic_function_from_rlib)" -eq "0" ]
5660

61+
# Check that a proc macro exports its public #[no_mangle] functions
62+
# FIXME(#99978) avoid exporting #[no_mangle] symbols for proc macros
63+
[ "$$($(NM) $(TMPDIR)/$(CDYLIB_NAME) | grep -v __imp_ | grep -c public_c_function_from_cdylib)" -eq "1" ]
64+
# Check that a proc macro exports the public #[no_mangle] functions of dependencies
65+
[ "$$($(NM) $(TMPDIR)/$(CDYLIB_NAME) | grep -v __imp_ | grep -c public_c_function_from_rlib)" -eq "1" ]
66+
# Check that a proc macro DOES NOT export any public Rust functions
67+
[ "$$($(NM) $(TMPDIR)/$(CDYLIB_NAME) | grep -v __imp_ | grep -c $(RE_ANY_RUST_SYMBOL))" -eq "0" ]
68+
5769
# FIXME(nbdd0121): This is broken in MinGW, see https://github.com/rust-lang/rust/pull/95604#issuecomment-1101564032
5870
ifndef IS_WINDOWS
5971
# Check that an executable does not export any dynamic symbols
@@ -75,6 +87,7 @@ endif
7587
$(RUSTC) -Zshare-generics=yes an_rlib.rs
7688
$(RUSTC) -Zshare-generics=yes a_cdylib.rs
7789
$(RUSTC) -Zshare-generics=yes a_rust_dylib.rs
90+
$(RUSTC) -Zshare-generics=yes a_proc_macro.rs
7891
$(RUSTC) -Zshare-generics=yes an_executable.rs
7992

8093
# Check that a cdylib exports its public #[no_mangle] functions
@@ -94,6 +107,14 @@ endif
94107
[ "$$($(NM) $(TMPDIR)/$(RDYLIB_NAME) | grep -v __imp_ | grep -c public_rust_function_from_rlib)" -eq "1" ]
95108
[ "$$($(NM) $(TMPDIR)/$(RDYLIB_NAME) | grep -v __imp_ | grep -c public_generic_function_from_rlib)" -eq "1" ]
96109

110+
# Check that a proc macro exports its public #[no_mangle] functions
111+
# FIXME(#99978) avoid exporting #[no_mangle] symbols for proc macros
112+
[ "$$($(NM) $(TMPDIR)/$(CDYLIB_NAME) | grep -v __imp_ | grep -c public_c_function_from_cdylib)" -eq "1" ]
113+
# Check that a proc macro exports the public #[no_mangle] functions of dependencies
114+
[ "$$($(NM) $(TMPDIR)/$(CDYLIB_NAME) | grep -v __imp_ | grep -c public_c_function_from_rlib)" -eq "1" ]
115+
# Check that a proc macro DOES NOT export any public Rust functions
116+
[ "$$($(NM) $(TMPDIR)/$(CDYLIB_NAME) | grep -v __imp_ | grep -c $(RE_ANY_RUST_SYMBOL))" -eq "0" ]
117+
97118
ifndef IS_WINDOWS
98119
# Check that an executable does not export any dynamic symbols
99120
[ "$$($(NM) $(TMPDIR)/$(EXE_NAME) | grep -v __imp_ | grep -c public_c_function_from_rlib)" -eq "0" ]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#![crate_type = "proc-macro"]
2+
3+
extern crate an_rlib;
4+
5+
// This should not be exported
6+
#[no_mangle]
7+
extern "C" fn public_c_function_from_cdylib() {
8+
an_rlib::public_c_function_from_rlib();
9+
}

src/test/ui/proc-macro/invalid-punct-ident-1.rs

-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
11
// aux-build:invalid-punct-ident.rs
2-
// ignore-stage1
3-
// only-linux
4-
//
5-
// FIXME: This should be a normal (stage1, all platforms) test in
6-
// src/test/ui/proc-macro once issue #59998 is fixed.
72

83
#[macro_use]
94
extern crate invalid_punct_ident;

src/test/ui/proc-macro/invalid-punct-ident-1.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: proc macro panicked
2-
--> $DIR/invalid-punct-ident-1.rs:11:1
2+
--> $DIR/invalid-punct-ident-1.rs:6:1
33
|
44
LL | invalid_punct!();
55
| ^^^^^^^^^^^^^^^^

src/test/ui/proc-macro/invalid-punct-ident-2.rs

-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
11
// aux-build:invalid-punct-ident.rs
2-
// ignore-stage1
3-
// only-linux
4-
//
5-
// FIXME: This should be a normal (stage1, all platforms) test in
6-
// src/test/ui/proc-macro once issue #59998 is fixed.
72

83
#[macro_use]
94
extern crate invalid_punct_ident;

src/test/ui/proc-macro/invalid-punct-ident-2.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: proc macro panicked
2-
--> $DIR/invalid-punct-ident-2.rs:11:1
2+
--> $DIR/invalid-punct-ident-2.rs:6:1
33
|
44
LL | invalid_ident!();
55
| ^^^^^^^^^^^^^^^^

src/test/ui/proc-macro/invalid-punct-ident-3.rs

-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
11
// aux-build:invalid-punct-ident.rs
2-
// ignore-stage1
3-
// only-linux
4-
//
5-
// FIXME: This should be a normal (stage1, all platforms) test in
6-
// src/test/ui/proc-macro once issue #59998 is fixed.
72

83
#[macro_use]
94
extern crate invalid_punct_ident;

src/test/ui/proc-macro/invalid-punct-ident-3.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: proc macro panicked
2-
--> $DIR/invalid-punct-ident-3.rs:11:1
2+
--> $DIR/invalid-punct-ident-3.rs:6:1
33
|
44
LL | invalid_raw_ident!();
55
| ^^^^^^^^^^^^^^^^^^^^

src/test/ui-fulldeps/issue-76270-panic-in-libproc-macro.rs src/test/ui/proc-macro/issue-76270-panic-in-libproc-macro.rs

-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
// aux-build:proc-macro-panic.rs
22
// edition:2018
3-
// ignore-stage1
4-
// only-linux
5-
//
6-
// FIXME: This should be a normal (stage1, all platforms) test in
7-
// src/test/ui/proc-macro once issue #59998 is fixed.
83

94
// Regression test for issue #76270
105
// Tests that we don't print an ICE message when a panic

src/test/ui-fulldeps/issue-76270-panic-in-libproc-macro.stderr src/test/ui/proc-macro/issue-76270-panic-in-libproc-macro.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: proc macro panicked
2-
--> $DIR/issue-76270-panic-in-libproc-macro.rs:15:1
2+
--> $DIR/issue-76270-panic-in-libproc-macro.rs:10:1
33
|
44
LL | proc_macro_panic::panic_in_libproc_macro!();
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

src/test/ui/proc-macro/load-panic-backtrace.rs

-10
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,8 @@
11
// aux-build:test-macros.rs
22
// compile-flags: -Z proc-macro-backtrace
33
// rustc-env:RUST_BACKTRACE=0
4-
5-
// FIXME https://github.com/rust-lang/rust/issues/59998
64
// normalize-stderr-test "thread '.*' panicked " -> ""
75
// normalize-stderr-test "note:.*RUST_BACKTRACE=1.*\n" -> ""
8-
// normalize-stderr-test "\nerror: internal compiler error.*\n\n" -> ""
9-
// normalize-stderr-test "note:.*unexpectedly panicked.*\n\n" -> ""
10-
// normalize-stderr-test "note: we would appreciate a bug report.*\n\n" -> ""
11-
// normalize-stderr-test "note: compiler flags.*\n\n" -> ""
12-
// normalize-stderr-test "note: rustc.*running on.*\n\n" -> ""
13-
// normalize-stderr-test "query stack during panic:\n" -> ""
14-
// normalize-stderr-test "we're just showing a limited slice of the query stack\n" -> ""
15-
// normalize-stderr-test "end of query stack\n" -> ""
166

177
#[macro_use]
188
extern crate test_macros;

src/test/ui/proc-macro/load-panic-backtrace.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
at 'panic-derive', $DIR/auxiliary/test-macros.rs:43:5
22
error: proc-macro derive panicked
3-
--> $DIR/load-panic-backtrace.rs:20:10
3+
--> $DIR/load-panic-backtrace.rs:10:10
44
|
55
LL | #[derive(Panic)]
66
| ^^^^^

0 commit comments

Comments
 (0)