• Stars
    star
    583
  • Rank 76,663 (Top 2 %)
  • Language
    Objective-C
  • Created about 16 years ago
  • Updated about 5 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

bionic

bionic is Android's C library, math library, and dynamic linker.

Using bionic as an app developer

See the user documentation.

Working on bionic itself

This documentation is about making changes to bionic itself.

What are the big pieces of bionic?

libc/ --- libc.so, libc.a

The C library. Stuff like fopen(3) and kill(2).

libm/ --- libm.so, libm.a

The math library. Traditionally Unix systems kept stuff like sin(3) and cos(3) in a separate library to save space in the days before shared libraries.

libdl/ --- libdl.so

The dynamic linker interface library. This is actually just a bunch of stubs that the dynamic linker replaces with pointers to its own implementation at runtime. This is where stuff like dlopen(3) lives.

libstdc++/ --- libstdc++.so

The C++ ABI support functions. The C++ compiler doesn't know how to implement thread-safe static initialization and the like, so it just calls functions that are supplied by the system. Stuff like __cxa_guard_acquire and __cxa_pure_virtual live here.

linker/ --- /system/bin/linker and /system/bin/linker64

The dynamic linker. When you run a dynamically-linked executable, its ELF file has a DT_INTERP entry that says "use the following program to start me". On Android, that's either linker or linker64 (depending on whether it's a 32-bit or 64-bit executable). It's responsible for loading the ELF executable into memory and resolving references to symbols (so that when your code tries to jump to fopen(3), say, it lands in the right place).

tests/ --- unit tests

The tests/ directory contains unit tests. Roughly arranged as one file per publicly-exported header file. tests/headers/ contains compile-only tests that just check that things are in the headers, whereas the "real" tests check actual behavior.

benchmarks/ --- benchmarks

The benchmarks/ directory contains benchmarks, with its own documentation.

What's in libc/?

libc/
  arch-arm/
  arch-arm64/
  arch-common/
  arch-x86/
  arch-x86_64/
    # Each architecture has its own subdirectory for stuff that isn't shared
    # because it's architecture-specific. There will be a .mk file in here that
    # drags in all the architecture-specific files.
    bionic/
      # Every architecture needs a handful of machine-specific assembler files.
      # They live here.
    string/
      # Most architectures have a handful of optional assembler files
      # implementing optimized versions of various routines. The <string.h>
      # functions are particular favorites.
    syscalls/
      # The syscalls directories contain script-generated assembler files.
      # See 'Adding system calls' later.

  include/
    # The public header files on everyone's include path. These are a mixture of
    # files written by us and files taken from BSD.

  kernel/
    # The kernel uapi header files. The "libc" headers that developers actually
    # use are a mixture of headers provided by the C library itself (which,
    # for bionic, are in bionic/libc/include/) and headers provided by the
    # kernel. This is because ISO C and POSIX will say things like "there is
    # a constant called PROT_NONE" or "there is a type called struct stat,
    # and it contains a field called st_size", but they won't necessarily say
    # what _value_ that constant has, or what _order_ the fields in a type
    # are in. Those are left to individual kernels' ABIs. In an effort to --
    # amongst other things, see https://lwn.net/Articles/507794/ for more
    # background -- reduce copy & paste, the Linux kernel makes all the types
    # and constants that make up the "userspace API" (uapi) available as
    # headers separate from their internal-use headers (which contain all kinds
    # of extra stuff that isn't available to userspace). We import the latest
    # released kernel's uapi headers in external/kernel-headers/, but we don't
    # use those headers directly in bionic. The bionic/libc/kernel/ directory
    # contains scrubbed copies of the originals from external/kernel-headers/.
    # The generate_uapi_headers.sh script should be used to go from a kernel
    # tree to external/kernel-headers/ --- this takes care of the
    # architecture-specific details. The update_all.py script should then be
    # used to regenerate bionic's copy from external/kernel-headers/.
    # The files in bionic must not be edited directly because any local changes
    # will be overwritten by the next update. "Updating kernel header files"
    # below has more information on this process.

  private/
    # These are private header files meant for use within bionic itself.

  dns/
    # Contains the DNS resolver (originates from NetBSD code).

  upstream-freebsd/
  upstream-netbsd/
  upstream-openbsd/
    # These directories contain upstream source with no local changes.
    # Any time we can just use a BSD implementation of something unmodified,
    # we should. Ideally these should probably have been three separate git
    # projects in external/, but they're here instead mostly by historical
    # accident (because it wouldn't have been easy to import just the tiny
    # subset of these operating systems that -- unlike Android -- just have
    # one huge repository rather than lots of little ones and a mechanism
    # like our `repo` tool).
    # The structure under these directories mimics the relevant upstream tree,
    # but in order to actually be able to compile this code in our tree
    # _without_ making modifications to the source files directly, we also
    # have the following subdirectories in each one that aren't upstream:
    android/
      include/
        # This is where we keep the hacks necessary to build BSD source
        # in our world. The *-compat.h files are automatically included
        # using -include, but we also provide equivalents for missing
        # header/source files needed by the BSD implementation.

  bionic/
    # This is the biggest mess. The C++ files are files we own, typically
    # because the Linux kernel interface is sufficiently different that we
    # can't use any of the BSD implementations. The C files are usually
    # legacy mess that needs to be sorted out, either by replacing it with
    # current upstream source in one of the upstream directories or by
    # switching the file to C++ and cleaning it up.

  malloc_debug/
    # The code that implements the functionality to enable debugging of
    # native allocation problems.

  stdio/
    # These are legacy files of dubious provenance. We're working to clean
    # this mess up, and this directory should disappear.

  tools/
    # Various tools used to maintain bionic.

  tzcode/
    # A modified superset of the IANA tzcode. Most of the modifications relate
    # to Android's use of a single file (with corresponding index) to contain
    # timezone data.
  zoneinfo/
    # Android-format timezone data.
    # See 'Updating tzdata' later.

Adding libc wrappers for system calls

The first question you should ask is "should I add a libc wrapper for this system call?". The answer is usually "no".

The answer is "yes" if the system call is part of the POSIX standard.

The answer is probably "yes" if the system call has a wrapper in at least one other C library (typically glibc/musl or Apple's libc).

The answer may be "yes" if the system call has three/four distinct users in different projects, and there isn't a more specific higher-level library that would make more sense as the place to add the wrapper.

In all other cases, you should use syscall(3) instead.

Adding a system call usually involves:

  1. Add an entry (or entries, in some cases) to SYSCALLS.TXT. See SYSCALLS.TXT itself for documentation on the format. See also the notes below for how to deal with tricky cases like off_t.

  2. Find the right header file to work in by looking up your system call on man7.org. (If there's no header file given, see the points above about whether we should really be adding this or not!)

  3. Add constants (and perhaps types) to the appropriate header file. Note that you should check to see whether the constants are already in kernel uapi header files, in which case you just need to make sure that the appropriate header file in libc/include/ #includes the relevant linux/ file or files.

  4. Add function declarations to the appropriate header file. Don't forget to include the appropriate __INTRODUCED_IN(), with the right API level for the first release your system call wrapper will be in. See libc/include/android/api_level.h for the API levels. If the header file doesn't exist, copy all of libc/include/sys/sysinfo.h into your new file --- it's a good short example to start from.

    Note also our style for naming arguments: always use two leading underscores (so developers are free to use any of the unadorned names as macros without breaking things), avoid abbreviations, and ideally try to use the same name as an existing system call (to reduce the amount of English vocabulary required by people who just want to use the function signatures). If there's a similar function already in the C library, check what names it's used. Finally, prefer the void* orthography we use over the void * you'll see on man7.org.)

  5. Add basic documentation to the header file. Again, the existing libc/include/sys/sysinfo.h is a good short example that shows the expected style.

    Most of the detail should actually be left to the man7.org page, with only a brief one-sentence explanation (usually based on the description in the NAME section of the man page) in our documentation. Always include the return value/error reporting details (you can find out what the system call returns from the RETURN VALUE of the man page), but try to match the wording and style wording from our existing documentation; we're trying to minimize the amount of English readers need to understand by using the exact same wording where possible). Explicitly say which version of Android the function was added to in the documentation because the documentation generation tool doesn't yet understand __INTRODUCED_IN().

    Explicitly call out any Android-specific changes/additions/limitations because they won't be on the man7.org page.

  6. Add the function name to the correct section in libc/libc.map.txt; it'll be near the end of the file. You may need to add a new section if you're the first to add a system call to this version of Android.

  7. Add a basic test. Don't try to test everything; concentrate on just testing the code that's actually in bionic, not all the functionality that's implemented in the kernel. For simple syscalls, that's just the auto-generated argument and return value marshalling.

    Add a test in the right file in tests/. We have one file per header, so if your system call is exposed in <unistd.h>, for example, your test would go in tests/unistd_test.cpp.

    A trivial test that deliberately supplies an invalid argument helps check that we're generating the right symbol and have the right declaration in the header file, and that the change to libc.map.txt from step 5 is correct. (You can use strace(1) manually to confirm that the correct system call is being made.)

    For testing the kernel side of things, we should prefer to rely on https://github.com/linux-test-project/ltp for kernel testing, but you'll want to check that external/ltp does contain tests for the syscall you're adding. Also check that external/ltp is using the libc wrapper for the syscall rather than calling it "directly" via syscall(3)!

Some system calls are harder than others. The most common problem is a 64-bit argument such as off64_t (a pointer to a 64-bit argument is fine, since pointers are always the "natural" size for the architecture regardless of the size of the thing they point to). Whenever you have a function that takes off_t or off64_t, you'll need to consider whether you actually need a foo() and a foo64(), and whether they will use the same underlying system call or are implemented as two different system calls. It's usually easiest to find a similar system call and copy and paste from that. You'll definitely need to test both on 32-bit and 64-bit. (These special cases warrant more testing than the easy cases, even if only manual testing with strace. Sadly it isn't always feasible to write a working test for the interesting cases -- offsets larger than 2GiB, say -- so you may end up just writing a "meaningless" program whose only purpose is to give you patterns to look for when run under strace(1).)

A general example of adding a system call: https://android-review.googlesource.com/c/platform/bionic/+/2073827

Debugging tips

  1. Key error for a new codename in libc/libc.map.txt

e.g. what you add in libc/libc.map.txt is:

LIBC_V { # introduced=Vanilla
  global:
    xxx; // the new system call you add
} LIBC_U;

The error output is:

Traceback (most recent call last):
  File "/path/tp/out/soong/.temp/Soong.python_qucjwd7g/symbolfile/__init__.py", line 171,
  in decode_api_level_tag
    decoded = str(decode_api_level(value, api_map))
  File "/path/to/out/soong/.temp/Soong.python_qucjwd7g/symbolfile/__init__.py", line 157,
  in decode_api_level
    return api_map[api]
KeyError: 'Vanilla'

Solution: Ask in the team and wait for the update.

  1. Use of undeclared identifier of the new system call in the test

Possible Solution: Check everything ready in the files mentioned above first. Maybe glibc matters. Follow the example and try #if defined(GLIBC).

Updating kernel header files

As mentioned above, this is currently a two-step process:

  1. Use generate_uapi_headers.sh to go from a Linux source tree to appropriate contents for external/kernel-headers/.
  2. Run update_all.py to scrub those headers and import them into bionic.

Note that if you're actually just trying to expose device-specific headers to build your device drivers, you shouldn't modify bionic. Instead use TARGET_DEVICE_KERNEL_HEADERS and friends described in config.mk.

Updating tzdata

This is handled by the libcore team, because they own icu, and that needs to be updated in sync with bionic). See system/timezone/README.android.

Verifying changes

If you make a change that is likely to have a wide effect on the tree (such as a libc header change), you should run make checkbuild. A regular make will not build the entire tree; just the minimum number of projects that are required for the device. Tests, additional developer tools, and various other modules will not be built. Note that make checkbuild will not be complete either, as make tests covers a few additional modules, but generally speaking make checkbuild is enough.

Running the tests

The tests are all built from the tests/ directory. There is a separate directory benchmarks/ containing benchmarks, and that has its own documentation on running the benchmarks.

Device tests

$ mma # In $ANDROID_ROOT/bionic.
$ adb root && adb remount && adb sync
$ adb shell /data/nativetest/bionic-unit-tests/bionic-unit-tests
$ adb shell \
    /data/nativetest/bionic-unit-tests-static/bionic-unit-tests-static
# Only for 64-bit targets
$ adb shell /data/nativetest64/bionic-unit-tests/bionic-unit-tests
$ adb shell \
    /data/nativetest64/bionic-unit-tests-static/bionic-unit-tests-static

Note that we use our own custom gtest runner that offers a superset of the options documented at https://github.com/google/googletest/blob/main/docs/advanced.md#running-test-programs-advanced-options, in particular for test isolation and parallelism (both on by default).

Device tests via CTS

Most of the unit tests are executed by CTS. By default, CTS runs as a non-root user, so the unit tests must also pass when not run as root. Some tests cannot do any useful work unless run as root. In this case, the test should check getuid() == 0 and do nothing otherwise (typically we log in this case to prevent accidents!). Obviously, if the test can be rewritten to not require root, that's an even better solution.

Currently, the list of bionic CTS tests is generated at build time by running a host version of the test executable and dumping the list of all tests. In order for this to continue to work, all architectures must have the same number of tests, and the host version of the executable must also have the same number of tests.

Running the gtests directly is orders of magnitude faster than using CTS, but in cases where you really have to run CTS:

$ make cts # In $ANDROID_ROOT.
$ adb unroot # Because real CTS doesn't run as root.
# This will sync any *test* changes, but not *code* changes:
$ cts-tradefed \
    run singleCommand cts --skip-preconditions -m CtsBionicTestCases

Host tests

The host tests require that you have lunched either an x86 or x86_64 target. Note that due to ABI limitations (specifically, the size of pthread_mutex_t), 32-bit bionic requires PIDs less than 65536. To enforce this, set /proc/sys/kernel/pid_max to 65536.

$ ./tests/run-on-host.sh 32
$ ./tests/run-on-host.sh 64   # For x86_64-bit *targets* only.

You can supply gtest flags as extra arguments to this script.

Against glibc

As a way to check that our tests do in fact test the correct behavior (and not just the behavior we think is correct), it is possible to run the tests against the host's glibc.

$ ./tests/run-on-host.sh glibc

Against musl

Another way to verify test behavior is to run against musl on the host. glibc musl don't always match, so this can be a good way to find the more complicated corners of the spec. If they do match, bionic probably should too!

$ OUT_DIR=$(ANDROID_BUILD_TOP)/musl-out ./tests/run-on-host.sh musl

Note: the alternate OUT_DIR is used to avoid causing excessive rebuilding when switching between glibc and musl. The first musl test run will be expensive because it will not reuse any already built artifacts, but subsequent runs will be cheaper than if you hadn't used it.

Gathering test coverage

To get test coverage for bionic, use //bionic/build/coverage.sh. Before running, follow the instructions at the top of the file to rebuild bionic with coverage instrumentation.

Attaching GDB to the tests

Bionic's test runner will run each test in its own process by default to prevent tests failures from impacting other tests. This also has the added benefit of running them in parallel, so they are much faster.

However, this also makes it difficult to run the tests under GDB. To prevent each test from being forked, run the tests with the flag --no-isolate.

32-bit ABI bugs

See 32-bit ABI bugs.

More Repositories

1

platform_frameworks_base

Java
10,545
star
2

platform_development

Python
2,699
star
3

platform_frameworks_support

Java
1,658
star
4

platform_system_core

C++
1,179
star
5

platform_packages_apps_settings

Java
489
star
6

kernel_common

C
473
star
7

platform_dalvik

Java
445
star
8

platform_build

Makefile
418
star
9

platform_packages_apps_music

Java
291
star
10

platform_packages_apps_contacts

Java
279
star
11

platform_packages_apps_calendar

Java
268
star
12

platform_manifest

233
star
13

platform_packages_apps_mms

Java
212
star
14

tools_repo

Python
193
star
15

platform_packages_apps_browser

Java
192
star
16

platform_packages_apps_camera

Java
182
star
17

platform_packages_apps_calculator

Java
167
star
18

platform_packages_apps_email

Java
134
star
19

platform_packages_apps_alarmclock

Java
127
star
20

platform_packages_apps_launcher

Java
126
star
21

kernel_msm

125
star
22

platform_packages_apps_packageinstaller

Java
120
star
23

platform_external_qemu

C
107
star
24

platform_external_protobuf

C++
103
star
25

platform_packages_apps_phone

97
star
26

platform_hardware_libhardware

C++
91
star
27

platform_external_webkit

C++
78
star
28

platform_packages_providers_downloadprovider

Java
71
star
29

platform_packages_providers_contactsprovider

Java
69
star
30

platform_external_opencore

C++
67
star
31

platform_external_openssl

57
star
32

platform_external_skia

C++
56
star
33

platform_hardware_ril

C++
56
star
34

platform_packages_apps_soundrecorder

Java
56
star
35

platform_packages_providers_telephonyprovider

Java
50
star
36

platform_frameworks_policies_base

Java
50
star
37

platform_prebuilt

C
49
star
38

platform_packages_providers_mediaprovider

Java
48
star
39

platform_packages_apps_stk

Java
44
star
40

platform_system_bluetooth

C
40
star
41

platform_external_wpa_supplicant

C
39
star
42

platform_external_apache-http

Java
39
star
43

platform_packages_apps_htmlviewer

Java
38
star
44

platform_external_libpcap

C
38
star
45

platform_external_sqlite

C++
36
star
46

platform_packages_providers_calendarprovider

Java
36
star
47

platform_external_freetype

C
35
star
48

platform_external_libxml2

C
34
star
49

platform_external_iptables

C
31
star
50

platform_external_sonivox

C
28
star
51

platform_packages_apps_googlesearch

Java
27
star
52

platform_external_icu4c

C++
26
star
53

platform_packages_apps_im

Java
26
star
54

platform_external_zlib

C
24
star
55

platform_packages_apps_voicedialer

Java
24
star
56

platform_external_giflib

C
23
star
57

platform_external_netcat

C
22
star
58

platform_external_bluez

C
22
star
59

platform_frameworks_opt_com.google.android

Makefile
22
star
60

platform_external_strace

C
22
star
61

platform_external_dhcpcd

C
21
star
62

platform_external_expat

C
21
star
63

platform_external_libpng

C
20
star
64

platform_external_ppp

C
19
star
65

platform_external_tcpdump

C
19
star
66

platform_external_dbus

C
19
star
67

platform_packages_apps_sync

18
star
68

platform_external_jpeg

C
17
star
69

platform_external_elfutils

C
17
star
70

platform_external_jdiff

Java
16
star
71

platform_external_srec

Roff
16
star
72

platform_external_emma

Java
16
star
73

platform_external_oprofile

C
14
star
74

platform_external_netperf

C
14
star
75

platform_external_aes

C
14
star
76

platform_packages_apps_updater

14
star
77

platform_external_jhead

C
14
star
78

platform_external_fdlibm

C
13
star
79

platform_packages_providers_googlecontactsprovider

Makefile
13
star
80

platform_external_gdata

Java
13
star
81

platform_packages_providers_drmprovider

Java
13
star
82

platform_external_tagsoup

Java
13
star
83

platform_external_dropbear

C
13
star
84

platform_packages_providers_improvider

Java
12
star
85

platform_system_wlan_ti

C
12
star
86

aosp-mirror.github.io

HTML
12
star
87

platform_external_ping

12
star
88

platform_external_googleclient

Makefile
12
star
89

platform_external_clearsilver

C
12
star
90

platform_external_esd

C
11
star
91

platform_external_neven

C
11
star
92

platform_external_tinyxml

C++
11
star
93

platform_external_yaffs2

C
11
star
94

platform_external_libffi

C
10
star
95

platform_external_tremor

C
9
star
96

platform_external_elfcopy

C
9
star
97

platform_external_safe-iop

C
9
star
98

.github

A repository for GitHub-specific configurations.
8
star
99

.allstar

2
star