diff --git a/src/common/utils/function.hpp b/src/common/utils/function.hpp index 242e9e08..b9925e99 100644 --- a/src/common/utils/function.hpp +++ b/src/common/utils/function.hpp @@ -21,25 +21,21 @@ namespace utils { } - template >> + template + requires(std::is_invocable_r_v) optional_function(F&& f) : func(std::forward(f)) { } - optional_function& operator=(std::function f) + template + requires(std::is_invocable_r_v) + optional_function& operator=(F&& f) { - func = std::move(f); + func = std::forward(f); return *this; } - template - requires(!std::is_same_v, std::function>) - optional_function& operator=(T&& t) - { - return this->operator=(std::function(std::forward(t))); - } - Ret operator()(Args... args) const { if (func) diff --git a/src/windows-emulator/module/module_manager.cpp b/src/windows-emulator/module/module_manager.cpp index cf6ab2c9..f2ae5791 100644 --- a/src/windows-emulator/module/module_manager.cpp +++ b/src/windows-emulator/module/module_manager.cpp @@ -95,6 +95,7 @@ mapped_module* module_manager::map_local_module(const std::filesystem::path& fil const auto image_base = mod.image_base; const auto entry = this->modules_.try_emplace(image_base, std::move(mod)); + this->on_module_load(entry.first->second); return &entry.first->second; } catch (const std::exception& e) @@ -146,6 +147,7 @@ bool module_manager::unmap(const uint64_t address, const logger& logger) logger.log("Unmapping %s (0x%" PRIx64 ")\n", mod->second.path.generic_string().c_str(), mod->second.image_base); + this->on_module_unload(mod->second); unmap_module(*this->memory_, mod->second); this->modules_.erase(mod); diff --git a/src/windows-emulator/module/module_manager.hpp b/src/windows-emulator/module/module_manager.hpp index 7d691a0f..6f729f89 100644 --- a/src/windows-emulator/module/module_manager.hpp +++ b/src/windows-emulator/module/module_manager.hpp @@ -3,6 +3,7 @@ #include "mapped_module.hpp" #include "../file_system.hpp" +#include class logger; @@ -10,6 +11,9 @@ class module_manager { public: using module_map = std::map; + utils::optional_function on_module_load{}; + utils::optional_function on_module_unload{}; + module_manager(memory_manager& memory, file_system& file_sys); void map_main_modules(const windows_path& executable_path, const windows_path& ntdll_path, diff --git a/src/windows-emulator/process_context.cpp b/src/windows-emulator/process_context.cpp index 7dbe27a6..0a15cb49 100644 --- a/src/windows-emulator/process_context.cpp +++ b/src/windows-emulator/process_context.cpp @@ -201,5 +201,7 @@ handle process_context::create_thread(memory_manager& memory, const uint64_t sta const uint64_t stack_size) { emulator_thread t{memory, *this, start_address, argument, stack_size, ++this->spawned_thread_count}; - return this->threads.store(std::move(t)); + auto h = this->threads.store(std::move(t)); + on_create_thread(h, *this->threads.get(h)); + return h; } diff --git a/src/windows-emulator/process_context.hpp b/src/windows-emulator/process_context.hpp index 339807f6..e03b3465 100644 --- a/src/windows-emulator/process_context.hpp +++ b/src/windows-emulator/process_context.hpp @@ -44,6 +44,8 @@ struct process_context const emulator_settings& emu_settings, const mapped_module& executable, const mapped_module& ntdll, const apiset::container& apiset_container); + utils::optional_function on_create_thread{}; + handle create_thread(memory_manager& memory, const uint64_t start_address, const uint64_t argument, const uint64_t stack_size); diff --git a/src/windows-emulator/syscalls.cpp b/src/windows-emulator/syscalls.cpp index 088f9fcd..872f6ccb 100644 --- a/src/windows-emulator/syscalls.cpp +++ b/src/windows-emulator/syscalls.cpp @@ -3613,6 +3613,7 @@ namespace } thread->exit_status = exit_status; + c.win_emu.callbacks.thread_terminated(thread_handle, *thread); if (thread == c.proc.active_thread) { c.win_emu.yield_thread(); diff --git a/src/windows-emulator/windows_emulator.cpp b/src/windows-emulator/windows_emulator.cpp index 5f20f4c1..abd92afc 100644 --- a/src/windows-emulator/windows_emulator.cpp +++ b/src/windows-emulator/windows_emulator.cpp @@ -220,6 +220,10 @@ void windows_emulator::setup_process(const application_settings& app_settings, c const auto& emu = this->emu(); auto& context = this->process; + mod_manager.on_module_load = std::move(callbacks.module_loaded); + mod_manager.on_module_unload = std::move(callbacks.module_unloaded); + context.on_create_thread = std::move(callbacks.thread_created); + this->mod_manager.map_main_modules(app_settings.application, R"(C:\Windows\System32\ntdll.dll)", R"(C:\Windows\System32\win32u.dll)", this->log); diff --git a/src/windows-emulator/windows_emulator.hpp b/src/windows-emulator/windows_emulator.hpp index 6665dc55..b6d6ef95 100644 --- a/src/windows-emulator/windows_emulator.hpp +++ b/src/windows-emulator/windows_emulator.hpp @@ -23,6 +23,10 @@ struct emulator_callbacks std::string_view syscall_name, x64_emulator::pointer_type prev_address, std::string_view prev_mod_name)> outofline_syscall{}; + utils::optional_function module_loaded{}; + utils::optional_function module_unloaded{}; + utils::optional_function thread_created{}; + utils::optional_function thread_terminated{}; }; struct application_settings @@ -53,7 +57,6 @@ class windows_emulator public: std::filesystem::path emulation_root{}; - emulator_callbacks callbacks{}; logger log{}; file_system file_sys; memory_manager memory; @@ -61,6 +64,7 @@ class windows_emulator module_manager mod_manager; process_context process; syscall_dispatcher dispatcher; + emulator_callbacks callbacks{}; windows_emulator(const emulator_settings& settings = {}, std::unique_ptr emu = create_default_x64_emulator());