1 //! Android ABI-compatibility module
3 //! The ABI of Android has changed quite a bit over time, and libstd attempts to
4 //! be both forwards and backwards compatible as much as possible. We want to
5 //! always work with the most recent version of Android, but we also want to
6 //! work with older versions of Android for whenever projects need to.
8 //! Our current minimum supported Android version is `android-9`, e.g., Android
9 //! with API level 9. We then in theory want to work on that and all future
10 //! versions of Android!
12 //! Some of the detection here is done at runtime via `dlopen` and
13 //! introspection. Other times no detection is performed at all and we just
14 //! provide a fallback implementation as some versions of Android we support
15 //! don't have the function.
17 //! You'll find more details below about why each compatibility shim is needed.
19 #![cfg(target_os = "android")]
21 use libc::{c_int, c_void, sighandler_t, size_t, ssize_t};
22 use libc::{ftruncate, pread, pwrite};
24 use super::{cvt, cvt_r};
27 // The `log2` and `log2f` functions apparently appeared in android-18, or at
28 // least you can see they're not present in the android-17 header [1] and they
29 // are present in android-18 [2].
31 // [1]: https://chromium.googlesource.com/android_tools/+/20ee6d20/ndk/platforms
32 // /android-17/arch-arm/usr/include/math.h
33 // [2]: https://chromium.googlesource.com/android_tools/+/20ee6d20/ndk/platforms
34 // /android-18/arch-arm/usr/include/math.h
36 // Note that these shims are likely less precise than directly calling `log2`,
37 // but hopefully that should be enough for now...
39 // Note that mathematically, for any arbitrary `y`:
41 // log_2(x) = log_y(x) / log_y(2)
42 // = log_y(x) / (1 / log_2(y))
43 // = log_y(x) * log_2(y)
45 // Hence because `ln` (log_e) is available on all Android we just choose `y = e`
48 // log_2(x) = ln(x) * log_2(e)
51 pub fn log2f32(f: f32) -> f32 {
52 f.ln() * crate::f32::consts::LOG2_E
56 pub fn log2f64(f: f64) -> f64 {
57 f.ln() * crate::f64::consts::LOG2_E
60 // Back in the day [1] the `signal` function was just an inline wrapper
61 // around `bsd_signal`, but starting in API level android-20 the `signal`
62 // symbols was introduced [2]. Finally, in android-21 the API `bsd_signal` was
65 // Basically this means that if we want to be binary compatible with multiple
66 // Android releases (oldest being 9 and newest being 21) then we need to check
67 // for both symbols and not actually link against either.
69 // [1]: https://chromium.googlesource.com/android_tools/+/20ee6d20/ndk/platforms
70 // /android-18/arch-arm/usr/include/signal.h
71 // [2]: https://chromium.googlesource.com/android_tools/+/fbd420/ndk_experimental
72 // /platforms/android-20/arch-arm
73 // /usr/include/signal.h
74 // [3]: https://chromium.googlesource.com/android_tools/+/20ee6d/ndk/platforms
75 // /android-21/arch-arm/usr/include/signal.h
76 pub unsafe fn signal(signum: c_int, handler: sighandler_t) -> sighandler_t {
77 weak!(fn signal(c_int, sighandler_t) -> sighandler_t);
78 weak!(fn bsd_signal(c_int, sighandler_t) -> sighandler_t);
80 let f = signal.get().or_else(|| bsd_signal.get());
81 let f = f.expect("neither `signal` nor `bsd_signal` symbols found");
85 // The `ftruncate64` symbol apparently appeared in android-12, so we do some
86 // dynamic detection to see if we can figure out whether `ftruncate64` exists.
88 // If it doesn't we just fall back to `ftruncate`, generating an error for
90 #[cfg(target_pointer_width = "32")]
91 pub fn ftruncate64(fd: c_int, size: u64) -> io::Result<()> {
92 weak!(fn ftruncate64(c_int, i64) -> c_int);
95 match ftruncate64.get() {
96 Some(f) => cvt_r(|| f(fd, size as i64)).map(drop),
98 if size > i32::MAX as u64 {
99 Err(io::Error::new(io::ErrorKind::InvalidInput, "cannot truncate >2GB"))
101 cvt_r(|| ftruncate(fd, size as i32)).map(drop)
108 #[cfg(target_pointer_width = "64")]
109 pub fn ftruncate64(fd: c_int, size: u64) -> io::Result<()> {
110 unsafe { cvt_r(|| ftruncate(fd, size as i64)).map(drop) }
113 #[cfg(target_pointer_width = "32")]
114 pub unsafe fn cvt_pread64(
119 ) -> io::Result<ssize_t> {
120 use crate::convert::TryInto;
121 weak!(fn pread64(c_int, *mut c_void, size_t, i64) -> ssize_t);
122 pread64.get().map(|f| cvt(f(fd, buf, count, offset))).unwrap_or_else(|| {
123 if let Ok(o) = offset.try_into() {
124 cvt(pread(fd, buf, count, o))
126 Err(io::Error::new(io::ErrorKind::InvalidInput, "cannot pread >2GB"))
131 #[cfg(target_pointer_width = "32")]
132 pub unsafe fn cvt_pwrite64(
137 ) -> io::Result<ssize_t> {
138 use crate::convert::TryInto;
139 weak!(fn pwrite64(c_int, *const c_void, size_t, i64) -> ssize_t);
140 pwrite64.get().map(|f| cvt(f(fd, buf, count, offset))).unwrap_or_else(|| {
141 if let Ok(o) = offset.try_into() {
142 cvt(pwrite(fd, buf, count, o))
144 Err(io::Error::new(io::ErrorKind::InvalidInput, "cannot pwrite >2GB"))
149 #[cfg(target_pointer_width = "64")]
150 pub unsafe fn cvt_pread64(
155 ) -> io::Result<ssize_t> {
156 cvt(pread(fd, buf, count, offset))
159 #[cfg(target_pointer_width = "64")]
160 pub unsafe fn cvt_pwrite64(
165 ) -> io::Result<ssize_t> {
166 cvt(pwrite(fd, buf, count, offset))