AsmGrader 0.0.0
Loading...
Searching...
No Matches
syscall.hpp
Go to the documentation of this file.
1#pragma once
2
4
5#include <fmt/compile.h>
6#include <fmt/format.h>
7#include <fmt/ranges.h>
8#include <range/v3/algorithm/copy.hpp>
9
10#include <array>
11#include <concepts>
12#include <cstddef>
13#include <ctime>
14#include <initializer_list>
15#include <span>
16#include <string_view>
17#include <type_traits>
18
19#include <sys/syscall.h>
20
21namespace asmgrader {
22
24{
25 enum class Type {
26 Unused = 0,
27 Int32,
28 Int64,
29 Uint32,
30 Uint64,
31 VoidPtr,
32 CString,
33 NTCStringArray, // Null-terminated C-string array
34 TimeSpecPtr, // for nanosleep(2)
35 };
36
39 template <typename T>
40 static constexpr Type type_of = []() {
41#define TC(t, r) \
42 else if (std::same_as<T, t>) { \
43 return r; \
44 }
45 using enum Type;
46
47 if constexpr (std::is_void_v<T>) {
48 return VoidPtr;
49 }
50 TC(i32, Int32)
51 TC(i64, Int64)
52 TC(u32, Uint32)
53 TC(u64, Uint64)
54
55#undef TC
56 }();
57
58 struct Param
59 {
61
62 std::string_view name;
63 };
64
65 consteval SyscallEntry()
66 : nr{-1} {}
67
68 constexpr SyscallEntry(int number, std::string_view syscall_name, Type return_type,
69 std::initializer_list<Param> parameters)
70 : nr{number}
71 , ret_type{return_type}
72 , num_params_{parameters.size()} {
73 ranges::copy(syscall_name, name_buffer_.begin());
74 ranges::copy(parameters, params_buffer_.begin());
75 }
76
77 static constexpr SyscallEntry unknown(int nr) {
78 decltype(name_buffer_) buffer{};
79 auto* end_it = fmt::format_to(buffer.data(), FMT_COMPILE("<unknown ({})>"), nr);
80
81 *end_it = '\0';
82
83 return {nr, Type::Unused, {}, buffer};
84 }
85
87 int nr{};
88
90 constexpr std::string_view name() const { return {name_buffer_.begin(), name_buffer_.end()}; }
91
92 constexpr auto parameters() const { return std::span{params_buffer_.begin(), num_params_}; }
93
95
96private:
97 // No syscall name is > 128 chars in length (or even close...)
98 static constexpr int MAX_NAME_LEN = 128;
99 // Underlying buffer for ``name`` so that we may use fmtlib to create custom `<unknown (NR)>` names
100 std::array<char, MAX_NAME_LEN> name_buffer_{};
101
103 static constexpr int MAX_NUM_PARAMS = 6;
104 std::array<Param, MAX_NUM_PARAMS> params_buffer_{};
105 std::size_t num_params_ = 0;
106
107 constexpr SyscallEntry(int number, Type return_type, std::array<Param, MAX_NUM_PARAMS> parameters,
108 std::array<char, MAX_NAME_LEN> name_buffer)
109 : nr{number}
110 , ret_type{return_type}
111 , name_buffer_{name_buffer}
112 , params_buffer_{parameters} {}
113};
114
115} // namespace asmgrader
116
117#include <asmgrader/subprocess/syscall_entries.inl> // IWYU pragma: export
Definition asm_buffer.hpp:19
Definition syscall.hpp:59
std::string_view name
Definition syscall.hpp:62
Type type
Definition syscall.hpp:60
Definition syscall.hpp:24
constexpr std::string_view name() const
Human-readable name of the syscall (e.g., openat, getpid)
Definition syscall.hpp:90
consteval SyscallEntry()
Definition syscall.hpp:65
Type ret_type
Definition syscall.hpp:94
constexpr SyscallEntry(int number, std::string_view syscall_name, Type return_type, std::initializer_list< Param > parameters)
Definition syscall.hpp:68
int nr
Syscall number (arch-specific)
Definition syscall.hpp:87
Type
Definition syscall.hpp:25
static constexpr Type type_of
Primarally for converting integer-aliases (e.g., size_t, mode_t, pid_t, etc.) to the corresponding en...
Definition syscall.hpp:40
constexpr auto parameters() const
Definition syscall.hpp:92
static constexpr SyscallEntry unknown(int nr)
Definition syscall.hpp:77
#define TC(t, r)