Debian Codegraph @

debian/rust-vendor/getrandom/src/backends/linux_raw.rs

raw
1 //! Implementation for Linux / Android using `asm!`-based syscalls.
2 pub use crate::util::{inner_u32, inner_u64};
3 use crate::{Error, MaybeUninit};
4
5 #[cfg(not(any(target_os = "android", target_os = "linux")))]
6 compile_error!("`linux_raw` backend can be enabled only for Linux/Android targets!");
7
8 #[path = "../utils/sanitizer.rs"]
9 mod utils;
10
11 #[allow(non_upper_case_globals)]
12 unsafe fn getrandom_syscall(buf: *mut u8, buflen: usize, flags: u32) -> isize {
13 let r0;
14
15 // Based on `rustix` and `linux-raw-sys` code.
16 cfg_if! {
17 if #[cfg(all(
18 target_arch = "arm",
19 any(target_abi = "eabi", target_abi = "eabihf"),
20 ))] {
21 const __NR_getrandom: u32 = 384;
22 // In thumb-mode, r7 is the frame pointer and is not permitted to be used in
23 // an inline asm operand, so we have to use a different register and copy it
24 // into r7 inside the inline asm.
25 // Theoretically, we could detect thumb mode in the build script, but several
26 // register moves are cheap enough compared to the syscall cost, so we do not
27 // bother with it.
28 unsafe {
29 core::arch::asm!(
30 "mov {tmp}, r7",
31 "mov r7, {nr}",
32 "svc 0",
33 "mov r7, {tmp}",
34 tmp = out(reg) _,
35 nr = const __NR_getrandom,
36 inlateout("r0") buf => r0,
37 in("r1") buflen,
38 in("r2") flags,
39 options(nostack, preserves_flags)
40 );
41 }
42 } else if #[cfg(all(
43 target_arch = "aarch64",
44 any(target_abi = "", target_abi = "ilp32"),
45 ))] {
46 // According to the ILP32 patch for the kernel that hasn't yet
47 // been merged into the mainline, "AARCH64/ILP32 ABI uses standard
48 // syscall table [...] with the exceptions listed below," where
49 // getrandom is not mentioned as an exception.
50 const __NR_getrandom: u32 = 278;
51 unsafe {
52 core::arch::asm!(
53 "svc 0",
54 in("x8") __NR_getrandom,
55 inlateout("x0") buf => r0,
56 in("x1") buflen,
57 in("x2") flags,
58 options(nostack, preserves_flags)
59 );
60 }
61 } else if #[cfg(all(
62 target_arch = "loongarch64",
63 any(target_abi = "", target_abi = "ilp32"),
64 ))] {
65 const __NR_getrandom: u32 = 278;
66 unsafe {
67 core::arch::asm!(
68 "syscall 0",
69 in("$a7") __NR_getrandom,
70 inlateout("$a0") buf => r0,
71 in("$a1") buflen,
72 in("$a2") flags,
73 options(nostack, preserves_flags)
74 );
75 }
76 } else if #[cfg(any(target_arch = "riscv32", target_arch = "riscv64"))] {
77 const __NR_getrandom: u32 = 278;
78 unsafe {
79 core::arch::asm!(
80 "ecall",
81 in("a7") __NR_getrandom,
82 inlateout("a0") buf => r0,
83 in("a1") buflen,
84 in("a2") flags,
85 options(nostack, preserves_flags)
86 );
87 }
88 } else if #[cfg(target_arch = "s390x")] {
89 const __NR_getrandom: u32 = 349;
90 unsafe {
91 core::arch::asm!(
92 "svc 0",
93 in("r1") __NR_getrandom,
94 inlateout("r2") buf => r0,
95 in("r3") buflen,
96 in("r4") flags,
97 options(nostack, preserves_flags)
98 );
99 }
100 } else if #[cfg(target_arch = "x86")] {
101 const __NR_getrandom: u32 = 355;
102 // `int 0x80` is famously slow, but implementing vDSO is too complex
103 // and `sysenter`/`syscall` have their own portability issues,
104 // so we use the simple "legacy" way of doing syscalls.
105 unsafe {
106 core::arch::asm!(
107 "int $$0x80",
108 in("eax") __NR_getrandom,
109 in("ebx") buf,
110 in("ecx") buflen,
111 in("edx") flags,
112 lateout("eax") r0,
113 options(nostack, preserves_flags)
114 );
115 }
116 } else if #[cfg(all(
117 target_arch = "x86_64",
118 any(target_abi = "", target_abi = "x32"),
119 ))] {
120 const __X32_SYSCALL_BIT: u32 = 0x40000000;
121 const OFFSET: u32 = if cfg!(target_pointer_width = "32") { __X32_SYSCALL_BIT } else { 0 };
122 const __NR_getrandom: u32 = OFFSET + 318;
123
124 unsafe {
125 core::arch::asm!(
126 "syscall",
127 in("rax") __NR_getrandom,
128 in("rdi") buf,
129 in("rsi") buflen,
130 in("rdx") flags,
131 lateout("rax") r0,
132 lateout("rcx") _,
133 lateout("r11") _,
134 options(nostack, preserves_flags)
135 );
136 }
137 } else {
138 compile_error!("`linux_raw` backend does not support this target arch");
139 }
140 }
141
142 r0
143 }
144
145 #[inline]
146 pub fn fill_inner(mut dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
147 // Value of this error code is stable across all target arches.
148 const EINTR: isize = -4;
149
150 loop {
151 let ret = unsafe { getrandom_syscall(dest.as_mut_ptr().cast(), dest.len(), 0) };
152 match usize::try_from(ret) {
153 Ok(0) => return Err(Error::UNEXPECTED),
154 Ok(len) => {
155 let (l, r) = dest.split_at_mut_checked(len).ok_or(Error::UNEXPECTED)?;
156 unsafe { utils::unpoison(l) };
157 dest = r;
158 if dest.is_empty() {
159 return Ok(());
160 }
161 }
162 Err(_) if ret == EINTR => continue,
163 Err(_) => {
164 let code = i32::try_from(ret).map_err(|_| Error::UNEXPECTED)?;
165 return Err(Error::from_neg_error_code(code));
166 }
167 }
168 }
169 }