Skip to content

Commit

Permalink
MemoryHook has been temporarily removed.
Browse files Browse the repository at this point in the history
  • Loading branch information
RenardDev committed Nov 13, 2024
1 parent 4070ea0 commit 6a2e9fc
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 15 deletions.
13 changes: 9 additions & 4 deletions Detours.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ namespace Detours {
// MEMORY_HOOK_RECORD
// ----------------------------------------------------------------

/*
typedef struct _MEMORY_HOOK_RECORD {
_MEMORY_HOOK_RECORD() {
m_pCallBack = nullptr;
Expand All @@ -168,6 +169,7 @@ namespace Detours {
std::deque<std::pair<std::unique_ptr<Page>, std::unique_ptr<Page>>> m_Pages;
std::deque<std::pair<std::pair<void*, size_t>, std::unique_ptr<Page>>> m_VirtualPages;
} MEMORY_HOOK_RECORD, *PMEMORY_HOOK_RECORD;
*/

// ----------------------------------------------------------------
// INTERRUPT_HOOK_RECORD
Expand All @@ -187,7 +189,7 @@ namespace Detours {
// Storage
// ----------------------------------------------------------------

static std::deque<std::unique_ptr<MEMORY_HOOK_RECORD>> g_MemoryHookRecords;
// static std::deque<std::unique_ptr<MEMORY_HOOK_RECORD>> g_MemoryHookRecords;
static std::deque<std::unique_ptr<INTERRUPT_HOOK_RECORD>> g_InterruptHookRecords;
static MemoryManager g_MemoryManager;
static Storage g_HookStorage;
Expand Down Expand Up @@ -6870,6 +6872,7 @@ namespace Detours {
// MemoryHookCallBack
// ----------------------------------------------------------------

/*
static ULONG_PTR GetRegisterValue(const PCONTEXT pCTX, unsigned int unRegister, unsigned int unSize) {
switch (unRegister) {
case RDR_RAX:
Expand Down Expand Up @@ -8120,6 +8123,7 @@ namespace Detours {

return false;
}
*/

// ----------------------------------------------------------------
// InterruptHookCallBack
Expand Down Expand Up @@ -8188,7 +8192,7 @@ namespace Detours {
if (m_pVEH) {

// Built-in CallBacks
AddCallBack(MemoryHookCallBack); // Memory Hooks
//AddCallBack(MemoryHookCallBack); // Memory Hooks
AddCallBack(InterruptHookCallBack); // Interrupt Hooks
}
}
Expand All @@ -8199,7 +8203,7 @@ namespace Detours {

// Built-in CallBacks
RemoveCallBack(InterruptHookCallBack); // Interrupt Hooks
RemoveCallBack(MemoryHookCallBack); // Memory Hooks
//RemoveCallBack(MemoryHookCallBack); // Memory Hooks
}
}

Expand Down Expand Up @@ -85190,7 +85194,7 @@ namespace Detours {
// ----------------------------------------------------------------
// Memory Hook
// ----------------------------------------------------------------

/*
bool HookMemory(const fnMemoryHookCallBack pCallBack, void* pAddress, size_t unSize, bool bAllowVirtualAddress) {
if (!pCallBack || !pAddress || !unSize) {
return false;
Expand Down Expand Up @@ -85310,6 +85314,7 @@ namespace Detours {

return false;
}
*/

// ----------------------------------------------------------------
// Interrupt Hook
Expand Down
18 changes: 9 additions & 9 deletions Detours.h
Original file line number Diff line number Diff line change
Expand Up @@ -5532,27 +5532,27 @@ namespace Detours {
namespace Hook {

// ----------------------------------------------------------------
// Memory Hook Operation
// Memory Hook Operation (Experimental!!!)
// ----------------------------------------------------------------

typedef enum _MEMORY_HOOK_OPERATION : unsigned char {
MEMORY_READ = 0,
MEMORY_WRITE = 1,
MEMORY_EXECUTE = 2
} MEMORY_HOOK_OPERATION, *PMEMORY_HOOK_OPERATION;
//typedef enum _MEMORY_HOOK_OPERATION : unsigned char {
// MEMORY_READ = 0,
// MEMORY_WRITE = 1,
// MEMORY_EXECUTE = 2
//} MEMORY_HOOK_OPERATION, *PMEMORY_HOOK_OPERATION;

// ----------------------------------------------------------------
// Memory Hook CallBack
// ----------------------------------------------------------------

using fnMemoryHookCallBack = bool(*)(const PCONTEXT pCTX, const void* pExceptionAddress, MEMORY_HOOK_OPERATION unOperation, const void* pHookAddress, const void* pAccessAddress, void** pNewAccessAddress);
//using fnMemoryHookCallBack = bool(*)(const PCONTEXT pCTX, const void* pExceptionAddress, MEMORY_HOOK_OPERATION unOperation, const void* pHookAddress, const void* pAccessAddress, void** pNewAccessAddress);

// ----------------------------------------------------------------
// Memory Hook
// ----------------------------------------------------------------

bool HookMemory(const fnMemoryHookCallBack pCallBack, void* pAddress, size_t unSize, bool bAllowVirtualAddress = false);
bool UnHookMemory(const fnMemoryHookCallBack pCallBack);
//bool HookMemory(const fnMemoryHookCallBack pCallBack, void* pAddress, size_t unSize, bool bAllowVirtualAddress = false);
//bool UnHookMemory(const fnMemoryHookCallBack pCallBack);

// ----------------------------------------------------------------
// Interrupt Hook CallBack
Expand Down
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,6 @@ A set of tools for working with software modifications in two files (Detours.h,
* RdGetOperandRlut - Retrieves the operand register lookup table for the decoded instruction.

# Detours::Hook
* HookMemory - Hooks a memory address with the specified callback.
* UnHookMemory - Unhooks a memory address with the specified callback.
* HookInterrupt - Hooks the specified interrupt with the given callback.
* UnHookInterrupt - Unhooks the specified interrupt.
* VTableFunctionHook - Class for hooking virtual table (VTable) functions.
Expand Down
4 changes: 4 additions & 0 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1559,6 +1559,7 @@ TEST_SUITE("Detours::Hook") {
typedef bool(__fastcall* fnFooOriginal)(void* pThis, void* /* unused */);
typedef bool(__fastcall* fnBooOriginal)(void* pThis, void* /* unused */);

/*
bool MemoryHook(const PCONTEXT pCTX, const void* pExceptionAddress, Detours::Hook::MEMORY_HOOK_OPERATION unOperation, const void* pHookAddress, const void* pAccessAddress, void** pNewAccessAddress) {
UNREFERENCED_PARAMETER(pCTX);
UNREFERENCED_PARAMETER(pExceptionAddress);
Expand Down Expand Up @@ -1630,6 +1631,7 @@ TEST_SUITE("Detours::Hook") {

return true;
}
*/

bool InterruptHook(const PCONTEXT pCTX, const unsigned char unInterrupt) {
_tprintf_s(_T("[InterruptHook] Called `int 0x%02X`\n"), unInterrupt);
Expand Down Expand Up @@ -1787,6 +1789,7 @@ TEST_SUITE("Detours::Hook") {
return true;
}

/*
TEST_CASE("MemoryHook 1") {
static Detours::Memory::Page Page;
void* pData = Page.Alloc(Page.GetPageCapacity());
Expand Down Expand Up @@ -1948,6 +1951,7 @@ TEST_SUITE("Detours::Hook") {
MESSAGE("Benckmark with 1 000 000 iterations (with hook): ", (Detours::KUserSharedData.SystemTime.LowPart - unBegin) / 10000, " ms");
CHECK(Detours::Hook::UnHookMemory(MemoryHook) == true);
}
*/

TEST_CASE("InterruptHook") { // TODO: Incorrect return from CallInterrupt on 64 bit.
CHECK(Detours::Hook::HookInterrupt(InterruptHook, 0x7E) == true);
Expand Down

0 comments on commit 6a2e9fc

Please sign in to comment.