From 995cc682f2d49a9036efd07d6926975a15a82a5d Mon Sep 17 00:00:00 2001 From: momo5502 Date: Fri, 23 Aug 2024 12:38:30 +0200 Subject: [PATCH] Small optimizations --- src/emulator/emulator.hpp | 4 ++-- src/emulator/typed_emulator.hpp | 18 +++--------------- src/unicorn_emulator/unicorn_x64_emulator.cpp | 7 ++++--- src/windows_emulator/syscalls.cpp | 2 +- 4 files changed, 10 insertions(+), 21 deletions(-) diff --git a/src/emulator/emulator.hpp b/src/emulator/emulator.hpp index d1fad6c2..9b8192a9 100644 --- a/src/emulator/emulator.hpp +++ b/src/emulator/emulator.hpp @@ -10,7 +10,7 @@ struct emulator_hook; using memory_operation = memory_permission; -using instruction_hook_callback = std::function; +using hook_callback = std::function; using simple_memory_hook_callback = std::function; using complex_memory_hook_callback = std::function; @@ -47,7 +47,7 @@ public: virtual emulator_hook* hook_memory_access(uint64_t address, size_t size, memory_operation filter, complex_memory_hook_callback callback) = 0; - virtual emulator_hook* hook_instruction(int instruction_type, instruction_hook_callback callback) = 0; + virtual emulator_hook* hook_instruction(int instruction_type, hook_callback callback) = 0; virtual void delete_hook(emulator_hook* hook) = 0; diff --git a/src/emulator/typed_emulator.hpp b/src/emulator/typed_emulator.hpp index 38db5a83..8ab14b06 100644 --- a/src/emulator/typed_emulator.hpp +++ b/src/emulator/typed_emulator.hpp @@ -2,8 +2,6 @@ #include "emulator.hpp" -using simple_instruction_hook_callback = std::function; - template class typed_emulator : public emulator @@ -62,23 +60,13 @@ public: return result; } - emulator_hook* hook_instruction(hookable_instructions instruction_type, instruction_hook_callback callback) + emulator_hook* hook_instruction(hookable_instructions instruction_type, hook_callback callback) { - return this->hook_instruction(instruction_type, [this, c = std::move(callback)] - { - const auto ip = static_cast(this->read_instruction_pointer()); - c(ip); - }); + return this->hook_instruction(static_cast(instruction_type), std::move(callback)); } - virtual emulator_hook* hook_instruction(hookable_instructions instruction_type, - simple_instruction_hook_callback callback) = 0; - private: - emulator_hook* hook_instruction(int instruction_type, instruction_hook_callback callback) override - { - return this->hook_instruction(static_cast(instruction_type), std::move(callback)); - } + emulator_hook* hook_instruction(int instruction_type, hook_callback callback) override = 0; void read_raw_register(int reg, void* value, size_t size) override = 0; void write_raw_register(int reg, const void* value, size_t size) override = 0; diff --git a/src/unicorn_emulator/unicorn_x64_emulator.cpp b/src/unicorn_emulator/unicorn_x64_emulator.cpp index eb821544..278cdfb0 100644 --- a/src/unicorn_emulator/unicorn_x64_emulator.cpp +++ b/src/unicorn_emulator/unicorn_x64_emulator.cpp @@ -243,10 +243,11 @@ namespace unicorn return result; } - emulator_hook* hook_instruction(x64_hookable_instructions instruction_type, - simple_instruction_hook_callback callback) + emulator_hook* hook_instruction(int instruction_type, + hook_callback callback) { - const auto uc_instruction = map_hookable_instruction(instruction_type); + const auto uc_instruction = map_hookable_instruction( + static_cast(instruction_type)); function_wrapper wrapper([c = std::move(callback)](uc_engine*) { diff --git a/src/windows_emulator/syscalls.cpp b/src/windows_emulator/syscalls.cpp index 04ccdf21..8b8ad8c4 100644 --- a/src/windows_emulator/syscalls.cpp +++ b/src/windows_emulator/syscalls.cpp @@ -507,7 +507,7 @@ namespace void handle_syscall(x64_emulator& emu, process_context& context) { - const auto address = emu.reg(x64_register::rip); + const auto address = emu.read_instruction_pointer(); const auto syscall_id = emu.reg(x64_register::eax); printf("Handling syscall: %X (%llX)\n", syscall_id, address);