]> git.lizzy.rs Git - rust.git/blob - src/libstd/sys/unix/android.rs
10436723a81d0d108f2c65da6c19497addea584d
[rust.git] / src / libstd / sys / unix / android.rs
1 // Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 //! Android ABI-compatibility module
12 //!
13 //! The ABI of Android has changed quite a bit over time, and libstd attempts to
14 //! be both forwards and backwards compatible as much as possible. We want to
15 //! always work with the most recent version of Android, but we also want to
16 //! work with older versions of Android for whenever projects need to.
17 //!
18 //! Our current minimum supported Android version is `android-9`, e.g. Android
19 //! with API level 9. We then in theory want to work on that and all future
20 //! versions of Android!
21 //!
22 //! Some of the detection here is done at runtime via `dlopen` and
23 //! introspection. Other times no detection is performed at all and we just
24 //! provide a fallback implementation as some versions of Android we support
25 //! don't have the function.
26 //!
27 //! You'll find more details below about why each compatibility shim is needed.
28
29 #![cfg(target_os = "android")]
30
31 use libc::{c_int, c_void, sighandler_t, size_t, ssize_t};
32 use libc::{ftruncate, pread, pwrite};
33
34 use io;
35 use super::{cvt, cvt_r};
36
37 // The `log2` and `log2f` functions apparently appeared in android-18, or at
38 // least you can see they're not present in the android-17 header [1] and they
39 // are present in android-18 [2].
40 //
41 // [1]: https://chromium.googlesource.com/android_tools/+/20ee6d20/ndk/platforms
42 //                                       /android-17/arch-arm/usr/include/math.h
43 // [2]: https://chromium.googlesource.com/android_tools/+/20ee6d20/ndk/platforms
44 //                                       /android-18/arch-arm/usr/include/math.h
45 //
46 // Note that these shims are likely less precise than directly calling `log2`,
47 // but hopefully that should be enough for now...
48 //
49 // Note that mathematically, for any arbitrary `y`:
50 //
51 //      log_2(x) = log_y(x) / log_y(2)
52 //               = log_y(x) / (1 / log_2(y))
53 //               = log_y(x) * log_2(y)
54 //
55 // Hence because `ln` (log_e) is available on all Android we just choose `y = e`
56 // and get:
57 //
58 //      log_2(x) = ln(x) * log_2(e)
59
60 #[cfg(not(test))]
61 pub fn log2f32(f: f32) -> f32 {
62     f.ln() * ::f32::consts::LOG2_E
63 }
64
65 #[cfg(not(test))]
66 pub fn log2f64(f: f64) -> f64 {
67     f.ln() * ::f64::consts::LOG2_E
68 }
69
70 // Back in the day [1] the `signal` function was just an inline wrapper
71 // around `bsd_signal`, but starting in API level android-20 the `signal`
72 // symbols was introduced [2]. Finally, in android-21 the API `bsd_signal` was
73 // removed [3].
74 //
75 // Basically this means that if we want to be binary compatible with multiple
76 // Android releases (oldest being 9 and newest being 21) then we need to check
77 // for both symbols and not actually link against either.
78 //
79 // [1]: https://chromium.googlesource.com/android_tools/+/20ee6d20/ndk/platforms
80 //                                       /android-18/arch-arm/usr/include/signal.h
81 // [2]: https://chromium.googlesource.com/android_tools/+/fbd420/ndk_experimental
82 //                                       /platforms/android-20/arch-arm
83 //                                       /usr/include/signal.h
84 // [3]: https://chromium.googlesource.com/android_tools/+/20ee6d/ndk/platforms
85 //                                       /android-21/arch-arm/usr/include/signal.h
86 pub unsafe fn signal(signum: c_int, handler: sighandler_t) -> sighandler_t {
87     weak!(fn signal(c_int, sighandler_t) -> sighandler_t);
88     weak!(fn bsd_signal(c_int, sighandler_t) -> sighandler_t);
89
90     let f = signal.get().or_else(|| bsd_signal.get());
91     let f = f.expect("neither `signal` nor `bsd_signal` symbols found");
92     f(signum, handler)
93 }
94
95 // The `ftruncate64` symbol apparently appeared in android-12, so we do some
96 // dynamic detection to see if we can figure out whether `ftruncate64` exists.
97 //
98 // If it doesn't we just fall back to `ftruncate`, generating an error for
99 // too-large values.
100 #[cfg(target_pointer_width = "32")]
101 pub fn ftruncate64(fd: c_int, size: u64) -> io::Result<()> {
102     weak!(fn ftruncate64(c_int, i64) -> c_int);
103
104     unsafe {
105         match ftruncate64.get() {
106             Some(f) => cvt_r(|| f(fd, size as i64)).map(|_| ()),
107             None => {
108                 if size > i32::max_value() as u64 {
109                     Err(io::Error::new(io::ErrorKind::InvalidInput,
110                                        "cannot truncate >2GB"))
111                 } else {
112                     cvt_r(|| ftruncate(fd, size as i32)).map(|_| ())
113                 }
114             }
115         }
116     }
117 }
118
119 #[cfg(target_pointer_width = "64")]
120 pub fn ftruncate64(fd: c_int, size: u64) -> io::Result<()> {
121     unsafe {
122         cvt_r(|| ftruncate(fd, size as i64)).map(|_| ())
123     }
124 }
125
126 #[cfg(target_pointer_width = "32")]
127 pub unsafe fn cvt_pread64(fd: c_int, buf: *mut c_void, count: size_t, offset: i64)
128     -> io::Result<ssize_t>
129 {
130     use convert::TryInto;
131     weak!(fn pread64(c_int, *mut c_void, size_t, i64) -> ssize_t);
132     pread64.get().map(|f| cvt(f(fd, buf, count, offset))).unwrap_or_else(|| {
133         if let Ok(o) = offset.try_into() {
134             cvt(pread(fd, buf, count, o))
135         } else {
136             Err(io::Error::new(io::ErrorKind::InvalidInput,
137                                "cannot pread >2GB"))
138         }
139     })
140 }
141
142 #[cfg(target_pointer_width = "32")]
143 pub unsafe fn cvt_pwrite64(fd: c_int, buf: *const c_void, count: size_t, offset: i64)
144     -> io::Result<ssize_t>
145 {
146     use convert::TryInto;
147     weak!(fn pwrite64(c_int, *const c_void, size_t, i64) -> ssize_t);
148     pwrite64.get().map(|f| cvt(f(fd, buf, count, offset))).unwrap_or_else(|| {
149         if let Ok(o) = offset.try_into() {
150             cvt(pwrite(fd, buf, count, o))
151         } else {
152             Err(io::Error::new(io::ErrorKind::InvalidInput,
153                                "cannot pwrite >2GB"))
154         }
155     })
156 }
157
158 #[cfg(target_pointer_width = "64")]
159 pub unsafe fn cvt_pread64(fd: c_int, buf: *mut c_void, count: size_t, offset: i64)
160     -> io::Result<ssize_t>
161 {
162     cvt(pread(fd, buf, count, offset))
163 }
164
165 #[cfg(target_pointer_width = "64")]
166 pub unsafe fn cvt_pwrite64(fd: c_int, buf: *const c_void, count: size_t, offset: i64)
167     -> io::Result<ssize_t>
168 {
169     cvt(pwrite(fd, buf, count, offset))
170 }