userspace/target/operating_system/linux/syscall/
mprotect.rs

1use crate::target::arch::{Arch, traits::Callable};
2
3pub use super::mmap::{Prot, prot};
4
5hooking!(MPROTECT);
6
7#[inline(always)]
8pub fn mprotect(addr: *mut u8, len: usize, prot: i32) -> crate::Result {
9    let arch_result = Arch::syscall3(NUMBER, addr as usize, len, prot as usize);
10    handle_result(arch_result)
11}
12
13pub mod ok {
14    r#struct!(OkSyscallMUnMap { value: usize });
15
16    result!( Ok; "MUnMap Ok"; usize; [
17        [0; OK; Default; usize; "Ok"; "All good"],
18    ]);
19
20    impl Ok {
21        pub fn from_no(no: usize) -> Self {
22            Ok::Default(no)
23        }
24    }
25}
26
27pub mod error {
28    result!(Error; "MUnMap error"; usize; [
29        [1; ERROR; Default; usize; "Error"; "Something wicked this way comes"],
30    ]);
31
32    impl Error {
33        pub fn from_no(no: usize) -> Self {
34            Error::Default(no)
35        }
36    }
37}
38
39pub use error::Error;
40pub use ok::Ok;
41
42pub type Result = core::result::Result<Ok, Error>;
43
44pub fn handle_result(result: crate::Result) -> crate::Result {
45    // Err(crate::Error::Default(1))
46    match result {
47        crate::Result::Ok(crate::Ok::Target(crate::target::Ok::Arch(
48            crate::target::arch::Ok::X86_64Syscall(
49                crate::target::arch::syscall::Ok::X86_64Syscall3(
50                    crate::target::arch::syscall::syscall3::Ok::Default(m),
51                ),
52            ),
53        ))) => core::result::Result::Ok(crate::Ok::Target(crate::target::Ok::Os(
54            crate::target::os::Ok::Syscall(crate::target::os::syscall::Ok::MProtect(
55                crate::target::os::syscall::mprotect::Ok::Default(m),
56            )),
57        ))),
58        _ => core::result::Result::Err(crate::Error::Target(crate::target::Error::Os(
59            crate::target::os::Error::Syscall(crate::target::os::syscall::Error::MProtect(
60                Error::Default(3),
61            )),
62        ))),
63    }
64}