Skip to main content

static_hook_basic/common/
mod.rs

1//! EN: Shared helpers for `dobby-rs-framework` examples.
2//! CN: `dobby-rs-framework` 示例的公共辅助代码。
3
4#![allow(dead_code)]
5
6use dobby_rs_framework::framework::ModuleHandle;
7use dobby_rs_framework::prelude::{LogOptions, init_logging};
8use dobby_rs_framework::{Error, Result};
9use std::ffi::CString;
10
11// EN: Tiny local targets used by multiple examples.
12// CN: 多个示例复用的本地 target。
13
14#[inline(never)]
15pub fn target_add(x: i32) -> i32 {
16    x + 1
17}
18
19#[inline(never)]
20pub fn target_mul(x: i32) -> i32 {
21    x * 2
22}
23
24#[inline(never)]
25pub fn target_sub(x: i32) -> i32 {
26    x - 1
27}
28
29// EN: Initialize logging for examples (safe to ignore errors).
30// CN: 初始化示例日志(失败可忽略)。
31pub fn init_example_logging() {
32    let _ = init_logging(LogOptions::default());
33}
34
35// EN: A small cross-platform library used in symbol/module examples.
36// CN: 符号/模块示例里用到的跨平台动态库名。
37
38#[cfg(windows)]
39pub const DEMO_LIB: &str = "kernel32.dll";
40#[cfg(target_os = "linux")]
41pub const DEMO_LIB: &str = "libc.so.6";
42#[cfg(target_os = "macos")]
43pub const DEMO_LIB: &str = "/usr/lib/libSystem.B.dylib";
44
45// EN/CN: libc image path (used when an explicit image is required).
46
47#[cfg(target_os = "linux")]
48pub const LIBC_IMAGE: &str = "libc.so.6";
49#[cfg(target_os = "macos")]
50pub const LIBC_IMAGE: &str = "/usr/lib/libSystem.B.dylib";
51
52// EN: Common "resolve and print" helper used by examples.
53// CN: 示例复用的“解析并打印”辅助函数。
54pub fn resolve_and_print(m: &ModuleHandle, symbol: &str) -> Result<()> {
55    let c = CString::new(symbol).map_err(|_| Error::InvalidInput)?;
56    let p = m.resolve(c.as_c_str()).ok_or(Error::SymbolNotFound)?;
57    println!("module.resolve({symbol}) = {p:p}");
58    Ok(())
59}
60
61/// EN: Reusable detours used by multiple examples.
62/// CN: 多个示例复用的 detour。
63pub mod detours {
64    #[cfg(unix)]
65    pub mod unix {
66        use core::ffi::{c_char, c_int};
67
68        pub type PutsFn = unsafe extern "C" fn(*const c_char) -> c_int;
69
70        #[inline(never)]
71        pub unsafe extern "C" fn detour_puts(s: *const c_char) -> c_int {
72            let original: PutsFn = dobby_rs_framework::dobby_original!(detour_puts, PutsFn);
73            unsafe { original(s) }
74        }
75    }
76
77    #[cfg(windows)]
78    pub mod windows {
79        pub type GetTickCountFn = unsafe extern "system" fn() -> u32;
80        pub type GetCurrentProcessIdFn = unsafe extern "system" fn() -> u32;
81
82        #[inline(never)]
83        pub unsafe extern "system" fn detour_get_tick_count() -> u32 {
84            let original: GetTickCountFn =
85                dobby_rs_framework::dobby_original!(detour_get_tick_count, GetTickCountFn);
86            (unsafe { original() }) + 1
87        }
88
89        #[inline(never)]
90        pub unsafe extern "system" fn detour_get_current_process_id() -> u32 {
91            let original: GetCurrentProcessIdFn = dobby_rs_framework::dobby_original!(
92                detour_get_current_process_id,
93                GetCurrentProcessIdFn
94            );
95            unsafe { original() }
96        }
97    }
98}