Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

need support for "thiscall" #51

Closed
zhefenfei opened this issue Feb 20, 2024 · 3 comments
Closed

need support for "thiscall" #51

zhefenfei opened this issue Feb 20, 2024 · 3 comments

Comments

@zhefenfei
Copy link

need support fo "thiscall"

@zhefenfei zhefenfei changed the title need support fo "thiscall" need support for "thiscall" Feb 20, 2024
@Hpmason
Copy link
Owner

Hpmason commented Feb 21, 2024

thiscall ABI should be supported, is behind a feature flag thiscall-abi. You should be able to modify your Cargo.toml to enable it:

[dependencies]
retour = { version = "0.3", features = ["thiscall-abi"] }

A little background on why the feature flag:
It was originally put under a feature flag because it was nightly-only and I wanted this crate to support stable rust. Even after it stabilizing in rust v1.73.0, it's still behind the flag to maintain a lower minimum supported rust version (MSRV).

If you have any issues with the flag enabled, let me know and I can try to help out further!

@zhefenfei
Copy link
Author

static_detour! {
static Test: unsafe extern "thiscall" fn(i32,i32,i32,i32)->i32;
}

pub fn set_hook() {
unsafe {
let original_call = std::mem::transmute::<
_,
unsafe extern "thiscall" fn(i32, i32, i32, i32) -> i32,
>(0x00BE5900);
Test.initialize(original_call, test_hook)
.unwrap()
.enable()
.unwrap()
};
}

#[no_mangle]
unsafe extern "thiscall" fn test_hook(arg0: i32, arg1: i32, arg2: i32, arg3: i32) -> i32 {
unsafe { Test.call(arg0, arg1, arg2, arg3) };
0
}

pls see this test in StaticDetour. i tried this ,but when initialize will show error like
=================error==========================================================
expected a Fn(i32, i32, i32, i32) closure, found unsafe extern "thiscall" fn(i32, i32, i32, i32) -> i32 {test_hook}
the trait Fn<(i32, i32, i32, i32)> is not implemented for fn item unsafe extern "thiscall" fn(i32, i32, i32, i32) -> i32 {test_hook}
unsafe function cannot be called generically without an unsafe blockrustcClick for full compiler diagnostic
string_pool.rs(5722, 14): required by a bound introduced by this call
statik.rs(107, 8): required by a bound in StaticDetour::<T>::initialize
ijl15::assembly::string_pool
unsafe extern "thiscall" fn test_hook(arg0: i32, arg1: i32, arg2: i32, arg3: i32) -> i32```
=================error==========================================================

i tried GenericDetour work is ok.but i wanna know why cause this problem.

@Hpmason
Copy link
Owner

Hpmason commented Feb 22, 2024

Ah I see the issue. This isn't an issue exclusive to "thiscall" ABI, it's an issue with unsafe hooks.

StaticDetour is meant to allow any function to be detoured by another function with the same arguments and return type. That's done through the Fn trait (not to be confused with the fn type ), the bound being D: Fn<T::Arguments, Output = T::Output> + Send + 'static. The Fn trait is only implemented for safe functions. There's a dicussion about it in the GitHub issue here that goes into some of the reasons why unsafe fns don't implement Fn trait.

In order to hook an unsafe/extern function, you can just use a safe, rust ABI, function:

static_detour! {
    static Test: unsafe extern "thiscall" fn(i32, i32, i32, i32) -> i32;
}

pub fn set_hook() {
    unsafe {
        let original_call = std::mem::transmute::<
            usize,
            unsafe extern "thiscall" fn(i32, i32, i32, i32) -> i32,
        >(0x00BE5900);
        Test.initialize(original_call, test_hook)
            .unwrap()
            .enable()
            .unwrap()
    };
}


fn test_hook(arg0: i32, arg1: i32, arg2: i32, arg3: i32) -> i32 {
    unsafe { Test.call(arg0, arg1, arg2, arg3) };
    0
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants