From e34a9e64686146967f9687f0287803df27f5694c Mon Sep 17 00:00:00 2001 From: momo5502 Date: Sun, 26 Jan 2025 07:04:41 +0100 Subject: [PATCH] Serialization fixes --- src/emulator/emulator.hpp | 5 ++++ src/emulator/memory_manager.cpp | 23 +++++++++++++------ src/emulator/memory_manager.hpp | 2 ++ src/emulator/serialization.hpp | 12 ++++++++++ .../serialization_test.cpp | 19 +++++++++++++++ src/windows-emulator/windows_emulator.cpp | 12 ++++++---- src/windows-emulator/windows_emulator.hpp | 4 +++- 7 files changed, 64 insertions(+), 13 deletions(-) diff --git a/src/emulator/emulator.hpp b/src/emulator/emulator.hpp index 858902e4..91fc0d80 100644 --- a/src/emulator/emulator.hpp +++ b/src/emulator/emulator.hpp @@ -55,6 +55,11 @@ class emulator : public cpu_interface, public memory_manager, public hook_interf void perform_deserialization(utils::buffer_deserializer& buffer, const bool is_snapshot) { + if (!is_snapshot) + { + this->unmap_all_memory(); + } + this->deserialize_state(buffer, is_snapshot); this->deserialize_memory_state(buffer, is_snapshot); } diff --git a/src/emulator/memory_manager.cpp b/src/emulator/memory_manager.cpp index 46c5f944..c16eddb7 100644 --- a/src/emulator/memory_manager.cpp +++ b/src/emulator/memory_manager.cpp @@ -94,6 +94,7 @@ namespace utils void memory_manager::serialize_memory_state(utils::buffer_serializer& buffer, const bool is_snapshot) const { + buffer.write_atomic(this->memory_layout_state_version_); buffer.write_map(this->reserved_regions_); if (is_snapshot) @@ -125,15 +126,10 @@ void memory_manager::deserialize_memory_state(utils::buffer_deserializer& buffer { if (!is_snapshot) { - for (const auto& reserved_region : this->reserved_regions_) - { - for (const auto& region : reserved_region.second.committed_regions) - { - this->unmap_memory(region.first, region.second.length); - } - } + assert(this->reserved_regions_.empty()); } + buffer.read_atomic(this->memory_layout_state_version_); buffer.read_map(this->reserved_regions_); if (is_snapshot) @@ -434,6 +430,19 @@ bool memory_manager::release_memory(const uint64_t address, size_t size) return true; } +void memory_manager::unmap_all_memory() +{ + for (const auto& reserved_region : this->reserved_regions_) + { + for (const auto& region : reserved_region.second.committed_regions) + { + this->unmap_memory(region.first, region.second.length); + } + } + + this->reserved_regions_.clear(); +} + uint64_t memory_manager::find_free_allocation_base(const size_t size, const uint64_t start) const { uint64_t start_address = std::max(MIN_ALLOCATION_ADDRESS, start ? start : 0x100000000ULL); diff --git a/src/emulator/memory_manager.hpp b/src/emulator/memory_manager.hpp index 39899a2c..9b6969c6 100644 --- a/src/emulator/memory_manager.hpp +++ b/src/emulator/memory_manager.hpp @@ -101,6 +101,8 @@ class memory_manager bool release_memory(uint64_t address, size_t size); + void unmap_all_memory(); + uint64_t allocate_memory(const size_t size, const memory_permission permissions, const bool reserve_only = false) { const auto allocation_base = this->find_free_allocation_base(size); diff --git a/src/emulator/serialization.hpp b/src/emulator/serialization.hpp index 10da2567..14e2b6af 100644 --- a/src/emulator/serialization.hpp +++ b/src/emulator/serialization.hpp @@ -157,6 +157,12 @@ namespace utils return object; } + template + void read_atomic(std::atomic& val) + { + val = this->read(); + } + template void read_optional(std::optional& val) { @@ -390,6 +396,12 @@ namespace utils } } + template + void write_atomic(const std::atomic& val) + { + this->write(val.load()); + } + template void write_optional(const std::optional& val) { diff --git a/src/windows-emulator-test/serialization_test.cpp b/src/windows-emulator-test/serialization_test.cpp index 73ca3e05..d86a4fde 100644 --- a/src/windows-emulator-test/serialization_test.cpp +++ b/src/windows-emulator-test/serialization_test.cpp @@ -2,6 +2,25 @@ namespace test { + TEST(SerializationTest, ResettingEmulatorWorks) + { + auto emu = create_sample_emulator(); + + utils::buffer_serializer serializer{}; + emu.serialize(serializer); + + emu.start(); + + ASSERT_TERMINATED_SUCCESSFULLY(emu); + + utils::buffer_deserializer deserializer{serializer.get_buffer()}; + emu.deserialize(deserializer); + + emu.start(); + + ASSERT_TERMINATED_SUCCESSFULLY(emu); + } + TEST(SerializationTest, SerializedDataIsReproducible) { auto emu1 = create_sample_emulator(); diff --git a/src/windows-emulator/windows_emulator.cpp b/src/windows-emulator/windows_emulator.cpp index e72a1795..242927e3 100644 --- a/src/windows-emulator/windows_emulator.cpp +++ b/src/windows-emulator/windows_emulator.cpp @@ -897,13 +897,13 @@ void windows_emulator::setup_process(const emulator_settings& settings) void windows_emulator::yield_thread() { - this->switch_thread = true; + this->switch_thread_ = true; this->emu().stop(); } void windows_emulator::perform_thread_switch() { - this->switch_thread = false; + this->switch_thread_ = false; while (!switch_to_next_thread(*this)) { // TODO: Optimize that @@ -931,7 +931,7 @@ void windows_emulator::on_instruction_execution(const uint64_t address) const auto thread_insts = ++thread.executed_instructions; if (thread_insts % MAX_INSTRUCTIONS_PER_TIME_SLICE == 0) { - this->switch_thread = true; + this->switch_thread_ = true; this->emu().stop(); } @@ -1088,14 +1088,14 @@ void windows_emulator::start(std::chrono::nanoseconds timeout, size_t count) while (true) { - if (this->switch_thread || !this->current_thread().is_thread_ready(*this)) + if (this->switch_thread_ || !this->current_thread().is_thread_ready(*this)) { this->perform_thread_switch(); } this->emu().start_from_ip(timeout, count); - if (!this->switch_thread && !this->emu().has_violation()) + if (!this->switch_thread_ && !this->emu().has_violation()) { break; } @@ -1128,6 +1128,7 @@ void windows_emulator::start(std::chrono::nanoseconds timeout, size_t count) void windows_emulator::serialize(utils::buffer_serializer& buffer) const { + buffer.write(this->switch_thread_); buffer.write(this->use_relative_time_); this->file_sys().serialize(buffer); this->emu().serialize(buffer); @@ -1145,6 +1146,7 @@ void windows_emulator::deserialize(utils::buffer_deserializer& buffer) return windows_emulator_wrapper{*this}; // }); + buffer.read(this->switch_thread_); buffer.read(this->use_relative_time_); this->file_sys().deserialize(buffer); diff --git a/src/windows-emulator/windows_emulator.hpp b/src/windows-emulator/windows_emulator.hpp index 332f6f4c..0c1c1a51 100644 --- a/src/windows-emulator/windows_emulator.hpp +++ b/src/windows-emulator/windows_emulator.hpp @@ -119,7 +119,6 @@ class windows_emulator bool verbose_calls{false}; bool buffer_stdout{false}; bool fuzzing{false}; - bool switch_thread{false}; void yield_thread(); void perform_thread_switch(); @@ -155,8 +154,11 @@ class windows_emulator file_system file_sys_; emulator_callbacks callbacks_{}; + + bool switch_thread_{false}; bool use_relative_time_{false}; bool silent_until_main_{false}; + std::unique_ptr emu_{}; std::vector syscall_hooks_{};