Skip to content

Commit bd2aaf2

Browse files
committed
added thread api for rust
1 parent 45703a7 commit bd2aaf2

File tree

5 files changed

+92
-26
lines changed

5 files changed

+92
-26
lines changed

bindings/rust/libmem/src/process.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,11 @@ impl fmt::Display for Process {
7070
}
7171

7272
unsafe extern "C" fn enum_processes_callback(
73-
process: *mut lm_process_t,
73+
raw_process: *mut lm_process_t,
7474
arg: *mut lm_void_t,
7575
) -> lm_bool_t {
7676
let processes = arg as *mut Vec<Process>;
77-
unsafe { (*processes).push((*process).into()) };
77+
unsafe { (*processes).push((*raw_process).into()) };
7878
LM_TRUE
7979
}
8080

@@ -96,10 +96,10 @@ pub fn enum_processes() -> Option<Vec<Process>> {
9696

9797
/// Retrieves information about the current process
9898
pub fn get_process() -> Option<Process> {
99-
let mut process: MaybeUninit<lm_process_t> = MaybeUninit::uninit();
99+
let mut raw_process: MaybeUninit<lm_process_t> = MaybeUninit::uninit();
100100
unsafe {
101-
if libmem_sys::LM_GetProcess(process.as_mut_ptr()) == LM_TRUE {
102-
Some(process.assume_init().into())
101+
if libmem_sys::LM_GetProcess(raw_process.as_mut_ptr()) == LM_TRUE {
102+
Some(raw_process.assume_init().into())
103103
} else {
104104
None
105105
}
@@ -108,10 +108,10 @@ pub fn get_process() -> Option<Process> {
108108

109109
/// Retrieves information about a specific process identified by its process ID
110110
pub fn get_process_ex(pid: Pid) -> Option<Process> {
111-
let mut process: MaybeUninit<lm_process_t> = MaybeUninit::uninit();
111+
let mut raw_process: MaybeUninit<lm_process_t> = MaybeUninit::uninit();
112112
unsafe {
113-
if libmem_sys::LM_GetProcessEx(pid, process.as_mut_ptr()) == LM_TRUE {
114-
Some(process.assume_init().into())
113+
if libmem_sys::LM_GetProcessEx(pid, raw_process.as_mut_ptr()) == LM_TRUE {
114+
Some(raw_process.assume_init().into())
115115
} else {
116116
None
117117
}
@@ -120,11 +120,11 @@ pub fn get_process_ex(pid: Pid) -> Option<Process> {
120120

121121
/// Searches for a process by its name
122122
pub fn find_process(name: &str) -> Option<Process> {
123-
let mut process: MaybeUninit<lm_process_t> = MaybeUninit::uninit();
123+
let mut raw_process: MaybeUninit<lm_process_t> = MaybeUninit::uninit();
124124
let process_name = CString::new(name).ok()?;
125125
unsafe {
126-
if libmem_sys::LM_FindProcess(process_name.as_ptr(), process.as_mut_ptr()) == LM_TRUE {
127-
Some(process.assume_init().into())
126+
if libmem_sys::LM_FindProcess(process_name.as_ptr(), raw_process.as_mut_ptr()) == LM_TRUE {
127+
Some(raw_process.assume_init().into())
128128
} else {
129129
None
130130
}

bindings/rust/libmem/src/thread.rs

Lines changed: 42 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::mem::MaybeUninit;
2+
13
use libmem_sys::{lm_bool_t, lm_process_t, lm_thread_t, lm_void_t, LM_TRUE};
24

35
use crate::{Pid, Process, Tid};
@@ -69,19 +71,48 @@ pub fn enum_threads_ex(process: &Process) -> Option<Vec<Thread>> {
6971
}
7072
}
7173

72-
#[cfg(test)]
73-
mod tests {
74-
use super::*;
74+
/// Gets the current thread it's running from
75+
pub fn get_thread() -> Option<Thread> {
76+
let mut raw_thread: MaybeUninit<lm_thread_t> = MaybeUninit::uninit();
77+
unsafe {
78+
if libmem_sys::LM_GetThread(raw_thread.as_mut_ptr() as *mut lm_thread_t) == LM_TRUE {
79+
Some(raw_thread.assume_init().into())
80+
} else {
81+
None
82+
}
83+
}
84+
}
7585

76-
#[test]
77-
fn test_enum_threads() {
78-
let threads = enum_threads().expect("Failed to enumerate threads in the current process");
79-
assert!(threads.len() > 0);
86+
/// Gets a thread from a specified remote process
87+
pub fn get_thread_ex(process: &Process) -> Option<Thread> {
88+
let mut raw_thread: MaybeUninit<lm_thread_t> = MaybeUninit::uninit();
89+
let raw_process: lm_process_t = process.to_owned().into();
90+
unsafe {
91+
if libmem_sys::LM_GetThreadEx(
92+
&raw_process as *const lm_process_t,
93+
raw_thread.as_mut_ptr() as *mut lm_thread_t,
94+
) == LM_TRUE
95+
{
96+
Some(raw_thread.assume_init().into())
97+
} else {
98+
None
99+
}
80100
}
101+
}
81102

82-
#[test]
83-
fn test_enum_threads_ex() {
84-
let threads = enum_threads().expect("Failed to enumerate threads in the current process");
85-
assert!(threads.len() > 0);
103+
/// Gets the process that owns a specified thread
104+
pub fn get_thread_process(thread: &Thread) -> Option<Process> {
105+
let mut raw_process: MaybeUninit<lm_process_t> = MaybeUninit::uninit();
106+
let raw_thread: lm_thread_t = thread.to_owned().into();
107+
unsafe {
108+
if libmem_sys::LM_GetThreadProcess(
109+
&raw_thread as *const lm_thread_t,
110+
raw_process.as_mut_ptr() as *mut lm_process_t,
111+
) == LM_TRUE
112+
{
113+
Some(raw_process.assume_init().into())
114+
} else {
115+
None
116+
}
86117
}
87118
}

bindings/rust/libmem/tests/common.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
use libmem::Process;
2-
use libmem_sys::LM_PID_BAD;
1+
use libmem::{Process, Thread};
2+
use libmem_sys::{LM_PID_BAD, LM_TID_BAD};
33

44
pub fn check_process(process: &Process) -> bool {
55
process.pid != LM_PID_BAD
@@ -9,3 +9,7 @@ pub fn check_process(process: &Process) -> bool {
99
&& process.path.len() > 0
1010
&& process.name.len() > 0
1111
}
12+
13+
pub fn check_thread(thread: &Thread) -> bool {
14+
thread.tid != LM_TID_BAD && thread.owner_pid != LM_PID_BAD
15+
}

bindings/rust/libmem/tests/local.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
mod common;
22

3-
use common::check_process;
3+
use common::{check_process, check_thread};
44
use libmem::*;
55

66
#[test]
7-
fn test_local_process() {
7+
fn test_get_process() {
88
let process = get_process().expect("Failed to get current process");
99
eprintln!("Current process: {}", process);
1010
assert!(check_process(&process));
@@ -20,3 +20,15 @@ fn test_get_system_bits() {
2020
let bits = get_system_bits();
2121
assert!(bits == 32 || bits == 64);
2222
}
23+
24+
#[test]
25+
fn test_enum_threads() {
26+
let threads = enum_threads().expect("Failed to enumerate threads in the current process");
27+
assert!(threads.len() > 0);
28+
}
29+
30+
#[test]
31+
fn test_get_thread() {
32+
let thread = get_thread().expect("Failed to get current thread");
33+
assert!(check_thread(&thread));
34+
}

bindings/rust/libmem/tests/remote.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,30 @@ mod common;
33
use common::check_process;
44
use libmem::*;
55

6+
use crate::common::check_thread;
7+
68
#[test]
79
fn test_remote_process() {
810
let process = find_process("cargo").expect("Failed to find remote process");
911
eprintln!("Found process: {}", process);
1012
assert!(check_process(&process));
1113

1214
assert!(is_process_alive(&process));
15+
16+
assert_eq!(
17+
process,
18+
get_process_ex(process.pid).expect("Failed to get process by PID")
19+
);
20+
21+
let threads =
22+
enum_threads_ex(&process).expect("Failed to enumerate threads in the current process");
23+
assert!(threads.len() > 0);
24+
25+
let thread = get_thread_ex(&process).expect("Failed to get remote process thread");
26+
assert!(check_thread(&thread));
27+
28+
assert_eq!(
29+
get_thread_process(&thread).expect("Failed to get thread's owner process"),
30+
process
31+
);
1332
}

0 commit comments

Comments
 (0)