forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrmake.rs
More file actions
167 lines (152 loc) · 6.86 KB
/
rmake.rs
File metadata and controls
167 lines (152 loc) · 6.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
// Dynamic libraries on Rust used to export a very high amount of symbols,
// going as far as filling the output with mangled names and generic function
// names. After the rework of #38117, this test checks that no mangled Rust symbols
// are exported, and that generics are only shown if explicitly requested.
// See https://github.com/rust-lang/rust/issues/37530
use run_make_support::object::read::Object;
use run_make_support::{bin_name, dynamic_lib_name, is_msvc, object, regex, rfs, rustc};
fn main() {
let cdylib_name = dynamic_lib_name("a_cdylib");
let rdylib_name = dynamic_lib_name("a_rust_dylib");
let exe_name = bin_name("an_executable");
let combined_cdylib_name = dynamic_lib_name("combined_rlib_dylib");
rustc().arg("-Zshare-generics=no").input("an_rlib.rs").run();
rustc().arg("-Zshare-generics=no").input("a_cdylib.rs").run();
rustc().arg("-Zshare-generics=no").input("a_rust_dylib.rs").run();
rustc().arg("-Zshare-generics=no").input("a_proc_macro.rs").run();
rustc().arg("-Zshare-generics=no").input("an_executable.rs").run();
rustc()
.arg("-Zshare-generics=no")
.input("a_cdylib.rs")
.crate_name("combined_rlib_dylib")
.crate_type("rlib,cdylib")
.run();
// Check that a cdylib exports its public #[no_mangle] functions
symbols_check(&cdylib_name, SymbolCheckType::StrSymbol("public_c_function_from_cdylib"), true);
// Check that a cdylib exports the public #[no_mangle] functions of dependencies
symbols_check(&cdylib_name, SymbolCheckType::StrSymbol("public_c_function_from_rlib"), true);
// Check that a cdylib DOES NOT export any public Rust functions
symbols_check(&cdylib_name, SymbolCheckType::AnyRustSymbol, false);
// Check that a Rust dylib exports its monomorphic functions
symbols_check(
&rdylib_name,
SymbolCheckType::StrSymbol("public_c_function_from_rust_dylib"),
true,
);
symbols_check(
&rdylib_name,
SymbolCheckType::StrSymbol("public_rust_function_from_rust_dylib"),
true,
);
// Check that a Rust dylib does not export generics if -Zshare-generics=no
symbols_check(
&rdylib_name,
SymbolCheckType::StrSymbol("public_generic_function_from_rust_dylib"),
false,
);
// Check that a Rust dylib exports the monomorphic functions from its dependencies
symbols_check(&rdylib_name, SymbolCheckType::StrSymbol("public_c_function_from_rlib"), true);
symbols_check(&rdylib_name, SymbolCheckType::StrSymbol("public_rust_function_from_rlib"), true);
// Check that a Rust dylib does not export generics if -Zshare-generics=no
symbols_check(
&rdylib_name,
SymbolCheckType::StrSymbol("public_generic_function_from_rlib"),
false,
);
// FIXME(nbdd0121): This is broken in MinGW, see https://github.com/rust-lang/rust/pull/95604#issuecomment-1101564032
if is_msvc() {
// Check that an executable does not export any dynamic symbols
symbols_check(&exe_name, SymbolCheckType::StrSymbol("public_c_function_from_rlib"), false);
symbols_check(
&exe_name,
SymbolCheckType::StrSymbol("public_rust_function_from_exe"),
false,
);
}
// Check the combined case, where we generate a cdylib and an rlib in the same
// compilation session:
// Check that a cdylib exports its public #[no_mangle] functions
symbols_check(
&combined_cdylib_name,
SymbolCheckType::StrSymbol("public_c_function_from_cdylib"),
true,
);
// Check that a cdylib exports the public #[no_mangle] functions of dependencies
symbols_check(
&combined_cdylib_name,
SymbolCheckType::StrSymbol("public_c_function_from_rlib"),
true,
);
// Check that a cdylib DOES NOT export any public Rust functions
symbols_check(&combined_cdylib_name, SymbolCheckType::AnyRustSymbol, false);
rustc().arg("-Zshare-generics=yes").input("an_rlib.rs").run();
rustc().arg("-Zshare-generics=yes").input("a_cdylib.rs").run();
rustc().arg("-Zshare-generics=yes").input("a_rust_dylib.rs").run();
rustc().arg("-Zshare-generics=yes").input("an_executable.rs").run();
// Check that a cdylib exports its public #[no_mangle] functions
symbols_check(&cdylib_name, SymbolCheckType::StrSymbol("public_c_function_from_cdylib"), true);
// Check that a cdylib exports the public #[no_mangle] functions of dependencies
symbols_check(&cdylib_name, SymbolCheckType::StrSymbol("public_c_function_from_rlib"), true);
// Check that a cdylib DOES NOT export any public Rust functions
symbols_check(&cdylib_name, SymbolCheckType::AnyRustSymbol, false);
// Check that a Rust dylib exports its monomorphic functions, including generics this time
symbols_check(
&rdylib_name,
SymbolCheckType::StrSymbol("public_c_function_from_rust_dylib"),
true,
);
symbols_check(
&rdylib_name,
SymbolCheckType::StrSymbol("public_rust_function_from_rust_dylib"),
true,
);
symbols_check(
&rdylib_name,
SymbolCheckType::StrSymbol("public_generic_function_from_rust_dylib"),
true,
);
// Check that a Rust dylib exports the monomorphic functions from its dependencies
symbols_check(&rdylib_name, SymbolCheckType::StrSymbol("public_c_function_from_rlib"), true);
symbols_check(&rdylib_name, SymbolCheckType::StrSymbol("public_rust_function_from_rlib"), true);
symbols_check(
&rdylib_name,
SymbolCheckType::StrSymbol("public_generic_function_from_rlib"),
true,
);
// FIXME(nbdd0121): This is broken in MinGW, see https://github.com/rust-lang/rust/pull/95604#issuecomment-1101564032
if is_msvc() {
// Check that an executable does not export any dynamic symbols
symbols_check(&exe_name, SymbolCheckType::StrSymbol("public_c_function_from_rlib"), false);
symbols_check(
&exe_name,
SymbolCheckType::StrSymbol("public_rust_function_from_exe"),
false,
);
}
}
#[track_caller]
fn symbols_check(path: &str, symbol_check_type: SymbolCheckType, exists_once: bool) {
let binary_data = rfs::read(path);
let file = object::File::parse(&*binary_data).unwrap();
let mut found: u64 = 0;
for export in file.exports().unwrap() {
let name = std::str::from_utf8(export.name()).unwrap();
if has_symbol(name, symbol_check_type) {
found += 1;
}
}
assert_eq!(found, exists_once as u64);
}
fn has_symbol(name: &str, symbol_check_type: SymbolCheckType) -> bool {
if let SymbolCheckType::StrSymbol(expected) = symbol_check_type {
name.contains(expected)
} else {
let regex = regex::Regex::new(r#"_ZN.*h.*E\|_R[a-zA-Z0-9_]+"#).unwrap();
regex.is_match(name)
}
}
#[derive(Clone, Copy)]
enum SymbolCheckType {
StrSymbol(&'static str),
AnyRustSymbol,
}