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 std::uintptr_t get_address() const { return address_; }
27
29 virtual Result<T> get_value() const;
30
34 template <MemoryIOCompatible<T> U>
35 // this constraint permits a simplified writing interface
36 // e.g., a std::string can be written to a char* buffer
38 T set_value(const U& val) const;
39
45 T zero() const
46 requires(MemoryWriteSupported<T>);
47
48protected:
49 Result<T> get_value_impl() const;
50
51 Program& get_program() const { return *prog_; }
52
53private:
54 Program* prog_;
55
56 std::uintptr_t address_;
57};
58
59template <typename T>
60 requires(MemoryReadSupported<T>)
61template <MemoryIOCompatible<T> U>
62 requires(MemoryWriteSupported<U>)
63T AsmData<T>::set_value(const U& val) const {
64 auto prev = TRY_OR_THROW(get_value(), "could not read previous data value");
65
66 MemoryIOBase& mio = prog_->get_subproc().get_tracer().get_memory_io();
67 TRY_OR_THROW(mio.write(address_, val), "could not set data value");
68
69 return prev;
70}
71
72template <typename T>
76{
77 if constexpr (std::default_initializable<T>) {
78 return set_value(T{});
79 } else {
80 UNIMPLEMENTED("zero-initialization is not implemented for types that are not default initializable!");
81 }
82}
83
84template <typename T>
87 MemoryIOBase& mio = prog_->get_subproc().get_tracer().get_memory_io();
88
89 return mio.read<T>(address_);
90}
91
92template <typename T>
95 auto value = get_value_impl();
96
97 std::string val_str = "<unformattable>";
98 if constexpr (fmt::formattable<T>) {
99 val_str = fmt::format("{}", value);
100 }
101 LOG_DEBUG("Read value {} for @ 0x{:X}", val_str, address_);
102
103 return value;
104}
105
106template <typename T>
107 requires(MemoryReadSupported<T>)
108AsmData<T>::AsmData(Program& prog, std::uintptr_t address)
109 : prog_{&prog}
110 , address_{address} {}
111
112} // namespace asmgrader
Definition asm_data.hpp:20
std::uintptr_t get_address() const
Definition asm_data.hpp:26
T zero() const
Zeros the object of type T residing in the asm program.
Definition asm_data.hpp:74
T set_value(const U &val) const
Set the value of type T in the asm program.
Definition asm_data.hpp:63
AsmData(Program &prog, std::uintptr_t address)
Definition asm_data.hpp:108
virtual ~AsmData()=default
virtual Result< T > get_value() const
Get the value currently present in the asm program.
Definition asm_data.hpp:94
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
Result< std::size_t > write(std::uintptr_t address, const T &data)
Definition memory_io_base.hpp:85
Result< T > read(std::uintptr_t address)
Definition memory_io_base.hpp:30
Definition program.hpp:31
#define TRY_OR_THROW(expr,...)
Definition macros.hpp:11
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:52
#define LOG_DEBUG(...)
Definition logging.hpp:44
Definition asm_buffer.hpp:19