Skip to content

Commit

Permalink
Implement serialization as a concept
Browse files Browse the repository at this point in the history
  • Loading branch information
momo5502 committed Sep 13, 2024
1 parent bdf179b commit 68cffae
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 30 deletions.
6 changes: 3 additions & 3 deletions src/emulator/emulator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ using memory_violation_hook_callback = std::function<memory_violation_continuati
uint64_t address, size_t size, memory_operation operation,
memory_violation_type type)>;

class emulator : public memory_manager, public utils::serializable
class emulator : public memory_manager
{
public:
emulator() = default;
Expand Down Expand Up @@ -97,12 +97,12 @@ class emulator : public memory_manager, public utils::serializable
return this->hook_simple_memory_access(address, size, std::move(callback), memory_operation::exec);
}

void serialize(utils::buffer_serializer& buffer) const final
void serialize(utils::buffer_serializer& buffer) const
{
this->perform_serialization(buffer, false);
}

void deserialize(utils::buffer_deserializer& buffer) final
void deserialize(utils::buffer_deserializer& buffer)
{
this->perform_deserialization(buffer, false);
}
Expand Down
13 changes: 11 additions & 2 deletions src/emulator/serialization.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,21 @@ namespace utils
class buffer_serializer;
class buffer_deserializer;

template <typename T>
concept Serializable = requires(T a, const T ac, buffer_serializer & serializer, buffer_deserializer & deserializer)
{
{ ac.serialize(serializer) } -> std::same_as<void>;
{ a.deserialize(deserializer) } -> std::same_as<void>;
};

/* Use concept instead, to prevent overhead of virtual function calls
struct serializable
{
virtual ~serializable() = default;
virtual void serialize(buffer_serializer& buffer) const = 0;
virtual void deserialize(buffer_deserializer& buffer) = 0;
};
*/

namespace detail
{
Expand Down Expand Up @@ -104,7 +113,7 @@ namespace utils
template <typename T>
void read(T& object)
{
if constexpr (std::is_base_of_v<serializable, T>)
if constexpr (Serializable<T>)
{
object.deserialize(*this);
}
Expand Down Expand Up @@ -258,7 +267,7 @@ namespace utils
template <typename T>
void write(const T& object)
{
if constexpr (std::is_base_of_v<serializable, T>)
if constexpr (Serializable<T>)
{
object.serialize(*this);
}
Expand Down
12 changes: 6 additions & 6 deletions src/windows_emulator/emulator_utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#include "memory_utils.hpp"

template <typename T>
class emulator_object : utils::serializable
class emulator_object
{
public:
using value_type = T;
Expand Down Expand Up @@ -68,12 +68,12 @@ class emulator_object : utils::serializable
this->write(obj, index);
}

void serialize(utils::buffer_serializer& buffer) const override
void serialize(utils::buffer_serializer& buffer) const
{
buffer.write(this->address_);
}

void deserialize(utils::buffer_deserializer& buffer) override
void deserialize(utils::buffer_deserializer& buffer)
{
buffer.read(this->address_);
}
Expand All @@ -83,7 +83,7 @@ class emulator_object : utils::serializable
uint64_t address_{};
};

class emulator_allocator : utils::serializable
class emulator_allocator
{
public:
emulator_allocator(emulator& emu)
Expand Down Expand Up @@ -159,14 +159,14 @@ class emulator_allocator : utils::serializable
return this->size_;
}

void serialize(utils::buffer_serializer& buffer) const override
void serialize(utils::buffer_serializer& buffer) const
{
buffer.write(this->address_);
buffer.write(this->size_);
buffer.write(this->active_address_);
}

void deserialize(utils::buffer_deserializer& buffer) override
void deserialize(utils::buffer_deserializer& buffer)
{
buffer.read(this->address_);
buffer.read(this->size_);
Expand Down
8 changes: 4 additions & 4 deletions src/windows_emulator/handles.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ constexpr handle make_pseudo_handle(const uint32_t id, const handle_types::type
}

template <handle_types::type Type, typename T>
requires(std::is_base_of_v<utils::serializable, T>)
class handle_store : utils::serializable
requires(utils::Serializable<T>)
class handle_store
{
public:
handle store(T value)
Expand Down Expand Up @@ -135,12 +135,12 @@ class handle_store : utils::serializable
return this->erase(hh);
}

void serialize(utils::buffer_serializer& buffer) const override
void serialize(utils::buffer_serializer& buffer) const
{
buffer.write_map(this->store_);
}

void deserialize(utils::buffer_deserializer& buffer) override
void deserialize(utils::buffer_deserializer& buffer)
{
buffer.read_map(this->store_);
}
Expand Down
6 changes: 3 additions & 3 deletions src/windows_emulator/module/module_manager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#include "mapped_module.hpp"
#include <emulator.hpp>

class module_manager : public utils::serializable
class module_manager
{
public:
module_manager(emulator& emu);
Expand All @@ -20,8 +20,8 @@ class module_manager : public utils::serializable
return nullptr;
}

void serialize(utils::buffer_serializer& buffer) const override;
void deserialize(utils::buffer_deserializer& buffer) override;
void serialize(utils::buffer_serializer& buffer) const;
void deserialize(utils::buffer_deserializer& buffer);

private:
emulator* emu_{};
Expand Down
24 changes: 12 additions & 12 deletions src/windows_emulator/process_context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

#include <x64_emulator.hpp>

struct event : utils::serializable
struct event
{
bool signaled{};
EVENT_TYPE type{};
Expand All @@ -24,59 +24,59 @@ struct event : utils::serializable
return res;
}

void serialize(utils::buffer_serializer& buffer) const override
void serialize(utils::buffer_serializer& buffer) const
{
buffer.write(this->signaled);
buffer.write(this->type);
}

void deserialize(utils::buffer_deserializer& buffer) override
void deserialize(utils::buffer_deserializer& buffer)
{
buffer.read(this->signaled);
buffer.read(this->type);
}
};

struct file : utils::serializable
struct file
{
utils::nt::handle<INVALID_HANDLE_VALUE> handle{};
std::wstring name{};

void serialize(utils::buffer_serializer& buffer) const override
void serialize(utils::buffer_serializer& buffer) const
{
buffer.write(this->name);
// TODO: Serialize handle
}

void deserialize(utils::buffer_deserializer& buffer) override
void deserialize(utils::buffer_deserializer& buffer)
{
buffer.read(this->name);
this->handle = INVALID_HANDLE_VALUE;
}
};

struct semaphore : utils::serializable
struct semaphore
{
std::wstring name{};
volatile uint32_t current_count{};
uint32_t max_count{};

void serialize(utils::buffer_serializer& buffer) const override
void serialize(utils::buffer_serializer& buffer) const
{
buffer.write(this->name);
buffer.write(this->current_count);
buffer.write(this->max_count);
}

void deserialize(utils::buffer_deserializer& buffer) override
void deserialize(utils::buffer_deserializer& buffer)
{
buffer.read(this->name);
buffer.read(this->current_count);
buffer.read(this->max_count);
}
};

struct process_context : utils::serializable
struct process_context
{
process_context(x64_emulator& emu)
: emu(&emu)
Expand Down Expand Up @@ -112,7 +112,7 @@ struct process_context : utils::serializable

bool verbose{false};

void serialize(utils::buffer_serializer& buffer) const override
void serialize(utils::buffer_serializer& buffer) const
{
buffer.write(this->executed_instructions);
buffer.write(this->teb);
Expand All @@ -133,7 +133,7 @@ struct process_context : utils::serializable
buffer.write(this->gs_segment);
}

void deserialize(utils::buffer_deserializer& buffer) override
void deserialize(utils::buffer_deserializer& buffer)
{
buffer.read(this->executed_instructions);
buffer.read(this->teb);
Expand Down

0 comments on commit 68cffae

Please sign in to comment.