Skip to content

Commit

Permalink
switch to safetyhook
Browse files Browse the repository at this point in the history
  • Loading branch information
Kenzzer committed Sep 11, 2024
1 parent 3f9fef7 commit 38827bb
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 38 deletions.
9 changes: 2 additions & 7 deletions extensions/dhooks/AMBuilder
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
49 changes: 18 additions & 31 deletions extensions/dhooks/DynamicHooks/hook.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
// >> INCLUDES
// ============================================================================
#include "hook.h"
#include <asm/asm.h>
#include <macro-assembler-x86.h>
#include "extension.h"
#include <jit/jit_helpers.h>
Expand Down Expand Up @@ -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<void*>();

// 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;
Expand Down
4 changes: 4 additions & 0 deletions extensions/dhooks/DynamicHooks/hook.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
#include "convention.h"
#include <am-hashmap.h>
#include <am-hashset.h>
#include <safetyhook.hpp>

// ============================================================================
// >> HookType_t
Expand Down Expand Up @@ -196,6 +197,9 @@ class CHook

ICallingConvention* m_pCallingConvention;

// SafetyHook
SafetyHookInline m_Hook{};

// Address of the bridge
void* m_pBridge;

Expand Down

0 comments on commit 38827bb

Please sign in to comment.