Pierre-Clément Tosi | 6a42fdc | 2022-12-08 13:51:05 +0000 | [diff] [blame] | 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 | |
Pierre-Clément Tosi | a32c37c | 2022-12-08 13:54:03 +0000 | [diff] [blame] | 17 | use core::ffi::c_char; |
| 18 | use core::ffi::c_int; |
Pierre-Clément Tosi | ba4af8a | 2022-12-20 17:32:37 +0000 | [diff] [blame] | 19 | use core::ffi::c_void; |
Pierre-Clément Tosi | a32c37c | 2022-12-08 13:54:03 +0000 | [diff] [blame] | 20 | use core::ffi::CStr; |
Pierre-Clément Tosi | ba4af8a | 2022-12-20 17:32:37 +0000 | [diff] [blame] | 21 | use core::slice; |
| 22 | use core::str; |
Pierre-Clément Tosi | a32c37c | 2022-12-08 13:54:03 +0000 | [diff] [blame] | 23 | |
Pierre-Clément Tosi | ba4af8a | 2022-12-20 17:32:37 +0000 | [diff] [blame] | 24 | use crate::console; |
Pierre-Clément Tosi | a32c37c | 2022-12-08 13:54:03 +0000 | [diff] [blame] | 25 | use crate::eprintln; |
Pierre-Clément Tosi | 6a42fdc | 2022-12-08 13:51:05 +0000 | [diff] [blame] | 26 | use crate::linker; |
| 27 | |
Pierre-Clément Tosi | ba4af8a | 2022-12-20 17:32:37 +0000 | [diff] [blame] | 28 | const EOF: c_int = -1; |
| 29 | |
Pierre-Clément Tosi | 6a42fdc | 2022-12-08 13:51:05 +0000 | [diff] [blame] | 30 | /// Reference to __stack_chk_guard. |
| 31 | pub static STACK_CHK_GUARD: &u64 = unsafe { &linker::__stack_chk_guard }; |
| 32 | |
| 33 | #[no_mangle] |
| 34 | extern "C" fn __stack_chk_fail() -> ! { |
| 35 | panic!("stack guard check failed"); |
| 36 | } |
Pierre-Clément Tosi | a32c37c | 2022-12-08 13:54:03 +0000 | [diff] [blame] | 37 | |
| 38 | /// Called from C to cause abnormal program termination. |
| 39 | #[no_mangle] |
| 40 | extern "C" fn abort() -> ! { |
| 41 | panic!("C code called abort()") |
| 42 | } |
| 43 | |
| 44 | /// Error number set and read by C functions. |
| 45 | pub static mut ERRNO: c_int = 0; |
| 46 | |
| 47 | #[no_mangle] |
| 48 | unsafe extern "C" fn __errno() -> *mut c_int { |
| 49 | &mut ERRNO as *mut _ |
| 50 | } |
| 51 | |
Pierre-Clément Tosi | ba4af8a | 2022-12-20 17:32:37 +0000 | [diff] [blame] | 52 | fn set_errno(value: c_int) { |
| 53 | // SAFETY - vmbase is currently single-threaded. |
| 54 | unsafe { ERRNO = value }; |
| 55 | } |
| 56 | |
Pierre-Clément Tosi | a32c37c | 2022-12-08 13:54:03 +0000 | [diff] [blame] | 57 | /// Reports a fatal error detected by Bionic. |
| 58 | /// |
| 59 | /// # Safety |
| 60 | /// |
| 61 | /// Input strings `prefix` and `format` must be properly NULL-terminated. |
| 62 | /// |
| 63 | /// # Note |
| 64 | /// |
| 65 | /// This Rust functions is missing the last argument of its C/C++ counterpart, a va_list. |
| 66 | #[no_mangle] |
| 67 | unsafe extern "C" fn async_safe_fatal_va_list(prefix: *const c_char, format: *const c_char) { |
| 68 | let prefix = CStr::from_ptr(prefix); |
| 69 | let format = CStr::from_ptr(format); |
| 70 | |
| 71 | if let (Ok(prefix), Ok(format)) = (prefix.to_str(), format.to_str()) { |
| 72 | // We don't bother with printf formatting. |
| 73 | eprintln!("FATAL BIONIC ERROR: {prefix}: \"{format}\" (unformatted)"); |
| 74 | } |
| 75 | } |
Pierre-Clément Tosi | ba4af8a | 2022-12-20 17:32:37 +0000 | [diff] [blame] | 76 | |
| 77 | #[repr(usize)] |
| 78 | /// Arbitrary token FILE pseudo-pointers used by C to refer to the default streams. |
| 79 | enum File { |
| 80 | Stdout = 0x7670cf00, |
| 81 | Stderr = 0x9d118200, |
| 82 | } |
| 83 | |
| 84 | impl TryFrom<usize> for File { |
| 85 | type Error = &'static str; |
| 86 | |
| 87 | fn try_from(value: usize) -> Result<Self, Self::Error> { |
| 88 | match value { |
| 89 | x if x == File::Stdout as _ => Ok(File::Stdout), |
| 90 | x if x == File::Stderr as _ => Ok(File::Stderr), |
| 91 | _ => Err("Received Invalid FILE* from C"), |
| 92 | } |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | #[no_mangle] |
| 97 | static stdout: File = File::Stdout; |
| 98 | #[no_mangle] |
| 99 | static stderr: File = File::Stderr; |
| 100 | |
| 101 | #[no_mangle] |
| 102 | extern "C" fn fputs(c_str: *const c_char, stream: usize) -> c_int { |
| 103 | // SAFETY - Just like libc, we need to assume that `s` is a valid NULL-terminated string. |
| 104 | let c_str = unsafe { CStr::from_ptr(c_str) }; |
| 105 | |
| 106 | if let (Ok(s), Ok(_)) = (c_str.to_str(), File::try_from(stream)) { |
| 107 | console::write_str(s); |
| 108 | 0 |
| 109 | } else { |
| 110 | set_errno(EOF); |
| 111 | EOF |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | #[no_mangle] |
| 116 | extern "C" fn fwrite(ptr: *const c_void, size: usize, nmemb: usize, stream: usize) -> usize { |
| 117 | let length = size.saturating_mul(nmemb); |
| 118 | |
| 119 | // SAFETY - Just like libc, we need to assume that `ptr` is valid. |
| 120 | let bytes = unsafe { slice::from_raw_parts(ptr as *const u8, length) }; |
| 121 | |
| 122 | if let (Ok(s), Ok(_)) = (str::from_utf8(bytes), File::try_from(stream)) { |
| 123 | console::write_str(s); |
| 124 | length |
| 125 | } else { |
| 126 | 0 |
| 127 | } |
| 128 | } |