diff --git a/include/safetyhook/utility.hpp b/include/safetyhook/utility.hpp index cb43386..3813e5c 100644 --- a/include/safetyhook/utility.hpp +++ b/include/safetyhook/utility.hpp @@ -10,13 +10,5 @@ template constexpr void store(uint8_t* address, const T& value) { } template -concept FnPtr = requires(T f) { - std::is_pointer_v - - // 32-bit MSVC doesn't seem to think `static __thiscall` functions are functions. -#if defined(_M_X64) - && std::is_function_v> -#endif - ; -}; +concept FnPtr = requires(T f) { std::is_pointer_v&& std::is_function_v>; }; } // namespace safetyhook \ No newline at end of file diff --git a/include/safetyhook/vmt_hook.hpp b/include/safetyhook/vmt_hook.hpp index c6f0e5b..e4ec0c4 100644 --- a/include/safetyhook/vmt_hook.hpp +++ b/include/safetyhook/vmt_hook.hpp @@ -72,7 +72,7 @@ class VmtHook final { ++index; // Skip RTTI pointer. hook.m_original_vm = m_new_vmt[index]; - hook.m_new_vm = reinterpret_cast(new_function); + store(reinterpret_cast(&hook.m_new_vm), new_function); hook.m_vmt_entry = &m_new_vmt[index]; m_new_vmt[index] = hook.m_new_vm; diff --git a/unittest/vmt_hook.cpp b/unittest/vmt_hook.cpp index 81e6412..120759f 100644 --- a/unittest/vmt_hook.cpp +++ b/unittest/vmt_hook.cpp @@ -18,8 +18,8 @@ TEST_CASE("VMT hook an object instance", "[vmt_hook]") { static SafetyHookVmt target_hook{}; static SafetyHookVm add_42_hook{}; - struct Hook { - static int __thiscall add_42(Target* self, int a) { return add_42_hook.thiscall(self, a) + 1337; } + struct Hook : Target { + int hooked_add_42(int a) { return add_42_hook.thiscall(this, a) + 1337; } }; auto vmt_result = SafetyHookVmt::create(target.get()); @@ -28,7 +28,7 @@ TEST_CASE("VMT hook an object instance", "[vmt_hook]") { target_hook = std::move(*vmt_result); - auto vm_result = target_hook.hook_method(1, Hook::add_42); + auto vm_result = target_hook.hook_method(1, &Hook::hooked_add_42); REQUIRE(vm_result); @@ -62,9 +62,9 @@ TEST_CASE("Resetting the VMT hook removes all VM hooks for that object", "[vmt_h static SafetyHookVm add_42_hook{}; static SafetyHookVm add_43_hook{}; - struct Hook { - static int __thiscall add_42(Target* self, int a) { return add_42_hook.thiscall(self, a) + 1337; } - static int __thiscall add_43(Target* self, int a) { return add_43_hook.thiscall(self, a) + 1337; } + struct Hook : Target { + int hooked_add_42(int a) { return add_42_hook.thiscall(this, a) + 1337; } + int hooked_add_43(int a) { return add_43_hook.thiscall(this, a) + 1337; } }; auto vmt_result = SafetyHookVmt::create(target.get()); @@ -73,7 +73,7 @@ TEST_CASE("Resetting the VMT hook removes all VM hooks for that object", "[vmt_h target_hook = std::move(*vmt_result); - auto vm_result = target_hook.hook_method(1, Hook::add_42); + auto vm_result = target_hook.hook_method(1, &Hook::hooked_add_42); REQUIRE(vm_result); @@ -81,7 +81,7 @@ TEST_CASE("Resetting the VMT hook removes all VM hooks for that object", "[vmt_h REQUIRE(target->add_42(1) == 1380); - vm_result = target_hook.hook_method(2, Hook::add_43); + vm_result = target_hook.hook_method(2, &Hook::hooked_add_43); REQUIRE(vm_result); @@ -113,8 +113,8 @@ TEST_CASE("VMT hooking an object maintains correct RTTI", "[vmt_hook]") { static SafetyHookVmt target_hook{}; static SafetyHookVm add_42_hook{}; - struct Hook { - static int __thiscall add_42(Target* self, int a) { return add_42_hook.thiscall(self, a) + 1337; } + struct Hook : Target { + int hooked_add_42(int a) { return add_42_hook.thiscall(this, a) + 1337; } }; auto vmt_result = SafetyHookVmt::create(target.get()); @@ -123,7 +123,7 @@ TEST_CASE("VMT hooking an object maintains correct RTTI", "[vmt_hook]") { target_hook = std::move(*vmt_result); - auto vm_result = target_hook.hook_method(1, Hook::add_42); + auto vm_result = target_hook.hook_method(1, &Hook::hooked_add_42); REQUIRE(vm_result);