Skip to content

Commit 1eedb30

Browse files
committed
🛠️refactor(syscall) streamline macro definitions and improve comments
🚀feat(tests) add `call_nt_close` function for handle closing ⚙️chore(dependencies) import `HANDLE` from `phnt` package
1 parent 6100bac commit 1eedb30

File tree

1 file changed

+22
-12
lines changed

1 file changed

+22
-12
lines changed

src/lib.rs

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -112,21 +112,27 @@ macro_rules! syscall {
112112
}
113113
}};
114114

115-
(@bind $($r1:tt)? ) => { syscall!(@emit [$($r1)? ]) };
116-
(@bind $r1:tt $r2:tt ) => { syscall!(@emit [$r1 $r2 ]) };
117-
(@bind $r1:tt $r2:tt $r3:tt ) => { syscall!(@emit [$r1 $r2 $r3 ]) };
115+
116+
// Syscalls without stack arguments are directly emitted
117+
(@bind $($r1:tt)?) => { syscall!(@emit [$($r1)?]) };
118+
(@bind $r1:tt $r2:tt) => { syscall!(@emit [$r1 $r2]) };
119+
(@bind $r1:tt $r2:tt $r3:tt) => { syscall!(@emit [$r1 $r2 $r3]) };
118120
(@bind $r1:tt $r2:tt $r3:tt $r4:tt) => { syscall!(@emit [$r1 $r2 $r3 $r4]) };
119121

120-
// Stack arguments are pushed in reverse order
121-
(@bind $r1:tt $r2:tt $r3:tt $r4:tt $($rest:tt)+)
122-
=> { syscall!(@bind_stack $r1 $r2 $r3 $r4 [$($rest)+]) };
122+
// Stack arguments are bound to be in reverse order
123+
(@bind $r1:tt $r2:tt $r3:tt $r4:tt $($rest:tt)+) => {
124+
syscall!(@bind_stack $r1 $r2 $r3 $r4 [$($rest)+])
125+
};
123126

124-
(@bind_stack $r1:tt $r2:tt $r3:tt $r4:tt [$head:tt $($rest:tt)*] $($reversed:expr)* )
125-
=> { syscall!(@bind_stack $r1 $r2 $r3 $r4 [$($rest)*] $head $($reversed)* ) };
127+
(@bind_stack $r1:tt $r2:tt $r3:tt $r4:tt [$head:tt $($rest:tt)*] $($reversed:expr)* ) => {
128+
syscall!(@bind_stack $r1 $r2 $r3 $r4 [$($rest)*] $head $($reversed)*)
129+
};
126130

127-
(@bind_stack $r1:tt $r2:tt $r3:tt $r4:tt [] $($stack:tt)*)
128-
=> { syscall!(@emit [$r1 $r2 $r3 $r4] $($stack)*) };
131+
(@bind_stack $r1:tt $r2:tt $r3:tt $r4:tt [] $($stack:tt)*) => {
132+
syscall!(@emit [$r1 $r2 $r3 $r4] $($stack)*)
133+
};
129134

135+
// Emits the actual syscall instruction
130136
(@emit [$($register:expr)*] $($stack:expr)*) => {{
131137
let [arg1, arg2, arg3, arg4, mut status] = {
132138
let mut data = MaybeUninit::<usize>::uninit_array::<5>();
@@ -174,7 +180,7 @@ macro_rules! syscall {
174180
mod tests {
175181
use super::*;
176182
#[cfg(test)]
177-
use phnt::ffi::{NtClose, NTSTATUS};
183+
use phnt::ffi::{NtClose, HANDLE, NTSTATUS};
178184

179185
extern "C" {
180186
fn NtTestAlert() -> NTSTATUS;
@@ -188,11 +194,15 @@ mod tests {
188194
fn NtNArgs6(_1: usize, _2: usize, _3: usize, _4: usize, _5: usize, _6: usize) -> NTSTATUS;
189195
}
190196

197+
fn call_nt_close(handle: HANDLE) -> NTSTATUS {
198+
syscall!(NtClose(handle))
199+
}
200+
191201
#[test]
192202
fn basic() {
193203
const STATUS_INVALID_HANDLE: i32 = 0xC0000008u32 as i32;
194204
const INVALID_HANDLE: *mut std::ffi::c_void = core::ptr::null_mut();
195-
assert_eq!(syscall!(NtClose(INVALID_HANDLE)), STATUS_INVALID_HANDLE);
205+
assert_eq!(call_nt_close(INVALID_HANDLE), STATUS_INVALID_HANDLE);
196206

197207
const STATUS_SUCCESS: i32 = 0x00000000i32;
198208
assert_eq!(syscall!(NtTestAlert()), STATUS_SUCCESS);

0 commit comments

Comments
 (0)