Name Date Size #Lines LOC

..--

apex/07-Oct-2023-414364

benchmarks/07-Oct-2023-1,112,5591,110,027

build/07-Oct-2023-333261

docs/07-Oct-2023-3,6152,878

libc/07-Oct-2023-328,500236,066

libdl/07-Oct-2023-805587

libfdtrack/07-Oct-2023-543394

libm/07-Oct-2023-31,93719,025

libstdc++/07-Oct-2023-6550

linker/07-Oct-2023-17,20911,215

tests/07-Oct-2023-298,577277,722

tools/07-Oct-2023-6,2725,002

.clang-formatD07-Oct-2023239

.gitignoreD07-Oct-202311 32

Android.mkD07-Oct-202381 52

CPPLINT.cfgD07-Oct-202375 32

CleanSpec.mkD07-Oct-20233 KiB688

METADATAD07-Oct-202339 43

OWNERSD07-Oct-2023119 86

PREUPLOAD.cfgD07-Oct-2023278 107

README.mdD07-Oct-202316.3 KiB394299

TEST_MAPPINGD07-Oct-20231.4 KiB9392

android-changes-for-ndk-developers.mdD07-Oct-202321.6 KiB482372

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 (typically glibc/musl or Apple's libc).
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 higher-level
154library that 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 an entry (or entries, in some cases) to SYSCALLS.TXT.
162     See SYSCALLS.TXT itself for documentation on the format.
163     See also the notes below for how to deal with tricky cases like `off_t`.
164  2. Find the right header file to work in by looking up your system call
165     on [man7.org](https://man7.org/linux/man-pages/dir_section_2.html).
166     (If there's no header file given, see the points above about whether we
167     should really be adding this or not!)
168  3. Add constants (and perhaps types) to the appropriate header file.
169     Note that you should check to see whether the constants are already in
170     kernel uapi header files, in which case you just need to make sure that
171     the appropriate header file in libc/include/ `#include`s the relevant
172     `linux/` file or files.
173  4. Add function declarations to the appropriate header file. Don't forget
174     to include the appropriate `__INTRODUCED_IN()`, with the right API level
175     for the first release your system call wrapper will be in. See
176     libc/include/android/api_level.h for the API levels.
177     If the header file doesn't exist, copy all of libc/include/sys/sysinfo.h
178     into your new file --- it's a good short example to start from.
179
180     Note also our style for naming arguments: always use two leading
181     underscores (so developers are free to use any of the unadorned names as
182     macros without breaking things), avoid abbreviations, and ideally try to
183     use the same name as an existing system call (to reduce the amount of
184     English vocabulary required by people who just want to use the function
185     signatures). If there's a similar function already in the C library,
186     check what names it's used. Finally, prefer the `void*` orthography we
187     use over the `void *` you'll see on man7.org.)
188  5. Add basic documentation to the header file. Again, the existing
189     libc/include/sys/sysinfo.h is a good short example that shows the
190     expected style.
191
192     Most of the detail should actually be left to the man7.org page, with
193     only a brief one-sentence explanation (usually based on the description
194     in the NAME section of the man page) in our documentation. Always
195     include the return value/error reporting details (you can find out
196     what the system call returns from the RETURN VALUE of the man page),
197     but try to match the wording and style wording from _our_ existing
198     documentation; we're trying to minimize the amount of English readers
199     need to understand by using the exact same wording where possible).
200     Explicitly say which version of Android the function was added to in
201     the documentation because the documentation generation tool doesn't yet
202     understand `__INTRODUCED_IN()`.
203
204     Explicitly call out any Android-specific changes/additions/limitations
205     because they won't be on the man7.org page.
206  6. Add the function name to the correct section in libc/libc.map.txt; it'll
207     be near the end of the file. You may need to add a new section if you're
208     the first to add a system call to this version of Android.
209  7. Add a basic test. Don't try to test everything; concentrate on just testing
210     the code that's actually in *bionic*, not all the functionality that's
211     implemented in the kernel. For simple syscalls, that's just the
212     auto-generated argument and return value marshalling.
213
214     Add a test in the right file in tests/. We have one file per header, so if
215     your system call is exposed in <unistd.h>, for example, your test would go
216     in tests/unistd_test.cpp.
217
218     A trivial test that deliberately supplies an invalid argument helps check
219     that we're generating the right symbol and have the right declaration in
220     the header file, and that the change to libc.map.txt from step 5 is
221     correct. (You can use strace(1) manually to confirm that the correct
222     system call is being made.)
223
224     For testing the *kernel* side of things, we should prefer to rely on
225     https://github.com/linux-test-project/ltp for kernel testing, but you'll
226     want to check that external/ltp does contain tests for the syscall you're
227     adding. Also check that external/ltp is using the libc wrapper for the
228     syscall rather than calling it "directly" via syscall(3)!
229
230Some system calls are harder than others. The most common problem is a 64-bit
231argument such as `off64_t` (a *pointer* to a 64-bit argument is fine, since
232pointers are always the "natural" size for the architecture regardless of the
233size of the thing they point to). Whenever you have a function that takes
234`off_t` or `off64_t`, you'll need to consider whether you actually need a foo()
235and a foo64(), and whether they will use the same underlying system call or are
236implemented as two different system calls. It's usually easiest to find a
237similar system call and copy and paste from that. You'll definitely need to test
238both on 32-bit and 64-bit. (These special cases warrant more testing than the
239easy cases, even if only manual testing with strace. Sadly it isn't always
240feasible to write a working test for the interesting cases -- offsets larger
241than 2GiB, say -- so you may end up just writing a "meaningless" program whose
242only purpose is to give you patterns to look for when run under strace(1).)
243
244A general example of adding a system call:
245https://android-review.googlesource.com/c/platform/bionic/+/2073827
246
247### Debugging tips
2481. Key error for a new codename in libc/libc.map.txt
249
250e.g. what you add in libc/libc.map.txt is:
251
252```
253LIBC_V { # introduced=Vanilla
254  global:
255    xxx; // the new system call you add
256} LIBC_U;
257```
258
259The error output is:
260
261```
262Traceback (most recent call last):
263  File "/path/tp/out/soong/.temp/Soong.python_qucjwd7g/symbolfile/__init__.py", line 171,
264  in decode_api_level_tag
265    decoded = str(decode_api_level(value, api_map))
266  File "/path/to/out/soong/.temp/Soong.python_qucjwd7g/symbolfile/__init__.py", line 157,
267  in decode_api_level
268    return api_map[api]
269KeyError: 'Vanilla'
270```
271
272Solution: Ask in the team and wait for the update.
273
2742. Use of undeclared identifier of the new system call in the test
275
276Possible Solution: Check everything ready in the files mentioned above first.
277Maybe glibc matters. Follow the example and try #if defined(__GLIBC__).
278
279## Updating kernel header files
280
281As mentioned above, this is currently a two-step process:
282
283  1. Use generate_uapi_headers.sh to go from a Linux source tree to appropriate
284     contents for external/kernel-headers/.
285  2. Run update_all.py to scrub those headers and import them into bionic.
286
287Note that if you're actually just trying to expose device-specific headers to
288build your device drivers, you shouldn't modify bionic. Instead use
289`TARGET_DEVICE_KERNEL_HEADERS` and friends described in [config.mk](https://android.googlesource.com/platform/build/+/master/core/config.mk#186).
290
291
292## Updating tzdata
293
294This is handled by the libcore team, because they own icu, and that needs to be
295updated in sync with bionic). See
296[system/timezone/README.android](https://android.googlesource.com/platform/system/timezone/+/master/README.android).
297
298
299## Verifying changes
300
301If you make a change that is likely to have a wide effect on the tree (such as a
302libc header change), you should run `make checkbuild`. A regular `make` will
303_not_ build the entire tree; just the minimum number of projects that are
304required for the device. Tests, additional developer tools, and various other
305modules will not be built. Note that `make checkbuild` will not be complete
306either, as `make tests` covers a few additional modules, but generally speaking
307`make checkbuild` is enough.
308
309
310## Running the tests
311
312The tests are all built from the tests/ directory.
313
314### Device tests
315
316    $ mma # In $ANDROID_ROOT/bionic.
317    $ adb root && adb remount && adb sync
318    $ adb shell /data/nativetest/bionic-unit-tests/bionic-unit-tests
319    $ adb shell \
320        /data/nativetest/bionic-unit-tests-static/bionic-unit-tests-static
321    # Only for 64-bit targets
322    $ adb shell /data/nativetest64/bionic-unit-tests/bionic-unit-tests
323    $ adb shell \
324        /data/nativetest64/bionic-unit-tests-static/bionic-unit-tests-static
325
326Note that we use our own custom gtest runner that offers a superset of the
327options documented at
328<https://github.com/google/googletest/blob/main/docs/advanced.md#running-test-programs-advanced-options>,
329in particular for test isolation and parallelism (both on by default).
330
331### Device tests via CTS
332
333Most of the unit tests are executed by CTS. By default, CTS runs as
334a non-root user, so the unit tests must also pass when not run as root.
335Some tests cannot do any useful work unless run as root. In this case,
336the test should check `getuid() == 0` and do nothing otherwise (typically
337we log in this case to prevent accidents!). Obviously, if the test can be
338rewritten to not require root, that's an even better solution.
339
340Currently, the list of bionic CTS tests is generated at build time by
341running a host version of the test executable and dumping the list of
342all tests. In order for this to continue to work, all architectures must
343have the same number of tests, and the host version of the executable
344must also have the same number of tests.
345
346Running the gtests directly is orders of magnitude faster than using CTS,
347but in cases where you really have to run CTS:
348
349    $ make cts # In $ANDROID_ROOT.
350    $ adb unroot # Because real CTS doesn't run as root.
351    # This will sync any *test* changes, but not *code* changes:
352    $ cts-tradefed \
353        run singleCommand cts --skip-preconditions -m CtsBionicTestCases
354
355### Host tests
356
357The host tests require that you have `lunch`ed either an x86 or x86_64 target.
358Note that due to ABI limitations (specifically, the size of pthread_mutex_t),
35932-bit bionic requires PIDs less than 65536. To enforce this, set /proc/sys/kernel/pid_max
360to 65536.
361
362    $ ./tests/run-on-host.sh 32
363    $ ./tests/run-on-host.sh 64   # For x86_64-bit *targets* only.
364
365You can supply gtest flags as extra arguments to this script.
366
367### Against glibc
368
369As a way to check that our tests do in fact test the correct behavior (and not
370just the behavior we think is correct), it is possible to run the tests against
371the host's glibc.
372
373    $ ./tests/run-on-host.sh glibc
374
375## Gathering test coverage
376
377To get test coverage for bionic, use `//bionic/build/coverage.sh`. Before
378running, follow the instructions at the top of the file to rebuild bionic with
379coverage instrumentation.
380
381## Attaching GDB to the tests
382
383Bionic's test runner will run each test in its own process by default to prevent
384tests failures from impacting other tests. This also has the added benefit of
385running them in parallel, so they are much faster.
386
387However, this also makes it difficult to run the tests under GDB. To prevent
388each test from being forked, run the tests with the flag `--no-isolate`.
389
390
391## 32-bit ABI bugs
392
393See [32-bit ABI bugs](docs/32-bit-abi.md).
394