Elliott Hughes | 84941fe | 2018-09-04 13:22:56 -0700 | [diff] [blame] | 1 | # bionic |
Elliott Hughes | 0bfcbaf | 2017-08-28 09:18:34 -0700 | [diff] [blame] | 2 | |
Elliott Hughes | 84941fe | 2018-09-04 13:22:56 -0700 | [diff] [blame] | 3 | [bionic](https://en.wikipedia.org/wiki/Bionic_(software)) is Android's |
| 4 | C library, math library, and dynamic linker. |
Elliott Hughes | 0bfcbaf | 2017-08-28 09:18:34 -0700 | [diff] [blame] | 5 | |
Elliott Hughes | 84941fe | 2018-09-04 13:22:56 -0700 | [diff] [blame] | 6 | # Using bionic as an app developer |
Elliott Hughes | 560cee6 | 2014-02-18 22:08:56 -0800 | [diff] [blame] | 7 | |
Elliott Hughes | 84941fe | 2018-09-04 13:22:56 -0700 | [diff] [blame] | 8 | See the [user documentation](docs/). |
| 9 | |
| 10 | # Working on bionic itself |
| 11 | |
| 12 | This documentation is about making changes to bionic itself. |
| 13 | |
| 14 | ## What are the big pieces of bionic? |
Elliott Hughes | 560cee6 | 2014-02-18 22:08:56 -0800 | [diff] [blame] | 15 | |
Dan Albert | 472cce5 | 2014-10-10 17:14:37 -0700 | [diff] [blame] | 16 | #### libc/ --- libc.so, libc.a |
Elliott Hughes | 560cee6 | 2014-02-18 22:08:56 -0800 | [diff] [blame] | 17 | |
Dan Albert | 472cce5 | 2014-10-10 17:14:37 -0700 | [diff] [blame] | 18 | The C library. Stuff like `fopen(3)` and `kill(2)`. |
Elliott Hughes | 560cee6 | 2014-02-18 22:08:56 -0800 | [diff] [blame] | 19 | |
Dan Albert | 472cce5 | 2014-10-10 17:14:37 -0700 | [diff] [blame] | 20 | #### libm/ --- libm.so, libm.a |
| 21 | |
| 22 | The math library. Traditionally Unix systems kept stuff like `sin(3)` and |
| 23 | `cos(3)` in a separate library to save space in the days before shared |
| 24 | libraries. |
| 25 | |
| 26 | #### libdl/ --- libdl.so |
| 27 | |
| 28 | The dynamic linker interface library. This is actually just a bunch of stubs |
| 29 | that the dynamic linker replaces with pointers to its own implementation at |
| 30 | runtime. This is where stuff like `dlopen(3)` lives. |
| 31 | |
| 32 | #### libstdc++/ --- libstdc++.so |
| 33 | |
| 34 | The C++ ABI support functions. The C++ compiler doesn't know how to implement |
| 35 | thread-safe static initialization and the like, so it just calls functions that |
| 36 | are supplied by the system. Stuff like `__cxa_guard_acquire` and |
| 37 | `__cxa_pure_virtual` live here. |
| 38 | |
| 39 | #### linker/ --- /system/bin/linker and /system/bin/linker64 |
| 40 | |
| 41 | The dynamic linker. When you run a dynamically-linked executable, its ELF file |
| 42 | has a `DT_INTERP` entry that says "use the following program to start me". On |
| 43 | Android, that's either `linker` or `linker64` (depending on whether it's a |
| 44 | 32-bit or 64-bit executable). It's responsible for loading the ELF executable |
| 45 | into memory and resolving references to symbols (so that when your code tries to |
| 46 | jump to `fopen(3)`, say, it lands in the right place). |
| 47 | |
| 48 | #### tests/ --- unit tests |
| 49 | |
| 50 | The `tests/` directory contains unit tests. Roughly arranged as one file per |
| 51 | publicly-exported header file. |
| 52 | |
| 53 | #### benchmarks/ --- benchmarks |
| 54 | |
Elliott Hughes | 0bfcbaf | 2017-08-28 09:18:34 -0700 | [diff] [blame] | 55 | The `benchmarks/` directory contains benchmarks, with its own [documentation](benchmarks/README.md). |
Elliott Hughes | 560cee6 | 2014-02-18 22:08:56 -0800 | [diff] [blame] | 56 | |
| 57 | |
Elliott Hughes | 84941fe | 2018-09-04 13:22:56 -0700 | [diff] [blame] | 58 | ## What's in libc/? |
Elliott Hughes | 560cee6 | 2014-02-18 22:08:56 -0800 | [diff] [blame] | 59 | |
Elliott Hughes | 84941fe | 2018-09-04 13:22:56 -0700 | [diff] [blame] | 60 | ``` |
Elliott Hughes | 560cee6 | 2014-02-18 22:08:56 -0800 | [diff] [blame] | 61 | libc/ |
| 62 | arch-arm/ |
| 63 | arch-arm64/ |
| 64 | arch-common/ |
| 65 | arch-mips/ |
| 66 | arch-mips64/ |
| 67 | arch-x86/ |
| 68 | arch-x86_64/ |
| 69 | # Each architecture has its own subdirectory for stuff that isn't shared |
| 70 | # because it's architecture-specific. There will be a .mk file in here that |
| 71 | # drags in all the architecture-specific files. |
| 72 | bionic/ |
| 73 | # Every architecture needs a handful of machine-specific assembler files. |
| 74 | # They live here. |
Elliott Hughes | 560cee6 | 2014-02-18 22:08:56 -0800 | [diff] [blame] | 75 | string/ |
| 76 | # Most architectures have a handful of optional assembler files |
| 77 | # implementing optimized versions of various routines. The <string.h> |
| 78 | # functions are particular favorites. |
| 79 | syscalls/ |
| 80 | # The syscalls directories contain script-generated assembler files. |
| 81 | # See 'Adding system calls' later. |
| 82 | |
| 83 | include/ |
| 84 | # The public header files on everyone's include path. These are a mixture of |
| 85 | # files written by us and files taken from BSD. |
| 86 | |
| 87 | kernel/ |
| 88 | # The kernel uapi header files. These are scrubbed copies of the originals |
| 89 | # in external/kernel-headers/. These files must not be edited directly. The |
| 90 | # generate_uapi_headers.sh script should be used to go from a kernel tree to |
| 91 | # external/kernel-headers/ --- this takes care of the architecture-specific |
| 92 | # details. The update_all.py script should be used to regenerate bionic's |
| 93 | # scrubbed headers from external/kernel-headers/. |
| 94 | |
| 95 | private/ |
| 96 | # These are private header files meant for use within bionic itself. |
| 97 | |
Calin Juravle | bd33537 | 2014-02-28 16:31:04 +0000 | [diff] [blame] | 98 | dns/ |
| 99 | # Contains the DNS resolver (originates from NetBSD code). |
Elliott Hughes | 560cee6 | 2014-02-18 22:08:56 -0800 | [diff] [blame] | 100 | |
Elliott Hughes | 560cee6 | 2014-02-18 22:08:56 -0800 | [diff] [blame] | 101 | upstream-freebsd/ |
| 102 | upstream-netbsd/ |
| 103 | upstream-openbsd/ |
| 104 | # These directories contain unmolested upstream source. Any time we can |
| 105 | # just use a BSD implementation of something unmodified, we should. |
Elliott Hughes | d39f3f2 | 2014-04-21 17:13:46 -0700 | [diff] [blame] | 106 | # The structure under these directories mimics the upstream tree, |
| 107 | # but there's also... |
| 108 | android/ |
| 109 | include/ |
| 110 | # This is where we keep the hacks necessary to build BSD source |
| 111 | # in our world. The *-compat.h files are automatically included |
| 112 | # using -include, but we also provide equivalents for missing |
| 113 | # header/source files needed by the BSD implementation. |
Elliott Hughes | 560cee6 | 2014-02-18 22:08:56 -0800 | [diff] [blame] | 114 | |
| 115 | bionic/ |
| 116 | # This is the biggest mess. The C++ files are files we own, typically |
| 117 | # because the Linux kernel interface is sufficiently different that we |
| 118 | # can't use any of the BSD implementations. The C files are usually |
| 119 | # legacy mess that needs to be sorted out, either by replacing it with |
| 120 | # current upstream source in one of the upstream directories or by |
| 121 | # switching the file to C++ and cleaning it up. |
| 122 | |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 123 | malloc_debug/ |
| 124 | # The code that implements the functionality to enable debugging of |
| 125 | # native allocation problems. |
| 126 | |
Elliott Hughes | 3ad8ecb | 2014-07-21 16:35:24 -0700 | [diff] [blame] | 127 | stdio/ |
| 128 | # These are legacy files of dubious provenance. We're working to clean |
| 129 | # this mess up, and this directory should disappear. |
| 130 | |
Elliott Hughes | 560cee6 | 2014-02-18 22:08:56 -0800 | [diff] [blame] | 131 | tools/ |
| 132 | # Various tools used to maintain bionic. |
| 133 | |
| 134 | tzcode/ |
| 135 | # A modified superset of the IANA tzcode. Most of the modifications relate |
| 136 | # to Android's use of a single file (with corresponding index) to contain |
| 137 | # time zone data. |
| 138 | zoneinfo/ |
| 139 | # Android-format time zone data. |
| 140 | # See 'Updating tzdata' later. |
Elliott Hughes | 84941fe | 2018-09-04 13:22:56 -0700 | [diff] [blame] | 141 | ``` |
Elliott Hughes | 560cee6 | 2014-02-18 22:08:56 -0800 | [diff] [blame] | 142 | |
| 143 | |
Elliott Hughes | 84941fe | 2018-09-04 13:22:56 -0700 | [diff] [blame] | 144 | ## Adding libc wrappers for system calls |
Elliott Hughes | 1a1b57c | 2018-02-08 09:38:54 -0800 | [diff] [blame] | 145 | |
| 146 | The first question you should ask is "should I add a libc wrapper for |
| 147 | this system call?". The answer is usually "no". |
| 148 | |
| 149 | The answer is "yes" if the system call is part of the POSIX standard. |
| 150 | |
| 151 | The answer is probably "yes" if the system call has a wrapper in at |
| 152 | least one other C library. |
| 153 | |
| 154 | The answer may be "yes" if the system call has three/four distinct |
| 155 | users in different projects, and there isn't a more specific library |
| 156 | that would make more sense as the place to add the wrapper. |
| 157 | |
| 158 | In all other cases, you should use |
| 159 | [syscall(3)](http://man7.org/linux/man-pages/man2/syscall.2.html) instead. |
Elliott Hughes | 560cee6 | 2014-02-18 22:08:56 -0800 | [diff] [blame] | 160 | |
| 161 | Adding a system call usually involves: |
| 162 | |
| 163 | 1. Add entries to SYSCALLS.TXT. |
| 164 | See SYSCALLS.TXT itself for documentation on the format. |
| 165 | 2. Run the gensyscalls.py script. |
| 166 | 3. Add constants (and perhaps types) to the appropriate header file. |
| 167 | Note that you should check to see whether the constants are already in |
| 168 | kernel uapi header files, in which case you just need to make sure that |
Elliott Hughes | 247904a | 2014-02-21 16:09:27 -0800 | [diff] [blame] | 169 | the appropriate POSIX header file in libc/include/ includes the |
Elliott Hughes | 560cee6 | 2014-02-18 22:08:56 -0800 | [diff] [blame] | 170 | relevant file or files. |
Elliott Hughes | 1a1b57c | 2018-02-08 09:38:54 -0800 | [diff] [blame] | 171 | 4. Add function declarations to the appropriate header file. Don't forget |
| 172 | to include the appropriate `__INTRODUCED_IN()`. |
Elliott Hughes | e2bfe2a | 2016-05-26 13:55:37 -0700 | [diff] [blame] | 173 | 5. Add the function name to the correct section in libc/libc.map.txt and |
| 174 | run `./libc/tools/genversion-scripts.py`. |
| 175 | 6. Add at least basic tests. Even a test that deliberately supplies |
Elliott Hughes | 560cee6 | 2014-02-18 22:08:56 -0800 | [diff] [blame] | 176 | an invalid argument helps check that we're generating the right symbol |
Elliott Hughes | e2bfe2a | 2016-05-26 13:55:37 -0700 | [diff] [blame] | 177 | and have the right declaration in the header file, and that you correctly |
| 178 | updated the maps in step 5. (You can use strace(1) to confirm that the |
| 179 | correct system call is being made.) |
Elliott Hughes | 560cee6 | 2014-02-18 22:08:56 -0800 | [diff] [blame] | 180 | |
| 181 | |
Elliott Hughes | 84941fe | 2018-09-04 13:22:56 -0700 | [diff] [blame] | 182 | ## Updating kernel header files |
Elliott Hughes | 560cee6 | 2014-02-18 22:08:56 -0800 | [diff] [blame] | 183 | |
| 184 | As mentioned above, this is currently a two-step process: |
| 185 | |
| 186 | 1. Use generate_uapi_headers.sh to go from a Linux source tree to appropriate |
| 187 | contents for external/kernel-headers/. |
| 188 | 2. Run update_all.py to scrub those headers and import them into bionic. |
| 189 | |
Elliott Hughes | a57c878 | 2017-07-20 10:36:27 -0700 | [diff] [blame] | 190 | Note that if you're actually just trying to expose device-specific headers to |
| 191 | build your device drivers, you shouldn't modify bionic. Instead use |
| 192 | `TARGET_DEVICE_KERNEL_HEADERS` and friends described in [config.mk](https://android.googlesource.com/platform/build/+/master/core/config.mk#186). |
| 193 | |
Elliott Hughes | 560cee6 | 2014-02-18 22:08:56 -0800 | [diff] [blame] | 194 | |
Elliott Hughes | 84941fe | 2018-09-04 13:22:56 -0700 | [diff] [blame] | 195 | ## Updating tzdata |
Elliott Hughes | 560cee6 | 2014-02-18 22:08:56 -0800 | [diff] [blame] | 196 | |
Elliott Hughes | 59fc2e8 | 2015-12-19 09:36:16 -0800 | [diff] [blame] | 197 | This is fully automated (and these days handled by the libcore team, because |
| 198 | they own icu, and that needs to be updated in sync with bionic): |
Elliott Hughes | 560cee6 | 2014-02-18 22:08:56 -0800 | [diff] [blame] | 199 | |
Elliott Hughes | 59fc2e8 | 2015-12-19 09:36:16 -0800 | [diff] [blame] | 200 | 1. Run update-tzdata.py in external/icu/tools/. |
Elliott Hughes | 560cee6 | 2014-02-18 22:08:56 -0800 | [diff] [blame] | 201 | |
Dan Albert | efee1ce | 2014-10-09 22:57:49 -0700 | [diff] [blame] | 202 | |
Elliott Hughes | 84941fe | 2018-09-04 13:22:56 -0700 | [diff] [blame] | 203 | ## Verifying changes |
Dan Albert | e66d57f | 2014-11-12 17:08:38 -0800 | [diff] [blame] | 204 | |
| 205 | If you make a change that is likely to have a wide effect on the tree (such as a |
| 206 | libc header change), you should run `make checkbuild`. A regular `make` will |
| 207 | _not_ build the entire tree; just the minimum number of projects that are |
| 208 | required for the device. Tests, additional developer tools, and various other |
| 209 | modules will not be built. Note that `make checkbuild` will not be complete |
| 210 | either, as `make tests` covers a few additional modules, but generally speaking |
| 211 | `make checkbuild` is enough. |
| 212 | |
| 213 | |
Elliott Hughes | 84941fe | 2018-09-04 13:22:56 -0700 | [diff] [blame] | 214 | ## Running the tests |
Dan Albert | efee1ce | 2014-10-09 22:57:49 -0700 | [diff] [blame] | 215 | |
| 216 | The tests are all built from the tests/ directory. |
| 217 | |
| 218 | ### Device tests |
| 219 | |
Elliott Hughes | 0e8804e | 2016-12-02 13:22:21 -0800 | [diff] [blame] | 220 | $ mma # In $ANDROID_ROOT/bionic. |
| 221 | $ adb root && adb remount && adb sync |
Sergii Piatakov | c3e3060 | 2017-11-02 13:41:32 +0200 | [diff] [blame] | 222 | $ adb shell /data/nativetest/bionic-unit-tests/bionic-unit-tests |
Dan Albert | efee1ce | 2014-10-09 22:57:49 -0700 | [diff] [blame] | 223 | $ adb shell \ |
Sergii Piatakov | c3e3060 | 2017-11-02 13:41:32 +0200 | [diff] [blame] | 224 | /data/nativetest/bionic-unit-tests-static/bionic-unit-tests-static |
Dan Albert | efee1ce | 2014-10-09 22:57:49 -0700 | [diff] [blame] | 225 | # Only for 64-bit targets |
Sergii Piatakov | c3e3060 | 2017-11-02 13:41:32 +0200 | [diff] [blame] | 226 | $ adb shell /data/nativetest64/bionic-unit-tests/bionic-unit-tests |
Dan Albert | efee1ce | 2014-10-09 22:57:49 -0700 | [diff] [blame] | 227 | $ adb shell \ |
Sergii Piatakov | c3e3060 | 2017-11-02 13:41:32 +0200 | [diff] [blame] | 228 | /data/nativetest64/bionic-unit-tests-static/bionic-unit-tests-static |
Dan Albert | efee1ce | 2014-10-09 22:57:49 -0700 | [diff] [blame] | 229 | |
Elliott Hughes | 20758d5 | 2016-07-19 14:09:10 -0700 | [diff] [blame] | 230 | Note that we use our own custom gtest runner that offers a superset of the |
| 231 | options documented at |
| 232 | <https://github.com/google/googletest/blob/master/googletest/docs/AdvancedGuide.md#running-test-programs-advanced-options>, |
| 233 | in particular for test isolation and parallelism (both on by default). |
| 234 | |
Elliott Hughes | 0e8804e | 2016-12-02 13:22:21 -0800 | [diff] [blame] | 235 | ### Device tests via CTS |
| 236 | |
| 237 | Most of the unit tests are executed by CTS. By default, CTS runs as |
| 238 | a non-root user, so the unit tests must also pass when not run as root. |
| 239 | Some tests cannot do any useful work unless run as root. In this case, |
| 240 | the test should check `getuid() == 0` and do nothing otherwise (typically |
| 241 | we log in this case to prevent accidents!). Obviously, if the test can be |
| 242 | rewritten to not require root, that's an even better solution. |
| 243 | |
| 244 | Currently, the list of bionic CTS tests is generated at build time by |
| 245 | running a host version of the test executable and dumping the list of |
| 246 | all tests. In order for this to continue to work, all architectures must |
| 247 | have the same number of tests, and the host version of the executable |
| 248 | must also have the same number of tests. |
| 249 | |
| 250 | Running the gtests directly is orders of magnitude faster than using CTS, |
| 251 | but in cases where you really have to run CTS: |
| 252 | |
| 253 | $ make cts # In $ANDROID_ROOT. |
| 254 | $ adb unroot # Because real CTS doesn't run as root. |
| 255 | # This will sync any *test* changes, but not *code* changes: |
| 256 | $ cts-tradefed \ |
| 257 | run singleCommand cts --skip-preconditions -m CtsBionicTestCases |
| 258 | |
Dan Albert | efee1ce | 2014-10-09 22:57:49 -0700 | [diff] [blame] | 259 | ### Host tests |
| 260 | |
| 261 | The host tests require that you have `lunch`ed either an x86 or x86_64 target. |
Josh Gao | 6cd1c92 | 2016-11-17 18:52:09 -0800 | [diff] [blame] | 262 | Note that due to ABI limitations (specifically, the size of pthread_mutex_t), |
| 263 | 32-bit bionic requires PIDs less than 65536. To enforce this, set /proc/sys/kernel/pid_max |
| 264 | to 65536. |
Dan Albert | efee1ce | 2014-10-09 22:57:49 -0700 | [diff] [blame] | 265 | |
Elliott Hughes | 86f1e04 | 2016-08-01 13:16:37 -0700 | [diff] [blame] | 266 | $ ./tests/run-on-host.sh 32 |
| 267 | $ ./tests/run-on-host.sh 64 # For x86_64-bit *targets* only. |
| 268 | |
| 269 | You can supply gtest flags as extra arguments to this script. |
Dan Albert | efee1ce | 2014-10-09 22:57:49 -0700 | [diff] [blame] | 270 | |
| 271 | ### Against glibc |
| 272 | |
| 273 | As a way to check that our tests do in fact test the correct behavior (and not |
| 274 | just the behavior we think is correct), it is possible to run the tests against |
Elliott Hughes | 86f1e04 | 2016-08-01 13:16:37 -0700 | [diff] [blame] | 275 | the host's glibc. |
Dan Albert | efee1ce | 2014-10-09 22:57:49 -0700 | [diff] [blame] | 276 | |
Elliott Hughes | 86f1e04 | 2016-08-01 13:16:37 -0700 | [diff] [blame] | 277 | $ ./tests/run-on-host.sh glibc |
Dan Albert | efee1ce | 2014-10-09 22:57:49 -0700 | [diff] [blame] | 278 | |
| 279 | |
Elliott Hughes | 84941fe | 2018-09-04 13:22:56 -0700 | [diff] [blame] | 280 | ## Gathering test coverage |
Dan Albert | efee1ce | 2014-10-09 22:57:49 -0700 | [diff] [blame] | 281 | |
| 282 | For either host or target coverage, you must first: |
| 283 | |
| 284 | * `$ export NATIVE_COVERAGE=true` |
| 285 | * Note that the build system is ignorant to this flag being toggled, i.e. if |
| 286 | you change this flag, you will have to manually rebuild bionic. |
| 287 | * Set `bionic_coverage=true` in `libc/Android.mk` and `libm/Android.mk`. |
| 288 | |
| 289 | ### Coverage from device tests |
| 290 | |
| 291 | $ mma |
| 292 | $ adb sync |
| 293 | $ adb shell \ |
| 294 | GCOV_PREFIX=/data/local/tmp/gcov \ |
| 295 | GCOV_PREFIX_STRIP=`echo $ANDROID_BUILD_TOP | grep -o / | wc -l` \ |
Sergii Piatakov | c3e3060 | 2017-11-02 13:41:32 +0200 | [diff] [blame] | 296 | /data/nativetest/bionic-unit-tests/bionic-unit-tests |
Dan Albert | efee1ce | 2014-10-09 22:57:49 -0700 | [diff] [blame] | 297 | $ acov |
| 298 | |
| 299 | `acov` will pull all coverage information from the device, push it to the right |
| 300 | directories, run `lcov`, and open the coverage report in your browser. |
| 301 | |
| 302 | ### Coverage from host tests |
| 303 | |
| 304 | First, build and run the host tests as usual (see above). |
| 305 | |
| 306 | $ croot |
| 307 | $ lcov -c -d $ANDROID_PRODUCT_OUT -o coverage.info |
| 308 | $ genhtml -o covreport coverage.info # or lcov --list coverage.info |
| 309 | |
| 310 | The coverage report is now available at `covreport/index.html`. |
Elliott Hughes | 0b1de06 | 2015-01-09 12:21:24 -0800 | [diff] [blame] | 311 | |
| 312 | |
Elliott Hughes | 84941fe | 2018-09-04 13:22:56 -0700 | [diff] [blame] | 313 | ## Attaching GDB to the tests |
Dan Albert | 1af434c | 2015-09-18 13:17:02 -0700 | [diff] [blame] | 314 | |
| 315 | Bionic's test runner will run each test in its own process by default to prevent |
| 316 | tests failures from impacting other tests. This also has the added benefit of |
| 317 | running them in parallel, so they are much faster. |
| 318 | |
| 319 | However, this also makes it difficult to run the tests under GDB. To prevent |
| 320 | each test from being forked, run the tests with the flag `--no-isolate`. |
| 321 | |
| 322 | |
Elliott Hughes | 84941fe | 2018-09-04 13:22:56 -0700 | [diff] [blame] | 323 | ## 32-bit ABI bugs |
Elliott Hughes | 0b1de06 | 2015-01-09 12:21:24 -0800 | [diff] [blame] | 324 | |
Elliott Hughes | 0bfcbaf | 2017-08-28 09:18:34 -0700 | [diff] [blame] | 325 | See [32-bit ABI bugs](docs/32-bit-abi.md). |