Skip to content
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

make windows compat_fn (crudely) work on Miri #95775

Merged
merged 2 commits into from
Apr 8, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 17 additions & 8 deletions library/std/src/sys/windows/compat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ macro_rules! compat_fn {
static INIT_TABLE_ENTRY: unsafe extern "C" fn() = init;

unsafe extern "C" fn init() {
PTR = get_f();
}

unsafe extern "C" fn get_f() -> Option<F> {
// There is no locking here. This code is executed before main() is entered, and
// is guaranteed to be single-threaded.
//
Expand All @@ -88,13 +92,13 @@ macro_rules! compat_fn {
let symbol_name: *const u8 = concat!(stringify!($symbol), "\0").as_ptr();
let module_handle = $crate::sys::c::GetModuleHandleA(module_name as *const i8);
if !module_handle.is_null() {
match $crate::sys::c::GetProcAddress(module_handle, symbol_name as *const i8).addr() {
0 => {}
n => {
PTR = Some(mem::transmute::<usize, F>(n));
}
let ptr = $crate::sys::c::GetProcAddress(module_handle, symbol_name as *const i8);
if !ptr.is_null() {
// Transmute to the right function pointer type.
return Some(mem::transmute(ptr));
}
}
return None;
}

#[allow(dead_code)]
Expand All @@ -105,10 +109,15 @@ macro_rules! compat_fn {
#[allow(dead_code)]
pub unsafe fn call($($argname: $argtype),*) -> $rettype {
if let Some(ptr) = PTR {
ptr($($argname),*)
} else {
$fallback_body
return ptr($($argname),*);
}
if cfg!(miri) {
// Miri does not run `init`, so we just call `get_f` each time.
if let Some(ptr) = get_f() {
return ptr($($argname),*);
}
}
$fallback_body
}
}

Expand Down