1 // Copyright 2022, The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 //! Low-level compatibility layer between baremetal Rust and Bionic C functions.
16
17 use crate::rand::fill_with_entropy;
18 use crate::read_sysreg;
19 use core::ffi::c_char;
20 use core::ffi::c_int;
21 use core::ffi::c_void;
22 use core::ffi::CStr;
23 use core::ptr::addr_of_mut;
24 use core::slice;
25 use core::str;
26
27 use cstr::cstr;
28 use log::error;
29 use log::info;
30
31 const EOF: c_int = -1;
32 const EIO: c_int = 5;
33
34 /// Bionic thread-local storage.
35 #[repr(C)]
36 pub struct Tls {
37 /// Unused.
38 _unused: [u8; 40],
39 /// Use by the compiler as stack canary value.
40 pub stack_guard: u64,
41 }
42
43 /// Bionic TLS.
44 ///
45 /// Provides the TLS used by Bionic code. This is unique as vmbase only supports one thread.
46 ///
47 /// Note that the linker script re-exports __bionic_tls.stack_guard as __stack_chk_guard for
48 /// compatibility with non-Bionic LLVM.
49 #[link_section = ".data.stack_protector"]
50 #[export_name = "__bionic_tls"]
51 pub static mut TLS: Tls = Tls { _unused: [0; 40], stack_guard: 0 };
52
53 /// Gets a reference to the TLS from the dedicated system register.
__get_tls() -> &'static mut Tls54 pub fn __get_tls() -> &'static mut Tls {
55 let tpidr = read_sysreg!("tpidr_el0");
56 // SAFETY: The register is currently only written to once, from entry.S, with a valid value.
57 unsafe { &mut *(tpidr as *mut Tls) }
58 }
59
60 #[no_mangle]
__stack_chk_fail() -> !61 extern "C" fn __stack_chk_fail() -> ! {
62 panic!("stack guard check failed");
63 }
64
65 /// Called from C to cause abnormal program termination.
66 #[no_mangle]
abort() -> !67 extern "C" fn abort() -> ! {
68 panic!("C code called abort()")
69 }
70
71 /// Error number set and read by C functions.
72 pub static mut ERRNO: c_int = 0;
73
74 #[no_mangle]
75 #[allow(unused_unsafe)]
__errno() -> *mut c_int76 unsafe extern "C" fn __errno() -> *mut c_int {
77 // SAFETY: C functions which call this are only called from the main thread, not from exception
78 // handlers.
79 unsafe { addr_of_mut!(ERRNO) as *mut _ }
80 }
81
set_errno(value: c_int)82 fn set_errno(value: c_int) {
83 // SAFETY: vmbase is currently single-threaded.
84 unsafe { ERRNO = value };
85 }
86
get_errno() -> c_int87 fn get_errno() -> c_int {
88 // SAFETY: vmbase is currently single-threaded.
89 unsafe { ERRNO }
90 }
91
92 #[no_mangle]
getentropy(buffer: *mut c_void, length: usize) -> c_int93 extern "C" fn getentropy(buffer: *mut c_void, length: usize) -> c_int {
94 if length > 256 {
95 // The maximum permitted value for the length argument is 256.
96 set_errno(EIO);
97 return -1;
98 }
99
100 // SAFETY: Just like libc, we need to assume that `ptr` is valid.
101 let buffer = unsafe { slice::from_raw_parts_mut(buffer.cast::<u8>(), length) };
102 fill_with_entropy(buffer).unwrap();
103
104 0
105 }
106
107 /// Reports a fatal error detected by Bionic.
108 ///
109 /// # Safety
110 ///
111 /// Input strings `prefix` and `format` must be valid and properly NUL-terminated.
112 ///
113 /// # Note
114 ///
115 /// This Rust function is missing the last argument of its C/C++ counterpart, a va_list.
116 #[no_mangle]
async_safe_fatal_va_list(prefix: *const c_char, format: *const c_char)117 unsafe extern "C" fn async_safe_fatal_va_list(prefix: *const c_char, format: *const c_char) {
118 // SAFETY: The caller guaranteed that both strings were valid and NUL-terminated.
119 let (prefix, format) = unsafe { (CStr::from_ptr(prefix), CStr::from_ptr(format)) };
120
121 if let (Ok(prefix), Ok(format)) = (prefix.to_str(), format.to_str()) {
122 // We don't bother with printf formatting.
123 error!("FATAL BIONIC ERROR: {prefix}: \"{format}\" (unformatted)");
124 }
125 }
126
127 #[cfg(target_arch = "aarch64")]
128 #[allow(clippy::enum_clike_unportable_variant)] // No risk if AArch64 only.
129 #[repr(usize)]
130 /// Fake FILE* values used by C to refer to the default streams.
131 ///
132 /// These values are intentionally invalid pointers so that dereferencing them will be caught.
133 enum CFilePtr {
134 // On AArch64 with TCR_EL1.EPD1 set or TCR_EL1.T1SZ > 12, these VAs can't be mapped.
135 Stdout = 0xfff0_badf_badf_bad0,
136 Stderr = 0xfff0_badf_badf_bad1,
137 }
138
139 impl CFilePtr {
write_lines(&self, s: &str)140 fn write_lines(&self, s: &str) {
141 for line in s.split_inclusive('\n') {
142 let (line, ellipsis) = if let Some(stripped) = line.strip_suffix('\n') {
143 (stripped, "")
144 } else {
145 (line, " ...")
146 };
147
148 match self {
149 Self::Stdout => info!("{line}{ellipsis}"),
150 Self::Stderr => error!("{line}{ellipsis}"),
151 }
152 }
153 }
154 }
155
156 impl TryFrom<usize> for CFilePtr {
157 type Error = &'static str;
158
try_from(value: usize) -> Result<Self, Self::Error>159 fn try_from(value: usize) -> Result<Self, Self::Error> {
160 match value {
161 x if x == Self::Stdout as _ => Ok(Self::Stdout),
162 x if x == Self::Stderr as _ => Ok(Self::Stderr),
163 _ => Err("Received Invalid FILE* from C"),
164 }
165 }
166 }
167
168 #[no_mangle]
169 static stdout: CFilePtr = CFilePtr::Stdout;
170 #[no_mangle]
171 static stderr: CFilePtr = CFilePtr::Stderr;
172
173 #[no_mangle]
fputs(c_str: *const c_char, stream: usize) -> c_int174 extern "C" fn fputs(c_str: *const c_char, stream: usize) -> c_int {
175 // SAFETY: Just like libc, we need to assume that `s` is a valid NULL-terminated string.
176 let c_str = unsafe { CStr::from_ptr(c_str) };
177
178 if let (Ok(s), Ok(f)) = (c_str.to_str(), CFilePtr::try_from(stream)) {
179 f.write_lines(s);
180 0
181 } else {
182 set_errno(EOF);
183 EOF
184 }
185 }
186
187 #[no_mangle]
fwrite(ptr: *const c_void, size: usize, nmemb: usize, stream: usize) -> usize188 extern "C" fn fwrite(ptr: *const c_void, size: usize, nmemb: usize, stream: usize) -> usize {
189 let length = size.saturating_mul(nmemb);
190
191 // SAFETY: Just like libc, we need to assume that `ptr` is valid.
192 let bytes = unsafe { slice::from_raw_parts(ptr as *const u8, length) };
193
194 if let (Ok(s), Ok(f)) = (str::from_utf8(bytes), CFilePtr::try_from(stream)) {
195 f.write_lines(s);
196 length
197 } else {
198 0
199 }
200 }
201
202 #[no_mangle]
strerror(n: c_int) -> *mut c_char203 extern "C" fn strerror(n: c_int) -> *mut c_char {
204 cstr_error(n).as_ptr().cast_mut().cast()
205 }
206
207 #[no_mangle]
perror(s: *const c_char)208 extern "C" fn perror(s: *const c_char) {
209 let prefix = if s.is_null() {
210 None
211 } else {
212 // SAFETY: Just like libc, we need to assume that `s` is a valid NULL-terminated string.
213 let c_str = unsafe { CStr::from_ptr(s) };
214 if c_str.is_empty() {
215 None
216 } else {
217 Some(c_str.to_str().unwrap())
218 }
219 };
220
221 let error = cstr_error(get_errno()).to_str().unwrap();
222
223 if let Some(prefix) = prefix {
224 error!("{prefix}: {error}");
225 } else {
226 error!("{error}");
227 }
228 }
229
cstr_error(n: c_int) -> &'static CStr230 fn cstr_error(n: c_int) -> &'static CStr {
231 // Messages taken from errno(1).
232 match n {
233 0 => cstr!("Success"),
234 1 => cstr!("Operation not permitted"),
235 2 => cstr!("No such file or directory"),
236 3 => cstr!("No such process"),
237 4 => cstr!("Interrupted system call"),
238 5 => cstr!("Input/output error"),
239 6 => cstr!("No such device or address"),
240 7 => cstr!("Argument list too long"),
241 8 => cstr!("Exec format error"),
242 9 => cstr!("Bad file descriptor"),
243 10 => cstr!("No child processes"),
244 11 => cstr!("Resource temporarily unavailable"),
245 12 => cstr!("Cannot allocate memory"),
246 13 => cstr!("Permission denied"),
247 14 => cstr!("Bad address"),
248 15 => cstr!("Block device required"),
249 16 => cstr!("Device or resource busy"),
250 17 => cstr!("File exists"),
251 18 => cstr!("Invalid cross-device link"),
252 19 => cstr!("No such device"),
253 20 => cstr!("Not a directory"),
254 21 => cstr!("Is a directory"),
255 22 => cstr!("Invalid argument"),
256 23 => cstr!("Too many open files in system"),
257 24 => cstr!("Too many open files"),
258 25 => cstr!("Inappropriate ioctl for device"),
259 26 => cstr!("Text file busy"),
260 27 => cstr!("File too large"),
261 28 => cstr!("No space left on device"),
262 29 => cstr!("Illegal seek"),
263 30 => cstr!("Read-only file system"),
264 31 => cstr!("Too many links"),
265 32 => cstr!("Broken pipe"),
266 33 => cstr!("Numerical argument out of domain"),
267 34 => cstr!("Numerical result out of range"),
268 35 => cstr!("Resource deadlock avoided"),
269 36 => cstr!("File name too long"),
270 37 => cstr!("No locks available"),
271 38 => cstr!("Function not implemented"),
272 39 => cstr!("Directory not empty"),
273 40 => cstr!("Too many levels of symbolic links"),
274 42 => cstr!("No message of desired type"),
275 43 => cstr!("Identifier removed"),
276 44 => cstr!("Channel number out of range"),
277 45 => cstr!("Level 2 not synchronized"),
278 46 => cstr!("Level 3 halted"),
279 47 => cstr!("Level 3 reset"),
280 48 => cstr!("Link number out of range"),
281 49 => cstr!("Protocol driver not attached"),
282 50 => cstr!("No CSI structure available"),
283 51 => cstr!("Level 2 halted"),
284 52 => cstr!("Invalid exchange"),
285 53 => cstr!("Invalid request descriptor"),
286 54 => cstr!("Exchange full"),
287 55 => cstr!("No anode"),
288 56 => cstr!("Invalid request code"),
289 57 => cstr!("Invalid slot"),
290 59 => cstr!("Bad font file format"),
291 60 => cstr!("Device not a stream"),
292 61 => cstr!("No data available"),
293 62 => cstr!("Timer expired"),
294 63 => cstr!("Out of streams resources"),
295 64 => cstr!("Machine is not on the network"),
296 65 => cstr!("Package not installed"),
297 66 => cstr!("Object is remote"),
298 67 => cstr!("Link has been severed"),
299 68 => cstr!("Advertise error"),
300 69 => cstr!("Srmount error"),
301 70 => cstr!("Communication error on send"),
302 71 => cstr!("Protocol error"),
303 72 => cstr!("Multihop attempted"),
304 73 => cstr!("RFS specific error"),
305 74 => cstr!("Bad message"),
306 75 => cstr!("Value too large for defined data type"),
307 76 => cstr!("Name not unique on network"),
308 77 => cstr!("File descriptor in bad state"),
309 78 => cstr!("Remote address changed"),
310 79 => cstr!("Can not access a needed shared library"),
311 80 => cstr!("Accessing a corrupted shared library"),
312 81 => cstr!(".lib section in a.out corrupted"),
313 82 => cstr!("Attempting to link in too many shared libraries"),
314 83 => cstr!("Cannot exec a shared library directly"),
315 84 => cstr!("Invalid or incomplete multibyte or wide character"),
316 85 => cstr!("Interrupted system call should be restarted"),
317 86 => cstr!("Streams pipe error"),
318 87 => cstr!("Too many users"),
319 88 => cstr!("Socket operation on non-socket"),
320 89 => cstr!("Destination address required"),
321 90 => cstr!("Message too long"),
322 91 => cstr!("Protocol wrong type for socket"),
323 92 => cstr!("Protocol not available"),
324 93 => cstr!("Protocol not supported"),
325 94 => cstr!("Socket type not supported"),
326 95 => cstr!("Operation not supported"),
327 96 => cstr!("Protocol family not supported"),
328 97 => cstr!("Address family not supported by protocol"),
329 98 => cstr!("Address already in use"),
330 99 => cstr!("Cannot assign requested address"),
331 100 => cstr!("Network is down"),
332 101 => cstr!("Network is unreachable"),
333 102 => cstr!("Network dropped connection on reset"),
334 103 => cstr!("Software caused connection abort"),
335 104 => cstr!("Connection reset by peer"),
336 105 => cstr!("No buffer space available"),
337 106 => cstr!("Transport endpoint is already connected"),
338 107 => cstr!("Transport endpoint is not connected"),
339 108 => cstr!("Cannot send after transport endpoint shutdown"),
340 109 => cstr!("Too many references: cannot splice"),
341 110 => cstr!("Connection timed out"),
342 111 => cstr!("Connection refused"),
343 112 => cstr!("Host is down"),
344 113 => cstr!("No route to host"),
345 114 => cstr!("Operation already in progress"),
346 115 => cstr!("Operation now in progress"),
347 116 => cstr!("Stale file handle"),
348 117 => cstr!("Structure needs cleaning"),
349 118 => cstr!("Not a XENIX named type file"),
350 119 => cstr!("No XENIX semaphores available"),
351 120 => cstr!("Is a named type file"),
352 121 => cstr!("Remote I/O error"),
353 122 => cstr!("Disk quota exceeded"),
354 123 => cstr!("No medium found"),
355 124 => cstr!("Wrong medium type"),
356 125 => cstr!("Operation canceled"),
357 126 => cstr!("Required key not available"),
358 127 => cstr!("Key has expired"),
359 128 => cstr!("Key has been revoked"),
360 129 => cstr!("Key was rejected by service"),
361 130 => cstr!("Owner died"),
362 131 => cstr!("State not recoverable"),
363 132 => cstr!("Operation not possible due to RF-kill"),
364 133 => cstr!("Memory page has hardware error"),
365 _ => cstr!("Unknown errno value"),
366 }
367 }
368