Skip to content

Commit

Permalink
VmtHook: Have hooks inherit from their targets
Browse files Browse the repository at this point in the history
  • Loading branch information
cursey committed Apr 24, 2023
1 parent b4b8abe commit 60dd639
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 19 deletions.
8 changes: 1 addition & 7 deletions include/safetyhook/utility.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,6 @@ template <typename T> constexpr void store(uint8_t* address, const T& value) {

template <typename T>
concept FnPtr = requires(T f) {
std::is_pointer_v<T>

// 32-bit MSVC doesn't seem to think `static __thiscall` functions are functions.
#if defined(_M_X64)
&& std::is_function_v<std::remove_pointer_t<T>>
#endif
;
std::is_pointer_v<T> && std::is_function_v<std::remove_pointer_t<T>>;
};
} // namespace safetyhook
2 changes: 1 addition & 1 deletion include/safetyhook/vmt_hook.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<uint8_t*>(new_function);
store(reinterpret_cast<uint8_t*>(&hook.m_new_vm), new_function);
hook.m_vmt_entry = &m_new_vmt[index];
m_new_vmt[index] = hook.m_new_vm;

Expand Down
22 changes: 11 additions & 11 deletions unittest/vmt_hook.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<int>(self, a) + 1337; }
struct Hook : Target {
int hooked_add_42(int a) { return add_42_hook.thiscall<int>(this, a) + 1337; }
};

auto vmt_result = SafetyHookVmt::create(target.get());
Expand All @@ -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);

Expand Down Expand Up @@ -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<int>(self, a) + 1337; }
static int __thiscall add_43(Target* self, int a) { return add_43_hook.thiscall<int>(self, a) + 1337; }
struct Hook : Target {
int hooked_add_42(int a) { return add_42_hook.thiscall<int>(this, a) + 1337; }
int hooked_add_43(int a) { return add_43_hook.thiscall<int>(this, a) + 1337; }
};

auto vmt_result = SafetyHookVmt::create(target.get());
Expand All @@ -73,15 +73,15 @@ 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);

add_42_hook = std::move(*vm_result);

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);

Expand Down Expand Up @@ -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<int>(self, a) + 1337; }
struct Hook : Target {
int hooked_add_42(int a) { return add_42_hook.thiscall<int>(this, a) + 1337; }
};

auto vmt_result = SafetyHookVmt::create(target.get());
Expand All @@ -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);

Expand Down

0 comments on commit 60dd639

Please sign in to comment.