summary refs log tree commit diff
path: root/ripple/minitrace/src/syscall_abi/arg.rs
diff options
context:
space:
mode:
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 ())
+	}
+}