AsmGrader 0.0.0
Loading...
Searching...
No Matches
asm_data.hpp
Go to the documentation of this file.
1#pragma once
2
8
9#include <fmt/base.h>
10
11#include <concepts>
12#include <cstdint>
13#include <string>
14
15namespace asmgrader {
16
17template <typename T>
18 requires(MemoryReadSupported<T>)
20{
21public:
22 AsmData(Program& prog, std::uintptr_t address);
23
24 virtual ~AsmData() = default;
25
26 // NOLINTNEXTLINE(google-runtime-operator) - nicer semantics?
27 std::uintptr_t operator&() const { return address_; }
28
29 std::uintptr_t get_address() const { return address_; }
30
33
37 template <MemoryIOCompatible<T> U>
38 // this constraint permits a simplified writing interface
39 // e.g., a std::string can be written to a char* buffer
41 T set_value(const U& val) const;
42
48 T zero() const
49 requires(MemoryWriteSupported<T>);
50
51protected:
52 Result<T> get_value_impl() const;
53
54 Program& get_program() const { return *prog_; }
55
56private:
57 Program* prog_;
58
59 std::uintptr_t address_;
60};
61
62template <typename T>
63 requires(MemoryReadSupported<T>)
64template <MemoryIOCompatible<T> U>
65 requires(MemoryWriteSupported<U>)
66T AsmData<T>::set_value(const U& val) const {
67 auto prev = TRY_OR_THROW(get_value(), "could not read previous data value");
68
69 MemoryIOBase& mio = prog_->get_subproc().get_tracer().get_memory_io();
70 TRY_OR_THROW(mio.write(address_, val), "could not set data value");
71
72 return prev;
73}
74
75template <typename T>
79{
80 if constexpr (std::default_initializable<T>) {
81 return set_value(T{});
82 } else {
83 UNIMPLEMENTED("zero-initialization is not implemented for types that are not default initializable!");
84 }
85}
86
87template <typename T>
90 MemoryIOBase& mio = prog_->get_subproc().get_tracer().get_memory_io();
91
92 return mio.read<T>(address_);
93}
94
95template <typename T>
98 auto value = get_value_impl();
99
100 std::string val_str = "<unformattable>";
101 if constexpr (fmt::formattable<T>) {
102 val_str = fmt::format("{}", value);
103 }
104 LOG_DEBUG("Read value {} for @ 0x{:X}", val_str, address_);
105
106 return value;
107}
108
109template <typename T>
110 requires(MemoryReadSupported<T>)
111AsmData<T>::AsmData(Program& prog, std::uintptr_t address)
112 : prog_{&prog}
113 , address_{address} {}
114
115} // namespace asmgrader
Definition asm_data.hpp:20
std::uintptr_t operator&() const
Definition asm_data.hpp:27
std::uintptr_t get_address() const
Definition asm_data.hpp:29
T zero() const
Zeros the object of type T residing in the asm program.
Definition asm_data.hpp:77
T set_value(const U &val) const
Set the value of type T in the asm program.
Definition asm_data.hpp:66
AsmData(Program &prog, std::uintptr_t address)
Definition asm_data.hpp:111
virtual ~AsmData()=default
Result< T > get_value() const
Get the value currently present in the asm program.
Definition asm_data.hpp:97
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:25
Result< std::size_t > write(std::uintptr_t address, const T &data)
Definition memory_io_base.hpp:87
Result< T > read(std::uintptr_t address)
Definition memory_io_base.hpp:32
Definition program.hpp:32
Definition concepts.hpp:23
Definition concepts.hpp:27
#define UNIMPLEMENTED(msg,...)
For features that have not yet / are not planned to be implemented, so that I don't bang my head agai...
Definition logging.hpp:49
#define LOG_DEBUG(...)
Definition logging.hpp:41
#define TRY_OR_THROW(expr,...)
Definition macros.hpp:11
Definition asm_buffer.hpp:20