Skip to content

Commit

Permalink
Add memory reading/writing helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
momo5502 committed Nov 9, 2024
1 parent 641d87c commit 71232d5
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 17 deletions.
24 changes: 22 additions & 2 deletions src/emulator/memory_manager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,25 +33,45 @@ class memory_manager
virtual ~memory_manager() = default;

template <typename T>
T read_memory(const uint64_t address)
T read_memory(const uint64_t address) const
{
T value{};
this->read_memory(address, &value, sizeof(value));
return value;
}

template <typename T>
T read_memory(const void* address)
T read_memory(const void* address) const
{
return this->read_memory<T>(reinterpret_cast<uint64_t>(address));
}

std::vector<std::byte> read_memory(const uint64_t address, const size_t size) const
{
std::vector<std::byte> data{};
data.resize(size);

this->read_memory(address, data.data(), data.size());

return data;
}

std::vector<std::byte> read_memory(const void* address, const size_t size) const
{
return this->read_memory(reinterpret_cast<uint64_t>(address), size);
}

template <typename T>
void write_memory(const uint64_t address, const T& value)
{
this->write_memory(address, &value, sizeof(value));
}

void write_memory(void* address, const void* data, const size_t size)
{
this->write_memory(reinterpret_cast<uint64_t>(address), data, size);
}

virtual void read_memory(uint64_t address, void* data, size_t size) const = 0;
virtual bool try_read_memory(uint64_t address, void* data, size_t size) const = 0;
virtual void write_memory(uint64_t address, const void* data, size_t size) = 0;
Expand Down
24 changes: 9 additions & 15 deletions src/windows-emulator/devices/afd_endpoint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,7 @@ namespace

NTSTATUS ioctl_bind(windows_emulator& win_emu, const io_device_context& c) const
{
std::vector<std::byte> data{};
data.resize(c.input_buffer_length);
win_emu.emu().read_memory(c.input_buffer, data.data(), c.input_buffer_length);
const auto data = win_emu.emu().read_memory(c.input_buffer, c.input_buffer_length);

constexpr auto address_offset = 4;

Expand Down Expand Up @@ -206,13 +204,13 @@ namespace
return STATUS_UNSUCCESSFUL;
}

emu.write_memory(reinterpret_cast<uint64_t>(buffer.buf), data.data(),
std::min(data.size(), static_cast<size_t>(recevied_data)));
const auto data_size = std::min(data.size(), static_cast<size_t>(recevied_data));
emu.write_memory(buffer.buf, data.data(), data_size);

if (receive_info.Address && address_length)
{
emu.write_memory(reinterpret_cast<uint64_t>(receive_info.Address), address.data(),
std::min(address.size(), static_cast<size_t>(address_length)));
const auto address_size = std::min(address.size(), static_cast<size_t>(address_length));
emu.write_memory(receive_info.Address, address.data(), address_size);
}

if (c.io_status_block)
Expand All @@ -237,17 +235,13 @@ namespace
const auto send_info = emu.read_memory<AFD_SEND_DATAGRAM_INFO>(c.input_buffer);
const auto buffer = emu.read_memory<WSABUF>(send_info.BufferArray);

std::vector<std::byte> address{};
address.resize(send_info.TdiConnInfo.RemoteAddressLength);
emu.read_memory(reinterpret_cast<uint64_t>(send_info.TdiConnInfo.RemoteAddress), address.data(),
address.size());
const auto address = emu.read_memory(send_info.TdiConnInfo.RemoteAddress,
send_info.TdiConnInfo.RemoteAddressLength);

const network::address target(reinterpret_cast<sockaddr*>(address.data()),
const network::address target(reinterpret_cast<const sockaddr*>(address.data()),
static_cast<int>(address.size()));

std::vector<std::byte> data{};
data.resize(buffer.len);
emu.read_memory(reinterpret_cast<uint64_t>(buffer.buf), data.data(), data.size());
const auto data = emu.read_memory(buffer.buf, buffer.len);

const auto sent_data = sendto(*this->s, reinterpret_cast<const char*>(data.data()),
static_cast<int>(data.size()), 0 /* ? */, &target.get_addr(),
Expand Down
9 changes: 9 additions & 0 deletions src/windows-emulator/io_device.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,15 @@ class io_device_container : public io_device
this->device_->deserialize(buffer);
}

template <typename T = io_device>
requires(std::is_base_of_v<io_device, T> || std::is_same_v<io_device, T>)
T* get_internal_device()
{
this->assert_validity();
auto* value = this->device_.get();
return dynamic_cast<T*>(value);
}

private:
std::wstring device_name_{};
std::unique_ptr<io_device> device_{};
Expand Down

0 comments on commit 71232d5

Please sign in to comment.