Skip to content

Commit 1332c85

Browse files
jieyouxulolbinarycat
andcommittedJan 13, 2025
tests: port extern-fn-reachable to rmake.rs
Co-authored-by: binarycat <binarycat@envs.net>
1 parent 2ae9916 commit 1332c85

File tree

4 files changed

+70
-27
lines changed

4 files changed

+70
-27
lines changed
 

‎src/tools/tidy/src/allowed_run_make_makefiles.txt

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
run-make/cat-and-grep-sanity-check/Makefile
2-
run-make/extern-fn-reachable/Makefile
32
run-make/jobserver-error/Makefile
43
run-make/split-debuginfo/Makefile
54
run-make/symbol-mangling-hashed/Makefile

‎tests/run-make/extern-fn-reachable/Makefile

-26
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,34 @@
11
#![crate_type = "dylib"]
22
#![allow(dead_code)]
33

4+
// `pub` extern fn here is a Rust nameres visibility concept, and should not affect symbol
5+
// visibility in the dylib.
46
#[no_mangle]
57
pub extern "C" fn fun1() {}
8+
9+
// (Lack of) `pub` for the extern fn here is a Rust nameres visibility concept, and should not
10+
// affect symbol visibility in the dylib.
611
#[no_mangle]
712
extern "C" fn fun2() {}
813

14+
// Modules are a Rust nameres concept, and should not affect symbol visibility in the dylib if the
15+
// extern fn is nested inside a module.
916
mod foo {
1017
#[no_mangle]
1118
pub extern "C" fn fun3() {}
1219
}
20+
21+
// Similarly, the Rust visibility of the containing module is a Rust nameres concept, and should not
22+
// affect symbol visibility in the dylib.
1323
pub mod bar {
1424
#[no_mangle]
1525
pub extern "C" fn fun4() {}
1626
}
1727

28+
// Non-extern `#[no_mangle]` fn should induce a symbol visible in the dylib.
1829
#[no_mangle]
1930
pub fn fun5() {}
31+
32+
// The Rust visibility of the fn should not affect is symbol visibility in the dylib.
33+
#[no_mangle]
34+
fn fun6() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
//! Smoke test to check that that symbols of `extern "C"` functions and `#[no_mangle]` rust
2+
//! functions:
3+
//!
4+
//! 1. Are externally visible in the dylib produced.
5+
//! 2. That the symbol visibility is orthogonal to the Rust nameres visibility of the functions
6+
//! involved.
7+
8+
//@ ignore-cross-compile
9+
10+
use run_make_support::object::{self, Object, ObjectSection, ObjectSymbol};
11+
use run_make_support::{dynamic_lib_name, is_darwin, is_msvc, path, rfs, rustc};
12+
13+
fn main() {
14+
let dylib = dynamic_lib_name("dylib");
15+
rustc().input("dylib.rs").output(&dylib).arg("-Cprefer-dynamic").run();
16+
let mut fun1_count = 0; // #[no_mangle] pub extern "C" fn fun1() {}
17+
let mut fun2_count = 0; // #[no_mangle] extern "C" fn fun2() {}
18+
let mut fun3_count = 0; // mod foo { #[no_mangle] pub extern "C" fn fun3() {} }
19+
let mut fun4_count = 0; // pub mod bar { #[no_mangle] pub extern "C" fn fun4() {} }
20+
let mut fun5_count = 0; // #[no_mangle] pub fn fun5() {}
21+
let mut fun6_count = 0; // #[no_mangle] fn fun6() {}
22+
23+
let p = if is_msvc() { path(dylib) } else { path(dylib) };
24+
let blob = rfs::read(&p);
25+
let file = object::File::parse(&*blob).unwrap();
26+
27+
for export in file.exports().unwrap() {
28+
let sym_name = export.name();
29+
let sym_name = String::from_utf8_lossy(sym_name);
30+
match sym_name.as_ref() {
31+
"fun1" => fun1_count += 1,
32+
"fun2" => fun2_count += 1,
33+
"fun3" => fun3_count += 1,
34+
"fun4" => fun4_count += 1,
35+
"fun5" => fun5_count += 1,
36+
"fun6" => fun6_count += 1,
37+
// Mach-O states that all exported symbols should have an underscore as prefix. At the
38+
// same time dlsym will implicitly add it, so outside of compilers, linkers and people
39+
// writing assembly, nobody needs to be aware of this.
40+
"_fun1" if is_darwin() => fun1_count += 1,
41+
"_fun2" if is_darwin() => fun2_count += 1,
42+
"_fun3" if is_darwin() => fun3_count += 1,
43+
"_fun4" if is_darwin() => fun4_count += 1,
44+
"_fun5" if is_darwin() => fun5_count += 1,
45+
"_fun6" if is_darwin() => fun6_count += 1,
46+
_ => {}
47+
}
48+
}
49+
assert_eq!(fun1_count, 1);
50+
assert_eq!(fun2_count, 1);
51+
assert_eq!(fun3_count, 1);
52+
assert_eq!(fun4_count, 1);
53+
assert_eq!(fun5_count, 1);
54+
assert_eq!(fun6_count, 1);
55+
}

0 commit comments

Comments
 (0)