From 38827bb1ca1e22739cc521ac710c352ed6f52e18 Mon Sep 17 00:00:00 2001 From: Kenzzer Date: Wed, 11 Sep 2024 20:40:20 +0200 Subject: [PATCH] switch to safetyhook --- extensions/dhooks/AMBuilder | 9 +---- extensions/dhooks/DynamicHooks/hook.cpp | 49 +++++++++---------------- extensions/dhooks/DynamicHooks/hook.h | 4 ++ 3 files changed, 24 insertions(+), 38 deletions(-) diff --git a/extensions/dhooks/AMBuilder b/extensions/dhooks/AMBuilder index 8c09573755..af4a532c84 100644 --- a/extensions/dhooks/AMBuilder +++ b/extensions/dhooks/AMBuilder @@ -42,19 +42,14 @@ for cxx in builder.targets: 'util.cpp', 'dynhooks_sourcepawn.cpp', '../../public/smsdk_ext.cpp', - 'asm/asm.c', - 'libudis86/decode.c', - 'libudis86/itab.c', - 'libudis86/syn-att.c', - 'libudis86/syn-intel.c', - 'libudis86/syn.c', - 'libudis86/udis86.c', # Dynamic Hooks os.path.join('DynamicHooks', 'registers.cpp') ] + SM.AddCDetour(binary) if binary.compiler.target.arch == 'x86': binary.sources += ['../../sourcepawn/vm/x86/assembler-x86.cpp'] + binary.compiler.cxxincludes += [ os.path.join(builder.sourcePath, 'public', 'jit', 'x86'), os.path.join(builder.sourcePath, 'sourcepawn', 'vm', 'x86') diff --git a/extensions/dhooks/DynamicHooks/hook.cpp b/extensions/dhooks/DynamicHooks/hook.cpp index 58fbab13ba..c6fe4626a6 100644 --- a/extensions/dhooks/DynamicHooks/hook.cpp +++ b/extensions/dhooks/DynamicHooks/hook.cpp @@ -35,7 +35,6 @@ // >> INCLUDES // ============================================================================ #include "hook.h" -#include #include #include "extension.h" #include @@ -64,45 +63,33 @@ CHook::CHook(void* pFunc, ICallingConvention* pConvention) if (!m_RetAddr.init()) return; - unsigned char* pTarget = (unsigned char *) pFunc; + m_pTrampoline = new void*; - // Determine the number of bytes we need to copy - int iBytesToCopy = copy_bytes(pTarget, NULL, JMP_SIZE); - - // Create a buffer for the bytes to copy + a jump to the rest of the - // function. - unsigned char* pCopiedBytes = (unsigned char *) smutils->GetScriptingEngine()->AllocatePageMemory(iBytesToCopy + JMP_SIZE); - - // Fill the array with NOP instructions - memset(pCopiedBytes, 0x90, iBytesToCopy + JMP_SIZE); - - // Copy the required bytes to our array - copy_bytes(pTarget, pCopiedBytes, JMP_SIZE); - - // Write a jump after the copied bytes to the function/bridge + number of bytes to copy - DoGatePatch(pCopiedBytes + iBytesToCopy, pTarget + iBytesToCopy); + m_pBridge = CreateBridge(); + if (!m_pBridge) + return; - // Save the trampoline - m_pTrampoline = (void *) pCopiedBytes; + auto result = safetyhook::InlineHook::create(pFunc, m_pBridge, safetyhook::InlineHook::Flags::StartDisabled); + if (!result) { + return; + } - // Create the bridge function - m_pBridge = CreateBridge(); + m_Hook = std::move(result.value()); + m_pTrampoline = m_Hook.original(); - // Write a jump to the bridge - DoGatePatch((unsigned char *) pFunc, m_pBridge); + m_Hook.enable(); } CHook::~CHook() { - // Copy back the previously copied bytes - copy_bytes((unsigned char *) m_pTrampoline, (unsigned char *) m_pFunc, JMP_SIZE); - - // Free the trampoline buffer - smutils->GetScriptingEngine()->FreePageMemory(m_pTrampoline); + if (m_Hook.enabled()) { + m_Hook.disable(); + } - // Free the asm bridge and new return address - smutils->GetScriptingEngine()->FreePageMemory(m_pBridge); - smutils->GetScriptingEngine()->FreePageMemory(m_pNewRetAddr); + if (m_pBridge) { + smutils->GetScriptingEngine()->FreePageMemory(m_pBridge); + smutils->GetScriptingEngine()->FreePageMemory(m_pNewRetAddr); + } delete m_pRegisters; delete m_pCallingConvention; diff --git a/extensions/dhooks/DynamicHooks/hook.h b/extensions/dhooks/DynamicHooks/hook.h index d7742d7a5b..2f4dd2a672 100644 --- a/extensions/dhooks/DynamicHooks/hook.h +++ b/extensions/dhooks/DynamicHooks/hook.h @@ -41,6 +41,7 @@ #include "convention.h" #include #include +#include // ============================================================================ // >> HookType_t @@ -196,6 +197,9 @@ class CHook ICallingConvention* m_pCallingConvention; + // SafetyHook + SafetyHookInline m_Hook{}; + // Address of the bridge void* m_pBridge;