Jump to content

Bionic (software)

From Wikipedia, the free encyclopedia

This is an old revision of this page, as edited by 216.239.45.130 (talk) at 05:34, 30 August 2017 (cite the bionic docs for claims about number of functions). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

Bionic
Developer(s)Open Handset Alliance
Initial releaseSeptember 23, 2008; 15 years ago (2008-09-23)[1]
Repository
Operating systemAndroid[2]
Platformx86, x86-64, ARM, ARM64, MIPS, MIPS64
TypeC standard library
LicenseThree-clause BSD licence
Websitedeveloper.android.com Edit this on Wikidata

Bionic is the standard C library (including libc, libdl, libm, and libpthread) developed by Google for its Android operating system.[2] Bionic is a BSD-licensed C library for use with the Linux kernel. This differs from other BSD C libraries which require a BSD kernel, and from the GNU C Library (glibc) which uses the GNU Lesser General Public License.

Original goals

The original publicly stated goals for Bionic were the following:[3][4]

    • Android is based on the Linux kernel, which is subject to the copyleft GNU General Public License (GPL) version 2.
    • The most widespread standard C library for the Linux kernel is the GNU C Library (glibc), which is subject to the GNU Lesser General Public License (LGPL), also a copyleft license. In contrast to the GPL, the LGPL explicitly allows for dynamic linking but it does not allow static linking of proprietary software.
    • The permissive BSD license is a non-copyleft license that is compatible in both directions. A BSD-licensed glibc substitute could act as an isolation layer between the copyleft core (kernel) and the non-copyleft applications, and was therefore chosen by Google for its Bionic as a glibc substitute.[6]
  • Small size: Bionic was much smaller than the GNU C Library; more importantly its memory requirements were (and remain) much lower.
  • Speed: Bionic was designed for CPUs at relatively low clock frequencies.

Supported architectures

Bionic only supports Linux kernels, but currently supports the arm, arm64, mips, mips64, x86, and x86-64 architectures. The platform itself requires armv7 with NEON since KitKat, though the NDK as of r15 still supports armv5 (which it calls armeabi).

Components

Some parts of the libc source, such as stdio, are from the BSDs (mainly OpenBSD), whereas others, such as the pthread implementation, were written from scratch.

The dynamic memory allocator implementation has changed over time. Before Lollipop there was a single native memory allocator, dlmalloc. For Lollipop and Marshmallow there were two implementations: dlmalloc and jemalloc. jemalloc gives much higher performance than dlmalloc, but at the cost of extra memory required for bookkeeping. Most devices used jemalloc but low-memory devices still used dlmalloc. For Nougat and later releases, all devices uses jemalloc. Low-memory devices use a "svelte" configuration of jemalloc that disables the tcache to nearly match the lower memory overhead of dlmalloc while keeping most of the speed of jemalloc.

Some 64-bit devices, like the Nexus 9, are effectively low-memory devices because of the extra space requirements of 64-bit pointers and hosting of two zygotes. (Zygote is an Android system service that is the parent of all Android application processes.[7])

The libm source is largely FreeBSD's, but with optimized assembler contributed by the various SoC vendors.

The dynamic linker (and libdl) were written from scratch.

Bionic doesn't include libthread_db (used by gdbserver), but the NDK does. The Android platform includes a statically-linked gdbserver so developers can use the latest gdb even on old devices.

There is no separate libpthread, libresolv, or librt on Android – the functionality is all in libc. For libpthread, there's no attempt to optimize for the single-threaded case because apps are in a multi-threaded environment even before the first instruction of third-party code is ever run.

The Android platform uses libc++ for the C++ standard library (releases up to and including Lollipop used stlport). The NDK currently offers stlport, libc++, and the GNU STL.

Differences from POSIX

Although bionic aims to implement all of POSIX, there are still (as of Oreo) about 70 functions missing[8]. There are also POSIX functions such as the endpwent/getpwent/setpwent family that are inapplicable to Android because it lacks a passwd database.

Some functions deliberately do not conform to the POSIX or C standards for security reasons, such as printf which does not support the %n format string.[9]

Relationship to the NDK

Platform code uses bionic directly, but third-party developers use the Android Native Development Kit (NDK). Many third-party developers still target older OS releases, which contributes to a widespread belief that bionic lacks many features. Gingerbread exported 803 functions from libc but Oreo exports 1278 (a 1.6x increase)[8].

Fortify source

As of Android Jelly Bean MR1 (4.2), bionic supports similar functionality to glibc's _FORTIFY_SOURCE,[10] which is a feature where unsafe string and memory functions (such as strcpy(), strcat(), and memcpy()) include checks for buffer overruns. These checks are performed at compile time if the buffer sizes can be determined at compile time, or run-time otherwise. Because fortify relies on runtime support from libc, its portability to older Android releases is limited.[11] The platform itself is built with _FORTIFY_SOURCE enabled.

Historically, one of the shortcomings of fortify has been that it's closely tied with gcc, which makes it very difficult to support well in other compilers, like Clang. This meant that when Android swapped to Clang as its default compiler[12], bionic's fortify implementation became substantially less useful. In Android Oreo (8.0), bionic's fortify was overhauled[13] with Clang in mind, resulting in fortify on Clang providing an experience on par with fortify on gcc. Since this overhaul, some checks were added above and beyond glibc's to catch code that -- while not necessarily causing undefined behavior -- is obviously incorrect. Because this new implementation requires no more libc support than the prior one, the clang-specific enhancements are available to applications targeting versions of Android before Oreo.

Controversies

For the creation of Bionic, Google used GPLv2-licensed Linux kernel header files. To get rid of the GPL, Google claimed that it cleaned the header files from any copyright-able work, reducing them to non-copyrightable "facts".[14][15] Google's behaviour was considered by Linus Torvalds as acceptable,[15] but their interpretation of the GPL has been challenged, for instance by Raymond Nimmer, a law professor at the University of Houston Law Center.[16]

See also

References

  1. ^ "Announcing the Android 1.0 SDK, release 1". September 9, 2008. Retrieved September 21, 2012.
  2. ^ a b "Android Anatomy and Physiology" (PDF). Google I/O. 2008-05-28. Archived from the original (PDF) on 2016-04-08. Retrieved 2014-05-23. {{cite web}}: Unknown parameter |deadurl= ignored (|url-status= suggested) (help)
  3. ^ Burnette, Ed (June 4, 2008). "Patrick Brady dissects Android". ZDNet.
  4. ^ Turner, David (2009-02-07). "Questions about Bionic". The name "Bionic" comes from the fact that it is part-BSD and part-Linux: its source code consists in a mix of BSD C library pieces with custom Linux-specific bits used to deal with threads, processes, signals and a few others things.
  5. ^ Anatomy-Physiology-of-an-Android (PDF), archived from the original (PDF) on 2016-04-08, retrieved 2017-07-15, License: we want to keep GPL out of user-space (p. 36) {{citation}}: Unknown parameter |dead-url= ignored (|url-status= suggested) (help)
  6. ^ Florian Mueller. "FOSS Patents".
  7. ^ "Zygote". Anatomy of Android.
  8. ^ a b "Android bionic status".
  9. ^ Elliott Hughes. "libc: #define to remove support for %n from printf(3)?".
  10. ^ "Jelly Bean". Android Developers. android.com. Retrieved 2013-12-27.
  11. ^ "Android 4.2 and FORTIFY_SOURCE". Android Security Discussions. groups.google.com. Retrieved 2013-12-27.
  12. ^ "Android NDK changelog". Android NDK. android.googlesource.com. Retrieved 2017-08-28.
  13. ^ "FORTIFY in Android". Android Developers Blog. android-developers.googleblog.com. Retrieved 2017-08-28.
  14. ^ Google android and the linux headers on theregister.com (2011)
  15. ^ a b Android: Sued by Microsoft, not by Linux "Microsoft launches new Android suit, Linus Torvalds' take on Linux kernel headers and Android" on ITworld (March 21, 2011)
  16. ^ Infringement and disclosure risk in development on copyleft platforms on ipinfoblog.com by Raymond Nimmer (2011)