• Home
Name Date Size #Lines LOC

..--

apex/03-May-2024-129122

benchmarks/03-May-2024-576,634574,221

build/03-May-2024-6354

docs/03-May-2024-2,3561,830

libc/03-May-2024-307,855218,730

libdl/03-May-2024-921691

libfdtrack/03-May-2024-331224

libm/03-May-2024-72,46156,217

libstdc++/03-May-2024-3627

linker/03-May-2024-15,4539,979

tests/03-May-2024-292,336273,073

tools/03-May-2024-6,1644,911

.clang-formatD03-May-2024239

.gitignoreD03-May-202432 43

Android.mkD03-May-202481 52

CPPLINT.cfgD03-May-202475 32

CleanSpec.mkD03-May-20243 KiB688

OWNERSD03-May-2024237 1310

PREUPLOAD.cfgD03-May-2024190 96

README.mdD03-May-202411.9 KiB321231

TEST_MAPPINGD03-May-2024325 2322

android-changes-for-ndk-developers.mdD03-May-202421.5 KiB475367

README.md

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