-
Notifications
You must be signed in to change notification settings - Fork 12.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add test for symbol visibility of #[naked]
functions
#128362
Merged
bors
merged 1 commit into
rust-lang:master
from
folkertdev:naked-function-symbol-visibility
Aug 7, 2024
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
#![feature(naked_functions, asm_const, linkage)] | ||
#![crate_type = "dylib"] | ||
|
||
use std::arch::asm; | ||
|
||
pub trait TraitWithConst { | ||
const COUNT: u32; | ||
} | ||
|
||
struct Test; | ||
|
||
impl TraitWithConst for Test { | ||
const COUNT: u32 = 1; | ||
} | ||
|
||
#[no_mangle] | ||
fn entry() { | ||
private_vanilla(); | ||
private_naked(); | ||
|
||
public_vanilla_generic::<Test>(); | ||
public_naked_generic::<Test>(); | ||
} | ||
|
||
extern "C" fn private_vanilla() -> u32 { | ||
42 | ||
} | ||
|
||
#[naked] | ||
extern "C" fn private_naked() -> u32 { | ||
unsafe { asm!("mov rax, 42", "ret", options(noreturn)) } | ||
} | ||
|
||
#[no_mangle] | ||
pub extern "C" fn public_vanilla() -> u32 { | ||
42 | ||
} | ||
|
||
#[naked] | ||
#[no_mangle] | ||
pub extern "C" fn public_naked() -> u32 { | ||
unsafe { asm!("mov rax, 42", "ret", options(noreturn)) } | ||
} | ||
|
||
pub extern "C" fn public_vanilla_generic<T: TraitWithConst>() -> u32 { | ||
T::COUNT | ||
} | ||
|
||
#[naked] | ||
pub extern "C" fn public_naked_generic<T: TraitWithConst>() -> u32 { | ||
unsafe { asm!("mov rax, {}", "ret", const T::COUNT, options(noreturn)) } | ||
} | ||
|
||
#[linkage = "external"] | ||
extern "C" fn vanilla_external_linkage() -> u32 { | ||
42 | ||
} | ||
|
||
#[naked] | ||
#[linkage = "external"] | ||
extern "C" fn naked_external_linkage() -> u32 { | ||
unsafe { asm!("mov rax, 42", "ret", options(noreturn)) } | ||
} | ||
|
||
#[cfg(not(windows))] | ||
#[linkage = "weak"] | ||
extern "C" fn vanilla_weak_linkage() -> u32 { | ||
42 | ||
} | ||
|
||
#[naked] | ||
#[cfg(not(windows))] | ||
#[linkage = "weak"] | ||
extern "C" fn naked_weak_linkage() -> u32 { | ||
unsafe { asm!("mov rax, 42", "ret", options(noreturn)) } | ||
} | ||
|
||
// functions that are declared in an `extern "C"` block are currently not exported | ||
// this maybe should change in the future, this is just tracking the current behavior | ||
// reported in https://github.com/rust-lang/rust/issues/128071 | ||
std::arch::global_asm! { | ||
".globl function_defined_in_global_asm", | ||
"function_defined_in_global_asm:", | ||
"ret", | ||
} | ||
|
||
extern "C" { | ||
pub fn function_defined_in_global_asm(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
//@ ignore-windows | ||
//@ only-x86_64 | ||
use run_make_support::object::read::{File, Object, Symbol}; | ||
use run_make_support::object::ObjectSymbol; | ||
use run_make_support::targets::is_windows; | ||
use run_make_support::{dynamic_lib_name, env_var, rfs, rustc}; | ||
|
||
fn main() { | ||
let rdylib_name = dynamic_lib_name("a_rust_dylib"); | ||
rustc().arg("-Zshare-generics=no").input("a_rust_dylib.rs").run(); | ||
|
||
let binary_data = rfs::read(&rdylib_name); | ||
let rdylib = File::parse(&*binary_data).unwrap(); | ||
|
||
// naked should mirror vanilla | ||
not_exported(&rdylib, "private_vanilla"); | ||
not_exported(&rdylib, "private_naked"); | ||
|
||
global_function(&rdylib, "public_vanilla"); | ||
global_function(&rdylib, "public_naked"); | ||
|
||
not_exported(&rdylib, "public_vanilla_generic"); | ||
not_exported(&rdylib, "public_naked_generic"); | ||
|
||
global_function(&rdylib, "vanilla_external_linkage"); | ||
global_function(&rdylib, "naked_external_linkage"); | ||
|
||
// FIXME: make this work on windows (gnu and msvc). See the PR | ||
// https://github.com/rust-lang/rust/pull/128362 for some approaches | ||
// that don't work | ||
// | ||
// #[linkage = "weak"] does not work well on windows, we get | ||
// | ||
// lib.def : error LNK2001: unresolved external symbol naked_weak_linkage␍ | ||
// lib.def : error LNK2001: unresolved external symbol vanilla_weak_linkage | ||
// | ||
// so just skip weak symbols on windows (for now) | ||
if !is_windows() { | ||
weak_function(&rdylib, "vanilla_weak_linkage"); | ||
weak_function(&rdylib, "naked_weak_linkage"); | ||
} | ||
folkertdev marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// functions that are declared in an `extern "C"` block are currently not exported | ||
// this maybe should change in the future, this is just tracking the current behavior | ||
// reported in https://github.com/rust-lang/rust/issues/128071 | ||
not_exported(&rdylib, "function_defined_in_global_asm"); | ||
|
||
// share generics should expose the generic functions | ||
rustc().arg("-Zshare-generics=yes").input("a_rust_dylib.rs").run(); | ||
let binary_data = rfs::read(&rdylib_name); | ||
let rdylib = File::parse(&*binary_data).unwrap(); | ||
|
||
global_function(&rdylib, "public_vanilla_generic"); | ||
global_function(&rdylib, "public_naked_generic"); | ||
} | ||
|
||
#[track_caller] | ||
fn global_function(file: &File, symbol_name: &str) { | ||
let symbols = find_dynamic_symbol(file, symbol_name); | ||
let [symbol] = symbols.as_slice() else { | ||
panic!("symbol {symbol_name} occurs {} times", symbols.len()) | ||
}; | ||
|
||
assert!(symbol.is_definition(), "`{symbol_name}` is not a function"); | ||
assert!(symbol.is_global(), "`{symbol_name}` is not marked as global"); | ||
} | ||
|
||
#[track_caller] | ||
fn weak_function(file: &File, symbol_name: &str) { | ||
let symbols = find_dynamic_symbol(file, symbol_name); | ||
let [symbol] = symbols.as_slice() else { | ||
panic!("symbol {symbol_name} occurs {} times", symbols.len()) | ||
}; | ||
|
||
assert!(symbol.is_definition(), "`{symbol_name}` is not a function"); | ||
assert!(symbol.is_weak(), "`{symbol_name}` is not marked as weak"); | ||
} | ||
|
||
#[track_caller] | ||
fn not_exported(file: &File, symbol_name: &str) { | ||
assert_eq!(find_dynamic_symbol(file, symbol_name).len(), 0) | ||
} | ||
|
||
fn find_subsequence(haystack: &[u8], needle: &[u8]) -> bool { | ||
haystack.windows(needle.len()).any(|window| window == needle) | ||
} | ||
|
||
fn find_dynamic_symbol<'file, 'data>( | ||
file: &'file File<'data>, | ||
expected: &str, | ||
) -> Vec<Symbol<'data, 'file>> { | ||
file.exports() | ||
.unwrap() | ||
.into_iter() | ||
.filter(|e| find_subsequence(e.name(), expected.as_bytes())) | ||
.filter_map(|e| file.symbol_by_name_bytes(e.name())) | ||
.collect() | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is the
only-x86_64
really necessary?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, the inline/global assembly will break on aarch64 right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right