From 0721d018ec0cd2d5677c23133c32fc023ee8908e Mon Sep 17 00:00:00 2001 From: praydog Date: Thu, 28 Mar 2024 15:41:04 -0700 Subject: [PATCH] HookManager: Modify to use thread based storage, safer stack behavior --- src/HookManager.cpp | 181 +++++++++++++++++++++++++++++--------------- src/HookManager.hpp | 43 ++++++++++- 2 files changed, 159 insertions(+), 65 deletions(-) diff --git a/src/HookManager.cpp b/src/HookManager.cpp index f6a15a196..2be6f38c6 100644 --- a/src/HookManager.cpp +++ b/src/HookManager.cpp @@ -57,11 +57,16 @@ HookManager::HookedFn::~HookedFn() { } HookManager::PreHookResult HookManager::HookedFn::on_pre_hook() { + std::shared_lock _{this->access_mux}; + auto any_skipped = false; + auto storage = get_storage(this); + const auto ret_addr_pre = storage->ret_addr_pre; + for (const auto& cb : cbs) { if (cb.pre_fn) { - if (cb.pre_fn(args, arg_tys, ret_addr_pre) == PreHookResult::SKIP_ORIGINAL) { + if (cb.pre_fn(storage->args_impl, arg_tys, ret_addr_pre) == PreHookResult::SKIP_ORIGINAL) { any_skipped = true; } } @@ -71,6 +76,12 @@ HookManager::PreHookResult HookManager::HookedFn::on_pre_hook() { } void HookManager::HookedFn::on_post_hook() { + std::shared_lock _{this->access_mux}; + + auto storage = get_storage(this); + auto& ret_val = storage->ret_val; + auto& ret_addr = storage->ret_addr; + for (const auto& cb : cbs) { if (cb.post_fn) { cb.post_fn(ret_val, ret_ty, ret_addr); @@ -79,7 +90,7 @@ void HookManager::HookedFn::on_post_hook() { } void HookManager::create_jitted_facilitator(std::unique_ptr& hook, sdk::REMethodDefinition* fn, std::function hook_initialization, std::function hook_create) { - auto& args = hook->args; + auto& args = hook->get_storage(hook.get())->args_impl; auto& arg_tys = hook->arg_tys; auto& fn_hook = hook->fn_hook; @@ -96,33 +107,42 @@ void HookManager::create_jitted_facilitator(std::unique_ptr