Skip to content

Commit

Permalink
Merge pull request #5001 from ognevny/use-windows-sys
Browse files Browse the repository at this point in the history
deps: use windows-sys instead of winapi
  • Loading branch information
syrusakbary authored Aug 11, 2024
2 parents c0122bc + 8941977 commit 56894ff
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 34 deletions.
17 changes: 13 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ wasmparser = { workspace = true, default-features = false, optional = true }

# - Mandatory dependencies for `sys` on Windows.
[target.'cfg(all(not(target_arch = "wasm32"), target_os = "windows"))'.dependencies]
winapi = "0.3"
windows-sys = "0.59"
# - Development Dependencies for `sys`.
[target.'cfg(not(target_arch = "wasm32"))'.dev-dependencies]
wat = "1.0"
Expand Down
2 changes: 1 addition & 1 deletion lib/compiler/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ wasmer-vm = { path = "../vm", version = "=4.3.5" }
region = { version = "3.0" }

[target.'cfg(target_os = "windows")'.dependencies]
winapi = { version = "0.3", features = ["winnt", "impl-default"] }
windows-sys = { version = "0.59", features = ["Win32_System_Diagnostics_Debug"] }

[features]
default = ["std"]
Expand Down
16 changes: 8 additions & 8 deletions lib/compiler/src/engine/unwind/windows_x64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@
//! Module for Windows x64 ABI unwind registry.
use std::collections::HashMap;
use wasmer_types::CompiledFunctionUnwindInfoReference;
use winapi::um::winnt;
use windows_sys::Win32::System::Diagnostics::Debug::{
RtlAddFunctionTable, RtlDeleteFunctionTable, IMAGE_RUNTIME_FUNCTION_ENTRY,
};

/// Represents a registry of function unwind information for Windows x64 ABI.
pub struct UnwindRegistry {
// A hashmap mapping the baseaddress with the registered runtime functions
functions: HashMap<usize, Vec<winnt::RUNTIME_FUNCTION>>,
functions: HashMap<usize, Vec<IMAGE_RUNTIME_FUNCTION_ENTRY>>,
published: bool,
}

Expand Down Expand Up @@ -39,16 +41,14 @@ impl UnwindRegistry {
_ => return Err("unsupported unwind information".to_string()),
};

let mut entry = winnt::RUNTIME_FUNCTION::default();
let mut entry: IMAGE_RUNTIME_FUNCTION_ENTRY = unsafe { std::mem::zeroed() };

entry.BeginAddress = func_start;
entry.EndAddress = func_start + func_len;

// The unwind information should be immediately following the function
// with padding for 4 byte alignment
unsafe {
*entry.u.UnwindInfoAddress_mut() = (entry.EndAddress + 3) & !3;
}
entry.Anonymous.UnwindInfoAddress = (entry.EndAddress + 3) & !3;
let entries = self.functions.entry(base_address).or_insert_with(Vec::new);

entries.push(entry);
Expand All @@ -73,7 +73,7 @@ impl UnwindRegistry {
"function table allocation was not aligned"
);
unsafe {
if winnt::RtlAddFunctionTable(
if RtlAddFunctionTable(
functions.as_mut_ptr(),
functions.len() as u32,
*base_address as u64,
Expand All @@ -94,7 +94,7 @@ impl Drop for UnwindRegistry {
if self.published {
unsafe {
for functions in self.functions.values_mut() {
winnt::RtlDeleteFunctionTable(functions.as_mut_ptr());
RtlDeleteFunctionTable(functions.as_mut_ptr());
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion lib/vm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ crossbeam-queue = "0.3.8"
mach2 = "0.4.2"

[target.'cfg(target_os = "windows")'.dependencies]
winapi = { version = "0.3", features = ["winbase", "memoryapi", "errhandlingapi"] }
windows-sys = { version = "0.59", features = ["Win32_System_Diagnostics_Debug", "Win32_System_Threading", "Win32_System_Kernel", "Win32_System_Memory"] }

[build-dependencies]
cc = "1.0"
Expand Down
15 changes: 7 additions & 8 deletions lib/vm/src/mmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,9 @@ impl Mmap {
_backing_file: Option<std::path::PathBuf>,
_memory_type: MmapType,
) -> Result<Self, String> {
use winapi::um::memoryapi::VirtualAlloc;
use winapi::um::winnt::{MEM_COMMIT, MEM_RESERVE, PAGE_NOACCESS, PAGE_READWRITE};
use windows_sys::Win32::System::Memory::{
VirtualAlloc, MEM_COMMIT, MEM_RESERVE, PAGE_NOACCESS, PAGE_READWRITE,
};

let page_size = region::page::size();
assert_le!(accessible_size, mapping_size);
Expand Down Expand Up @@ -272,9 +273,8 @@ impl Mmap {
/// `self`'s reserved memory.
#[cfg(target_os = "windows")]
pub fn make_accessible(&mut self, start: usize, len: usize) -> Result<(), String> {
use winapi::ctypes::c_void;
use winapi::um::memoryapi::VirtualAlloc;
use winapi::um::winnt::{MEM_COMMIT, PAGE_READWRITE};
use std::ffi::c_void;
use windows_sys::Win32::System::Memory::{VirtualAlloc, MEM_COMMIT, PAGE_READWRITE};
let page_size = region::page::size();
assert_eq!(start & (page_size - 1), 0);
assert_eq!(len & (page_size - 1), 0);
Expand Down Expand Up @@ -396,9 +396,8 @@ impl Drop for Mmap {
#[cfg(target_os = "windows")]
fn drop(&mut self) {
if self.len() != 0 {
use winapi::ctypes::c_void;
use winapi::um::memoryapi::VirtualFree;
use winapi::um::winnt::MEM_RELEASE;
use std::ffi::c_void;
use windows_sys::Win32::System::Memory::{VirtualFree, MEM_RELEASE};
let r = unsafe { VirtualFree(self.ptr as *mut c_void, 0, MEM_RELEASE) };
assert_ne!(r, 0);
}
Expand Down
26 changes: 18 additions & 8 deletions lib/vm/src/trap/traphandlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ cfg_if::cfg_if! {
pub type TrapHandlerFn<'a> = dyn Fn(libc::c_int, *const libc::siginfo_t, *const libc::c_void) -> bool + Send + Sync + 'a;
} else if #[cfg(target_os = "windows")] {
/// Function which may handle custom signals while processing traps.
pub type TrapHandlerFn<'a> = dyn Fn(winapi::um::winnt::PEXCEPTION_POINTERS) -> bool + Send + Sync + 'a;
pub type TrapHandlerFn<'a> = dyn Fn(*mut windows_sys::Win32::System::Diagnostics::Debug::EXCEPTION_POINTERS) -> bool + Send + Sync + 'a;
}
}

Expand Down Expand Up @@ -465,10 +465,20 @@ cfg_if::cfg_if! {
};
}
} else if #[cfg(target_os = "windows")] {
use winapi::um::errhandlingapi::*;
use winapi::um::winnt::*;
use winapi::um::minwinbase::*;
use winapi::vc::excpt::*;
use windows_sys::Win32::System::Diagnostics::Debug::{
AddVectoredExceptionHandler,
CONTEXT,
EXCEPTION_CONTINUE_EXECUTION,
EXCEPTION_CONTINUE_SEARCH,
EXCEPTION_POINTERS,
};
use windows_sys::Win32::Foundation::{
EXCEPTION_ACCESS_VIOLATION,
EXCEPTION_ILLEGAL_INSTRUCTION,
EXCEPTION_INT_DIVIDE_BY_ZERO,
EXCEPTION_INT_OVERFLOW,
EXCEPTION_STACK_OVERFLOW,
};

unsafe fn platform_init() {
// our trap handler needs to go first, so that we can recover from
Expand All @@ -480,8 +490,8 @@ cfg_if::cfg_if! {
}

unsafe extern "system" fn exception_handler(
exception_info: PEXCEPTION_POINTERS
) -> LONG {
exception_info: *mut EXCEPTION_POINTERS
) -> i32 {
// Check the kind of exception, since we only handle a subset within
// wasm code. If anything else happens we want to defer to whatever
// the rest of the system wants to do for this exception.
Expand Down Expand Up @@ -993,7 +1003,7 @@ pub fn lazy_per_thread_init() -> Result<(), Trap> {
// We need additional space on the stack to handle stack overflow
// exceptions. Rust's initialization code sets this to 0x5000 but this
// seems to be insufficient in practice.
use winapi::um::processthreadsapi::SetThreadStackGuarantee;
use windows_sys::Win32::System::Threading::SetThreadStackGuarantee;
if unsafe { SetThreadStackGuarantee(&mut 0x10000) } == 0 {
panic!("failed to set thread stack guarantee");
}
Expand Down
4 changes: 2 additions & 2 deletions lib/wasix/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ async-trait = { version = "^0.1" }
urlencoding = { version = "^2" }
serde_derive = { version = "^1" }
serde_json = { version = "^1" }
serde_yaml.workspace = true
serde_yaml.workspace = true
weezl = { version = "^0.1" }
hex = { version = "^0.4" }
linked_hash_set = { version = "0.1" }
Expand Down Expand Up @@ -124,7 +124,7 @@ libc.workspace = true
termios = { version = "0.3" }

[target.'cfg(windows)'.dependencies]
winapi = { version = "0.3", features = ["sysinfoapi"] }
windows-sys = { version = "0.59", features = ["Win32_System_SystemInformation"] }

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
terminal_size = { version = "0.3.0" }
Expand Down
3 changes: 2 additions & 1 deletion lib/wasix/src/syscalls/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ pub fn platform_clock_time_get(
) -> Result<i64, wasi::Errno> {
let nanos = match clock_id {
wasi::Snapshot0Clockid::Monotonic => {
let tick_ms = unsafe { winapi::um::sysinfoapi::GetTickCount64() };
let tick_ms =
unsafe { windows_sys::Win32::System::SystemInformation::GetTickCount64() };
tick_ms * 1_000_000
}
wasi::Snapshot0Clockid::Realtime => {
Expand Down

0 comments on commit 56894ff

Please sign in to comment.