summary refs log tree commit diff
path: root/ripple/minitrace/src/syscall_abi/arg.rs
diff options
context:
space:
mode:
authoredef <edef@unfathomable.blue>2022-07-30 19:29:15 +0000
committeredef <edef@unfathomable.blue>2022-07-30 19:29:15 +0000
commit82652914c933f50931338e4bbc924013c358fe71 (patch)
treea77f0d48e59d497720de3f51a7ef4f928d361b15 /ripple/minitrace/src/syscall_abi/arg.rs
parentdd94473c5724f8215790a9195df96cfa7bd6a04b (diff)
downloadunf-legacy-82652914c933f50931338e4bbc924013c358fe71.tar.zst
ripple/minitrace/syscall_abi: init
Factor out the rather sprawling syscall ABI definitions from the main
program. The macros, argument parsing, and file descriptor code get
some space to breathe too.

Change-Id: I0aa01b6b94e4d4b770bb9ef59926e2236c50b258
Diffstat (limited to 'ripple/minitrace/src/syscall_abi/arg.rs')
-rw-r--r--ripple/minitrace/src/syscall_abi/arg.rs84
1 files changed, 84 insertions, 0 deletions
diff --git a/ripple/minitrace/src/syscall_abi/arg.rs b/ripple/minitrace/src/syscall_abi/arg.rs
new file mode 100644
index 0000000..b25757c
--- /dev/null
+++ b/ripple/minitrace/src/syscall_abi/arg.rs
@@ -0,0 +1,84 @@
+// SPDX-FileCopyrightText: edef <edef@unfathomable.blue>
+// SPDX-License-Identifier: OSL-3.0
+
+use {crate::Process, std::ffi::CString};
+
+pub(crate) trait ProcessSyscallArg: Sized {
+	fn try_from_process_reg(process: &Process, reg: u64) -> Option<Self>;
+}
+
+impl ProcessSyscallArg for CString {
+	fn try_from_process_reg(process: &Process, reg: u64) -> Option<Self> {
+		process.read_mem_cstr(reg).ok()
+	}
+}
+
+impl<T: SyscallArg> ProcessSyscallArg for T {
+	fn try_from_process_reg(_process: &Process, reg: u64) -> Option<Self> {
+		SyscallArg::try_from_reg(reg)
+	}
+}
+
+pub(crate) trait SyscallArg: Sized {
+	fn try_from_reg(reg: u64) -> Option<Self>;
+}
+
+impl SyscallArg for u16 {
+	fn try_from_reg(reg: u64) -> Option<Self> {
+		reg.try_into().ok()
+	}
+}
+
+impl SyscallArg for u32 {
+	fn try_from_reg(reg: u64) -> Option<Self> {
+		reg.try_into().ok()
+	}
+}
+
+impl SyscallArg for u64 {
+	fn try_from_reg(reg: u64) -> Option<Self> {
+		Some(reg)
+	}
+}
+
+impl SyscallArg for i32 {
+	fn try_from_reg(reg: u64) -> Option<Self> {
+		Some(u32::try_from(reg).ok()? as i32)
+	}
+}
+
+impl SyscallArg for *mut i32 {
+	fn try_from_reg(reg: u64) -> Option<Self> {
+		Some(usize::try_from_reg(reg)? as *mut i32)
+	}
+}
+
+impl SyscallArg for usize {
+	fn try_from_reg(reg: u64) -> Option<Self> {
+		reg.try_into().ok()
+	}
+}
+
+impl SyscallArg for *const u8 {
+	fn try_from_reg(reg: u64) -> Option<Self> {
+		Some(usize::try_from_reg(reg)? as *const u8)
+	}
+}
+
+impl SyscallArg for *mut u8 {
+	fn try_from_reg(reg: u64) -> Option<Self> {
+		Some(usize::try_from_reg(reg)? as *mut u8)
+	}
+}
+
+impl SyscallArg for *mut () {
+	fn try_from_reg(reg: u64) -> Option<Self> {
+		Some(usize::try_from_reg(reg)? as *mut ())
+	}
+}
+
+impl SyscallArg for *const () {
+	fn try_from_reg(reg: u64) -> Option<Self> {
+		Some(usize::try_from_reg(reg)? as *const ())
+	}
+}