Skip to content

Commit d7ddc3a

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

File tree

2 files changed

+177
-0
lines changed

2 files changed

+177
-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,88 @@
1+
//@ only-x86_64
2+
use run_make_support::object::read::{File, Object, Symbol};
3+
use run_make_support::object::ObjectSymbol;
4+
use run_make_support::targets::is_windows;
5+
use run_make_support::{dynamic_lib_name, env_var, rfs, rustc};
6+
7+
fn main() {
8+
let rdylib_name = dynamic_lib_name("a_rust_dylib");
9+
rustc().arg("-Zshare-generics=no").input("a_rust_dylib.rs").run();
10+
11+
let binary_data = rfs::read(&rdylib_name);
12+
let rdylib = File::parse(&*binary_data).unwrap();
13+
14+
// naked should mirror vanilla
15+
not_exported(&rdylib, "private_vanilla");
16+
not_exported(&rdylib, "private_naked");
17+
18+
global_function(&rdylib, "public_vanilla");
19+
global_function(&rdylib, "public_naked");
20+
21+
not_exported(&rdylib, "public_vanilla_generic");
22+
not_exported(&rdylib, "public_naked_generic");
23+
24+
global_function(&rdylib, "vanilla_external_linkage");
25+
global_function(&rdylib, "naked_external_linkage");
26+
27+
// #[linkage = "weak"] does not work well on windows, we get
28+
//
29+
// lib.def : error LNK2001: unresolved external symbol naked_weak_linkage␍
30+
// lib.def : error LNK2001: unresolved external symbol vanilla_weak_linkage
31+
32+
if !is_windows() {
33+
weak_function(&rdylib, "vanilla_weak_linkage");
34+
weak_function(&rdylib, "naked_weak_linkage");
35+
}
36+
37+
// functions that are declared in an `extern "C"` block are currently not exported
38+
// this maybe should change in the future, this is just tracking the current behavior
39+
// reported in https://github.com/rust-lang/rust/issues/128071
40+
not_exported(&rdylib, "function_defined_in_global_asm");
41+
42+
// share generics should expose the generic functions
43+
rustc().arg("-Zshare-generics=yes").input("a_rust_dylib.rs").run();
44+
let binary_data = rfs::read(&rdylib_name);
45+
let rdylib = File::parse(&*binary_data).unwrap();
46+
47+
global_function(&rdylib, "public_vanilla_generic");
48+
global_function(&rdylib, "public_naked_generic");
49+
}
50+
51+
#[track_caller]
52+
fn global_function(file: &File, symbol_name: &str) {
53+
let symbols = find_dynamic_symbol(file, symbol_name);
54+
let [symbol] = symbols.as_slice() else {
55+
panic!("symbol {symbol_name} occurs {} times", symbols.len())
56+
};
57+
58+
assert!(symbol.is_definition(), "`{symbol_name}` is not a function");
59+
assert!(symbol.is_global(), "`{symbol_name}` is not marked as global");
60+
}
61+
62+
#[track_caller]
63+
fn weak_function(file: &File, symbol_name: &str) {
64+
let symbols = find_dynamic_symbol(file, symbol_name);
65+
let [symbol] = symbols.as_slice() else {
66+
panic!("symbol {symbol_name} occurs {} times", symbols.len())
67+
};
68+
69+
assert!(symbol.is_definition(), "`{symbol_name}` is not a function");
70+
assert!(symbol.is_weak(), "`{symbol_name}` is not marked as weak");
71+
}
72+
73+
#[track_caller]
74+
fn not_exported(file: &File, symbol_name: &str) {
75+
assert_eq!(find_dynamic_symbol(file, symbol_name).len(), 0)
76+
}
77+
78+
fn find_dynamic_symbol<'file, 'data>(
79+
file: &'file File<'data>,
80+
expected: &str,
81+
) -> Vec<Symbol<'data, 'file>> {
82+
file.dynamic_symbols()
83+
.filter(|symbol| {
84+
let name = symbol.name().unwrap();
85+
!name.contains("__imp_") && name.contains(expected)
86+
})
87+
.collect()
88+
}

0 commit comments

Comments
 (0)