Skip to content
Merged
Show file tree
Hide file tree
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
Empty file modified .github/workflows/ci-preemptive.sh
100755 → 100644
Empty file.
Empty file modified .github/workflows/ci.sh
100755 → 100644
Empty file.
2 changes: 2 additions & 0 deletions core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,12 @@ io-uring = { workspace = true, optional = true }

[target.'cfg(windows)'.dependencies]
windows-sys = { workspace = true, features = [
"Win32_Security",
"Win32_System_IO",
"Win32_Foundation",
"Win32_System_Kernel",
"Win32_System_Threading",
"Win32_Storage_FileSystem",
"Win32_Networking_WinSock",
"Win32_System_SystemInformation",
"Win32_System_Diagnostics_Debug",
Expand Down
6 changes: 6 additions & 0 deletions core/src/common/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,12 @@ pub enum Syscall {
pthread_mutex_trylock,
pthread_mutex_lock,
pthread_mutex_unlock,
#[cfg(windows)]
CreateFileW,
#[cfg(windows)]
SetFilePointerEx,
#[cfg(windows)]
WaitOnAddress,
}

impl Syscall {
Expand Down
10 changes: 10 additions & 0 deletions core/src/syscall/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,16 @@ pub extern "C" fn reset_errno() {
set_errno(0);
}

#[macro_export]
macro_rules! syscall_mod {
($($mod_name: ident);*) => {
$(
pub use $mod_name::$mod_name;
mod $mod_name;
)*
}
}

#[macro_export]
macro_rules! log_syscall {
( $socket:expr, $done:expr, $once_result:expr ) => {
Expand Down
10 changes: 1 addition & 9 deletions core/src/syscall/unix/mod.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
use crate::syscall_mod;
use std::ffi::c_int;

macro_rules! syscall_mod {
($($mod_name: ident);*) => {
$(
pub use $mod_name::$mod_name;
mod $mod_name;
)*
}
}

macro_rules! impl_facade {
( $struct_name:ident, $trait_name: ident, $syscall: ident($($arg: ident : $arg_type: ty),*) -> $result: ty ) => {
#[repr(C)]
Expand Down
6 changes: 5 additions & 1 deletion core/src/syscall/unix/pthread_cond_timedwait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ impl<I: PthreadCondTimedwaitSyscall> PthreadCondTimedwaitSyscall
.unwrap_or(u64::MAX)
};
loop {
let mut left_time = abstimeout.saturating_sub(now());
if 0 == left_time {
return libc::ETIMEDOUT;
}
let r = self.inner.pthread_cond_timedwait(
fn_ptr,
cond,
Expand All @@ -83,7 +87,7 @@ impl<I: PthreadCondTimedwaitSyscall> PthreadCondTimedwaitSyscall
if libc::ETIMEDOUT != r {
return r;
}
let left_time = abstimeout.saturating_sub(now());
left_time = abstimeout.saturating_sub(now());
if 0 == left_time {
return libc::ETIMEDOUT;
}
Expand Down
91 changes: 91 additions & 0 deletions core/src/syscall/windows/CreateFileW.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
use std::ffi::c_uint;
use once_cell::sync::Lazy;
use windows_sys::core::PCWSTR;
use windows_sys::Win32::Foundation::HANDLE;
use windows_sys::Win32::Security::SECURITY_ATTRIBUTES;
use windows_sys::Win32::Storage::FileSystem::{
FILE_CREATION_DISPOSITION, FILE_FLAGS_AND_ATTRIBUTES, FILE_SHARE_MODE,
};

#[must_use]
pub extern "system" fn CreateFileW(
fn_ptr: Option<
&extern "system" fn(
PCWSTR,
c_uint,
FILE_SHARE_MODE,
*const SECURITY_ATTRIBUTES,
FILE_CREATION_DISPOSITION,
FILE_FLAGS_AND_ATTRIBUTES,
HANDLE,
) -> HANDLE,
>,
lpfilename: PCWSTR,
dwdesiredaccess: c_uint,
dwsharemode: FILE_SHARE_MODE,
lpsecurityattributes: *const SECURITY_ATTRIBUTES,
dwcreationdisposition: FILE_CREATION_DISPOSITION,
dwflagsandattributes: FILE_FLAGS_AND_ATTRIBUTES,
htemplatefile: HANDLE,
) -> HANDLE {
static CHAIN: Lazy<CreateFileWSyscallFacade<RawCreateFileWSyscall>> =
Lazy::new(Default::default);
CHAIN.CreateFileW(
fn_ptr,
lpfilename,
dwdesiredaccess,
dwsharemode,
lpsecurityattributes,
dwcreationdisposition,
dwflagsandattributes,
htemplatefile
)
}

trait CreateFileWSyscall {
extern "system" fn CreateFileW(
&self,
fn_ptr: Option<
&extern "system" fn(
PCWSTR,
c_uint,
FILE_SHARE_MODE,
*const SECURITY_ATTRIBUTES,
FILE_CREATION_DISPOSITION,
FILE_FLAGS_AND_ATTRIBUTES,
HANDLE,
) -> HANDLE,
>,
lpfilename: PCWSTR,
dwdesiredaccess: c_uint,
dwsharemode: FILE_SHARE_MODE,
lpsecurityattributes: *const SECURITY_ATTRIBUTES,
dwcreationdisposition: FILE_CREATION_DISPOSITION,
dwflagsandattributes: FILE_FLAGS_AND_ATTRIBUTES,
htemplatefile: HANDLE,
) -> HANDLE;
}

impl_facade!(CreateFileWSyscallFacade, CreateFileWSyscall,
CreateFileW(
lpfilename: PCWSTR,
dwdesiredaccess: c_uint,
dwsharemode: FILE_SHARE_MODE,
lpsecurityattributes: *const SECURITY_ATTRIBUTES,
dwcreationdisposition: FILE_CREATION_DISPOSITION,
dwflagsandattributes: FILE_FLAGS_AND_ATTRIBUTES,
htemplatefile: HANDLE
) -> HANDLE
);

impl_raw!(RawCreateFileWSyscall, CreateFileWSyscall, windows_sys::Win32::Storage::FileSystem,
CreateFileW(
lpfilename: PCWSTR,
dwdesiredaccess: c_uint,
dwsharemode: FILE_SHARE_MODE,
lpsecurityattributes: *const SECURITY_ATTRIBUTES,
dwcreationdisposition: FILE_CREATION_DISPOSITION,
dwflagsandattributes: FILE_FLAGS_AND_ATTRIBUTES,
htemplatefile: HANDLE
) -> HANDLE
);
46 changes: 46 additions & 0 deletions core/src/syscall/windows/SetFilePointerEx.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
use std::ffi::c_longlong;
use once_cell::sync::Lazy;
use windows_sys::Win32::Foundation::{BOOL, HANDLE};
use windows_sys::Win32::Storage::FileSystem::SET_FILE_POINTER_MOVE_METHOD;

#[must_use]
pub extern "system" fn SetFilePointerEx(
fn_ptr: Option<&extern "system" fn(HANDLE, c_longlong, *mut c_longlong, SET_FILE_POINTER_MOVE_METHOD) -> BOOL>,
hfile: HANDLE,
lidistancetomove: c_longlong,
lpnewfilepointer : *mut c_longlong,
dwmovemethod : SET_FILE_POINTER_MOVE_METHOD
) -> BOOL {
static CHAIN: Lazy<SetFilePointerExSyscallFacade<RawSetFilePointerExSyscall>> =
Lazy::new(Default::default);
CHAIN.SetFilePointerEx(fn_ptr, hfile, lidistancetomove, lpnewfilepointer, dwmovemethod)
}

trait SetFilePointerExSyscall {
extern "system" fn SetFilePointerEx(
&self,
fn_ptr: Option<&extern "system" fn(HANDLE, c_longlong, *mut c_longlong, SET_FILE_POINTER_MOVE_METHOD) -> BOOL>,
hfile: HANDLE,
lidistancetomove: c_longlong,
lpnewfilepointer : *mut c_longlong,
dwmovemethod : SET_FILE_POINTER_MOVE_METHOD
) -> BOOL;
}

impl_facade!(SetFilePointerExSyscallFacade, SetFilePointerExSyscall,
SetFilePointerEx(
hfile: HANDLE,
lidistancetomove: c_longlong,
lpnewfilepointer : *mut c_longlong,
dwmovemethod : SET_FILE_POINTER_MOVE_METHOD
) -> BOOL
);

impl_raw!(RawSetFilePointerExSyscall, SetFilePointerExSyscall, windows_sys::Win32::Storage::FileSystem,
SetFilePointerEx(
hfile: HANDLE,
lidistancetomove: c_longlong,
lpnewfilepointer : *mut c_longlong,
dwmovemethod : SET_FILE_POINTER_MOVE_METHOD
) -> BOOL
);
105 changes: 105 additions & 0 deletions core/src/syscall/windows/WaitOnAddress.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
use std::ffi::{c_uint, c_void};
use std::time::Duration;
use once_cell::sync::Lazy;
use windows_sys::Win32::Foundation::{BOOL, ERROR_TIMEOUT, FALSE, TRUE};
use crate::common::{get_timeout_time, now};
use crate::net::EventLoops;
use crate::syscall::common::reset_errno;
use crate::syscall::set_errno;

#[must_use]
pub extern "system" fn WaitOnAddress(
fn_ptr: Option<&extern "system" fn(*const c_void, *const c_void, usize, c_uint) -> BOOL>,
address: *const c_void,
compareaddress: *const c_void,
addresssize: usize,
dwmilliseconds: c_uint
) -> BOOL {
static CHAIN: Lazy<WaitOnAddressSyscallFacade<NioWaitOnAddressSyscall<RawWaitOnAddressSyscall>>> =
Lazy::new(Default::default);
CHAIN.WaitOnAddress(fn_ptr, address, compareaddress, addresssize, dwmilliseconds)
}

trait WaitOnAddressSyscall {
extern "system" fn WaitOnAddress(
&self,
fn_ptr: Option<&extern "system" fn(*const c_void, *const c_void, usize, c_uint) -> BOOL>,
address: *const c_void,
compareaddress: *const c_void,
addresssize: usize,
dwmilliseconds: c_uint
) -> BOOL;
}

impl_facade!(WaitOnAddressSyscallFacade, WaitOnAddressSyscall,
WaitOnAddress(
address: *const c_void,
compareaddress: *const c_void,
addresssize: usize,
dwmilliseconds: c_uint
) -> BOOL
);

#[repr(C)]
#[derive(Debug, Default)]
struct NioWaitOnAddressSyscall<I: WaitOnAddressSyscall> {
inner: I,
}

impl<I: WaitOnAddressSyscall> WaitOnAddressSyscall for NioWaitOnAddressSyscall<I> {
extern "system" fn WaitOnAddress(
&self,
fn_ptr: Option<&extern "system" fn(*const c_void, *const c_void, usize, c_uint) -> BOOL>,
address: *const c_void,
compareaddress: *const c_void,
addresssize: usize,
dwmilliseconds: c_uint
) -> BOOL {
let timeout = get_timeout_time(Duration::from_millis(dwmilliseconds.into()));
loop {
let mut left_time = timeout.saturating_sub(now());
if 0 == left_time {
set_errno(ERROR_TIMEOUT);
return FALSE;
}
let r = self.inner.WaitOnAddress(
fn_ptr,
address,
compareaddress,
addresssize,
(left_time / 1_000_000).min(1).try_into().expect("overflow"),
);
if TRUE == r {
reset_errno();
return r;
}
left_time = timeout.saturating_sub(now());
if 0 == left_time {
set_errno(ERROR_TIMEOUT);
return FALSE;
}
let wait_time = if left_time > 10_000_000 {
10_000_000
} else {
left_time
};
if EventLoops::wait_event(Some(Duration::new(
wait_time / 1_000_000_000,
(wait_time % 1_000_000_000) as _,
)))
.is_err()
{
return r;
}
}
}
}

impl_raw!(RawWaitOnAddressSyscall, WaitOnAddressSyscall, windows_sys::Win32::System::Threading,
WaitOnAddress(
address: *const c_void,
compareaddress: *const c_void,
addresssize: usize,
dwmilliseconds: c_uint
) -> BOOL
);
40 changes: 17 additions & 23 deletions core/src/syscall/windows/mod.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,8 @@
use crate::syscall_mod;
use dashmap::DashSet;
use once_cell::sync::Lazy;
use windows_sys::Win32::Networking::WinSock::SOCKET;

pub use accept::accept;
pub use ioctlsocket::ioctlsocket;
pub use listen::listen;
pub use recv::recv;
pub use send::send;
pub use shutdown::shutdown;
pub use socket::socket;
pub use Sleep::Sleep;
pub use WSARecv::WSARecv;
pub use WSASend::WSASend;
pub use WSASocketW::WSASocketW;

macro_rules! impl_facade {
( $struct_name:ident, $trait_name: ident, $syscall: ident($($arg: ident : $arg_type: ty),*) -> $result: ty ) => {
#[repr(C)]
Expand Down Expand Up @@ -450,17 +439,22 @@ macro_rules! impl_raw {
}
}

mod Sleep;
mod WSARecv;
mod WSASend;
mod WSASocketW;
mod accept;
mod ioctlsocket;
mod listen;
mod recv;
mod send;
mod shutdown;
mod socket;
syscall_mod!(
Sleep;
WSARecv;
WSASend;
WSASocketW;
accept;
ioctlsocket;
listen;
recv;
send;
shutdown;
socket;
CreateFileW;
SetFilePointerEx;
WaitOnAddress
);

static NON_BLOCKING: Lazy<DashSet<SOCKET>> = Lazy::new(Default::default);

Expand Down
7 changes: 4 additions & 3 deletions hook/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@ libc.workspace = true

[target.'cfg(windows)'.dependencies]
windows-sys = { workspace = true, features = [
"Win32_Security",
"Win32_Foundation",
"Win32_System_Diagnostics_Debug",
"Win32_System_Threading",
"Win32_Security",
"Win32_Storage_FileSystem",
"Win32_System_LibraryLoader",
"Win32_System_SystemServices"
"Win32_System_SystemServices",
"Win32_System_Diagnostics_Debug"
] }
minhook.workspace = true

Expand Down
Loading
Loading