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