AsmGrader 0.0.0
Loading...
Searching...
No Matches
asm_buffer.hpp
Go to the documentation of this file.
1#pragma once
2
8
9#include <range/v3/algorithm/fill.hpp>
10#include <range/v3/range/conversion.hpp>
11#include <range/v3/to_container.hpp>
12#include <range/v3/view/take_while.hpp>
13#include <range/v3/view/transform.hpp>
14
15#include <cstddef>
16#include <cstdint>
17#include <string>
18
19namespace asmgrader {
20
21template <std::size_t NumBytes>
22class AsmBuffer : public AsmData<ByteArray<NumBytes>>
23{
24public:
25 explicit AsmBuffer(Program& prog);
26
27 std::size_t size() const;
28
29 std::string str() const;
30
31 ByteArray<NumBytes> fill(std::byte byte) const;
32
33private:
34 static std::uintptr_t get_alloced_address(Program& prog) { return prog.alloc_mem(NumBytes); }
35};
36
37template <std::size_t NumBytes>
38std::size_t AsmBuffer<NumBytes>::size() const {
39 return NumBytes;
40}
41
42template <std::size_t NumBytes>
44 auto prev = TRY_OR_THROW(AsmData<ByteArray<NumBytes>>::get_value(), "could not read previous data value");
45
47 ranges::fill(buf, byte);
48
49 AsmData<ByteArray<NumBytes>>::set_value(buf);
50
51 return prev;
52}
53
54template <std::size_t NumBytes>
55std::string AsmBuffer<NumBytes>::str() const {
56 MemoryIOBase& mio = AsmData<ByteArray<NumBytes>>::get_program().get_subproc().get_tracer().get_memory_io();
57
58 auto addr = AsmData<ByteArray<NumBytes>>::get_address();
59
60 auto byte_str = TRY_OR_THROW(mio.read<ByteArray<NumBytes>>(addr), "failed to read buffer");
61
62 return byte_str | ranges::views::transform([](std::byte byte) { return std::to_integer<char>(byte); }) |
63 ranges::views::take_while([](char chr) { return chr != 0; }) | ranges::to<std::string>();
64}
65
66template <std::size_t NumBytes>
68 : AsmData<ByteArray<NumBytes>>{prog, get_alloced_address(prog)} {}
69
70} // namespace asmgrader
Definition asm_buffer.hpp:23
AsmBuffer(Program &prog)
Definition asm_buffer.hpp:67
std::size_t size() const
Definition asm_buffer.hpp:38
ByteArray< NumBytes > fill(std::byte byte) const
Definition asm_buffer.hpp:43
std::string str() const
Definition asm_buffer.hpp:55
Definition asm_data.hpp:20
Definition byte_array.hpp:23
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< T > read(std::uintptr_t address)
Definition memory_io_base.hpp:30
Definition program.hpp:31
std::uintptr_t alloc_mem(std::size_t amt)
Definition program.cpp:87
#define TRY_OR_THROW(expr,...)
Definition macros.hpp:11
Definition asm_buffer.hpp:19