AsmGrader 0.0.0
Loading...
Searching...
No Matches
test_context.hpp
Go to the documentation of this file.
1#pragma once
2
12#include <asmgrader/logging.hpp>
17
18#include <fmt/base.h>
19#include <fmt/format.h>
20
21#include <array>
22#include <concepts>
23#include <cstddef>
24#include <cstdint>
25#include <functional>
26#include <optional>
27#include <string>
28#include <string_view>
29#include <type_traits>
30#include <utility>
31#include <vector>
32
33#include <sys/user.h>
34
35namespace asmgrader {
36
37class TestBase;
38
44{
45public:
46 explicit TestContext(TestBase& test, Program program,
47 std::function<void(const RequirementResult&)> on_requirement = common::noop) noexcept;
48
49 [[deprecated("Use require(Requirement)")]]
50 bool require(bool condition, const std::string& msg,
52 [[deprecated("Use require(Requirement)")]]
53 bool require(bool condition, RequirementResult::DebugInfo debug_info = RequirementResult::DebugInfo{});
54
55 template <exprs::Operator Op>
57
61
63
65 std::string_view get_name() const;
66
72 void restart_program();
73
74 // Low-level exececutor of an arbitrary syscall
75 Result<SyscallRecord> exec_syscall(u64 sys_nr, std::array<std::uint64_t, 6> args);
76
78 std::string get_stdout();
79
81 std::string get_full_stdout();
82
85 std::size_t flush_stdin();
86
88 const std::vector<SyscallRecord>& get_syscall_records() const;
89
92
94 void send_stdin(const std::string& input);
95
98 template <typename T>
99 AsmSymbol<T> find_symbol(std::string_view name);
100
102 template <std::size_t NumBytes>
104
106 template <typename Func>
107 AsmFunction<Func> find_function(std::string name);
108
110 RunResult run();
111
112private:
113 bool require_impl(bool condition, const std::string& description,
114 const std::optional<exprs::ExpressionRepr>& expression_repr,
115 const RequirementResult::DebugInfo& debug_info);
116
117 TestBase* associated_test_;
118 Program prog_;
119
121 TestResult result_;
122
123 // ########### Callbacks
124
126 std::function<void(const RequirementResult&)> on_requirement_;
127};
128
129template <exprs::Operator Op>
131 return require_impl(static_cast<bool>(req.get_res()), req.get_description(), req.get_expr_repr(), debug_info);
132}
133
134template <std::size_t NumBytes>
136 AsmBuffer<NumBytes> buffer{prog_};
137
138 return buffer;
139}
140
141template <typename T>
143 static_assert(MemoryReadSupported<T>, "Specified type does not have supported memory read operation! See docs.");
144
145 auto symbol = prog_.get_symtab().find(name);
146
147 if (!symbol) {
148 LOG_DEBUG("Could not resolve symbol {:?}", name);
149 return {prog_, std::string{name}, ErrorKind::UnresolvedSymbol};
150 }
151
152 return {prog_, std::string{name}, symbol->address};
153}
154
155template <typename Func>
157 auto loc = prog_.get_symtab().find(name);
158
159 if (!loc) {
160 return {prog_, std::move(name), ErrorKind::UnresolvedSymbol};
161 }
162
163 return {prog_, std::move(name), loc->address};
164}
165
166} // namespace asmgrader
Definition asm_buffer.hpp:24
Definition asm_function.hpp:119
Definition asm_symbol.hpp:54
std::variant wrapper for a partial implementation of C++23's expected type
Definition expected.hpp:34
Definition program.hpp:32
SymbolTable & get_symtab()
Definition program.cpp:86
Definition requirement.hpp:214
bool get_res() const
Definition requirement.hpp:249
std::string get_description() const
Definition requirement.hpp:247
exprs::ExpressionRepr get_expr_repr() const
Definition requirement.hpp:252
Definition run_result.hpp:6
std::optional< Symbol > find(std::string_view name) const
Definition symbol_table.cpp:21
Base class primarily for a user-written test.
Definition test_base.hpp:17
User-facing API for use within an assignment test case for: Interacting with or querying data for the...
Definition test_context.hpp:44
TestResult finalize()
Obtain the final test results Run after the test is complete. Note: has no ill effects if run before ...
Definition test_context.cpp:55
TestContext(TestBase &test, Program program, std::function< void(const RequirementResult &)> on_requirement=common::noop) noexcept
Definition test_context.cpp:44
bool require(bool condition, const std::string &msg, RequirementResult::DebugInfo debug_info=RequirementResult::DebugInfo{})
Definition test_context.cpp:75
std::string get_full_stdout()
Get all stdout from since the beginning of the test invokation.
Definition test_context.cpp:87
RegistersState get_registers() const
Get the current register state of the program.
Definition test_context.cpp:193
std::string get_stdout()
Get any new stdout from the program since the last call to this function.
Definition test_context.cpp:83
AsmSymbol< T > find_symbol(std::string_view name)
Find a named symbol in the associated program.
Definition test_context.hpp:142
void send_stdin(const std::string &input)
Get any new stdout from the program since the last call to this function.
Definition test_context.cpp:91
AsmFunction< Func > find_function(std::string name)
Find a named function in the associated program.
Definition test_context.hpp:156
Result< SyscallRecord > exec_syscall(u64 sys_nr, std::array< std::uint64_t, 6 > args)
Definition test_context.cpp:122
void restart_program()
Restarts the entire program.
Definition test_context.cpp:118
AsmBuffer< NumBytes > create_buffer()
Create a buffer of NumBytes
Definition test_context.hpp:135
std::size_t flush_stdin()
Flushes any reamaining unread data in the stdin buffer Returns: number of bytes flushed,...
Definition test_context.cpp:130
std::string_view get_name() const
Test name getter.
Definition test_context.cpp:79
const std::vector< SyscallRecord > & get_syscall_records() const
Obtain a list of the syscalls that have been executed so far.
Definition test_context.cpp:126
RunResult run()
Run the program normally from _start, stopping at the first exit(2) or exit_group(2) syscall invocati...
Definition test_context.cpp:95
Definition concepts.hpp:23
Defines data classes to store result data for the current run session.
#define LOG_DEBUG(...)
Definition logging.hpp:41
constexpr auto noop
Usefule as the default argument to function callbacks.
Definition functional.hpp:6
Definition asm_buffer.hpp:20
@ UnresolvedSymbol
Failed to resolve a named symbol in a program.
All header-only as this is relatively low level and we want operations to be fast (inlinable)
Definition registers_state.hpp:301
Definition grading_session.hpp:83
Definition grading_session.hpp:75
Definition grading_session.hpp:99