blob: b4a2f7b3c44813a0901cfe38ee891dfdcd7a38b1 [file] [log] [blame]
Pierre-Clément Tosi6a42fdc2022-12-08 13:51:05 +00001// 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 Tosia32c37c2022-12-08 13:54:03 +000017use core::ffi::c_char;
18use core::ffi::c_int;
19use core::ffi::CStr;
20
21use crate::eprintln;
Pierre-Clément Tosi6a42fdc2022-12-08 13:51:05 +000022use crate::linker;
23
24/// Reference to __stack_chk_guard.
25pub static STACK_CHK_GUARD: &u64 = unsafe { &linker::__stack_chk_guard };
26
27#[no_mangle]
28extern "C" fn __stack_chk_fail() -> ! {
29 panic!("stack guard check failed");
30}
Pierre-Clément Tosia32c37c2022-12-08 13:54:03 +000031
32/// Called from C to cause abnormal program termination.
33#[no_mangle]
34extern "C" fn abort() -> ! {
35 panic!("C code called abort()")
36}
37
38/// Error number set and read by C functions.
39pub static mut ERRNO: c_int = 0;
40
41#[no_mangle]
42unsafe 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]
56unsafe 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}