Skip to content

Commit 889f7fe

Browse files
hook pthread_mutex_lock/pthread_mutex_unlock/WaitOnAddress and add tests
Agent-Logs-Url: https://github.com/acl-dev/open-coroutine/sessions/a4199b83-4d02-43f6-aa5a-f79a5303135a Co-authored-by: loongs-zhang <38336731+loongs-zhang@users.noreply.github.com>
1 parent f90cb74 commit 889f7fe

4 files changed

Lines changed: 41 additions & 6 deletions

File tree

core/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ anyhow.workspace = true
7474
slab.workspace = true
7575
backtrace.workspace = true
7676

77+
[target.'cfg(unix)'.dev-dependencies]
78+
libc.workspace = true
79+
7780
[features]
7881
default = ["log", "syscall"]
7982

core/tests/scheduler.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,40 @@ fn scheduler_listener() -> std::io::Result<()> {
130130
Ok(())
131131
}
132132

133+
#[cfg(unix)]
134+
#[test]
135+
fn scheduler_pthread_mutex_lock() -> std::io::Result<()> {
136+
use std::sync::atomic::{AtomicUsize, Ordering};
137+
138+
static COUNTER: AtomicUsize = AtomicUsize::new(0);
139+
140+
let mut scheduler = Scheduler::default();
141+
for _ in 0..3 {
142+
_ = scheduler.submit_co(
143+
|_, _| {
144+
let mut mutex = libc::PTHREAD_MUTEX_INITIALIZER;
145+
let r = open_coroutine_core::syscall::pthread_mutex_lock(
146+
None,
147+
std::ptr::addr_of_mut!(mutex),
148+
);
149+
assert_eq!(0, r, "pthread_mutex_lock failed with {r}");
150+
COUNTER.fetch_add(1, Ordering::SeqCst);
151+
let r = open_coroutine_core::syscall::pthread_mutex_unlock(
152+
None,
153+
std::ptr::addr_of_mut!(mutex),
154+
);
155+
assert_eq!(0, r, "pthread_mutex_unlock failed with {r}");
156+
None
157+
},
158+
None,
159+
None,
160+
)?;
161+
}
162+
scheduler.try_schedule()?;
163+
assert_eq!(3, COUNTER.load(Ordering::SeqCst));
164+
Ok(())
165+
}
166+
133167
#[test]
134168
fn scheduler_try_cancel_coroutine() -> std::io::Result<()> {
135169
let mut scheduler = Scheduler::default();

hook/src/syscall/unix.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,8 @@ impl_hook!(RENAMEAT, renameat(olddirfd: c_int, oldpath: *const c_char, newdirfd:
8080
#[cfg(target_os = "linux")]
8181
impl_hook!(RENAMEAT2, renameat2(olddirfd: c_int, oldpath: *const c_char, newdirfd: c_int, newpath: *const c_char, flags: c_uint) -> c_int);
8282

83+
impl_hook!(PTHREAD_MUTEX_LOCK, pthread_mutex_lock(lock: *mut pthread_mutex_t) -> c_int);
84+
impl_hook!(PTHREAD_MUTEX_UNLOCK, pthread_mutex_unlock(lock: *mut pthread_mutex_t) -> c_int);
85+
8386
// NOTE: unhook poll due to mio's poller
8487
// impl_hook!(POLL, poll(fds: *mut pollfd, nfds: nfds_t, timeout: c_int) -> c_int);
85-
86-
// NOTE: unhook pthread_mutex_lock/pthread_mutex_unlock due to stack overflow or bug
87-
// impl_hook!(PTHREAD_MUTEX_LOCK, pthread_mutex_lock(lock: *mut pthread_mutex_t) -> c_int);
88-
// impl_hook!(PTHREAD_MUTEX_UNLOCK, pthread_mutex_unlock(lock: *mut pthread_mutex_t) -> c_int);

hook/src/syscall/windows.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,7 @@ unsafe fn attach() -> std::io::Result<()> {
8888
impl_hook!("ws2_32.dll", WSASOCKETW, WSASocketW(domain: c_int, ty: WINSOCK_SOCKET_TYPE, protocol: IPPROTO, lpprotocolinfo: *const WSAPROTOCOL_INFOW, g: c_uint, dw_flags: c_uint) -> SOCKET);
8989
impl_hook!("ws2_32.dll", SELECT, select(nfds: c_int, readfds: *mut FD_SET, writefds: *mut FD_SET, errorfds: *mut FD_SET, timeout: *mut TIMEVAL) -> c_int);
9090
impl_hook!("ws2_32.dll", WSAPOLL, WSAPoll(fds: *mut WSAPOLLFD, nfds: c_uint, timeout: c_int) -> c_int);
91-
// NOTE: unhook WaitOnAddress due to stack overflow or bug
92-
// impl_hook!("api-ms-win-core-synch-l1-2-0.dll", WAITONADDRESS, WaitOnAddress(address: *const c_void, compareaddress: *const c_void, addresssize: usize, dwmilliseconds: c_uint) -> BOOL);
91+
impl_hook!("api-ms-win-core-synch-l1-2-0.dll", WAITONADDRESS, WaitOnAddress(address: *const c_void, compareaddress: *const c_void, addresssize: usize, dwmilliseconds: c_uint) -> BOOL);
9392

9493
// Enable the hook
9594
minhook::MinHook::enable_all_hooks().map_err(|_| Error::other("init all hooks failed !"))

0 commit comments

Comments
 (0)