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; |
| 19 | use core::ffi::CStr; |
| 20 | |
| 21 | use crate::eprintln; |
Pierre-Clément Tosi | 6a42fdc | 2022-12-08 13:51:05 +0000 | [diff] [blame] | 22 | use crate::linker; |
| 23 | |
| 24 | /// Reference to __stack_chk_guard. |
| 25 | pub static STACK_CHK_GUARD: &u64 = unsafe { &linker::__stack_chk_guard }; |
| 26 | |
| 27 | #[no_mangle] |
| 28 | extern "C" fn __stack_chk_fail() -> ! { |
| 29 | panic!("stack guard check failed"); |
| 30 | } |
Pierre-Clément Tosi | a32c37c | 2022-12-08 13:54:03 +0000 | [diff] [blame] | 31 | |
| 32 | /// Called from C to cause abnormal program termination. |
| 33 | #[no_mangle] |
| 34 | extern "C" fn abort() -> ! { |
| 35 | panic!("C code called abort()") |
| 36 | } |
| 37 | |
| 38 | /// Error number set and read by C functions. |
| 39 | pub static mut ERRNO: c_int = 0; |
| 40 | |
| 41 | #[no_mangle] |
| 42 | unsafe extern "C" fn __errno() -> *mut c_int { |
| 43 | &mut ERRNO as *mut _ |
| 44 | } |
| 45 | |
| 46 | /// Reports a fatal error detected by Bionic. |
| 47 | /// |
| 48 | /// # Safety |
| 49 | /// |
| 50 | /// Input strings `prefix` and `format` must be properly NULL-terminated. |
| 51 | /// |
| 52 | /// # Note |
| 53 | /// |
| 54 | /// This Rust functions is missing the last argument of its C/C++ counterpart, a va_list. |
| 55 | #[no_mangle] |
| 56 | unsafe extern "C" fn async_safe_fatal_va_list(prefix: *const c_char, format: *const c_char) { |
| 57 | let prefix = CStr::from_ptr(prefix); |
| 58 | let format = CStr::from_ptr(format); |
| 59 | |
| 60 | if let (Ok(prefix), Ok(format)) = (prefix.to_str(), format.to_str()) { |
| 61 | // We don't bother with printf formatting. |
| 62 | eprintln!("FATAL BIONIC ERROR: {prefix}: \"{format}\" (unformatted)"); |
| 63 | } |
| 64 | } |