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