Elliott Hughes | a348174 | 2017-11-28 14:47:17 -0800 | [diff] [blame] | 1 | # 32-bit ABI bugs |
Elliott Hughes | 0bfcbaf | 2017-08-28 09:18:34 -0700 | [diff] [blame] | 2 | |
Elliott Hughes | a348174 | 2017-11-28 14:47:17 -0800 | [diff] [blame] | 3 | ## 32-bit `off_t` and `_FILE_OFFSET_BITS=64` |
Elliott Hughes | 0bfcbaf | 2017-08-28 09:18:34 -0700 | [diff] [blame] | 4 | |
| 5 | On 32-bit Android, `off_t` is a signed 32-bit integer. This limits functions |
| 6 | that use `off_t` to working on files no larger than 2GiB. |
| 7 | |
| 8 | Android does not require the `_LARGEFILE_SOURCE` macro to be used to make |
| 9 | `fseeko` and `ftello` available. Instead they're always available from API |
| 10 | level 24 where they were introduced, and never available before then. |
| 11 | |
| 12 | Android also does not require the `_LARGEFILE64_SOURCE` macro to be used |
| 13 | to make `off64_t` and corresponding functions such as `ftruncate64` available. |
| 14 | Instead, whatever subset of those functions was available at your target API |
| 15 | level will be visible. |
| 16 | |
| 17 | There are a couple of exceptions to note. Firstly, `off64_t` and the single |
| 18 | function `lseek64` were available right from the beginning in API 3. Secondly, |
| 19 | Android has always silently inserted `O_LARGEFILE` into any open call, so if |
| 20 | all you need are functions like `read` that don't take/return `off_t`, large |
| 21 | files have always worked. |
| 22 | |
| 23 | Android support for `_FILE_OFFSET_BITS=64` (which turns `off_t` into `off64_t` |
| 24 | and replaces each `off_t` function with its `off64_t` counterpart, such as |
| 25 | `lseek` in the source becoming `lseek64` at runtime) was added late. Even when |
| 26 | it became available for the platform, it wasn't available from the NDK until |
| 27 | r15. Before NDK r15, `_FILE_OFFSET_BITS=64` silently did nothing: all code |
| 28 | compiled with that was actually using a 32-bit `off_t`. With a new enough NDK, |
| 29 | the situation becomes complicated. If you're targeting an API before 21, almost |
| 30 | all functions that take an `off_t` become unavailable. You've asked for their |
| 31 | 64-bit equivalents, and none of them (except `lseek`/`lseek64`) exist. As you |
| 32 | increase your target API level, you'll have more and more of the functions |
| 33 | available. API 12 adds some of the `<unistd.h>` functions, API 21 adds `mmap`, |
| 34 | and by API 24 you have everything including `<stdio.h>`. See the |
Elliott Hughes | a348174 | 2017-11-28 14:47:17 -0800 | [diff] [blame] | 35 | [linker map](libc/libc.map.txt) for full details. Note also that in NDK r16 and |
Elliott Hughes | 3d98adf | 2017-11-28 20:01:57 -0800 | [diff] [blame] | 36 | later, if you're using Clang we'll inline an `mmap64` implementation in the |
| 37 | headers when you target an API before 21 because it's an easy special case |
| 38 | that's often needed. This means that code using `_FILE_OFFSET_BITS=64` |
| 39 | and `mmap` (but no other functions that are unavailable at your target |
| 40 | API level) will always compile. |
Elliott Hughes | a348174 | 2017-11-28 14:47:17 -0800 | [diff] [blame] | 41 | |
Elliott Hughes | 3d98adf | 2017-11-28 20:01:57 -0800 | [diff] [blame] | 42 | If your code stops compiling when you move to NDK r15 or later, removing every |
Elliott Hughes | a348174 | 2017-11-28 14:47:17 -0800 | [diff] [blame] | 43 | definition of `_FILE_OFFSET_BITS=64` will restore the behavior you used to have: |
Elliott Hughes | 3d98adf | 2017-11-28 20:01:57 -0800 | [diff] [blame] | 44 | you'll have a 32-bit `off_t` and use the 32-bit functions. Make sure you |
| 45 | grep thoroughly in both your source and your build system: many people |
| 46 | aren't aware that `_FILE_OFFSET_BITS` is set. You might also have to |
| 47 | remove references to `__USE_FILE_OFFSET64` --- this is the internal |
| 48 | flag that should never be set by user code but sometimes is (by zlib, |
| 49 | for example). If you think you have removed these but your code still |
| 50 | doesn't compile, you can insert this just before the line that's failing |
| 51 | to double check: |
| 52 | ``` |
| 53 | #if _FILE_OFFSET_BITS == 64 |
| 54 | #error "oops, file _FILE_OFFSET_BITS == 64" |
| 55 | #elif defined(__USE_FILE_OFFSET64) |
| 56 | #error "oops, __USE_FILE_OFFSET64 is defined" |
| 57 | #endif |
| 58 | ``` |
Elliott Hughes | 0bfcbaf | 2017-08-28 09:18:34 -0700 | [diff] [blame] | 59 | |
| 60 | In the 64-bit ABI, `off_t` is always 64-bit. |
| 61 | |
Elliott Hughes | 3e23591 | 2018-02-01 14:21:51 -0800 | [diff] [blame] | 62 | For source compatibility, the names containing `64` are also available |
| 63 | in the 64-bit ABI even though they're identical to the non-`64` names. |
| 64 | |
Elliott Hughes | 0bfcbaf | 2017-08-28 09:18:34 -0700 | [diff] [blame] | 65 | |
Elliott Hughes | a348174 | 2017-11-28 14:47:17 -0800 | [diff] [blame] | 66 | ## `sigset_t` is too small for real-time signals |
Elliott Hughes | 0bfcbaf | 2017-08-28 09:18:34 -0700 | [diff] [blame] | 67 | |
Elliott Hughes | 56a9fda | 2020-02-13 22:23:17 -0800 | [diff] [blame] | 68 | On 32-bit Android, `sigset_t` is too small for ARM and x86. This means that |
| 69 | there is no support for real-time signals in 32-bit code. Android P (API |
| 70 | level 28) adds `sigset64_t` and a corresponding function for every function |
| 71 | that takes a `sigset_t` (so `sigprocmask64` takes a `sigset64_t` where |
| 72 | `sigprocmask` takes a `sigset_t`). |
Elliott Hughes | 0bfcbaf | 2017-08-28 09:18:34 -0700 | [diff] [blame] | 73 | |
Elliott Hughes | 3e23591 | 2018-02-01 14:21:51 -0800 | [diff] [blame] | 74 | On 32-bit Android, `struct sigaction` is also too small because it contains |
| 75 | a `sigset_t`. We also offer a `struct sigaction64` and `sigaction64` function |
| 76 | to work around this. |
| 77 | |
Elliott Hughes | 0bfcbaf | 2017-08-28 09:18:34 -0700 | [diff] [blame] | 78 | In the 64-bit ABI, `sigset_t` is the correct size for every architecture. |
| 79 | |
Elliott Hughes | 3e23591 | 2018-02-01 14:21:51 -0800 | [diff] [blame] | 80 | For source compatibility, the names containing `64` are also available |
| 81 | in the 64-bit ABI even though they're identical to the non-`64` names. |
| 82 | |
Elliott Hughes | 0bfcbaf | 2017-08-28 09:18:34 -0700 | [diff] [blame] | 83 | |
Elliott Hughes | 1cd4d42 | 2020-05-19 12:39:50 -0700 | [diff] [blame] | 84 | ## `time_t` is 32-bit on LP32 (y2038) |
Elliott Hughes | 0bfcbaf | 2017-08-28 09:18:34 -0700 | [diff] [blame] | 85 | |
Elliott Hughes | 1cd4d42 | 2020-05-19 12:39:50 -0700 | [diff] [blame] | 86 | On 32-bit Android, `time_t` is 32-bit, which will overflow in 2038. |
Elliott Hughes | 0bfcbaf | 2017-08-28 09:18:34 -0700 | [diff] [blame] | 87 | |
Elliott Hughes | 1cd4d42 | 2020-05-19 12:39:50 -0700 | [diff] [blame] | 88 | In the 64-bit ABI, `time_t` is 64-bit, which will not overflow until |
| 89 | long after the death of the star around which we currently circle. |
| 90 | |
| 91 | The header `<time64.h>` and type `time64_t` exist as a workaround, |
| 92 | but the kernel interfaces exposed on 32-bit Android all use the 32-bit |
| 93 | `time_t` and `struct timespec`/`struct timeval`. Linux 5.x kernels |
| 94 | do offer extra interfaces so that 32-bit processes can pass 64-bit |
| 95 | times to/from the kernel, but we do not plan on adding support for |
| 96 | these to the C library. Convenient use of the new calls would require |
| 97 | an equivalent to `_FILE_OFFSET_BITS=64`, which we wouldn't be able |
| 98 | to globally flip for reasons similar to `_FILE_OFFSET_BITS`, mentioned |
| 99 | above. All apps are already required to offer 64-bit variants, and we |
| 100 | expect 64-bit-only devices within the next few years. |
Elliott Hughes | 1d01fe8 | 2017-10-23 10:07:55 -0700 | [diff] [blame] | 101 | |
Elliott Hughes | 5905d6f | 2018-01-30 15:09:51 -0800 | [diff] [blame] | 102 | |
Elliott Hughes | a348174 | 2017-11-28 14:47:17 -0800 | [diff] [blame] | 103 | ## `pthread_mutex_t` is too small for large pids |
Elliott Hughes | 1d01fe8 | 2017-10-23 10:07:55 -0700 | [diff] [blame] | 104 | |
| 105 | This doesn't generally affect Android devices, because on devices |
Elliott Hughes | 5905d6f | 2018-01-30 15:09:51 -0800 | [diff] [blame] | 106 | `/proc/sys/kernel/pid_max` is usually too small to hit our 16-bit limit, |
Elliott Hughes | 1d01fe8 | 2017-10-23 10:07:55 -0700 | [diff] [blame] | 107 | but 32-bit bionic's `pthread_mutex` is a total of 32 bits, leaving just |
| 108 | 16 bits for the owner thread id. This means bionic isn't able to support |
| 109 | mutexes for tids that don't fit in 16 bits. This typically manifests as |
| 110 | a hang in `pthread_mutex_lock` if the libc startup code doesn't detect |
| 111 | this condition and abort. |
Elliott Hughes | 8880cab | 2024-02-13 00:43:24 +0000 | [diff] [blame] | 112 | |
| 113 | |
| 114 | ## `getuid()` and friends wrongly set errno for very large results |
| 115 | |
| 116 | This doesn't generally affect Android devices, because we don't have any |
| 117 | uids/gids/pids large enough, but 32-bit Android doesn't take into account |
| 118 | that functions like getuid() potentially have return values that cover the |
| 119 | entire 32-bit, and can't fail. This means that the usual "if the result is |
| 120 | between -1 and -4096, set errno and return -1" code is inappropriate for |
| 121 | these functions. Since LP32 is unlikely to be still supported long before |
| 122 | those limits could ever matter, although -- unlike the others in this |
| 123 | document -- this defect is actually fixable, it doesn't seem worth fixing. |