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