Skip to content

Commit

Permalink
Fix deserialization with 'used' emulator instance (#113)
Browse files Browse the repository at this point in the history
  • Loading branch information
momo5502 authored Jan 26, 2025
2 parents 780ff47 + 82a99ee commit d1e7080
Show file tree
Hide file tree
Showing 9 changed files with 100 additions and 17 deletions.
5 changes: 5 additions & 0 deletions src/emulator/emulator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
23 changes: 16 additions & 7 deletions src/emulator/memory_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions src/emulator/memory_manager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
12 changes: 12 additions & 0 deletions src/emulator/serialization.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,12 @@ namespace utils
return object;
}

template <typename T>
void read_atomic(std::atomic<T>& val)
{
val = this->read<T>();
}

template <typename T>
void read_optional(std::optional<T>& val)
{
Expand Down Expand Up @@ -390,6 +396,12 @@ namespace utils
}
}

template <typename T>
void write_atomic(const std::atomic<T>& val)
{
this->write(val.load());
}

template <typename T>
void write_optional(const std::optional<T>& val)
{
Expand Down
27 changes: 27 additions & 0 deletions src/windows-emulator-test/serialization_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,33 @@

namespace test
{
TEST(SerializationTest, ResettingEmulatorWorks)
{
auto emu = create_sample_emulator();

utils::buffer_serializer start_state{};
emu.serialize(start_state);

emu.start();

ASSERT_TERMINATED_SUCCESSFULLY(emu);

utils::buffer_serializer end_state1{};
emu.serialize(end_state1);

utils::buffer_deserializer deserializer{start_state.get_buffer()};
emu.deserialize(deserializer);

emu.start();

ASSERT_TERMINATED_SUCCESSFULLY(emu);

utils::buffer_serializer end_state2{};
emu.serialize(end_state2);

ASSERT_EQ(end_state1.get_buffer(), end_state2.get_buffer());
}

TEST(SerializationTest, SerializedDataIsReproducible)
{
auto emu1 = create_sample_emulator();
Expand Down
15 changes: 15 additions & 0 deletions src/windows-emulator/process_context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,11 @@ class moved_marker
return this->was_moved_;
}

void mark_as_moved()
{
this->was_moved_ = true;
}

private:
bool was_moved_{false};
};
Expand Down Expand Up @@ -498,6 +503,11 @@ class emulator_thread : ref_counted_object
buffer.read_vector(this->last_registers);
}

void leak_memory()
{
this->marker.mark_as_moved();
}

private:
void setup_registers(x64_emulator& emu, const process_context& context) const;

Expand Down Expand Up @@ -657,6 +667,11 @@ struct process_context
buffer.read_vector(this->default_register_set);
buffer.read(this->spawned_thread_count);

for (auto& thread : this->threads | std::views::values)
{
thread.leak_memory();
}

buffer.read(this->threads);

this->active_thread = this->threads.get(buffer.read<uint64_t>());
Expand Down
12 changes: 9 additions & 3 deletions src/windows-emulator/syscall_dispatcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ void syscall_dispatcher::dispatch(windows_emulator& win_emu)
if (mod != context.ntdll && mod != context.win32u)
{
win_emu.callbacks().inline_syscall(syscall_id, address, mod ? mod->name.c_str() : "<N/A>",
entry->second.name.c_str());
entry->second.name);

win_emu.log.print(color::blue, "Executing inline syscall: %s (0x%X) at 0x%" PRIx64 " (%s)\n",
entry->second.name.c_str(), syscall_id, address, mod ? mod->name.c_str() : "<N/A>");
}
Expand All @@ -101,7 +102,10 @@ void syscall_dispatcher::dispatch(windows_emulator& win_emu)
if (mod->is_within(context.previous_ip))
{
const auto rsp = c.emu.read_stack_pointer();
const auto return_address = c.emu.read_memory<uint64_t>(rsp);

uint64_t return_address{};
c.emu.try_read_memory(rsp, &return_address, sizeof(return_address));

const auto* mod_name = context.mod_manager.find_name(return_address);

win_emu.log.print(color::dark_gray,
Expand All @@ -111,9 +115,11 @@ void syscall_dispatcher::dispatch(windows_emulator& win_emu)
else
{
const auto* previous_mod = context.mod_manager.find_by_address(context.previous_ip);

win_emu.callbacks().outofline_syscall(syscall_id, address, mod ? mod->name.c_str() : "<N/A>",
entry->second.name.c_str(), context.previous_ip,
entry->second.name, context.previous_ip,
previous_mod ? previous_mod->name.c_str() : "<N/A>");

win_emu.log.print(color::blue,
"Crafted out-of-line syscall: %s (0x%X) at 0x%" PRIx64 " (%s) via 0x%" PRIx64
" (%s)\n",
Expand Down
17 changes: 11 additions & 6 deletions src/windows-emulator/windows_emulator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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();
}

Expand Down Expand Up @@ -960,7 +960,10 @@ void windows_emulator::on_instruction_execution(const uint64_t address)
if (export_entry != binary->address_names.end())
{
const auto rsp = this->emu().read_stack_pointer();
const auto return_address = this->emu().read_memory<uint64_t>(rsp);

uint64_t return_address{};
this->emu().try_read_memory(rsp, &return_address, sizeof(return_address));

const auto* mod_name = this->process().mod_manager.find_name(return_address);

log.print(is_interesting_call ? color::yellow : color::dark_gray,
Expand Down Expand Up @@ -1088,14 +1091,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;
}
Expand Down Expand Up @@ -1128,6 +1131,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);
Expand All @@ -1145,6 +1149,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);

Expand Down
4 changes: 3 additions & 1 deletion src/windows-emulator/windows_emulator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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<x64_emulator> emu_{};
std::vector<instruction_hook_callback> syscall_hooks_{};

Expand Down

0 comments on commit d1e7080

Please sign in to comment.