diff --git a/include/safetyhook/thread_freezer.hpp b/include/safetyhook/thread_freezer.hpp index 04361f2..74ea23d 100644 --- a/include/safetyhook/thread_freezer.hpp +++ b/include/safetyhook/thread_freezer.hpp @@ -6,15 +6,11 @@ #include #include -#if __has_include() -#include -#elif __has_include() -#include -#else -#error "Windows.h not found" -#endif - namespace safetyhook { +using ThreadId = uint32_t; +using ThreadHandle = void*; +using ThreadContext = void*; + /// @brief Executes a function while all other threads are frozen. Also allows for visiting each frozen thread and /// modifying it's context. /// @param run_fn The function to run while all other threads are frozen. @@ -22,12 +18,12 @@ namespace safetyhook { /// @note The visit function will be called in the order that the threads were frozen. /// @note The visit function will be called before the run function. /// @note Keep the logic inside run_fn and visit_fn as simple as possible to avoid deadlocks. -void execute_while_frozen( - const std::function& run_fn, const std::function& visit_fn = {}); +void execute_while_frozen(const std::function& run_fn, + const std::function& visit_fn = {}); /// @brief Will modify the context of a thread's IP to point to a new address if its IP is at the old address. /// @param ctx The thread context to modify. /// @param old_ip The old IP address. /// @param new_ip The new IP address. -void fix_ip(CONTEXT& ctx, uint8_t* old_ip, uint8_t* new_ip); +void fix_ip(ThreadContext ctx, uint8_t* old_ip, uint8_t* new_ip); } // namespace safetyhook \ No newline at end of file diff --git a/src/inline_hook.cpp b/src/inline_hook.cpp index 4a311bc..e0acc03 100644 --- a/src/inline_hook.cpp +++ b/src/inline_hook.cpp @@ -337,7 +337,7 @@ std::expected InlineHook::e9_hook(const std::shared_ptr error = result.error(); } }, - [this](uint32_t, HANDLE, CONTEXT& ctx) { + [this](auto, auto, auto ctx) { for (size_t i = 0; i < m_original_bytes.size(); ++i) { fix_ip(ctx, m_target + i, m_trampoline.data() + i); } @@ -404,7 +404,7 @@ std::expected InlineHook::ff_hook(const std::shared_ptr error = result.error(); } }, - [this](uint32_t, HANDLE, CONTEXT& ctx) { + [this](auto, auto, auto ctx) { for (size_t i = 0; i < m_original_bytes.size(); ++i) { fix_ip(ctx, m_target + i, m_trampoline.data() + i); } @@ -431,7 +431,7 @@ void InlineHook::destroy() { std::copy(m_original_bytes.begin(), m_original_bytes.end(), m_target); } }, - [this](uint32_t, HANDLE, CONTEXT& ctx) { + [this](auto, auto, auto ctx) { for (size_t i = 0; i < m_original_bytes.size(); ++i) { fix_ip(ctx, m_trampoline.data() + i, m_target + i); } diff --git a/src/thread_freezer.cpp b/src/thread_freezer.cpp index a0438b6..1ac88d9 100644 --- a/src/thread_freezer.cpp +++ b/src/thread_freezer.cpp @@ -20,7 +20,7 @@ NtGetNextThread(HANDLE ProcessHandle, HANDLE ThreadHandle, ACCESS_MASK DesiredAc namespace safetyhook { void execute_while_frozen( - const std::function& run_fn, const std::function& visit_fn) { + const std::function& run_fn, const std::function& visit_fn) { // Freeze all threads. int num_threads_frozen; auto first_run = true; @@ -73,7 +73,8 @@ void execute_while_frozen( } if (visit_fn) { - visit_fn(thread_id, thread, thread_ctx); + visit_fn(static_cast(thread_id), static_cast(thread), + static_cast(&thread_ctx)); } ++num_threads_frozen; @@ -116,11 +117,13 @@ void execute_while_frozen( } } -void fix_ip(CONTEXT& ctx, uint8_t* old_ip, uint8_t* new_ip) { +void fix_ip(ThreadContext thread_ctx, uint8_t* old_ip, uint8_t* new_ip) { + auto* ctx = reinterpret_cast(thread_ctx); + #ifdef _M_X64 - auto ip = ctx.Rip; + auto ip = ctx->Rip; #else - auto ip = ctx.Eip; + auto ip = ctx->Eip; #endif if (ip == reinterpret_cast(old_ip)) { @@ -128,9 +131,9 @@ void fix_ip(CONTEXT& ctx, uint8_t* old_ip, uint8_t* new_ip) { } #ifdef _M_X64 - ctx.Rip = ip; + ctx->Rip = ip; #else - ctx.Eip = ip; + ctx->Eip = ip; #endif } } // namespace safetyhook \ No newline at end of file