AsmGrader 0.0.0
Loading...
Searching...
No Matches
memory_io_serde.hpp
Go to the documentation of this file.
1#pragma once
2
6
7#include <range/v3/algorithm/copy.hpp>
8
9#include <array>
10#include <concepts>
11#include <cstddef>
12#include <cstdint>
13#include <ctime>
14#include <iterator>
15#include <string>
16#include <string_view>
17#include <type_traits>
18#include <vector>
19
20namespace asmgrader {
21
30template <typename T>
32
33namespace detail {
34
35// FIXME: Some of this is probably UB
36
37template <typename T>
39 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
40 const auto* first = reinterpret_cast<const std::byte*>(&data);
41
42 // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
43 return ByteVector{first, first + sizeof(data)};
44}
45
46template <typename T>
47const T* reinterpret_raw(const std::vector<std::byte>& data) {
48 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
49 return reinterpret_cast<const T*>(data.data());
50}
51
52template <typename T>
54 constexpr std::size_t VALUE_SIZE = sizeof(typename T::value_type);
55 const std::size_t size = std::size(range) * VALUE_SIZE;
56 ByteVector result(size);
57
58 for (auto it = result.begin(); const auto& elem : range) {
59 it = ranges::copy(reinterpret_raw(elem), it).out;
60 }
61
62 return result;
63}
64
65} // namespace detail
66
67template <>
69{
70 static ByteVector to_bytes(const ByteVector& data) { return data; }
71};
72
73// "Raw" conversions (essentially just reinterpret_cast)
74template <typename T>
75 requires std::is_arithmetic_v<T> || std::same_as<T, std::timespec> || std::is_pointer_v<T> ||
76 std::is_bounded_array_v<T> || (std::is_aggregate_v<T> && !std::is_array_v<T>)
78{
79 static Result<T> read(std::uintptr_t address, MemoryIOBase& mio) {
80 const auto raw_data = TRY(mio.read_block_impl(address, sizeof(T)));
81
82 return raw_data.bit_cast_to<T>();
83 }
84
85 static ByteVector to_bytes(const T& data) { return detail::reinterpret_raw(data); }
86};
87
88template <>
89struct MemoryIOSerde<std::string>
90{
91 static Result<std::string> read(std::uintptr_t address, MemoryIOBase& mio) {
92 auto is_null_term = [](std::byte chr) { return std::to_integer<char>(chr) == '\0'; };
93 const auto raw_data = TRY(mio.read_until(address, is_null_term));
94
95 return data_to_str(raw_data);
96 }
97
98 static ByteVector to_bytes(const std::string& data) {
99 // Include '\0'
100 auto raw_data = ByteVector::from(data);
101 raw_data.push_back(static_cast<std::byte>('\0'));
102
103 return raw_data;
104 }
105
106 static std::string data_to_str(const ByteVector& data) { return data.to_range<std::string>(); }
107};
108
109template <std::size_t Length>
111{
112 static Result<NonTermString<Length>> read(std::uintptr_t address, MemoryIOBase& mio) {
113 const auto raw_data = TRY(mio.read_block_impl(address, Length));
114
115 return {.string = MemoryIOSerde<std::string>::data_to_str(raw_data)};
116 }
117
119 return detail::reinterpret_raw_each(std::string_view{data.string, data.string + data.LENGTH});
120 }
121};
122
123} // namespace asmgrader
Definition byte_vector.hpp:32
auto begin()
Definition byte_vector.hpp:84
Range to_range() const
T should be a stdlib-compatible container type where std::byte is convertible to T::value_type.
Definition byte_vector.hpp:117
static ByteVector from(const Range &range)
Definition byte_vector.hpp:142
std::variant wrapper for a partial implementation of C++23's expected type
Definition expected.hpp:34
Base class for interacting with a tracee's memory in a variety of ways at a (relatively) high-level F...
Definition memory_io_base.hpp:23
#define TRY(val)
If the supplied argument is an error (unexpected) type, then propegate it up the call stack....
Definition error_types.hpp:46
ByteVector reinterpret_raw(const T &data)
Definition memory_io_serde.hpp:38
ByteVector reinterpret_raw_each(const T &range)
Definition memory_io_serde.hpp:53
Definition asm_buffer.hpp:19
static ByteVector to_bytes(const ByteVector &data)
Definition memory_io_serde.hpp:70
static ByteVector to_bytes(const NonTermString< Length > &data)
Definition memory_io_serde.hpp:118
static Result< NonTermString< Length > > read(std::uintptr_t address, MemoryIOBase &mio)
Definition memory_io_serde.hpp:112
static Result< T > read(std::uintptr_t address, MemoryIOBase &mio)
Definition memory_io_serde.hpp:79
static ByteVector to_bytes(const T &data)
Definition memory_io_serde.hpp:85
static ByteVector to_bytes(const std::string &data)
Definition memory_io_serde.hpp:98
static std::string data_to_str(const ByteVector &data)
Definition memory_io_serde.hpp:106
static Result< std::string > read(std::uintptr_t address, MemoryIOBase &mio)
Definition memory_io_serde.hpp:91
Example class implementation:
Definition memory_io_serde.hpp:31
Definition non_terminated_str.hpp:10
static constexpr auto LENGTH
Definition non_terminated_str.hpp:12
const char * string
Definition non_terminated_str.hpp:11