summary refs log tree commit diff
path: root/ripple/minitrace/src/syscall_abi/macros.rs
blob: 07e6099fb3fb204f91b77b12a864a7770fab0f53 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// SPDX-FileCopyrightText: edef <edef@unfathomable.blue>
// SPDX-License-Identifier: OSL-3.0

#[cfg(test)]
use std::fmt::{self, Debug};

#[cfg(test)]
pub(crate) fn libc_check<T: Debug + Eq + fmt::LowerHex>(
	item: &'static str,
	(our_name, our_value): (&'static str, T),
	(libc_name, libc_value): (&'static str, T),
) {
	match () {
		_ if libc_name.ends_with(our_name) => {}
		_ if libc_name.starts_with(&format!("{our_name}_")) => {}
		() => panic!("{libc_name} doesn't match {our_name}"),
	}

	assert!(
		our_value == libc_value,
		"{item}::{our_name} ({our_value:#x}) != libc::{libc_name} ({libc_value:#x})",
	);
}

#[macro_export]
macro_rules! syscall_bitflags {
	(
		$(
			struct $BitFlags:ident: $T:ty {
				$(
					const $FLAG:ident = $value:expr => $LIBC_FLAG:ident;
				)*
			}
		)*
	) => {
		#[test]
		fn verify_syscall_bitflags() {
			$(
				$BitFlags::verify();
			)*
		}

		$(
			$crate::bitflags! {
				pub(crate) struct $BitFlags: $T {
					$(
						const $FLAG = $value;
					)*
				}
			}

			impl $BitFlags {
				#[cfg(test)]
				fn verify() {
					$(
						$crate::syscall_abi::macros::libc_check(
							stringify!($BitFlags),
							(stringify!($FLAG), Self::$FLAG.bits()),
							(stringify!($LIBC_FLAG), $crate::libc::$LIBC_FLAG)
						);
					)*
				}
			}

			impl $crate::syscall_abi::SyscallArg for $BitFlags {
				fn try_from_reg(reg: u64) -> Option<Self> {
					$crate::syscall_abi::SyscallArg::try_from_reg(reg).and_then(Self::from_bits)
				}
			}
		)*
	};
}

#[macro_export]
macro_rules! syscall_enums {
	(
		$(
			enum $Enum:ident: $T:ty {
				$(
					$VARIANT:ident = $value:literal $(=> $LIBC_VALUE:ident)?,
				)*
			}
		)*
	) => {
		#[test]
		fn verify_syscall_enums() {
			$(
				$Enum::verify();
			)*
		}

		$(
			#[derive(Debug, Copy, Clone, Eq, PartialEq)]
			#[allow(non_camel_case_types)]
			pub(crate) enum $Enum {
				$($VARIANT = $value),*
			}

			impl $crate::syscall_abi::SyscallArg for $Enum {
				fn try_from_reg(reg: u64) -> Option<Self> {
					let reg = <$T as $crate::syscall_abi::SyscallArg>::try_from_reg(reg)?;
					Some(match reg {
						$(
							$value => $Enum::$VARIANT,
						)*
						_ => return None
					})
				}
			}

			impl $Enum {
				#[cfg(test)]
				fn verify() {
					$(
						$(
							$crate::syscall_abi::macros::libc_check(
								stringify!($Enum),
								(stringify!($VARIANT), Self::$VARIANT as $T),
								(stringify!($LIBC_VALUE), $crate::libc::$LIBC_VALUE)
							);
						)?
					)*
				}
			}
		)*
	};
}

#[macro_export]
macro_rules! define_syscalls {
	(enum $SyscallEntry:ident {
		$(fn $syscall:ident ( $($arg:ident : $Arg:ty),* ) -> $Ret:ty = $nr:literal ;)*
	}) => {
		#[derive(Debug, Clone)]
		#[allow(non_camel_case_types)]
		// TODO(edef): re-enable dead_code lint when we start fully interpreting syscall args
		#[allow(dead_code)]
		pub(crate) enum $SyscallEntry {
			$($syscall {
				$($arg : $Arg),*
			}),*
		}

		impl $SyscallEntry {
			pub(crate) fn from_regs(process: &Process, regs: $crate::libc::user_regs_struct) -> anyhow::Result<$SyscallEntry> {
				use anyhow::bail;
				Ok(match (regs.orig_rax, [regs.rdi, regs.rsi, regs.rdx, regs.r10, regs.r8, regs.r9]) {
					$(
						($nr, [$($arg,)* ..]) => $SyscallEntry::$syscall {
							$($arg: match ProcessSyscallArg::try_from_process_reg(process, $arg) {
								Some(x) => x,
								None => bail!("couldn't parse {}(2) {}: {:#08x}", stringify!($syscall), stringify!($arg), $arg)
							}),*
						},
					)*
					(n, _) => bail!("unknown syscall number {n}")
				})
			}
		}
	}
}