]> git.lizzy.rs Git - rust.git/blob - src/libstd/sys/unix/android.rs
Rollup merge of #60429 - estebank:pub-path, r=michaelwoerister
[rust.git] / src / libstd / sys / unix / android.rs
1 //! Android ABI-compatibility module
2 //!
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.
7 //!
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!
11 //!
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.
16 //!
17 //! You'll find more details below about why each compatibility shim is needed.
18
19 #![cfg(target_os = "android")]
20
21 use libc::{c_int, c_void, sighandler_t, size_t, ssize_t};
22 use libc::{ftruncate, pread, pwrite};
23
24 use crate::io;
25 use super::{cvt, cvt_r};
26
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].
30 //
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
35 //
36 // Note that these shims are likely less precise than directly calling `log2`,
37 // but hopefully that should be enough for now...
38 //
39 // Note that mathematically, for any arbitrary `y`:
40 //
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)
44 //
45 // Hence because `ln` (log_e) is available on all Android we just choose `y = e`
46 // and get:
47 //
48 //      log_2(x) = ln(x) * log_2(e)
49
50 #[cfg(not(test))]
51 pub fn log2f32(f: f32) -> f32 {
52     f.ln() * crate::f32::consts::LOG2_E
53 }
54
55 #[cfg(not(test))]
56 pub fn log2f64(f: f64) -> f64 {
57     f.ln() * crate::f64::consts::LOG2_E
58 }
59
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
63 // removed [3].
64 //
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.
68 //
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);
79
80     let f = signal.get().or_else(|| bsd_signal.get());
81     let f = f.expect("neither `signal` nor `bsd_signal` symbols found");
82     f(signum, handler)
83 }
84
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.
87 //
88 // If it doesn't we just fall back to `ftruncate`, generating an error for
89 // too-large values.
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);
93
94     unsafe {
95         match ftruncate64.get() {
96             Some(f) => cvt_r(|| f(fd, size as i64)).map(|_| ()),
97             None => {
98                 if size > i32::max_value() as u64 {
99                     Err(io::Error::new(io::ErrorKind::InvalidInput,
100                                        "cannot truncate >2GB"))
101                 } else {
102                     cvt_r(|| ftruncate(fd, size as i32)).map(|_| ())
103                 }
104             }
105         }
106     }
107 }
108
109 #[cfg(target_pointer_width = "64")]
110 pub fn ftruncate64(fd: c_int, size: u64) -> io::Result<()> {
111     unsafe {
112         cvt_r(|| ftruncate(fd, size as i64)).map(|_| ())
113     }
114 }
115
116 #[cfg(target_pointer_width = "32")]
117 pub unsafe fn cvt_pread64(fd: c_int, buf: *mut c_void, count: size_t, offset: i64)
118     -> io::Result<ssize_t>
119 {
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))
125         } else {
126             Err(io::Error::new(io::ErrorKind::InvalidInput,
127                                "cannot pread >2GB"))
128         }
129     })
130 }
131
132 #[cfg(target_pointer_width = "32")]
133 pub unsafe fn cvt_pwrite64(fd: c_int, buf: *const c_void, count: size_t, offset: i64)
134     -> io::Result<ssize_t>
135 {
136     use crate::convert::TryInto;
137     weak!(fn pwrite64(c_int, *const c_void, size_t, i64) -> ssize_t);
138     pwrite64.get().map(|f| cvt(f(fd, buf, count, offset))).unwrap_or_else(|| {
139         if let Ok(o) = offset.try_into() {
140             cvt(pwrite(fd, buf, count, o))
141         } else {
142             Err(io::Error::new(io::ErrorKind::InvalidInput,
143                                "cannot pwrite >2GB"))
144         }
145     })
146 }
147
148 #[cfg(target_pointer_width = "64")]
149 pub unsafe fn cvt_pread64(fd: c_int, buf: *mut c_void, count: size_t, offset: i64)
150     -> io::Result<ssize_t>
151 {
152     cvt(pread(fd, buf, count, offset))
153 }
154
155 #[cfg(target_pointer_width = "64")]
156 pub unsafe fn cvt_pwrite64(fd: c_int, buf: *const c_void, count: size_t, offset: i64)
157     -> io::Result<ssize_t>
158 {
159     cvt(pwrite(fd, buf, count, offset))
160 }