Skip to content

Commit 3462e45

Browse files
committed
add test for symbol visibility of #[naked] functions
1 parent a886938 commit 3462e45

File tree

2 files changed

+183
-0
lines changed

2 files changed

+183
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
#![feature(naked_functions, asm_const, linkage)]
2+
#![crate_type = "dylib"]
3+
4+
use std::arch::asm;
5+
6+
pub trait TraitWithConst {
7+
const COUNT: u32;
8+
}
9+
10+
struct Test;
11+
12+
impl TraitWithConst for Test {
13+
const COUNT: u32 = 1;
14+
}
15+
16+
#[no_mangle]
17+
fn entry() {
18+
private_vanilla();
19+
private_naked();
20+
21+
public_vanilla_generic::<Test>();
22+
public_naked_generic::<Test>();
23+
}
24+
25+
extern "C" fn private_vanilla() -> u32 {
26+
42
27+
}
28+
29+
#[naked]
30+
extern "C" fn private_naked() -> u32 {
31+
unsafe { asm!("mov rax, 42", "ret", options(noreturn)) }
32+
}
33+
34+
#[no_mangle]
35+
pub extern "C" fn public_vanilla() -> u32 {
36+
42
37+
}
38+
39+
#[naked]
40+
#[no_mangle]
41+
pub extern "C" fn public_naked() -> u32 {
42+
unsafe { asm!("mov rax, 42", "ret", options(noreturn)) }
43+
}
44+
45+
pub extern "C" fn public_vanilla_generic<T: TraitWithConst>() -> u32 {
46+
T::COUNT
47+
}
48+
49+
#[naked]
50+
pub extern "C" fn public_naked_generic<T: TraitWithConst>() -> u32 {
51+
unsafe { asm!("mov rax, {}", "ret", const T::COUNT, options(noreturn)) }
52+
}
53+
54+
#[linkage = "external"]
55+
extern "C" fn vanilla_external_linkage() -> u32 {
56+
42
57+
}
58+
59+
#[naked]
60+
#[linkage = "external"]
61+
extern "C" fn naked_external_linkage() -> u32 {
62+
unsafe { asm!("mov rax, 42", "ret", options(noreturn)) }
63+
}
64+
65+
#[cfg(not(windows))]
66+
#[linkage = "weak"]
67+
extern "C" fn vanilla_weak_linkage() -> u32 {
68+
42
69+
}
70+
71+
#[naked]
72+
#[cfg(not(windows))]
73+
#[linkage = "weak"]
74+
extern "C" fn naked_weak_linkage() -> u32 {
75+
unsafe { asm!("mov rax, 42", "ret", options(noreturn)) }
76+
}
77+
78+
// functions that are declared in an `extern "C"` block are currently not exported
79+
// this maybe should change in the future, this is just tracking the current behavior
80+
// reported in https://github.com/rust-lang/rust/issues/128071
81+
std::arch::global_asm! {
82+
".globl function_defined_in_global_asm",
83+
"function_defined_in_global_asm:",
84+
"ret",
85+
}
86+
87+
extern "C" {
88+
pub fn function_defined_in_global_asm();
89+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
//@ only-linux
2+
//@ only-x86_64
3+
use run_make_support::object::read::{File, Object, Symbol};
4+
use run_make_support::object::ObjectSymbol;
5+
use run_make_support::targets::is_windows;
6+
use run_make_support::{dynamic_lib_name, env_var, rfs, rustc};
7+
8+
fn main() {
9+
let rdylib_name = dynamic_lib_name("a_rust_dylib");
10+
rustc().arg("-Zshare-generics=no").input("a_rust_dylib.rs").run();
11+
12+
let binary_data = rfs::read(&rdylib_name);
13+
let rdylib = File::parse(&*binary_data).unwrap();
14+
15+
// naked should mirror vanilla
16+
not_exported(&rdylib, "private_vanilla");
17+
not_exported(&rdylib, "private_naked");
18+
19+
global_function(&rdylib, "public_vanilla");
20+
global_function(&rdylib, "public_naked");
21+
22+
not_exported(&rdylib, "public_vanilla_generic");
23+
not_exported(&rdylib, "public_naked_generic");
24+
25+
global_function(&rdylib, "vanilla_external_linkage");
26+
global_function(&rdylib, "naked_external_linkage");
27+
28+
// #[linkage = "weak"] does not work well on windows, we get
29+
//
30+
// lib.def : error LNK2001: unresolved external symbol naked_weak_linkage␍
31+
// lib.def : error LNK2001: unresolved external symbol vanilla_weak_linkage
32+
//
33+
// so just skip weak symbols on windows (for now)
34+
if !is_windows() {
35+
weak_function(&rdylib, "vanilla_weak_linkage");
36+
weak_function(&rdylib, "naked_weak_linkage");
37+
}
38+
39+
// functions that are declared in an `extern "C"` block are currently not exported
40+
// this maybe should change in the future, this is just tracking the current behavior
41+
// reported in https://github.com/rust-lang/rust/issues/128071
42+
not_exported(&rdylib, "function_defined_in_global_asm");
43+
44+
// share generics should expose the generic functions
45+
rustc().arg("-Zshare-generics=yes").input("a_rust_dylib.rs").run();
46+
let binary_data = rfs::read(&rdylib_name);
47+
let rdylib = File::parse(&*binary_data).unwrap();
48+
49+
global_function(&rdylib, "public_vanilla_generic");
50+
global_function(&rdylib, "public_naked_generic");
51+
}
52+
53+
#[track_caller]
54+
fn global_function(file: &File, symbol_name: &str) {
55+
let symbols = find_dynamic_symbol(file, symbol_name);
56+
let [symbol] = symbols.as_slice() else {
57+
panic!("symbol {symbol_name} occurs {} times", symbols.len())
58+
};
59+
60+
assert!(symbol.is_definition(), "`{symbol_name}` is not a function");
61+
assert!(symbol.is_global(), "`{symbol_name}` is not marked as global");
62+
}
63+
64+
#[track_caller]
65+
fn weak_function(file: &File, symbol_name: &str) {
66+
let symbols = find_dynamic_symbol(file, symbol_name);
67+
let [symbol] = symbols.as_slice() else {
68+
panic!("symbol {symbol_name} occurs {} times", symbols.len())
69+
};
70+
71+
assert!(symbol.is_definition(), "`{symbol_name}` is not a function");
72+
assert!(symbol.is_weak(), "`{symbol_name}` is not marked as weak");
73+
}
74+
75+
#[track_caller]
76+
fn not_exported(file: &File, symbol_name: &str) {
77+
assert_eq!(find_dynamic_symbol(file, symbol_name).len(), 0)
78+
}
79+
80+
fn find_subsequence(haystack: &[u8], needle: &[u8]) -> bool {
81+
haystack.windows(needle.len()).any(|window| window == needle)
82+
}
83+
84+
fn find_dynamic_symbol<'file, 'data>(
85+
file: &'file File<'data>,
86+
expected: &str,
87+
) -> Vec<Symbol<'data, 'file>> {
88+
file.exports()
89+
.unwrap()
90+
.into_iter()
91+
.filter(|e| find_subsequence(e.name(), expected.as_bytes()))
92+
.filter_map(|e| file.symbol_by_name_bytes(e.name()))
93+
.collect()
94+
}

0 commit comments

Comments
 (0)