Skip to content

Commit

Permalink
fix(hook): fix for address (void*) hook
Browse files Browse the repository at this point in the history
  • Loading branch information
RimuruChan committed Feb 1, 2024
1 parent a36e58e commit be5d592
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 17 deletions.
29 changes: 17 additions & 12 deletions src/ll/api/memory/Hook.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,7 @@ LLNDAPI FuncPtr resolveIdentifier(std::string_view identifier, bool disableError
template <class T>
concept FuncPtrType = std::is_function_v<std::remove_pointer_t<T>> || std::is_member_function_pointer_v<T>;

template <class T>
requires(FuncPtrType<T> || std::is_same_v<T, uintptr_t>)
template <FuncPtrType T>
constexpr FuncPtr resolveIdentifier(T identifier) {
return toFuncPtr(identifier);
}
Expand All @@ -111,10 +110,14 @@ constexpr FuncPtr resolveIdentifier(std::string_view identifier) {
return resolveIdentifier(identifier);
}

// redirect to resolveIdentifier(uintptr_t)
template <class T>
constexpr FuncPtr resolveIdentifier(uintptr_t address) {
return resolveIdentifier(address);
return toFuncPtr(address);
}

template <class T>
constexpr FuncPtr resolveIdentifier(void* address) {
return address;
}

template <class, FixedString>
Expand Down Expand Up @@ -175,15 +178,15 @@ struct __declspec(empty_bases) Hook {};
\
template <class T> \
struct _ConstDetector { \
static constexpr bool value = false; \
[[maybe_unused]] static constexpr bool value = false; \
explicit constexpr _ConstDetector(T) {} \
}; \
template <class T> \
[[maybe_unused]] _ConstDetector(T) -> _ConstDetector<T>; \
[[maybe_unused]] _ConstDetector(_RawFuncType) -> _ConstDetector<_RawFuncType>; \
template <> \
struct _ConstDetector<_RawConstFuncType> { \
static constexpr bool value = true; \
[[maybe_unused]] static constexpr bool value = true; \
explicit constexpr _ConstDetector(_RawConstFuncType) {} \
}; \
template <class T = _RawFuncType, std::enable_if_t<std::is_member_function_pointer_v<T>, int> = 0> \
Expand All @@ -200,11 +203,13 @@ struct __declspec(empty_bases) Hook {};
\
template <class T> \
static consteval void detector() { \
if constexpr (::ll::memory::virtualDetector<_OriginFuncType, IDENTIFIER>()) { \
static_assert( \
::ll::concepts::always_false<T>, \
#IDENTIFIER " is a virtual function, you need use prefix $ workaround to hook it." \
); \
if constexpr (requires { ::ll::memory::virtualDetector<T, IDENTIFIER>(); }) { \
if constexpr (::ll::memory::virtualDetector<T, IDENTIFIER>()) { \
static_assert( \
::ll::concepts::always_false<T>, \
#IDENTIFIER " is a virtual function, you need use prefix $ workaround to hook it." \
); \
} \
} \
} \
\
Expand All @@ -216,7 +221,7 @@ struct __declspec(empty_bases) Hook {};
STATIC RET_TYPE detour(__VA_ARGS__); \
\
static int hook(bool suspendThreads = true) { \
detector<DEF_TYPE>(); \
detector<_OriginFuncType>(); \
_HookTarget = ::ll::memory::resolveIdentifier<_OriginFuncType>(IDENTIFIER); \
if (_HookTarget == nullptr) { \
return -1; \
Expand Down
40 changes: 35 additions & 5 deletions src/ll/test/HookTest.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
#include "ll/api/memory/Hook.h"

#include <string>

#include "ll/api/memory/Hook.h"
#include "ll/api/memory/Memory.h"

// NOLINTBEGIN
namespace {
class TestClass {
class TestOverloadClass {
protected:
int some = 0;

Expand All @@ -14,8 +16,36 @@ class TestClass {
};

// they will choose non-const version if possible
LL_TYPE_INSTANCE_HOOK(TestHook, HookPriority::Normal, TestClass, &TestClass::test, int, int a, int b) {
LL_TYPE_INSTANCE_HOOK(TestHook, HookPriority::Normal, TestOverloadClass, &TestOverloadClass::test, int, int a, int b) {
some = 1;
return origin(a, b);
}

class TestPtrClass {
protected:
int some = 0;

public:
int test(int a, int b) { return a + b; }
};

// they will choose non-const version if possible
LL_TYPE_INSTANCE_HOOK(
TestPtrHook,
HookPriority::Normal,
TestPtrClass,
ll::memory::toFuncPtr(&TestPtrClass::test),
int,
__int64 a,
__int64 b
) {
some = 1;
return origin(a, b);
}

LL_TYPE_INSTANCE_HOOK(TestUintptrHook, HookPriority::Normal, TestPtrClass, 0x1145141919810, int, __int64 a, __int64 b) {
some = 1;
return a + b;
return origin(a, b);
}
} // namespace
// NOLINTEND

0 comments on commit be5d592

Please sign in to comment.