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::console;
18 use crate::eprintln;
19 use crate::rand::fill_with_entropy;
20 use crate::read_sysreg;
21 use core::ffi::c_char;
22 use core::ffi::c_int;
23 use core::ffi::c_void;
24 use core::ffi::CStr;
25 use core::ptr::addr_of_mut;
26 use core::slice;
27 use core::str;
28 
29 use cstr::cstr;
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]
__errno() -> *mut c_int75 unsafe extern "C" fn __errno() -> *mut c_int {
76     // SAFETY: C functions which call this are only called from the main thread, not from exception
77     // handlers.
78     unsafe { addr_of_mut!(ERRNO) as *mut _ }
79 }
80 
set_errno(value: c_int)81 fn set_errno(value: c_int) {
82     // SAFETY: vmbase is currently single-threaded.
83     unsafe { ERRNO = value };
84 }
85 
get_errno() -> c_int86 fn get_errno() -> c_int {
87     // SAFETY: vmbase is currently single-threaded.
88     unsafe { ERRNO }
89 }
90 
91 #[no_mangle]
getentropy(buffer: *mut c_void, length: usize) -> c_int92 extern "C" fn getentropy(buffer: *mut c_void, length: usize) -> c_int {
93     if length > 256 {
94         // The maximum permitted value for the length argument is 256.
95         set_errno(EIO);
96         return -1;
97     }
98 
99     // SAFETY: Just like libc, we need to assume that `ptr` is valid.
100     let buffer = unsafe { slice::from_raw_parts_mut(buffer.cast::<u8>(), length) };
101     fill_with_entropy(buffer).unwrap();
102 
103     0
104 }
105 
106 /// Reports a fatal error detected by Bionic.
107 ///
108 /// # Safety
109 ///
110 /// Input strings `prefix` and `format` must be valid and properly NUL-terminated.
111 ///
112 /// # Note
113 ///
114 /// This Rust functions is missing the last argument of its C/C++ counterpart, a va_list.
115 #[no_mangle]
async_safe_fatal_va_list(prefix: *const c_char, format: *const c_char)116 unsafe extern "C" fn async_safe_fatal_va_list(prefix: *const c_char, format: *const c_char) {
117     // SAFETY: The caller guaranteed that both strings were valid and NUL-terminated.
118     let (prefix, format) = unsafe { (CStr::from_ptr(prefix), CStr::from_ptr(format)) };
119 
120     if let (Ok(prefix), Ok(format)) = (prefix.to_str(), format.to_str()) {
121         // We don't bother with printf formatting.
122         eprintln!("FATAL BIONIC ERROR: {prefix}: \"{format}\" (unformatted)");
123     }
124 }
125 
126 #[repr(usize)]
127 /// Arbitrary token FILE pseudo-pointers used by C to refer to the default streams.
128 enum File {
129     Stdout = 0x7670cf00,
130     Stderr = 0x9d118200,
131 }
132 
133 impl TryFrom<usize> for File {
134     type Error = &'static str;
135 
try_from(value: usize) -> Result<Self, Self::Error>136     fn try_from(value: usize) -> Result<Self, Self::Error> {
137         match value {
138             x if x == File::Stdout as _ => Ok(File::Stdout),
139             x if x == File::Stderr as _ => Ok(File::Stderr),
140             _ => Err("Received Invalid FILE* from C"),
141         }
142     }
143 }
144 
145 #[no_mangle]
146 static stdout: File = File::Stdout;
147 #[no_mangle]
148 static stderr: File = File::Stderr;
149 
150 #[no_mangle]
fputs(c_str: *const c_char, stream: usize) -> c_int151 extern "C" fn fputs(c_str: *const c_char, stream: usize) -> c_int {
152     // SAFETY: Just like libc, we need to assume that `s` is a valid NULL-terminated string.
153     let c_str = unsafe { CStr::from_ptr(c_str) };
154 
155     if let (Ok(s), Ok(_)) = (c_str.to_str(), File::try_from(stream)) {
156         console::write_str(s);
157         0
158     } else {
159         set_errno(EOF);
160         EOF
161     }
162 }
163 
164 #[no_mangle]
fwrite(ptr: *const c_void, size: usize, nmemb: usize, stream: usize) -> usize165 extern "C" fn fwrite(ptr: *const c_void, size: usize, nmemb: usize, stream: usize) -> usize {
166     let length = size.saturating_mul(nmemb);
167 
168     // SAFETY: Just like libc, we need to assume that `ptr` is valid.
169     let bytes = unsafe { slice::from_raw_parts(ptr as *const u8, length) };
170 
171     if let (Ok(s), Ok(_)) = (str::from_utf8(bytes), File::try_from(stream)) {
172         console::write_str(s);
173         length
174     } else {
175         0
176     }
177 }
178 
179 #[no_mangle]
strerror(n: c_int) -> *mut c_char180 extern "C" fn strerror(n: c_int) -> *mut c_char {
181     cstr_error(n).as_ptr().cast_mut().cast()
182 }
183 
184 #[no_mangle]
perror(s: *const c_char)185 extern "C" fn perror(s: *const c_char) {
186     let prefix = if s.is_null() {
187         None
188     } else {
189         // SAFETY: Just like libc, we need to assume that `s` is a valid NULL-terminated string.
190         let c_str = unsafe { CStr::from_ptr(s) };
191         // TODO(Rust 1.71): if c_str.is_empty() {
192         if c_str.to_bytes().is_empty() {
193             None
194         } else {
195             Some(c_str.to_str().unwrap())
196         }
197     };
198 
199     let error = cstr_error(get_errno()).to_str().unwrap();
200 
201     if let Some(prefix) = prefix {
202         eprintln!("{prefix}: {error}");
203     } else {
204         eprintln!("{error}");
205     }
206 }
207 
cstr_error(n: c_int) -> &'static CStr208 fn cstr_error(n: c_int) -> &'static CStr {
209     // Messages taken from errno(1).
210     match n {
211         0 => cstr!("Success"),
212         1 => cstr!("Operation not permitted"),
213         2 => cstr!("No such file or directory"),
214         3 => cstr!("No such process"),
215         4 => cstr!("Interrupted system call"),
216         5 => cstr!("Input/output error"),
217         6 => cstr!("No such device or address"),
218         7 => cstr!("Argument list too long"),
219         8 => cstr!("Exec format error"),
220         9 => cstr!("Bad file descriptor"),
221         10 => cstr!("No child processes"),
222         11 => cstr!("Resource temporarily unavailable"),
223         12 => cstr!("Cannot allocate memory"),
224         13 => cstr!("Permission denied"),
225         14 => cstr!("Bad address"),
226         15 => cstr!("Block device required"),
227         16 => cstr!("Device or resource busy"),
228         17 => cstr!("File exists"),
229         18 => cstr!("Invalid cross-device link"),
230         19 => cstr!("No such device"),
231         20 => cstr!("Not a directory"),
232         21 => cstr!("Is a directory"),
233         22 => cstr!("Invalid argument"),
234         23 => cstr!("Too many open files in system"),
235         24 => cstr!("Too many open files"),
236         25 => cstr!("Inappropriate ioctl for device"),
237         26 => cstr!("Text file busy"),
238         27 => cstr!("File too large"),
239         28 => cstr!("No space left on device"),
240         29 => cstr!("Illegal seek"),
241         30 => cstr!("Read-only file system"),
242         31 => cstr!("Too many links"),
243         32 => cstr!("Broken pipe"),
244         33 => cstr!("Numerical argument out of domain"),
245         34 => cstr!("Numerical result out of range"),
246         35 => cstr!("Resource deadlock avoided"),
247         36 => cstr!("File name too long"),
248         37 => cstr!("No locks available"),
249         38 => cstr!("Function not implemented"),
250         39 => cstr!("Directory not empty"),
251         40 => cstr!("Too many levels of symbolic links"),
252         42 => cstr!("No message of desired type"),
253         43 => cstr!("Identifier removed"),
254         44 => cstr!("Channel number out of range"),
255         45 => cstr!("Level 2 not synchronized"),
256         46 => cstr!("Level 3 halted"),
257         47 => cstr!("Level 3 reset"),
258         48 => cstr!("Link number out of range"),
259         49 => cstr!("Protocol driver not attached"),
260         50 => cstr!("No CSI structure available"),
261         51 => cstr!("Level 2 halted"),
262         52 => cstr!("Invalid exchange"),
263         53 => cstr!("Invalid request descriptor"),
264         54 => cstr!("Exchange full"),
265         55 => cstr!("No anode"),
266         56 => cstr!("Invalid request code"),
267         57 => cstr!("Invalid slot"),
268         59 => cstr!("Bad font file format"),
269         60 => cstr!("Device not a stream"),
270         61 => cstr!("No data available"),
271         62 => cstr!("Timer expired"),
272         63 => cstr!("Out of streams resources"),
273         64 => cstr!("Machine is not on the network"),
274         65 => cstr!("Package not installed"),
275         66 => cstr!("Object is remote"),
276         67 => cstr!("Link has been severed"),
277         68 => cstr!("Advertise error"),
278         69 => cstr!("Srmount error"),
279         70 => cstr!("Communication error on send"),
280         71 => cstr!("Protocol error"),
281         72 => cstr!("Multihop attempted"),
282         73 => cstr!("RFS specific error"),
283         74 => cstr!("Bad message"),
284         75 => cstr!("Value too large for defined data type"),
285         76 => cstr!("Name not unique on network"),
286         77 => cstr!("File descriptor in bad state"),
287         78 => cstr!("Remote address changed"),
288         79 => cstr!("Can not access a needed shared library"),
289         80 => cstr!("Accessing a corrupted shared library"),
290         81 => cstr!(".lib section in a.out corrupted"),
291         82 => cstr!("Attempting to link in too many shared libraries"),
292         83 => cstr!("Cannot exec a shared library directly"),
293         84 => cstr!("Invalid or incomplete multibyte or wide character"),
294         85 => cstr!("Interrupted system call should be restarted"),
295         86 => cstr!("Streams pipe error"),
296         87 => cstr!("Too many users"),
297         88 => cstr!("Socket operation on non-socket"),
298         89 => cstr!("Destination address required"),
299         90 => cstr!("Message too long"),
300         91 => cstr!("Protocol wrong type for socket"),
301         92 => cstr!("Protocol not available"),
302         93 => cstr!("Protocol not supported"),
303         94 => cstr!("Socket type not supported"),
304         95 => cstr!("Operation not supported"),
305         96 => cstr!("Protocol family not supported"),
306         97 => cstr!("Address family not supported by protocol"),
307         98 => cstr!("Address already in use"),
308         99 => cstr!("Cannot assign requested address"),
309         100 => cstr!("Network is down"),
310         101 => cstr!("Network is unreachable"),
311         102 => cstr!("Network dropped connection on reset"),
312         103 => cstr!("Software caused connection abort"),
313         104 => cstr!("Connection reset by peer"),
314         105 => cstr!("No buffer space available"),
315         106 => cstr!("Transport endpoint is already connected"),
316         107 => cstr!("Transport endpoint is not connected"),
317         108 => cstr!("Cannot send after transport endpoint shutdown"),
318         109 => cstr!("Too many references: cannot splice"),
319         110 => cstr!("Connection timed out"),
320         111 => cstr!("Connection refused"),
321         112 => cstr!("Host is down"),
322         113 => cstr!("No route to host"),
323         114 => cstr!("Operation already in progress"),
324         115 => cstr!("Operation now in progress"),
325         116 => cstr!("Stale file handle"),
326         117 => cstr!("Structure needs cleaning"),
327         118 => cstr!("Not a XENIX named type file"),
328         119 => cstr!("No XENIX semaphores available"),
329         120 => cstr!("Is a named type file"),
330         121 => cstr!("Remote I/O error"),
331         122 => cstr!("Disk quota exceeded"),
332         123 => cstr!("No medium found"),
333         124 => cstr!("Wrong medium type"),
334         125 => cstr!("Operation canceled"),
335         126 => cstr!("Required key not available"),
336         127 => cstr!("Key has expired"),
337         128 => cstr!("Key has been revoked"),
338         129 => cstr!("Key was rejected by service"),
339         130 => cstr!("Owner died"),
340         131 => cstr!("State not recoverable"),
341         132 => cstr!("Operation not possible due to RF-kill"),
342         133 => cstr!("Memory page has hardware error"),
343         _ => cstr!("Unknown errno value"),
344     }
345 }
346