From 64759f04a4bbfeb05f48752fe758454992c3c6c6 Mon Sep 17 00:00:00 2001 From: Dan Gohman Date: Fri, 1 Jul 2022 08:41:02 -0700 Subject: [PATCH] Migrate cranelift-jit from `winapi` to `windows-sys` (#4363) * Migrate cranelift-jit from `winapi` to `windows-sys` Following up on #4346, this migrates one more place in the tree from winapi to windows-sys. --- Cargo.lock | 2 +- cranelift/jit/Cargo.toml | 9 +++++++-- cranelift/jit/src/backend.rs | 14 +++++++++----- cranelift/jit/src/memory.rs | 5 +++-- 4 files changed, 20 insertions(+), 10 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index f6d8f01c96..61be157344 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -624,7 +624,7 @@ dependencies = [ "memmap2", "region", "target-lexicon", - "winapi", + "windows-sys", ] [[package]] diff --git a/cranelift/jit/Cargo.toml b/cranelift/jit/Cargo.toml index 69cb479cca..8936cf9260 100644 --- a/cranelift/jit/Cargo.toml +++ b/cranelift/jit/Cargo.toml @@ -21,8 +21,13 @@ target-lexicon = "0.12" memmap2 = { version = "0.2.1", optional = true } log = { version = "0.4.6", default-features = false } -[target.'cfg(target_os = "windows")'.dependencies] -winapi = { version = "0.3", features = ["winbase", "memoryapi"] } +[target.'cfg(windows)'.dependencies.windows-sys] +version = "0.36.0" +features = [ + "Win32_Foundation", + "Win32_System_LibraryLoader", + "Win32_System_Memory", +] [features] selinux-fix = ['memmap2'] diff --git a/cranelift/jit/src/backend.rs b/cranelift/jit/src/backend.rs index 3e0cc7368b..dfc62c4864 100644 --- a/cranelift/jit/src/backend.rs +++ b/cranelift/jit/src/backend.rs @@ -889,6 +889,10 @@ fn lookup_with_dlsym(name: &str) -> Option<*const u8> { #[cfg(windows)] fn lookup_with_dlsym(name: &str) -> Option<*const u8> { + use std::os::windows::io::RawHandle; + use windows_sys::Win32::Foundation::HINSTANCE; + use windows_sys::Win32::System::LibraryLoader; + const MSVCRT_DLL: &[u8] = b"msvcrt.dll\0"; let c_str = CString::new(name).unwrap(); @@ -899,15 +903,15 @@ fn lookup_with_dlsym(name: &str) -> Option<*const u8> { // try to find the searched symbol in the currently running executable ptr::null_mut(), // try to find the searched symbol in local c runtime - winapi::um::libloaderapi::GetModuleHandleA(MSVCRT_DLL.as_ptr().cast::()), + LibraryLoader::GetModuleHandleA(MSVCRT_DLL.as_ptr()) as RawHandle, ]; for handle in &handles { - let addr = winapi::um::libloaderapi::GetProcAddress(*handle, c_str_ptr); - if addr.is_null() { - continue; + let addr = LibraryLoader::GetProcAddress(*handle as HINSTANCE, c_str_ptr.cast()); + match addr { + None => continue, + Some(addr) => return Some(addr as *const u8), } - return Some(addr as *const u8); } None diff --git a/cranelift/jit/src/memory.rs b/cranelift/jit/src/memory.rs index c183da9db2..02f274c72f 100644 --- a/cranelift/jit/src/memory.rs +++ b/cranelift/jit/src/memory.rs @@ -62,8 +62,9 @@ impl PtrLen { #[cfg(target_os = "windows")] fn with_size(size: usize) -> io::Result { - use winapi::um::memoryapi::VirtualAlloc; - use winapi::um::winnt::{MEM_COMMIT, MEM_RESERVE, PAGE_READWRITE}; + use windows_sys::Win32::System::Memory::{ + VirtualAlloc, MEM_COMMIT, MEM_RESERVE, PAGE_READWRITE, + }; // VirtualAlloc always rounds up to the next multiple of the page size let ptr = unsafe {