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

Program crashes when unhooking #11

Closed
Nukoooo opened this issue Feb 13, 2023 · 6 comments · Fixed by #12
Closed

Program crashes when unhooking #11

Nukoooo opened this issue Feb 13, 2023 · 6 comments · Fixed by #12

Comments

@Nukoooo
Copy link

Nukoooo commented Feb 13, 2023

image

Code to reproduce:

SafetyHookInline hook;

__declspec(noinline) void SayHello(int times)
{
    std::cout << "Hello #" << times << std::endl;
}

void Hooked_SayHello(int times)
{
    hook->call<void, int>(1337);
}

void SayHelloInfinitely()
{
    int count = 0;
    while (true)
    {
        SayHello(count++);
    }
}

int main()
{
    // Starting a thread for SayHello
    std::thread t(SayHelloInfinitely);
    t.detach();

    {
        auto builder = SafetyHookFactory::acquire();
        hook = builder.create_inline(SayHello, Hooked_SayHello);
    }

    std::this_thread::sleep_for(std::chrono::seconds(1));
    hook.reset();
    std::this_thread::sleep_for(std::chrono::seconds(1));

    return 0;
}

Although this can be "fixed" by checking nullptr for hook, it doesn't really live up to the library name

Note: The library used in the code is from Update README.md

@Nukoooo Nukoooo changed the title Program crashes when calling original and unhooking Program crashes when unhooking Feb 13, 2023
@cursey
Copy link
Owner

cursey commented Feb 14, 2023

I've tried addressing your concerns in #12. It still isn't perfectly safe in my opinion but it is an improvement.

@Nukoooo
Copy link
Author

Nukoooo commented Feb 14, 2023

Thank you so much for that! I tried the PR in my production and it partially fixes the issue, because it still crashes on unhooking from time to time. Wouldn't it be better to freeze all the threads while unhooking? I might be missing something but I don't see any thread-freezing related in unhook code while readme says Stops all other threads when creating or deleting hooks

@angelfor3v3r
Copy link
Contributor

When you call unique_ptr.reset then ~InlineHook gets called which will freeze threads. I don't know what your use cases for unhooking is but it's generally very unsafe unless you have full control over when the function you're hooking is being called. No hooking library, from what I know, fixes this.

@cursey
Copy link
Owner

cursey commented Feb 15, 2023

Wouldn't it be better to freeze all the threads while unhooking? I might be missing something but I don't see any thread-freezing related in unhook code while readme says Stops all other threads when creating or deleting hooks

It freezes the threads when it acquires a builder from the factory:

void InlineHook::destroy() {
if (m_trampoline == 0) {
return;
}
auto builder = Factory::acquire();

it still crashes on unhooking from time to time.

Yeah. I think I can further improve the safety by adding a mutex to InlineHook but that would incur a (small) performance penalty when call'ing the original function. If I add this, I'll probably make it opt-in by providing a call_safe method or something that users can choose to use if unhook safety is important for them.

@cursey
Copy link
Owner

cursey commented Feb 15, 2023

I went ahead and implemented the mutex idea for all the *call methods since safety is a priority for this library I felt I should just keep it as fool-proof as possible (0c2147b).

@Nukoooo
Copy link
Author

Nukoooo commented Feb 15, 2023

Thanks for the clarification 👍. I think mutex addresses the issue and don't experience with crashing when unhooking now

@cursey cursey linked a pull request Feb 16, 2023 that will close this issue
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

Successfully merging a pull request may close this issue.

3 participants