Skip to content

Commit

Permalink
minhook fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
muhopensores committed Jan 2, 2023
1 parent 85d34d4 commit 37c43c6
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 6 deletions.
3 changes: 2 additions & 1 deletion dependencies/minhook/include/MinHook.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*
/*
* MinHook - The Minimalistic API Hooking Library for x64/x86
* Copyright (C) 2009-2017 Tsuda Kageyu.
* All rights reserved.
Expand Down Expand Up @@ -180,6 +180,7 @@ extern "C" {
// Translates the MH_STATUS to its name as a string.
const char * WINAPI MH_StatusToString(MH_STATUS status);

void WINAPI MH_SetSkipLocking(BOOL b);
#ifdef __cplusplus
}
#endif
Expand Down
18 changes: 17 additions & 1 deletion dependencies/minhook/src/hook.c
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ volatile LONG g_isLocked = FALSE;

// Private heap handle. If not NULL, this library is initialized.
HANDLE g_hHeap = NULL;

static BOOL g_skipLocks = FALSE;
// Hook entries.
struct
{
Expand All @@ -100,7 +100,14 @@ struct
UINT size; // Actual number of data items
} g_hooks;



//-------------------------------------------------------------------------
// NOTE(edit): hacks to avoid deadlocks when we already froze threads from outside
void WINAPI MH_SetSkipLocking(BOOL b) {
g_skipLocks = b;
}
//-------------------------------------------------------------------------
// Returns INVALID_HOOK_POS if not found.
static UINT FindHookEntry(LPVOID pTarget)
{
Expand Down Expand Up @@ -306,6 +313,10 @@ static VOID EnumerateThreads(PFROZEN_THREADS pThreads)
//-------------------------------------------------------------------------
static VOID Freeze(PFROZEN_THREADS pThreads, UINT pos, UINT action)
{
// NOTE(edit): deadlock hack
if (g_skipLocks) {
return;
}
pThreads->pItems = NULL;
pThreads->capacity = 0;
pThreads->size = 0;
Expand All @@ -330,6 +341,10 @@ static VOID Freeze(PFROZEN_THREADS pThreads, UINT pos, UINT action)
//-------------------------------------------------------------------------
static VOID Unfreeze(PFROZEN_THREADS pThreads)
{
// NOTE(edit): deadlock hack
if (g_skipLocks) {
return;
}
if (pThreads->pItems != NULL)
{
UINT i;
Expand Down Expand Up @@ -435,6 +450,7 @@ static MH_STATUS EnableAllHooksLL(BOOL enable)
//-------------------------------------------------------------------------
static VOID EnterSpinLock(VOID)
{

SIZE_T spinCount = 0;

// Wait until the flag is FALSE.
Expand Down
23 changes: 22 additions & 1 deletion src/ModFramework.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,10 @@ ModFramework::ModFramework()
#endif
// WARNING(): i hacked on init for time critical stuff here
// TimeCritical init stuff
#if 0
std::thread init_thread([this]() {
m_mods = std::make_unique<Mods>();
FunctionHook::set_mh_skip_locks(TRUE);
auto threads = utils::SuspendAllOtherThreads();
m_mods->load_time_critical_mods();
auto e = m_mods->on_initialize();
Expand All @@ -55,10 +57,29 @@ ModFramework::ModFramework()
}
}
utils::ResumeThreads(threads);
FunctionHook::set_mh_skip_locks(FALSE);
});

init_thread.detach();
std::this_thread::sleep_for(std::chrono::seconds(1));
#else
m_mods = std::make_unique<Mods>();
FunctionHook::set_mh_skip_locks(TRUE);
auto threads = utils::SuspendAllOtherThreads();
m_mods->load_time_critical_mods();
auto e = m_mods->on_initialize();

if (e) {
if (e->empty()) {
m_error = "An unknown error has occurred.";
}
else {
m_error = *e;
}
}
utils::ResumeThreads(threads);
FunctionHook::set_mh_skip_locks(FALSE);
#endif
/*std::this_thread::sleep_for(std::chrono::seconds(1));*/

m_d3d9_hook = std::make_unique<D3D9Hook>();

Expand Down
10 changes: 7 additions & 3 deletions src/utility/FunctionHook.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
using namespace std;


bool g_isMinHookInitialized{ false };
bool g_is_min_hook_initialized{ false };

FunctionHook::FunctionHook(Address target, Address destination)
: m_target{ 0 },
Expand All @@ -16,8 +16,8 @@ FunctionHook::FunctionHook(Address target, Address destination)
spdlog::info("Attempting to hook {:p}->{:p}", target.ptr(), destination.ptr());

// Initialize MinHook if it hasn't been already.
if (!g_isMinHookInitialized && MH_Initialize() == MH_OK) {
g_isMinHookInitialized = true;
if (!g_is_min_hook_initialized && MH_Initialize() == MH_OK) {
g_is_min_hook_initialized = true;
}

// Create the hook. Call create afterwards to prevent race conditions accessing FunctionHook before it leaves its constructor.
Expand Down Expand Up @@ -65,6 +65,10 @@ bool FunctionHook::disable() {
return true;
}

void FunctionHook::set_mh_skip_locks(BOOL b) {
MH_SetSkipLocking(b);
}

bool FunctionHook::remove() {
// Don't try to remove invalid hooks.
if (m_original == 0) {
Expand Down
2 changes: 2 additions & 0 deletions src/utility/FunctionHook.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ class FunctionHook {
return m_original != 0;
}

static void set_mh_skip_locks(BOOL b);

FunctionHook& operator=(const FunctionHook& other) = delete;
FunctionHook& operator=(FunctionHook&& other) = delete;

Expand Down

0 comments on commit 37c43c6

Please sign in to comment.