1 //! Helper functions for `prctl` syscalls.
2 
3 #![allow(unsafe_code)]
4 
5 use crate::backend::c::{c_int, c_void};
6 use crate::backend::prctl::syscalls;
7 use crate::io;
8 use crate::utils::as_mut_ptr;
9 use bitflags::bitflags;
10 use core::mem::MaybeUninit;
11 use core::ptr::null_mut;
12 
13 bitflags! {
14     /// `PR_PAC_AP*`.
15     #[repr(transparent)]
16     #[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
17     pub struct PointerAuthenticationKeys: u32 {
18         /// `PR_PAC_APIAKEY`—Instruction authentication key `A`.
19         const INSTRUCTION_AUTHENTICATION_KEY_A = linux_raw_sys::prctl::PR_PAC_APIAKEY;
20         /// `PR_PAC_APIBKEY`—Instruction authentication key `B`.
21         const INSTRUCTION_AUTHENTICATION_KEY_B = linux_raw_sys::prctl::PR_PAC_APIBKEY;
22         /// `PR_PAC_APDAKEY`—Data authentication key `A`.
23         const DATA_AUTHENTICATION_KEY_A = linux_raw_sys::prctl::PR_PAC_APDAKEY;
24         /// `PR_PAC_APDBKEY`—Data authentication key `B`.
25         const DATA_AUTHENTICATION_KEY_B = linux_raw_sys::prctl::PR_PAC_APDBKEY;
26         /// `PR_PAC_APGAKEY`—Generic authentication `A` key.
27         const GENERIC_AUTHENTICATION_KEY_A = linux_raw_sys::prctl::PR_PAC_APGAKEY;
28 
29         /// <https://docs.rs/bitflags/*/bitflags/#externally-defined-flags>
30         const _ = !0;
31     }
32 }
33 
34 #[inline]
prctl_1arg(option: c_int) -> io::Result<c_int>35 pub(crate) unsafe fn prctl_1arg(option: c_int) -> io::Result<c_int> {
36     const NULL: *mut c_void = null_mut();
37     syscalls::prctl(option, NULL, NULL, NULL, NULL)
38 }
39 
40 #[inline]
prctl_2args(option: c_int, arg2: *mut c_void) -> io::Result<c_int>41 pub(crate) unsafe fn prctl_2args(option: c_int, arg2: *mut c_void) -> io::Result<c_int> {
42     const NULL: *mut c_void = null_mut();
43     syscalls::prctl(option, arg2, NULL, NULL, NULL)
44 }
45 
46 #[inline]
prctl_3args( option: c_int, arg2: *mut c_void, arg3: *mut c_void, ) -> io::Result<c_int>47 pub(crate) unsafe fn prctl_3args(
48     option: c_int,
49     arg2: *mut c_void,
50     arg3: *mut c_void,
51 ) -> io::Result<c_int> {
52     syscalls::prctl(option, arg2, arg3, null_mut(), null_mut())
53 }
54 
55 #[inline]
prctl_get_at_arg2_optional<P>(option: i32) -> io::Result<P>56 pub(crate) unsafe fn prctl_get_at_arg2_optional<P>(option: i32) -> io::Result<P> {
57     let mut value: MaybeUninit<P> = MaybeUninit::uninit();
58     prctl_2args(option, value.as_mut_ptr().cast())?;
59     Ok(value.assume_init())
60 }
61 
62 #[inline]
prctl_get_at_arg2<P, T>(option: i32) -> io::Result<T> where P: Default, T: TryFrom<P, Error = io::Errno>,63 pub(crate) unsafe fn prctl_get_at_arg2<P, T>(option: i32) -> io::Result<T>
64 where
65     P: Default,
66     T: TryFrom<P, Error = io::Errno>,
67 {
68     let mut value: P = Default::default();
69     prctl_2args(option, as_mut_ptr(&mut value).cast())?;
70     TryFrom::try_from(value)
71 }
72