Inline hook not applying with program optimization enabled
altoid29 opened this issue · 1 comments
Hello, I've just successfully got the library implemented and working fine. My testing was of course on Debug mode & worked completely fine. But I switched to Release and tried the hook (since that's obviously where it's going to be used in in real-use).
I've discovered a strange issue that the hook isn't applying when I have Optimization enabled on Visual Studio 2022 Preview.
Location: Configuration Properties > Optimization > Optimization (first).
Strangely, when using all other optimization settings, setting them to yes / enabled, no issue occurs. But when I have the first Optimization option not "Disabled (/Od)", it doesn't work.
I've attached some images with different configurations:
Disabled one succeeds, but any other optimization will not.
Here is the code used:
#include <iostream>
#include <Windows.h>
#include "safetyhook/safetyhook.hpp"
SafetyHookInline g_hook{};
void SomeFunction(int arg)
{
printf("SomeHook Called with %i\n", arg);
}
void SomeFunctionHk(int arg)
{
printf("Hook called\n");
return g_hook.call<void>(100);
}
int main()
{
g_hook = safetyhook::create_inline(reinterpret_cast<void*>(SomeFunction), reinterpret_cast<void*>(SomeFunctionHk));
SomeFunction(10);
std::cin.get();
return 0;
}
Compiler optimization is inlining "SomeFunction".
SafetyHook is like any other hooking library where you should have an idea what you're hooking, but since you're leaving it up to the compiler here then you can't guarantee that "SomeFunction" won't get inlined. Generally, you'd reverse engineer the target function to know the layout of it beforehand.
A fix for this case would be to use __declspec(noinline)
(or __attribute__((noinline))
on GCC/Clang) on SomeFunction
:
__declspec(noinline) void SomeFunction(int arg)
{
printf("SomeHook Called with %i\n", arg);
}