Skip to content

Commit

Permalink
readd libudis86 for dhooks only
Browse files Browse the repository at this point in the history
  • Loading branch information
Kenzzer committed May 20, 2024
1 parent 167ab55 commit b5648f8
Show file tree
Hide file tree
Showing 18 changed files with 12,034 additions and 43 deletions.
13 changes: 12 additions & 1 deletion extensions/dhooks/AMBuilder
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import os

for cxx in builder.targets:
binary = SM.ExtLibrary(builder, cxx, 'dhooks.ext')
SM.AddCDetour(binary)

# Only x86 on Linux and Windows is supported.
if binary.compiler.target.platform == 'mac' or binary.compiler.target.arch != 'x86':
Expand All @@ -20,6 +19,7 @@ for cxx in builder.targets:
binary.compiler.cxxincludes += [
os.path.join(SM.mms_root, 'core'),
os.path.join(SM.mms_root, 'core', 'sourcehook'),
os.path.join(builder.sourcePath, 'extensions', 'dhooks'),
os.path.join(builder.sourcePath, 'public', 'jit'),
os.path.join(builder.sourcePath, 'public', 'jit', 'x86'),
os.path.join(builder.sourcePath, 'sourcepawn', 'include'),
Expand All @@ -28,6 +28,10 @@ for cxx in builder.targets:
os.path.join(builder.sourcePath, 'extensions', 'dhooks', 'DynamicHooks'),
]

binary.compiler.includes += [
os.path.join(builder.sourcePath, 'extensions', 'dhooks')
]

binary.sources += [
'extension.cpp',
'listeners.cpp',
Expand All @@ -37,6 +41,13 @@ 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',
'../../sourcepawn/vm/x86/assembler-x86.cpp',
]

Expand Down
41 changes: 4 additions & 37 deletions extensions/dhooks/DynamicHooks/hook.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,22 +64,7 @@ CHook::CHook(void* pFunc, ICallingConvention* pConvention)
if (!m_RetAddr.init())
return;

m_pTrampoline = new void*;

m_pBridge = CreateBridge();
if (!m_pBridge)
return;

auto result = safetyhook::InlineHook::create(pFunc, m_pBridge, safetyhook::InlineHook::Flags::StartDisabled);
if (!result) {
return;
}

m_Hook = std::move(result.value());
*m_pTrampoline = m_Hook.original<void*>();

m_Hook.enable();
/*unsigned char* pTarget = (unsigned char *) pFunc;
unsigned char* pTarget = (unsigned char *) pFunc;

// Determine the number of bytes we need to copy
int iBytesToCopy = copy_bytes(pTarget, NULL, JMP_SIZE);
Expand All @@ -104,27 +89,11 @@ CHook::CHook(void* pFunc, ICallingConvention* pConvention)
m_pBridge = CreateBridge();

// Write a jump to the bridge
DoGatePatch((unsigned char *) pFunc, m_pBridge);*/
DoGatePatch((unsigned char *) pFunc, m_pBridge);
}

CHook::~CHook()
{
if (m_pTrampoline) {
delete m_pTrampoline;
}

if (m_Hook.enabled()) {
m_Hook.disable();
}

if (m_pBridge) {
smutils->GetScriptingEngine()->FreePageMemory(m_pBridge);
smutils->GetScriptingEngine()->FreePageMemory(m_pNewRetAddr);
}

delete m_pRegisters;
delete m_pCallingConvention;
/*
// Copy back the previously copied bytes
copy_bytes((unsigned char *) m_pTrampoline, (unsigned char *) m_pFunc, JMP_SIZE);

Expand All @@ -136,7 +105,7 @@ CHook::~CHook()
smutils->GetScriptingEngine()->FreePageMemory(m_pNewRetAddr);

delete m_pRegisters;
delete m_pCallingConvention;*/
delete m_pCallingConvention;
}

void CHook::AddCallback(HookType_t eHookType, HookHandlerFn* pCallback)
Expand Down Expand Up @@ -278,9 +247,7 @@ void* CHook::CreateBridge()
masm.j(equal, &label_supercede);

// Jump to the trampoline
masm.movl(eax, Operand(ExternalAddress(m_pTrampoline)));
masm.movl(eax, Operand(eax, 0));
masm.jmp(eax);
masm.jmp(ExternalAddress(m_pTrampoline));

// This code will be executed if a pre-hook returns ReturnAction_Supercede
masm.bind(&label_supercede);
Expand Down
6 changes: 1 addition & 5 deletions extensions/dhooks/DynamicHooks/hook.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
#include "convention.h"
#include <am-hashmap.h>
#include <am-hashset.h>
#include <safetyhook.hpp>

// ============================================================================
// >> HookType_t
Expand Down Expand Up @@ -197,14 +196,11 @@ class CHook

ICallingConvention* m_pCallingConvention;

// SafetyHook
SafetyHookInline m_Hook{};

// Address of the bridge
void* m_pBridge;

// Address of the trampoline
void** m_pTrampoline;
void* m_pTrampoline;

// Register storage
CRegisters* m_pRegisters;
Expand Down
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit b5648f8

Please sign in to comment.