1*8d67ca89SAndroid Build Coastguard Worker# bionic maintainer overview 2*8d67ca89SAndroid Build Coastguard Worker 3*8d67ca89SAndroid Build Coastguard Worker[bionic](https://en.wikipedia.org/wiki/Bionic_(software)) is Android's 4*8d67ca89SAndroid Build Coastguard WorkerC library, math library, and dynamic linker. 5*8d67ca89SAndroid Build Coastguard Worker 6*8d67ca89SAndroid Build Coastguard WorkerThis document is a high-level overview of making changes to bionic itself. 7*8d67ca89SAndroid Build Coastguard WorkerIf you're trying to _use_ bionic, or want more in-depth information about 8*8d67ca89SAndroid Build Coastguard Workersome part of the implementation, see [all the bionic documentation](docs/). 9*8d67ca89SAndroid Build Coastguard Worker 10*8d67ca89SAndroid Build Coastguard Worker## What are the big pieces of bionic? 11*8d67ca89SAndroid Build Coastguard Worker 12*8d67ca89SAndroid Build Coastguard Worker#### libc/ --- libc.so, libc.a 13*8d67ca89SAndroid Build Coastguard Worker 14*8d67ca89SAndroid Build Coastguard WorkerThe C library. Stuff like `fopen(3)` and `kill(2)`. 15*8d67ca89SAndroid Build Coastguard Worker 16*8d67ca89SAndroid Build Coastguard Worker#### libm/ --- libm.so, libm.a 17*8d67ca89SAndroid Build Coastguard Worker 18*8d67ca89SAndroid Build Coastguard WorkerThe math library. Traditionally Unix systems kept stuff like `sin(3)` and 19*8d67ca89SAndroid Build Coastguard Worker`cos(3)` in a separate library to save space in the days before shared 20*8d67ca89SAndroid Build Coastguard Workerlibraries. 21*8d67ca89SAndroid Build Coastguard Worker 22*8d67ca89SAndroid Build Coastguard Worker#### libdl/ --- libdl.so 23*8d67ca89SAndroid Build Coastguard Worker 24*8d67ca89SAndroid Build Coastguard WorkerThe dynamic linker interface library. This is actually just a bunch of stubs 25*8d67ca89SAndroid Build Coastguard Workerthat the dynamic linker replaces with pointers to its own implementation at 26*8d67ca89SAndroid Build Coastguard Workerruntime. This is where stuff like `dlopen(3)` lives. 27*8d67ca89SAndroid Build Coastguard Worker 28*8d67ca89SAndroid Build Coastguard Worker#### libstdc++/ --- libstdc++.so 29*8d67ca89SAndroid Build Coastguard Worker 30*8d67ca89SAndroid Build Coastguard WorkerThe C++ ABI support functions. The C++ compiler doesn't know how to implement 31*8d67ca89SAndroid Build Coastguard Workerthread-safe static initialization and the like, so it just calls functions that 32*8d67ca89SAndroid Build Coastguard Workerare supplied by the system. Stuff like `__cxa_guard_acquire` and 33*8d67ca89SAndroid Build Coastguard Worker`__cxa_pure_virtual` live here. 34*8d67ca89SAndroid Build Coastguard Worker 35*8d67ca89SAndroid Build Coastguard Worker#### linker/ --- /system/bin/linker and /system/bin/linker64 36*8d67ca89SAndroid Build Coastguard Worker 37*8d67ca89SAndroid Build Coastguard WorkerThe dynamic linker. When you run a dynamically-linked executable, its ELF file 38*8d67ca89SAndroid Build Coastguard Workerhas a `DT_INTERP` entry that says "use the following program to start me". On 39*8d67ca89SAndroid Build Coastguard WorkerAndroid, that's either `linker` or `linker64` (depending on whether it's a 40*8d67ca89SAndroid Build Coastguard Worker32-bit or 64-bit executable). It's responsible for loading the ELF executable 41*8d67ca89SAndroid Build Coastguard Workerinto memory and resolving references to symbols (so that when your code tries to 42*8d67ca89SAndroid Build Coastguard Workerjump to `fopen(3)`, say, it lands in the right place). 43*8d67ca89SAndroid Build Coastguard Worker 44*8d67ca89SAndroid Build Coastguard Worker#### tests/ --- unit tests 45*8d67ca89SAndroid Build Coastguard Worker 46*8d67ca89SAndroid Build Coastguard WorkerThe `tests/` directory contains unit tests. Roughly arranged as one file per 47*8d67ca89SAndroid Build Coastguard Workerpublicly-exported header file. `tests/headers/` contains compile-only tests 48*8d67ca89SAndroid Build Coastguard Workerthat just check that things are _in_ the headers, whereas the "real" tests 49*8d67ca89SAndroid Build Coastguard Workercheck actual _behavior_. 50*8d67ca89SAndroid Build Coastguard Worker 51*8d67ca89SAndroid Build Coastguard Worker#### benchmarks/ --- benchmarks 52*8d67ca89SAndroid Build Coastguard Worker 53*8d67ca89SAndroid Build Coastguard WorkerThe `benchmarks/` directory contains benchmarks, with its own [documentation](benchmarks/README.md). 54*8d67ca89SAndroid Build Coastguard Worker 55*8d67ca89SAndroid Build Coastguard Worker 56*8d67ca89SAndroid Build Coastguard Worker## What's in libc/? 57*8d67ca89SAndroid Build Coastguard Worker 58*8d67ca89SAndroid Build Coastguard Worker``` 59*8d67ca89SAndroid Build Coastguard Workerlibc/ 60*8d67ca89SAndroid Build Coastguard Worker arch-arm/ 61*8d67ca89SAndroid Build Coastguard Worker arch-arm64/ 62*8d67ca89SAndroid Build Coastguard Worker arch-common/ 63*8d67ca89SAndroid Build Coastguard Worker arch-x86/ 64*8d67ca89SAndroid Build Coastguard Worker arch-x86_64/ 65*8d67ca89SAndroid Build Coastguard Worker # Each architecture has its own subdirectory for stuff that isn't shared 66*8d67ca89SAndroid Build Coastguard Worker # because it's architecture-specific. There will be a .mk file in here that 67*8d67ca89SAndroid Build Coastguard Worker # drags in all the architecture-specific files. 68*8d67ca89SAndroid Build Coastguard Worker bionic/ 69*8d67ca89SAndroid Build Coastguard Worker # Every architecture needs a handful of machine-specific assembler files. 70*8d67ca89SAndroid Build Coastguard Worker # They live here. 71*8d67ca89SAndroid Build Coastguard Worker string/ 72*8d67ca89SAndroid Build Coastguard Worker # Most architectures have a handful of optional assembler files 73*8d67ca89SAndroid Build Coastguard Worker # implementing optimized versions of various routines. The <string.h> 74*8d67ca89SAndroid Build Coastguard Worker # functions are particular favorites. 75*8d67ca89SAndroid Build Coastguard Worker syscalls/ 76*8d67ca89SAndroid Build Coastguard Worker # The syscalls directories contain script-generated assembler files. 77*8d67ca89SAndroid Build Coastguard Worker # See 'Adding system calls' later. 78*8d67ca89SAndroid Build Coastguard Worker 79*8d67ca89SAndroid Build Coastguard Worker include/ 80*8d67ca89SAndroid Build Coastguard Worker # The public header files on everyone's include path. These are a mixture of 81*8d67ca89SAndroid Build Coastguard Worker # files written by us and files taken from BSD. 82*8d67ca89SAndroid Build Coastguard Worker 83*8d67ca89SAndroid Build Coastguard Worker kernel/ 84*8d67ca89SAndroid Build Coastguard Worker # The kernel uapi header files. The "libc" headers that developers actually 85*8d67ca89SAndroid Build Coastguard Worker # use are a mixture of headers provided by the C library itself (which, 86*8d67ca89SAndroid Build Coastguard Worker # for bionic, are in bionic/libc/include/) and headers provided by the 87*8d67ca89SAndroid Build Coastguard Worker # kernel. This is because ISO C and POSIX will say things like "there is 88*8d67ca89SAndroid Build Coastguard Worker # a constant called PROT_NONE" or "there is a type called struct stat, 89*8d67ca89SAndroid Build Coastguard Worker # and it contains a field called st_size", but they won't necessarily say 90*8d67ca89SAndroid Build Coastguard Worker # what _value_ that constant has, or what _order_ the fields in a type 91*8d67ca89SAndroid Build Coastguard Worker # are in. Those are left to individual kernels' ABIs. In an effort to -- 92*8d67ca89SAndroid Build Coastguard Worker # amongst other things, see https://lwn.net/Articles/507794/ for more 93*8d67ca89SAndroid Build Coastguard Worker # background -- reduce copy & paste, the Linux kernel makes all the types 94*8d67ca89SAndroid Build Coastguard Worker # and constants that make up the "userspace API" (uapi) available as 95*8d67ca89SAndroid Build Coastguard Worker # headers separate from their internal-use headers (which contain all kinds 96*8d67ca89SAndroid Build Coastguard Worker # of extra stuff that isn't available to userspace). We import the latest 97*8d67ca89SAndroid Build Coastguard Worker # released kernel's uapi headers in external/kernel-headers/, but we don't 98*8d67ca89SAndroid Build Coastguard Worker # use those headers directly in bionic. The bionic/libc/kernel/ directory 99*8d67ca89SAndroid Build Coastguard Worker # contains scrubbed copies of the originals from external/kernel-headers/. 100*8d67ca89SAndroid Build Coastguard Worker # The generate_uapi_headers.sh script should be used to go from a kernel 101*8d67ca89SAndroid Build Coastguard Worker # tree to external/kernel-headers/ --- this takes care of the 102*8d67ca89SAndroid Build Coastguard Worker # architecture-specific details. The update_all.py script should then be 103*8d67ca89SAndroid Build Coastguard Worker # used to regenerate bionic's copy from external/kernel-headers/. 104*8d67ca89SAndroid Build Coastguard Worker # The files in bionic must not be edited directly because any local changes 105*8d67ca89SAndroid Build Coastguard Worker # will be overwritten by the next update. "Updating kernel header files" 106*8d67ca89SAndroid Build Coastguard Worker # below has more information on this process. 107*8d67ca89SAndroid Build Coastguard Worker 108*8d67ca89SAndroid Build Coastguard Worker private/ 109*8d67ca89SAndroid Build Coastguard Worker # These are private header files meant for use within bionic itself. 110*8d67ca89SAndroid Build Coastguard Worker 111*8d67ca89SAndroid Build Coastguard Worker dns/ 112*8d67ca89SAndroid Build Coastguard Worker # Contains the DNS resolver (originates from NetBSD code). 113*8d67ca89SAndroid Build Coastguard Worker 114*8d67ca89SAndroid Build Coastguard Worker upstream-freebsd/ 115*8d67ca89SAndroid Build Coastguard Worker upstream-netbsd/ 116*8d67ca89SAndroid Build Coastguard Worker upstream-openbsd/ 117*8d67ca89SAndroid Build Coastguard Worker # These directories contain upstream source with no local changes. 118*8d67ca89SAndroid Build Coastguard Worker # Any time we can just use a BSD implementation of something unmodified, 119*8d67ca89SAndroid Build Coastguard Worker # we should. Ideally these should probably have been three separate git 120*8d67ca89SAndroid Build Coastguard Worker # projects in external/, but they're here instead mostly by historical 121*8d67ca89SAndroid Build Coastguard Worker # accident (because it wouldn't have been easy to import just the tiny 122*8d67ca89SAndroid Build Coastguard Worker # subset of these operating systems that -- unlike Android -- just have 123*8d67ca89SAndroid Build Coastguard Worker # one huge repository rather than lots of little ones and a mechanism 124*8d67ca89SAndroid Build Coastguard Worker # like our `repo` tool). 125*8d67ca89SAndroid Build Coastguard Worker # The structure under these directories mimics the relevant upstream tree, 126*8d67ca89SAndroid Build Coastguard Worker # but in order to actually be able to compile this code in our tree 127*8d67ca89SAndroid Build Coastguard Worker # _without_ making modifications to the source files directly, we also 128*8d67ca89SAndroid Build Coastguard Worker # have the following subdirectories in each one that aren't upstream: 129*8d67ca89SAndroid Build Coastguard Worker android/ 130*8d67ca89SAndroid Build Coastguard Worker include/ 131*8d67ca89SAndroid Build Coastguard Worker # This is where we keep the hacks necessary to build BSD source 132*8d67ca89SAndroid Build Coastguard Worker # in our world. The *-compat.h files are automatically included 133*8d67ca89SAndroid Build Coastguard Worker # using -include, but we also provide equivalents for missing 134*8d67ca89SAndroid Build Coastguard Worker # header/source files needed by the BSD implementation. 135*8d67ca89SAndroid Build Coastguard Worker 136*8d67ca89SAndroid Build Coastguard Worker bionic/ 137*8d67ca89SAndroid Build Coastguard Worker # This is the biggest mess. The C++ files are files we own, typically 138*8d67ca89SAndroid Build Coastguard Worker # because the Linux kernel interface is sufficiently different that we 139*8d67ca89SAndroid Build Coastguard Worker # can't use any of the BSD implementations. The C files are usually 140*8d67ca89SAndroid Build Coastguard Worker # legacy mess that needs to be sorted out, either by replacing it with 141*8d67ca89SAndroid Build Coastguard Worker # current upstream source in one of the upstream directories or by 142*8d67ca89SAndroid Build Coastguard Worker # switching the file to C++ and cleaning it up. 143*8d67ca89SAndroid Build Coastguard Worker 144*8d67ca89SAndroid Build Coastguard Worker malloc_debug/ 145*8d67ca89SAndroid Build Coastguard Worker # The code that implements the functionality to enable debugging of 146*8d67ca89SAndroid Build Coastguard Worker # native allocation problems. 147*8d67ca89SAndroid Build Coastguard Worker 148*8d67ca89SAndroid Build Coastguard Worker stdio/ 149*8d67ca89SAndroid Build Coastguard Worker # These are legacy files of dubious provenance. We're working to clean 150*8d67ca89SAndroid Build Coastguard Worker # this mess up, and this directory should disappear. 151*8d67ca89SAndroid Build Coastguard Worker 152*8d67ca89SAndroid Build Coastguard Worker tools/ 153*8d67ca89SAndroid Build Coastguard Worker # Various tools used to maintain bionic. 154*8d67ca89SAndroid Build Coastguard Worker 155*8d67ca89SAndroid Build Coastguard Worker tzcode/ 156*8d67ca89SAndroid Build Coastguard Worker # A modified superset of the IANA tzcode. Most of the modifications relate 157*8d67ca89SAndroid Build Coastguard Worker # to Android's use of a single file (with corresponding index) to contain 158*8d67ca89SAndroid Build Coastguard Worker # timezone data. 159*8d67ca89SAndroid Build Coastguard Worker zoneinfo/ 160*8d67ca89SAndroid Build Coastguard Worker # Android-format timezone data. 161*8d67ca89SAndroid Build Coastguard Worker # See 'Updating tzdata' later. 162*8d67ca89SAndroid Build Coastguard Worker``` 163*8d67ca89SAndroid Build Coastguard Worker 164*8d67ca89SAndroid Build Coastguard Worker 165*8d67ca89SAndroid Build Coastguard Worker## Adding libc wrappers for system calls 166*8d67ca89SAndroid Build Coastguard Worker 167*8d67ca89SAndroid Build Coastguard WorkerThe first question you should ask is "should I add a libc wrapper for 168*8d67ca89SAndroid Build Coastguard Workerthis system call?". The answer is usually "no". 169*8d67ca89SAndroid Build Coastguard Worker 170*8d67ca89SAndroid Build Coastguard WorkerThe answer is "yes" if the system call is part of the POSIX standard. 171*8d67ca89SAndroid Build Coastguard Worker 172*8d67ca89SAndroid Build Coastguard WorkerThe answer is probably "yes" if the system call has a wrapper in at 173*8d67ca89SAndroid Build Coastguard Workerleast one other C library (typically glibc/musl or Apple's libc). 174*8d67ca89SAndroid Build Coastguard Worker 175*8d67ca89SAndroid Build Coastguard WorkerThe answer may be "yes" if the system call has three/four distinct 176*8d67ca89SAndroid Build Coastguard Workerusers in different projects, and there isn't a more specific higher-level 177*8d67ca89SAndroid Build Coastguard Workerlibrary that would make more sense as the place to add the wrapper. 178*8d67ca89SAndroid Build Coastguard Worker 179*8d67ca89SAndroid Build Coastguard WorkerIn all other cases, you should use 180*8d67ca89SAndroid Build Coastguard Worker[syscall(3)](https://man7.org/linux/man-pages/man2/syscall.2.html) instead. 181*8d67ca89SAndroid Build Coastguard Worker 182*8d67ca89SAndroid Build Coastguard WorkerAdding a system call usually involves: 183*8d67ca89SAndroid Build Coastguard Worker 184*8d67ca89SAndroid Build Coastguard Worker 1. Add an entry (or entries, in some cases) to SYSCALLS.TXT. 185*8d67ca89SAndroid Build Coastguard Worker See SYSCALLS.TXT itself for documentation on the format. 186*8d67ca89SAndroid Build Coastguard Worker See also the notes below for how to deal with tricky cases like `off_t`. 187*8d67ca89SAndroid Build Coastguard Worker 2. Find the right header file to work in by looking up your system call 188*8d67ca89SAndroid Build Coastguard Worker on [man7.org](https://man7.org/linux/man-pages/dir_section_2.html). 189*8d67ca89SAndroid Build Coastguard Worker (If there's no header file given, see the points above about whether we 190*8d67ca89SAndroid Build Coastguard Worker should really be adding this or not!) 191*8d67ca89SAndroid Build Coastguard Worker 3. Add constants (and perhaps types) to the appropriate header file. 192*8d67ca89SAndroid Build Coastguard Worker Note that you should check to see whether the constants are already in 193*8d67ca89SAndroid Build Coastguard Worker kernel uapi header files, in which case you just need to make sure that 194*8d67ca89SAndroid Build Coastguard Worker the appropriate header file in libc/include/ `#include`s the relevant 195*8d67ca89SAndroid Build Coastguard Worker `linux/` file or files. 196*8d67ca89SAndroid Build Coastguard Worker 4. Add function declarations to the appropriate header file. Don't forget 197*8d67ca89SAndroid Build Coastguard Worker to include the appropriate `__INTRODUCED_IN()`, with the right API level 198*8d67ca89SAndroid Build Coastguard Worker for the first release your system call wrapper will be in. See 199*8d67ca89SAndroid Build Coastguard Worker libc/include/android/api_level.h for the API levels. 200*8d67ca89SAndroid Build Coastguard Worker If the header file doesn't exist, copy all of libc/include/sys/sysinfo.h 201*8d67ca89SAndroid Build Coastguard Worker into your new file --- it's a good short example to start from. 202*8d67ca89SAndroid Build Coastguard Worker 203*8d67ca89SAndroid Build Coastguard Worker Note also our style for naming arguments: always use two leading 204*8d67ca89SAndroid Build Coastguard Worker underscores (so developers are free to use any of the unadorned names as 205*8d67ca89SAndroid Build Coastguard Worker macros without breaking things), avoid abbreviations, and ideally try to 206*8d67ca89SAndroid Build Coastguard Worker use the same name as an existing system call (to reduce the amount of 207*8d67ca89SAndroid Build Coastguard Worker English vocabulary required by people who just want to use the function 208*8d67ca89SAndroid Build Coastguard Worker signatures). If there's a similar function already in the C library, 209*8d67ca89SAndroid Build Coastguard Worker check what names it's used. Finally, prefer the `void*` orthography we 210*8d67ca89SAndroid Build Coastguard Worker use over the `void *` you'll see on man7.org.) 211*8d67ca89SAndroid Build Coastguard Worker 5. Add basic documentation to the header file. Again, the existing 212*8d67ca89SAndroid Build Coastguard Worker libc/include/sys/sysinfo.h is a good short example that shows the 213*8d67ca89SAndroid Build Coastguard Worker expected style. 214*8d67ca89SAndroid Build Coastguard Worker 215*8d67ca89SAndroid Build Coastguard Worker Most of the detail should actually be left to the man7.org page, with 216*8d67ca89SAndroid Build Coastguard Worker only a brief one-sentence explanation (usually based on the description 217*8d67ca89SAndroid Build Coastguard Worker in the NAME section of the man page) in our documentation. Always 218*8d67ca89SAndroid Build Coastguard Worker include the return value/error reporting details (you can find out 219*8d67ca89SAndroid Build Coastguard Worker what the system call returns from the RETURN VALUE of the man page), 220*8d67ca89SAndroid Build Coastguard Worker but try to match the wording and style wording from _our_ existing 221*8d67ca89SAndroid Build Coastguard Worker documentation; we're trying to minimize the amount of English readers 222*8d67ca89SAndroid Build Coastguard Worker need to understand by using the exact same wording where possible). 223*8d67ca89SAndroid Build Coastguard Worker Explicitly say which version of Android the function was added to in 224*8d67ca89SAndroid Build Coastguard Worker the documentation because the documentation generation tool doesn't yet 225*8d67ca89SAndroid Build Coastguard Worker understand `__INTRODUCED_IN()`. 226*8d67ca89SAndroid Build Coastguard Worker 227*8d67ca89SAndroid Build Coastguard Worker Explicitly call out any Android-specific changes/additions/limitations 228*8d67ca89SAndroid Build Coastguard Worker because they won't be on the man7.org page. 229*8d67ca89SAndroid Build Coastguard Worker 6. Add the function name to the correct section in libc/libc.map.txt; it'll 230*8d67ca89SAndroid Build Coastguard Worker be near the end of the file. You may need to add a new section if you're 231*8d67ca89SAndroid Build Coastguard Worker the first to add a system call to this version of Android. 232*8d67ca89SAndroid Build Coastguard Worker 7. Add a basic test. Don't try to test everything; concentrate on just testing 233*8d67ca89SAndroid Build Coastguard Worker the code that's actually in *bionic*, not all the functionality that's 234*8d67ca89SAndroid Build Coastguard Worker implemented in the kernel. For simple syscalls, that's just the 235*8d67ca89SAndroid Build Coastguard Worker auto-generated argument and return value marshalling. 236*8d67ca89SAndroid Build Coastguard Worker 237*8d67ca89SAndroid Build Coastguard Worker Add a test in the right file in tests/. We have one file per header, so if 238*8d67ca89SAndroid Build Coastguard Worker your system call is exposed in <unistd.h>, for example, your test would go 239*8d67ca89SAndroid Build Coastguard Worker in tests/unistd_test.cpp. 240*8d67ca89SAndroid Build Coastguard Worker 241*8d67ca89SAndroid Build Coastguard Worker A trivial test that deliberately supplies an invalid argument helps check 242*8d67ca89SAndroid Build Coastguard Worker that we're generating the right symbol and have the right declaration in 243*8d67ca89SAndroid Build Coastguard Worker the header file, and that the change to libc.map.txt from step 5 is 244*8d67ca89SAndroid Build Coastguard Worker correct. (You can use strace(1) manually to confirm that the correct 245*8d67ca89SAndroid Build Coastguard Worker system call is being made.) 246*8d67ca89SAndroid Build Coastguard Worker 247*8d67ca89SAndroid Build Coastguard Worker For testing the *kernel* side of things, we should prefer to rely on 248*8d67ca89SAndroid Build Coastguard Worker https://github.com/linux-test-project/ltp for kernel testing, but you'll 249*8d67ca89SAndroid Build Coastguard Worker want to check that external/ltp does contain tests for the syscall you're 250*8d67ca89SAndroid Build Coastguard Worker adding. Also check that external/ltp is using the libc wrapper for the 251*8d67ca89SAndroid Build Coastguard Worker syscall rather than calling it "directly" via syscall(3)! 252*8d67ca89SAndroid Build Coastguard Worker 253*8d67ca89SAndroid Build Coastguard WorkerSome system calls are harder than others. The most common problem is a 64-bit 254*8d67ca89SAndroid Build Coastguard Workerargument such as `off64_t` (a *pointer* to a 64-bit argument is fine, since 255*8d67ca89SAndroid Build Coastguard Workerpointers are always the "natural" size for the architecture regardless of the 256*8d67ca89SAndroid Build Coastguard Workersize of the thing they point to). Whenever you have a function that takes 257*8d67ca89SAndroid Build Coastguard Worker`off_t` or `off64_t`, you'll need to consider whether you actually need a foo() 258*8d67ca89SAndroid Build Coastguard Workerand a foo64(), and whether they will use the same underlying system call or are 259*8d67ca89SAndroid Build Coastguard Workerimplemented as two different system calls. It's usually easiest to find a 260*8d67ca89SAndroid Build Coastguard Workersimilar system call and copy and paste from that. You'll definitely need to test 261*8d67ca89SAndroid Build Coastguard Workerboth on 32-bit and 64-bit. (These special cases warrant more testing than the 262*8d67ca89SAndroid Build Coastguard Workereasy cases, even if only manual testing with strace. Sadly it isn't always 263*8d67ca89SAndroid Build Coastguard Workerfeasible to write a working test for the interesting cases -- offsets larger 264*8d67ca89SAndroid Build Coastguard Workerthan 2GiB, say -- so you may end up just writing a "meaningless" program whose 265*8d67ca89SAndroid Build Coastguard Workeronly purpose is to give you patterns to look for when run under strace(1).) 266*8d67ca89SAndroid Build Coastguard Worker 267*8d67ca89SAndroid Build Coastguard WorkerA general example of adding a system call: 268*8d67ca89SAndroid Build Coastguard Workerhttps://android-review.googlesource.com/c/platform/bionic/+/2073827 269*8d67ca89SAndroid Build Coastguard Worker 270*8d67ca89SAndroid Build Coastguard Worker### Debugging tips 271*8d67ca89SAndroid Build Coastguard Worker1. Key error for a new codename in libc/libc.map.txt 272*8d67ca89SAndroid Build Coastguard Worker 273*8d67ca89SAndroid Build Coastguard Workere.g. what you add in libc/libc.map.txt is: 274*8d67ca89SAndroid Build Coastguard Worker 275*8d67ca89SAndroid Build Coastguard Worker``` 276*8d67ca89SAndroid Build Coastguard WorkerLIBC_V { # introduced=Vanilla 277*8d67ca89SAndroid Build Coastguard Worker global: 278*8d67ca89SAndroid Build Coastguard Worker xxx; // the new system call you add 279*8d67ca89SAndroid Build Coastguard Worker} LIBC_U; 280*8d67ca89SAndroid Build Coastguard Worker``` 281*8d67ca89SAndroid Build Coastguard Worker 282*8d67ca89SAndroid Build Coastguard WorkerThe error output is: 283*8d67ca89SAndroid Build Coastguard Worker 284*8d67ca89SAndroid Build Coastguard Worker``` 285*8d67ca89SAndroid Build Coastguard WorkerTraceback (most recent call last): 286*8d67ca89SAndroid Build Coastguard Worker File "/path/tp/out/soong/.temp/Soong.python_qucjwd7g/symbolfile/__init__.py", line 171, 287*8d67ca89SAndroid Build Coastguard Worker in decode_api_level_tag 288*8d67ca89SAndroid Build Coastguard Worker decoded = str(decode_api_level(value, api_map)) 289*8d67ca89SAndroid Build Coastguard Worker File "/path/to/out/soong/.temp/Soong.python_qucjwd7g/symbolfile/__init__.py", line 157, 290*8d67ca89SAndroid Build Coastguard Worker in decode_api_level 291*8d67ca89SAndroid Build Coastguard Worker return api_map[api] 292*8d67ca89SAndroid Build Coastguard WorkerKeyError: 'Vanilla' 293*8d67ca89SAndroid Build Coastguard Worker``` 294*8d67ca89SAndroid Build Coastguard Worker 295*8d67ca89SAndroid Build Coastguard WorkerSolution: Ask in the team and wait for the update. 296*8d67ca89SAndroid Build Coastguard Worker 297*8d67ca89SAndroid Build Coastguard Worker2. Use of undeclared identifier of the new system call in the test 298*8d67ca89SAndroid Build Coastguard Worker 299*8d67ca89SAndroid Build Coastguard WorkerPossible Solution: Check everything ready in the files mentioned above first. 300*8d67ca89SAndroid Build Coastguard WorkerMaybe glibc matters. Follow the example and try #if defined(__GLIBC__). 301*8d67ca89SAndroid Build Coastguard Worker 302*8d67ca89SAndroid Build Coastguard Worker## Updating kernel header files 303*8d67ca89SAndroid Build Coastguard Worker 304*8d67ca89SAndroid Build Coastguard WorkerAs mentioned above, this is currently a two-step process: 305*8d67ca89SAndroid Build Coastguard Worker 306*8d67ca89SAndroid Build Coastguard Worker 1. Use generate_uapi_headers.sh to go from a Linux source tree to appropriate 307*8d67ca89SAndroid Build Coastguard Worker contents for external/kernel-headers/. 308*8d67ca89SAndroid Build Coastguard Worker 2. Run update_all.py to scrub those headers and import them into bionic. 309*8d67ca89SAndroid Build Coastguard Worker 310*8d67ca89SAndroid Build Coastguard WorkerNote that if you're actually just trying to expose device-specific headers to 311*8d67ca89SAndroid Build Coastguard Workerbuild your device drivers, you shouldn't modify bionic. Instead use 312*8d67ca89SAndroid Build Coastguard Worker`TARGET_DEVICE_KERNEL_HEADERS` and friends described in [config.mk](https://android.googlesource.com/platform/build/+/main/core/config.mk#186). 313*8d67ca89SAndroid Build Coastguard Worker 314*8d67ca89SAndroid Build Coastguard Worker 315*8d67ca89SAndroid Build Coastguard Worker## Updating tzdata 316*8d67ca89SAndroid Build Coastguard Worker 317*8d67ca89SAndroid Build Coastguard WorkerThis is handled by the libcore team, because they own icu, and that needs to be 318*8d67ca89SAndroid Build Coastguard Workerupdated in sync with bionic). See 319*8d67ca89SAndroid Build Coastguard Worker[system/timezone/README.android](https://android.googlesource.com/platform/system/timezone/+/main/README.android). 320*8d67ca89SAndroid Build Coastguard Worker 321*8d67ca89SAndroid Build Coastguard Worker 322*8d67ca89SAndroid Build Coastguard Worker## Verifying changes 323*8d67ca89SAndroid Build Coastguard Worker 324*8d67ca89SAndroid Build Coastguard WorkerIf you make a change that is likely to have a wide effect on the tree (such as a 325*8d67ca89SAndroid Build Coastguard Workerlibc header change), you should run `make checkbuild`. A regular `make` will 326*8d67ca89SAndroid Build Coastguard Worker_not_ build the entire tree; just the minimum number of projects that are 327*8d67ca89SAndroid Build Coastguard Workerrequired for the device. Tests, additional developer tools, and various other 328*8d67ca89SAndroid Build Coastguard Workermodules will not be built. Note that `make checkbuild` will not be complete 329*8d67ca89SAndroid Build Coastguard Workereither, as `make tests` covers a few additional modules, but generally speaking 330*8d67ca89SAndroid Build Coastguard Worker`make checkbuild` is enough. 331*8d67ca89SAndroid Build Coastguard Worker 332*8d67ca89SAndroid Build Coastguard Worker 333*8d67ca89SAndroid Build Coastguard Worker## Running the tests 334*8d67ca89SAndroid Build Coastguard Worker 335*8d67ca89SAndroid Build Coastguard WorkerThe tests are all built from the tests/ directory. There is a separate 336*8d67ca89SAndroid Build Coastguard Workerdirectory `benchmarks/` containing benchmarks, and that has its own 337*8d67ca89SAndroid Build Coastguard Workerdocumentation on [running the benchmarks](benchmarks/README.md). 338*8d67ca89SAndroid Build Coastguard Worker 339*8d67ca89SAndroid Build Coastguard Worker### Device tests 340*8d67ca89SAndroid Build Coastguard Worker 341*8d67ca89SAndroid Build Coastguard Worker $ mma # In $ANDROID_ROOT/bionic. 342*8d67ca89SAndroid Build Coastguard Worker $ adb root && adb remount && adb sync 343*8d67ca89SAndroid Build Coastguard Worker $ adb shell /data/nativetest/bionic-unit-tests/bionic-unit-tests 344*8d67ca89SAndroid Build Coastguard Worker $ adb shell \ 345*8d67ca89SAndroid Build Coastguard Worker /data/nativetest/bionic-unit-tests-static/bionic-unit-tests-static 346*8d67ca89SAndroid Build Coastguard Worker # Only for 64-bit targets 347*8d67ca89SAndroid Build Coastguard Worker $ adb shell /data/nativetest64/bionic-unit-tests/bionic-unit-tests 348*8d67ca89SAndroid Build Coastguard Worker $ adb shell \ 349*8d67ca89SAndroid Build Coastguard Worker /data/nativetest64/bionic-unit-tests-static/bionic-unit-tests-static 350*8d67ca89SAndroid Build Coastguard Worker 351*8d67ca89SAndroid Build Coastguard WorkerNote that we use our own custom gtest runner that offers a superset of the 352*8d67ca89SAndroid Build Coastguard Workeroptions documented at 353*8d67ca89SAndroid Build Coastguard Worker<https://github.com/google/googletest/blob/main/docs/advanced.md#running-test-programs-advanced-options>, 354*8d67ca89SAndroid Build Coastguard Workerin particular for test isolation and parallelism (both on by default). 355*8d67ca89SAndroid Build Coastguard Worker 356*8d67ca89SAndroid Build Coastguard Worker### Device tests via CTS 357*8d67ca89SAndroid Build Coastguard Worker 358*8d67ca89SAndroid Build Coastguard WorkerMost of the unit tests are executed by CTS. By default, CTS runs as 359*8d67ca89SAndroid Build Coastguard Workera non-root user, so the unit tests must also pass when not run as root. 360*8d67ca89SAndroid Build Coastguard WorkerSome tests cannot do any useful work unless run as root. In this case, 361*8d67ca89SAndroid Build Coastguard Workerthe test should check `getuid() == 0` and do nothing otherwise (typically 362*8d67ca89SAndroid Build Coastguard Workerwe log in this case to prevent accidents!). Obviously, if the test can be 363*8d67ca89SAndroid Build Coastguard Workerrewritten to not require root, that's an even better solution. 364*8d67ca89SAndroid Build Coastguard Worker 365*8d67ca89SAndroid Build Coastguard WorkerCurrently, the list of bionic CTS tests is generated at build time by 366*8d67ca89SAndroid Build Coastguard Workerrunning a host version of the test executable and dumping the list of 367*8d67ca89SAndroid Build Coastguard Workerall tests. In order for this to continue to work, all architectures must 368*8d67ca89SAndroid Build Coastguard Workerhave the same number of tests, and the host version of the executable 369*8d67ca89SAndroid Build Coastguard Workermust also have the same number of tests. 370*8d67ca89SAndroid Build Coastguard Worker 371*8d67ca89SAndroid Build Coastguard WorkerRunning the gtests directly is orders of magnitude faster than using CTS, 372*8d67ca89SAndroid Build Coastguard Workerbut in cases where you really have to run CTS: 373*8d67ca89SAndroid Build Coastguard Worker 374*8d67ca89SAndroid Build Coastguard Worker $ make cts # In $ANDROID_ROOT. 375*8d67ca89SAndroid Build Coastguard Worker $ adb unroot # Because real CTS doesn't run as root. 376*8d67ca89SAndroid Build Coastguard Worker # This will sync any *test* changes, but not *code* changes: 377*8d67ca89SAndroid Build Coastguard Worker $ cts-tradefed \ 378*8d67ca89SAndroid Build Coastguard Worker run singleCommand cts --skip-preconditions -m CtsBionicTestCases 379*8d67ca89SAndroid Build Coastguard Worker 380*8d67ca89SAndroid Build Coastguard Worker### Host tests 381*8d67ca89SAndroid Build Coastguard Worker 382*8d67ca89SAndroid Build Coastguard WorkerThe host tests require that you have `lunch`ed either an x86 or x86_64 target. 383*8d67ca89SAndroid Build Coastguard WorkerNote that due to ABI limitations (specifically, the size of pthread_mutex_t), 384*8d67ca89SAndroid Build Coastguard Worker32-bit bionic requires PIDs less than 65536. To enforce this, set /proc/sys/kernel/pid_max 385*8d67ca89SAndroid Build Coastguard Workerto 65536. 386*8d67ca89SAndroid Build Coastguard Worker 387*8d67ca89SAndroid Build Coastguard Worker $ ./tests/run-on-host.sh 32 388*8d67ca89SAndroid Build Coastguard Worker $ ./tests/run-on-host.sh 64 # For x86_64-bit *targets* only. 389*8d67ca89SAndroid Build Coastguard Worker 390*8d67ca89SAndroid Build Coastguard WorkerYou can supply gtest flags as extra arguments to this script. 391*8d67ca89SAndroid Build Coastguard Worker 392*8d67ca89SAndroid Build Coastguard Worker### Against glibc 393*8d67ca89SAndroid Build Coastguard Worker 394*8d67ca89SAndroid Build Coastguard WorkerAs a way to check that our tests do in fact test the correct behavior (and not 395*8d67ca89SAndroid Build Coastguard Workerjust the behavior we think is correct), it is possible to run the tests against 396*8d67ca89SAndroid Build Coastguard Workerthe host's glibc. 397*8d67ca89SAndroid Build Coastguard Worker 398*8d67ca89SAndroid Build Coastguard Worker $ ./tests/run-on-host.sh glibc 399*8d67ca89SAndroid Build Coastguard Worker 400*8d67ca89SAndroid Build Coastguard Worker### Against musl 401*8d67ca89SAndroid Build Coastguard Worker 402*8d67ca89SAndroid Build Coastguard WorkerAnother way to verify test behavior is to run against musl on the host. glibc 403*8d67ca89SAndroid Build Coastguard Workermusl don't always match, so this can be a good way to find the more complicated 404*8d67ca89SAndroid Build Coastguard Workercorners of the spec. If they *do* match, bionic probably should too! 405*8d67ca89SAndroid Build Coastguard Worker 406*8d67ca89SAndroid Build Coastguard Worker $ OUT_DIR=$(ANDROID_BUILD_TOP)/musl-out ./tests/run-on-host.sh musl 407*8d67ca89SAndroid Build Coastguard Worker 408*8d67ca89SAndroid Build Coastguard WorkerNote: the alternate OUT_DIR is used to avoid causing excessive rebuilding when 409*8d67ca89SAndroid Build Coastguard Workerswitching between glibc and musl. The first musl test run will be expensive 410*8d67ca89SAndroid Build Coastguard Workerbecause it will not reuse any already built artifacts, but subsequent runs will 411*8d67ca89SAndroid Build Coastguard Workerbe cheaper than if you hadn't used it. 412*8d67ca89SAndroid Build Coastguard Worker 413*8d67ca89SAndroid Build Coastguard Worker## Gathering test coverage 414*8d67ca89SAndroid Build Coastguard Worker 415*8d67ca89SAndroid Build Coastguard WorkerTo get test coverage for bionic, use `//bionic/build/coverage.sh`. Before 416*8d67ca89SAndroid Build Coastguard Workerrunning, follow the instructions at the top of the file to rebuild bionic with 417*8d67ca89SAndroid Build Coastguard Workercoverage instrumentation. 418*8d67ca89SAndroid Build Coastguard Worker 419*8d67ca89SAndroid Build Coastguard Worker## Attaching GDB to the tests 420*8d67ca89SAndroid Build Coastguard Worker 421*8d67ca89SAndroid Build Coastguard WorkerBionic's test runner will run each test in its own process by default to prevent 422*8d67ca89SAndroid Build Coastguard Workertests failures from impacting other tests. This also has the added benefit of 423*8d67ca89SAndroid Build Coastguard Workerrunning them in parallel, so they are much faster. 424*8d67ca89SAndroid Build Coastguard Worker 425*8d67ca89SAndroid Build Coastguard WorkerHowever, this also makes it difficult to run the tests under GDB. To prevent 426*8d67ca89SAndroid Build Coastguard Workereach test from being forked, run the tests with the flag `--no-isolate`. 427*8d67ca89SAndroid Build Coastguard Worker 428*8d67ca89SAndroid Build Coastguard Worker 429*8d67ca89SAndroid Build Coastguard Worker## 32-bit ABI bugs 430*8d67ca89SAndroid Build Coastguard Worker 431*8d67ca89SAndroid Build Coastguard WorkerSee [32-bit ABI bugs](docs/32-bit-abi.md). 432