blob: 6f88cf69b80e4915e48c103c5b4955e746c87032 [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;
Pierre-Clément Tosiba4af8a2022-12-20 17:32:37 +000019use core::ffi::c_void;
Pierre-Clément Tosia32c37c2022-12-08 13:54:03 +000020use core::ffi::CStr;
Pierre-Clément Tosiba4af8a2022-12-20 17:32:37 +000021use core::slice;
22use core::str;
Pierre-Clément Tosia32c37c2022-12-08 13:54:03 +000023
Pierre-Clément Tosiba4af8a2022-12-20 17:32:37 +000024use crate::console;
Pierre-Clément Tosia32c37c2022-12-08 13:54:03 +000025use crate::eprintln;
Pierre-Clément Tosi6a42fdc2022-12-08 13:51:05 +000026use crate::linker;
27
Pierre-Clément Tosiba4af8a2022-12-20 17:32:37 +000028const EOF: c_int = -1;
29
Pierre-Clément Tosi6a42fdc2022-12-08 13:51:05 +000030/// Reference to __stack_chk_guard.
31pub static STACK_CHK_GUARD: &u64 = unsafe { &linker::__stack_chk_guard };
32
33#[no_mangle]
34extern "C" fn __stack_chk_fail() -> ! {
35 panic!("stack guard check failed");
36}
Pierre-Clément Tosia32c37c2022-12-08 13:54:03 +000037
38/// Called from C to cause abnormal program termination.
39#[no_mangle]
40extern "C" fn abort() -> ! {
41 panic!("C code called abort()")
42}
43
44/// Error number set and read by C functions.
45pub static mut ERRNO: c_int = 0;
46
47#[no_mangle]
48unsafe extern "C" fn __errno() -> *mut c_int {
49 &mut ERRNO as *mut _
50}
51
Pierre-Clément Tosiba4af8a2022-12-20 17:32:37 +000052fn set_errno(value: c_int) {
53 // SAFETY - vmbase is currently single-threaded.
54 unsafe { ERRNO = value };
55}
56
Pierre-Clément Tosia32c37c2022-12-08 13:54:03 +000057/// 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]
67unsafe 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 Tosiba4af8a2022-12-20 17:32:37 +000076
77#[repr(usize)]
78/// Arbitrary token FILE pseudo-pointers used by C to refer to the default streams.
79enum File {
80 Stdout = 0x7670cf00,
81 Stderr = 0x9d118200,
82}
83
84impl 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]
97static stdout: File = File::Stdout;
98#[no_mangle]
99static stderr: File = File::Stderr;
100
101#[no_mangle]
102extern "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]
116extern "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}