Skip to content

Commit 1fe1b65

Browse files
authored
Update/replace deps (darfink#22)
* Replace lazy_static with once_cell * Replace winapi with windows crate * update all deps * fix `_reserved` param type
1 parent 7d6ab8a commit 1fe1b65

File tree

5 files changed

+87
-97
lines changed

5 files changed

+87
-97
lines changed

Cargo.toml

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,16 @@ edition = "2018"
1616

1717
[dependencies]
1818
cfg-if = "1.0.0"
19-
generic-array = "0.14.1"
20-
lazy_static = "1.4"
21-
libc = "0.2.45"
22-
mmap = { package = "mmap-fixed-fixed", version = "0.1.0" }
19+
generic-array = "0.14.7"
20+
once_cell = "1.18.0"
21+
libc = "0.2.145"
22+
mmap = { package = "mmap-fixed-fixed", version = "0.1.3" }
2323
region = "3.0.0"
2424
slice-pool = "0.4.1"
2525

2626
[dev-dependencies]
2727
matches = "0.1.10"
28-
ctor = "0.2.0"
29-
once_cell = "1.17.1"
28+
ctor = "0.2.2"
3029

3130
[features]
3231
default = ["nightly"]
@@ -47,5 +46,6 @@ crate-type = ["cdylib"]
4746
[target."cfg(any(target_arch = \"x86\", target_arch = \"x86_64\"))".dependencies]
4847
udis = { package = "libudis86-sys", version = "0.2.1" }
4948

50-
[target."cfg(windows)".dev-dependencies]
51-
winapi = { version = "0.3.9", features = ["minwindef", "windef", "winnt", "libloaderapi"] }
49+
[target."cfg(windows)".dev-dependencies.windows]
50+
version = "0.48"
51+
features = ["Win32_Foundation", "Win32_System_LibraryLoader", "Win32_System_SystemServices"]

examples/cat_detour.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
#![cfg(all(not(windows), feature = "nightly"))]
22

3+
use retour::static_detour;
34
use std::ffi::CString;
45
use std::os::raw::c_char;
56
use std::os::raw::c_int;
6-
use retour::static_detour;
77

88
extern "C" {
99
fn open(pathname: *const c_char, flags: c_int) -> c_int;
@@ -22,8 +22,8 @@ fn definitely_open(_: *const c_char, _: c_int) -> c_int {
2222

2323
#[ctor::ctor]
2424
fn main() {
25-
unsafe {
26-
Opentour.initialize(open, definitely_open).unwrap();
27-
Opentour.enable().unwrap();
28-
}
25+
unsafe {
26+
Opentour.initialize(open, definitely_open).unwrap();
27+
Opentour.enable().unwrap();
28+
}
2929
}

examples/kernel32_detour.rs

Lines changed: 35 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,59 @@
11
#![cfg(windows)]
22
#![allow(non_upper_case_globals, non_snake_case, non_camel_case_types)]
33

4+
use std::{ffi::CStr, os::raw::c_void};
5+
46
use once_cell::sync::Lazy;
57
use retour::GenericDetour;
6-
use winapi::um::libloaderapi::{GetProcAddress, LoadLibraryA};
7-
use winapi::shared::minwindef::DWORD;
8-
use winapi::shared::minwindef::HMODULE;
9-
use winapi::shared::minwindef::HINSTANCE;
10-
use winapi::shared::minwindef::LPVOID;
11-
use winapi::shared::minwindef::BOOL;
12-
use winapi::shared::ntdef::TRUE;
13-
use winapi::shared::ntdef::LPCSTR;
14-
use winapi::um::winnt::{DLL_PROCESS_ATTACH, DLL_PROCESS_DETACH, DLL_THREAD_ATTACH, DLL_THREAD_DETACH};
15-
16-
type fn_LoadLibraryA = extern "system" fn(LPCSTR) -> HMODULE;
8+
use windows::{
9+
core::PCSTR,
10+
Win32::{
11+
Foundation::{BOOL, HANDLE, HMODULE},
12+
System::{
13+
LibraryLoader::{GetProcAddress, LoadLibraryA},
14+
SystemServices::{
15+
DLL_PROCESS_ATTACH, DLL_PROCESS_DETACH, DLL_THREAD_ATTACH, DLL_THREAD_DETACH,
16+
},
17+
},
18+
},
19+
};
20+
type fn_LoadLibraryA = extern "system" fn(PCSTR) -> HMODULE;
1721

1822
static hook_LoadLibraryA: Lazy<GenericDetour<fn_LoadLibraryA>> = Lazy::new(|| {
19-
let library_handle = unsafe { LoadLibraryA("kernel32.dll\0".as_ptr() as _) };
20-
let address = unsafe { GetProcAddress(library_handle, "LoadLibraryA\0".as_ptr() as _) };
23+
let library_handle = unsafe { LoadLibraryA(PCSTR(b"kernel32.dll\0".as_ptr() as _)) }.unwrap();
24+
let address = unsafe { GetProcAddress(library_handle, PCSTR(b"LoadLibraryA\0".as_ptr() as _)) };
2125
let ori: fn_LoadLibraryA = unsafe { std::mem::transmute(address) };
22-
return unsafe {
23-
GenericDetour::new(ori, our_LoadLibraryA).unwrap()
24-
};
26+
return unsafe { GenericDetour::new(ori, our_LoadLibraryA).unwrap() };
2527
});
2628

27-
fn strlen(s: *const i8) -> usize {
28-
let mut i = 0;
29-
unsafe {
30-
while *s.offset(i) != 0 {
31-
i += 1;
32-
}
33-
}
34-
i as usize
35-
}
36-
37-
fn lpcstr_to_rust_string(input: LPCSTR) -> String {
38-
if input.is_null() {
39-
return String::from("(null)");
40-
}
41-
let length = strlen(input);
42-
let slice: &[u8] = unsafe { std::slice::from_raw_parts(input as *const u8, length) };
43-
return String::from_utf8(slice.to_vec()).unwrap();
44-
}
45-
46-
extern "system" fn our_LoadLibraryA(lpFileName: LPCSTR) -> HMODULE {
47-
println!("our_LoadLibraryA lpFileName = {}", lpcstr_to_rust_string(lpFileName));
29+
extern "system" fn our_LoadLibraryA(lpFileName: PCSTR) -> HMODULE {
30+
let file_name = unsafe { CStr::from_ptr(lpFileName.as_ptr() as _) };
31+
println!("our_LoadLibraryA lpFileName = {:?}", file_name);
4832
unsafe { hook_LoadLibraryA.disable().unwrap() };
4933
let ret_val = hook_LoadLibraryA.call(lpFileName);
50-
println!("our_LoadLibraryA lpFileName = {} ret_val = {:p}", lpcstr_to_rust_string(lpFileName), ret_val);
34+
println!(
35+
"our_LoadLibraryA lpFileName = {:?} ret_val = {:#X}",
36+
file_name, ret_val.0
37+
);
5138
unsafe { hook_LoadLibraryA.enable().unwrap() };
5239
return ret_val;
5340
}
5441

5542
#[no_mangle]
56-
unsafe extern "system" fn DllMain(_hinst: HINSTANCE, reason: DWORD, _reserved: LPVOID) -> BOOL {
43+
unsafe extern "system" fn DllMain(_hinst: HANDLE, reason: u32, _reserved: *mut c_void) -> BOOL {
5744
match reason {
5845
DLL_PROCESS_ATTACH => {
5946
println!("attaching");
60-
unsafe { hook_LoadLibraryA.enable().unwrap(); }
61-
}
47+
unsafe {
48+
hook_LoadLibraryA.enable().unwrap();
49+
}
50+
},
6251
DLL_PROCESS_DETACH => {
6352
println!("detaching");
64-
}
65-
DLL_THREAD_ATTACH => {}
66-
DLL_THREAD_DETACH => {}
67-
_ => {}
53+
},
54+
DLL_THREAD_ATTACH => {},
55+
DLL_THREAD_DETACH => {},
56+
_ => {},
6857
};
69-
return TRUE as BOOL;
58+
return BOOL::from(true);
7059
}

examples/messageboxw_detour.rs

Lines changed: 32 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,23 @@
44
//! Ensure the crate is compiled as a 'cdylib' library to allow C interop.
55
use retour::static_detour;
66
use std::error::Error;
7+
use std::ffi::c_int;
8+
use std::os::raw::c_void;
79
use std::{ffi::CString, iter, mem};
8-
use winapi::ctypes::c_int;
9-
use winapi::shared::minwindef::{BOOL, DWORD, HINSTANCE, LPVOID, TRUE, UINT};
10-
use winapi::shared::windef::HWND;
11-
use winapi::um::libloaderapi::{GetModuleHandleW, GetProcAddress};
12-
use winapi::um::winnt::{DLL_PROCESS_ATTACH, LPCWSTR};
10+
use windows::core::{PCSTR, PCWSTR};
11+
use windows::w;
12+
use windows::Win32::Foundation::{BOOL, HANDLE, HWND};
13+
use windows::Win32::System::LibraryLoader::{GetModuleHandleW, GetProcAddress};
14+
use windows::Win32::System::SystemServices::{
15+
DLL_PROCESS_ATTACH, DLL_PROCESS_DETACH, DLL_THREAD_ATTACH, DLL_THREAD_DETACH,
16+
};
1317

1418
static_detour! {
15-
static MessageBoxWHook: unsafe extern "system" fn(HWND, LPCWSTR, LPCWSTR, UINT) -> c_int;
19+
static MessageBoxWHook: unsafe extern "system" fn(HWND, PCWSTR, PCWSTR, u32) -> c_int;
1620
}
1721

1822
// A type alias for `MessageBoxW` (makes the transmute easy on the eyes)
19-
type FnMessageBoxW = unsafe extern "system" fn(HWND, LPCWSTR, LPCWSTR, UINT) -> c_int;
23+
type FnMessageBoxW = unsafe extern "system" fn(HWND, PCWSTR, PCWSTR, u32) -> c_int;
2024

2125
/// Called when the DLL is attached to the process.
2226
unsafe fn main() -> Result<(), Box<dyn Error>> {
@@ -37,10 +41,10 @@ unsafe fn main() -> Result<(), Box<dyn Error>> {
3741
}
3842

3943
/// Called whenever `MessageBoxW` is invoked in the process.
40-
fn messageboxw_detour(hwnd: HWND, text: LPCWSTR, _caption: LPCWSTR, u_type: UINT) -> c_int {
44+
fn messageboxw_detour(hwnd: HWND, text: PCWSTR, _caption: PCWSTR, msgbox_style: u32) -> c_int {
4145
// Call the original `MessageBoxW`, but replace the caption
42-
let replaced_caption = "Detoured!\0".encode_utf16().collect::<Vec<u16>>();
43-
unsafe { MessageBoxWHook.call(hwnd, text, replaced_caption.as_ptr() as _, u_type) }
46+
let replaced_caption = w!("Detoured!");
47+
unsafe { MessageBoxWHook.call(hwnd, text, replaced_caption, msgbox_style) }
4448
}
4549

4650
/// Returns a module symbol's absolute address.
@@ -51,29 +55,27 @@ fn get_module_symbol_address(module: &str, symbol: &str) -> Option<usize> {
5155
.collect::<Vec<u16>>();
5256
let symbol = CString::new(symbol).unwrap();
5357
unsafe {
54-
let handle = GetModuleHandleW(module.as_ptr());
55-
match GetProcAddress(handle, symbol.as_ptr()) as usize {
56-
0 => None,
57-
n => Some(n),
58+
let handle = GetModuleHandleW(PCWSTR(module.as_ptr() as _)).unwrap();
59+
match GetProcAddress(handle, PCSTR(symbol.as_ptr() as _)) {
60+
Some(func) => Some(func as usize),
61+
None => None,
5862
}
5963
}
6064
}
6165

6266
#[no_mangle]
63-
#[allow(non_snake_case)]
64-
pub unsafe extern "system" fn DllMain(
65-
_module: HINSTANCE,
66-
call_reason: DWORD,
67-
_reserved: LPVOID,
68-
) -> BOOL {
69-
if call_reason == DLL_PROCESS_ATTACH {
70-
// A console may be useful for printing to 'stdout'
71-
// winapi::um::consoleapi::AllocConsole();
72-
73-
// Preferably a thread should be created here instead, since as few
74-
// operations as possible should be performed within `DllMain`.
75-
main().is_ok() as BOOL
76-
} else {
77-
TRUE
78-
}
67+
unsafe extern "system" fn DllMain(_hinst: HANDLE, reason: u32, _reserved: *mut c_void) -> BOOL {
68+
match reason {
69+
DLL_PROCESS_ATTACH => {
70+
println!("attaching");
71+
unsafe { main().unwrap() }
72+
},
73+
DLL_PROCESS_DETACH => {
74+
println!("detaching");
75+
},
76+
DLL_THREAD_ATTACH => {},
77+
DLL_THREAD_DETACH => {},
78+
_ => {},
79+
};
80+
return BOOL::from(true);
7981
}

src/arch/memory.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
1+
use once_cell::sync::Lazy;
2+
13
use crate::{alloc, arch, error::Result, pic};
2-
use lazy_static::lazy_static;
34
use std::sync::Mutex;
45

5-
lazy_static! {
6-
/// Shared allocator for all detours.
7-
pub static ref POOL: Mutex<alloc::ThreadAllocator> = {
8-
// Use a range of +/- 2 GB for seeking a memory block
9-
Mutex::new(alloc::ThreadAllocator::new(arch::meta::DETOUR_RANGE))
10-
};
11-
}
6+
/// Shared allocator for all detours.
7+
pub static POOL: Lazy<Mutex<alloc::ThreadAllocator>> = Lazy::new(|| {
8+
// Use a range of +/- 2 GB for seeking a memory block
9+
Mutex::new(alloc::ThreadAllocator::new(arch::meta::DETOUR_RANGE))
10+
});
1211

1312
/// Allocates PIC code at the specified address.
1413
pub fn allocate_pic(

0 commit comments

Comments
 (0)