aboutsummaryrefslogtreecommitdiff
path: root/libc/bionic/libc_init_static.cpp
Commit message (Collapse)AuthorAgeFilesLines
* [GWP-ASan] Provide runtime configuration through an env var + sysprop.Mitch Phillips2022-04-201-24/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | This patch introduces GWP-ASan system properties and environment variables to control the internal sampling rates of GWP-ASan. This can be used for: 1. "Torture testing" the system, i.e. running it under an extremely high sampling rate under GWP-ASan. 2. Increasing sampling remotely to allow further crash report collection of rare issues. There are three sets of system properites: 1. libc.debug.gwp_asan.*.system_default: Default values for native executables and system apps. 2. libc.debug.gwp_asan.*.app_default: Default values for non-system apps, and 3. libc.debug.gwp_asan.*.<basename/app_name>: Default values for an individual app or native process. There are three variables that can be changed: 1. The allocation sampling rate (default: 2500) - using the environment variable GWP_ASAN_SAMPLE_RATE or the libc.debug.gwp_asan.sample_rate.* system property. 2. The process sampling rate (default: 128 for system apps/processes, 1 for opted-in apps) - using the environment variable GWP_ASAN_PROCESS_SAMPLING or the libc.debug.gwp_asan.process_sampling.* system property, 3. The number of slots available (default: 32) - using the environment variable GWP_ASAN_MAX_ALLOCS or the libc.debug.gwp_asan.max_allocs.* system property. If not specified, #3 will be calculated as a ratio of the default |2500 SampleRate : 32 slots|. So, a sample rate of "1250" (i.e. twice as frequent sampling) will result in a doubling of the max_allocs to "64". Bug: 219651032 Test: atest bionic-unit-tests Change-Id: Idb40a2a4d074e01ce3c4e635ad639a91a32d570f
* [MTE] Relax ELF note.Mitch Phillips2022-02-081-12/+23
| | | | | | | | | | | | | | | | | | | | | | Currently, the ELF note parsing in the loader is not permissive. This patch relaxes the restrictions on the bits of the ELF note that could be extended in the future. This may allow more MTE options in apexes. If we add some extra metadata bit (say, in bit 5) in the future, and then build MTE into every apex, we don't want it to crash on Android 13 devices (we'd much rather it just be a no-op). Remove the (unused anywhere) NT_MEMTAG_DEFAULT and call it NT_MEMTAG_NONE. And finally, make the tests work on bionic-unit-tests-static. We previously didn't deploy the test binaries, so add them as a data dependency. Bug: N/A Test: atest bionic-unit-tests-static Change-Id: I13530faad55c719c6eb848297f8ce378e18afbfc
* Merge "[MTE] add sysprop to set mte state globally"Treehugger Robot2022-02-081-2/+4
|\
| * [MTE] add sysprop to set mte state globallyFlorian Mayer2022-02-071-2/+4
| | | | | | | | | | | | this can be used to run CTS w/ maximum MTE coverage on normal builds. Change-Id: I527b8549f30fd4bd9511d94ca142ea08b72d1a78
* | [NFCI] Change Android's NT_TYPE to NT_ANDROID_TYPE.Mitch Phillips2022-02-071-1/+1
|/ | | | | | | | | | | | | | | Normally, platform-specific note types in the toolchain are prefixed with the platform name. Because we're exposing the NT_TYPE_MEMTAG and synthesizing the note in the toolchain in an upcoming patch (https://reviews.llvm.org/D118948), it's been requested that we change the name to include the platform prefix. While NT_TYPE_IDENT and NT_TYPE_KUSER aren't known about or synthesized by the toolchain, update those references as well for consistency. Bug: N/A Test: Build Android Change-Id: I7742e4917ae275d59d7984991664ea48028053a1
* Treat static binaries "the same" for the profiling signals.Elliott Hughes2021-09-291-0/+11
| | | | | | | | | | Strictly this still isn't quite the same, because they won't actually be profiled, but at least they won't *crash* now if they're sent a profiling signal. Bug: http://b/201497662 Test: treehugger Change-Id: I0728492eed77584cd850d28897056996387e6671
* Allow the kernel to upgrade ASYNC mode processes to SYNC mode.Peter Collingbourne2021-07-011-1/+5
| | | | | | | | | | | | | | | | On devices where the performance of ASYNC mode is similar to SYNC mode on certain CPUs, OEMs may choose to configure the kernel to prefer SYNC mode on those CPUs by writing the value "sync" to the sysfs node: /sys/devices/system/cpu/cpu<N>/mte_tcf_preferred The kernel will only respect the per-CPU preference if the user program allows this by specifying the preferred mode as a member of a set of allowed modes. Since only kernels with r.android.com/1754670 support specifying multiple modes, fall back to trying to specify a single mode if that doesn't work. Bug: 189966263 Change-Id: Ie7ada3b073178b7967f0819cbdadc2d8e3a2c648
* Add some slack at the end of large allocations when target SDK level < S.Peter Collingbourne2021-03-051-0/+1
| | | | | | | | | | | | | | This works around buggy applications that read a few bytes past the end of their allocation, which would otherwise cause a segfault with the concurrent Scudo change that aligns large allocations to the right. Because the implementation of android_set_application_target_sdk_version() lives in the linker, we need to introduce a hook so that libc is notified when the target SDK version changes. Bug: 181344545 Change-Id: Id4be6645b94fad3f64ae48afd16c0154f1de448f
* [MTE] Add MEMTAG_OPTIONS and arm64.memtag.process.* sysprop parsing.Mitch Phillips2021-01-141-28/+107
| | | | | | | | | | | | | | | | | | | | | | | | | | These two options allow for ARM MTE to be enabled dynamically (instead of at compile time via the ELF note). They are settable from a non-root shell to allow device owners to test system binaries with MTE. The following values may be set: 1. 'off' -> No MTE, uses TBI on compatible devices. 2. 'sync' -> SYNC MTE. 3. 'async' -> ASYNC MTE. The following methods can be used to launch a process (sanitizer-status) with configurable values: 1. adb shell MEMTAG_OPTIONS=async sanitizer-status 2. adb shell setprop arm64.memtag.process.sanitizer-status async && \ adb shell sanitizer-status Note: The system server will require some special handing in the zygote pre-fork to check the sysprops. The zygote should always have the ELF note. TODO in a subsequent patch. Bug: 135772972 Bug: 172365548 Test: Launching sanitizer-status above using both the settings. Change-Id: Ic1dbf3985a3f23521ec86725ec482c8f6739c182
* Use ELF notes to set the desired memory tagging level.Evgenii Stepanov2021-01-061-0/+93
| | | | | | | | | | | Use a note in executables to specify (none|sync|async) heap tagging level. To be extended with (heap x stack x globals) in the future. A missing note disables all tagging. Bug: b/135772972 Test: bionic-unit-tests (in a future change) Change-Id: Iab145a922c7abe24cdce17323f9e0c1063cc1321
* Put __*_ARRAY__ symbols before prioritized init/fini funcsMatthew Maurer2020-11-021-4/+4
| | | | | | | | | | | | | | | | | | | | | | | | | | | | A constructor or destructor function with an integral priority is placed in an .init_array or .fini_array section with the priority suffixed to the section name: - __attribute__((constructor)) ==> .init_array - __attribute__((constructor(42))) ==> .init_array.42 The suffixed init/fini sections appear before the unsuffixed sections, so the prioritized functions appeared before the __{INIT,FINI}_ARRAY__ symbols and were dropped when the symbols were used. The (static) linker doesn't recognize priority suffixes on .preinit_array. This bug affected .init_array and .fini_array for static executables. For dynamic executables, only .fini_array was affected, because .init_array is handled by the dynamic loader instead, which uses DT_INIT_ARRAY[SZ]. For DSOs, neither is affected, because the two sections are only handled by the dynamic loader. This patch also fixes a minor inconsistency where dynamic init/preinit were passed argc/argv/envp, but static were not. Bug: http://b/170983066 Test: bionic-unit-tests Change-Id: I0fffa776e5d9bdb6f8af06b4c1af148236742fef
* Add a thread-properties APIVy Nguyen2020-08-111-0/+1
| | | | | | | | | | (Based on proposal at https://sourceware.org/glibc/wiki/ThreadPropertiesAPI) This includes API to: - locate static and dynamic TLS - register thread-exit and dynamic TLS creation/destruction callbacks Change-Id: Icd9d29a5b2f47495395645e19d3b2c96826f19c8
* Update a few comments.Elliott Hughes2020-02-101-7/+4
| | | | | Test: N/A Change-Id: Id5e21eaff9bc43256b8f54e708dfc862ee7e763a
* Move bionic_macros.h from private to platform.Josh Gao2020-01-021-1/+1
| | | | | Test: treehugger Change-Id: Ie473914f4c8924c7240b3ac22093a9daf42fc948
* Make private/bionic_page.h available as platform/bionic/page.h.Elliott Hughes2019-12-121-1/+1
| | | | | | Bug: http://b/145825270 Test: treehugger Change-Id: I52958f370c0df2c02cb8250e2cf0705d4e93e908
* Init fork handler after __libc_init_malloc.Mitch Phillips2019-11-141-0/+1
| | | | | | | | | | pthread_atfork may call malloc() during its once-init. This causes problems with allocators (GWP-ASan) that require explicit initialisation before calls to malloc(). Bug: 135634846 Test: atest bionic Change-Id: I1810a00465db99d5aa34fa6f74dea5908a628d3a
* Use ifuncs in the linkerRyan Prichard2019-11-051-0/+3
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Using ifuncs allows the linker to select faster versions of libc functions like strcmp, making linking faster. The linker continues to first initialize TLS, then call the ifunc resolvers. There are small amounts of code in Bionic that need to avoid calling functions selected using ifuncs (generally string.h APIs). I've tried to compile those pieces with -ffreestanding. Maybe it's unnecessary, but maybe it could help avoid compiler-inserted memset calls, and maybe it will be useful later on. The ifuncs are called in a special early pass using special __rel[a]_iplt_start / __rel[a]_iplt_end symbols. The linker will encounter the ifuncs again as R_*_IRELATIVE dynamic relocations, so they're skipped on the second pass. Break linker_main.cpp into its own liblinker_main library so it can be compiled with -ffreestanding. On walleye, this change fixes a recent 2.3% linker64 start-up time regression (156.6ms -> 160.2ms), but it also helps the 32-bit time by about 1.9% on the same benchmark. I'm measuring the run-time using a synthetic benchmark based on loading libandroid_servers.so. Test: bionic unit tests, manual benchmarking Bug: none Merged-In: Ieb9446c2df13a66fc0d377596756becad0af6995 Change-Id: Ieb9446c2df13a66fc0d377596756becad0af6995 (cherry picked from commit 772bcbb0c2f7a87b18021849528240ef0c617d94)
* Adopt GNU calling convention for ifunc resolvers.Peter Collingbourne2019-10-281-4/+3
| | | | | | | | | | | | | | | | | | | | | | | In order for an ifunc resolver to detect the presence of certain CPU features, access to getauxval(AT_HWCAP) or getauxval(AT_HWCAP2) may be required. In order for getauxval() to work, it needs to access the pointer to the auxiliary vector stored by the linker in the libc shared globals data structure. Accessing the shared globals requires libc to call the __libc_shared_globals() function exported by the linker. However, in order to call this function, libc must be fully relocated, which is not guaranteed to be the case at the point when ifunc resolvers are called. glibc solves this problem by passing the values of getauxval(AT_HWCAP) (and getauxval(AT_HWCAP2) on aarch64) as arguments to the ifunc resolver. Since this seems to be not only the most straightforward way to solve the problem but also improves our compatibility with glibc, we adopt their calling convention. This change is ABI compatible with old resolvers because the arguments are passed in registers, so the old resolvers will simply ignore the new arguments. Bug: 135772972 Change-Id: Ie65bd6e7067f0c878df3d348c815fda61dc12de2
* Call __hwasan_init_static() during libc startup in statically linked ↵Peter Collingbourne2019-03-191-3/+4
| | | | | | | | | | | | | | | | | executables. __hwasan_init() was segfaulting when called from here because it was calling into libc functions which required more of libc to be initialized. Instead, call __hwasan_init_static(), which does a minimal amount of initialization for statically linked executables, just enough that we can run instrumented code. __hwasan_init() itself will end up being called later (most likely from a global ctor) after libc is fully initialized. We'll need to wait for LLVM r352816+r352823 to land in our toolchain before landing this. Change-Id: I12ffc7e08f6dd161e4ff2088f8d56265af7baedf
* Make static ifunc resolvers optional.Dan Albert2019-02-201-2/+24
| | | | | | | | | | Gold isn't emitting these symbols, so we don't necessarily have the support for them (gold is still the default for most architectures in the NDK). Test: bionic static unit tests Bug: None Change-Id: Ifc360cb6c26571fb3f0309adb0faf0af7ee5b36f
* Merge "Implement ifunc support for static executables."Treehugger Robot2019-02-031-0/+25
|\
| * Implement ifunc support for static executables.Peter Collingbourne2019-01-251-0/+25
| | | | | | | | | | | | | | | | | | | | | | | | A static executable is almost entirely statically relocated by the linker, with the exception of IRELATIVE relocations, which must be resolved by libc by enumerating the relocations using the special linker-defined symbols __rela?_iplt_{start,end}. This patch implements ifunc support by enumerating the relocations in this way. Bug: 112482891 Test: /data/nativetest{,64}/bionic-unit-tests-static/bionic-unit-tests-static on walleye_hwasan-userdebug Change-Id: Ia5522a190da0b86e095b141d5d4e68dd7dd4b695
* | Implement dynamic TLS accesses and allocationRyan Prichard2019-01-251-4/+7
|/ | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Initialize a thread's DTV to an empty zeroed DTV. Allocate the DTV and any ELF module's TLS segment on-demand in __tls_get_addr. Use a generation counter, incremented in the linker, to signal when threads should update/reallocate their DTV objects. A generation count of 0 always indicates the constant zero DTV. Once a DTV is allocated, it isn't freed until the thread exits, because a signal handler could interrupt the fast path of __tls_get_addr between accessing the DTV slot and reading a field of the DTV. Bionic keeps a linked list of DTV objects so it can free them at thread-exit. Dynamic TLS memory is allocated using a BionicAllocator instance in libc_shared_globals. For async-signal safety, access to the linker/libc-shared state is protected by first blocking signals, then by acquiring the reader-writer lock, TlsModules::rwlock. A write lock is needed to allocate or free memory. In pthread_exit, unconditionally block signals before freeing dynamic TLS memory or freeing the shadow call stack. ndk_cruft.cpp: Avoid including pthread_internal.h inside an extern "C". (The header now includes a C++ template that doesn't compile inside extern "C".) Bug: http://b/78026329 Bug: http://b/123094171 Test: bionic unit tests Change-Id: I3c9b12921c9e68b33dcc1d1dd276bff364eff5d7
* Avoid a dlopen abort on an invalid TLS alignmentRyan Prichard2019-01-161-5/+9
| | | | | | | | | If the alignment of a TLS segment in a shared object is invalid, return an error through dlerror() rather than aborting the process. Bug: http://b/78026329 Test: bionic unit tests Change-Id: I60e589ddd8ca897f485d55af089f08bd3ff5b1fa
* Record TLS modules and layout static TLS memoryRyan Prichard2019-01-161-3/+18
| | | | | | Bug: http://b/78026329 Test: bionic unit tests Change-Id: Ibf1bf5ec864c7830e4cd1cb882842b644e6182ae
* StaticTlsLayout: add exe/tcb and solib layoutRyan Prichard2019-01-161-1/+1
| | | | | | | | | | | | Replace reserve_tcb with reserve_exe_segment_and_tcb, which lays out both the TCB and the executable's TLS segment, accounting for the difference in layout between variant 1 and variant 2 targets. The function isn't actually called with a non-null TlsSegment* yet. Bug: http://b/78026329 Test: bionic unit tests Change-Id: Ibd6238577423a7d0451f36da7e64912046959796
* Reorganize static TLS memory for ELF TLSRyan Prichard2019-01-111-7/+18
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | For ELF TLS "local-exec" accesses, the static linker assumes that an executable's TLS segment is located at a statically-known offset from the thread pointer (i.e. "variant 1" for ARM and "variant 2" for x86). Because these layouts are incompatible, Bionic generally needs to allocate its TLS slots differently between different architectures. To allow per-architecture TLS slots: - Replace the TLS_SLOT_xxx enumerators with macros. New ARM slots are generally negative, while new x86 slots are generally positive. - Define a bionic_tcb struct that provides two things: - a void* raw_slots_storage[BIONIC_TLS_SLOTS] field - an inline accessor function: void*& tls_slot(size_t tpindex); For ELF TLS, it's necessary to allocate a temporary TCB (i.e. TLS slots), because the runtime linker doesn't know how large the static TLS area is until after it has loaded all of the initial solibs. To accommodate Golang, it's necessary to allocate the pthread keys at a fixed, small, positive offset from the thread pointer. This CL moves the pthread keys into bionic_tls, then allocates a single mapping per thread that looks like so: - stack guard - stack [omitted for main thread and with pthread_attr_setstack] - static TLS: - bionic_tcb [exec TLS will either precede or succeed the TCB] - bionic_tls [prefixed by the pthread keys] - [solib TLS segments will be placed here] - guard page As before, if the new mapping includes a stack, the pthread_internal_t is allocated on it. At startup, Bionic allocates a temporary bionic_tcb object on the stack, then allocates a temporary bionic_tls object using mmap. This mmap is delayed because the linker can't currently call async_safe_fatal() before relocating itself. Later, Bionic allocates a stack-less thread mapping for the main thread, and copies slots from the temporary TCB to the new TCB. (See *::copy_from_bootstrap methods.) Bug: http://b/78026329 Test: bionic unit tests Test: verify that a Golang app still works Test: verify that a Golang app crashes if bionic_{tls,tcb} are swapped Merged-In: I6543063752f4ec8ef6dc9c7f2a06ce2a18fc5af3 Change-Id: I6543063752f4ec8ef6dc9c7f2a06ce2a18fc5af3 (cherry picked from commit 1e660b70da625fcbf1e43dfae09b7b4817fa1660)
* libc_shared_globals: add a constexpr ctorRyan Prichard2019-01-031-3/+1
| | | | | | | | | | | | | Having a constexpr constructor should guarantee that the static `globals` variable in __libc_shared_globals is initialized statically (as opposed to dynamically), which is important because __libc_shared_globals is called very early (before the linker has relocated itself). With the constructor, though, the fields can safely have in-line default initializers. Bug: none Test: bionic unit tests Change-Id: Icde821557369625734a4d85d7ff55428bad5c247
* Replace TLS_SLOT_BIONIC_PREINIT w/ shared globalsRyan Prichard2018-12-041-2/+2
| | | | | | | | | | | | | | | Instead of passing the address of a KernelArgumentBlock to libc.so for initialization, use __loader_shared_globals() to initialize globals. Most of the work happened in the previous CLs. This CL switches a few KernelArgumentBlock::getauxval calls to [__bionic_]getauxval and stops routing the KernelArgumentBlock address through the libc init functions. Bug: none Test: bionic unit tests Change-Id: I96c7b02c21d55c454558b7a5a9243c682782f2dd Merged-In: I96c7b02c21d55c454558b7a5a9243c682782f2dd (cherry picked from commit 746ad15912cfa82271424747e94d8125acc43d8c)
* Split main thread init into early+late functionsRyan Prichard2018-12-041-6/+4
| | | | | | | | | | | | | | | | | | Split __libc_init_main_thread into __libc_init_main_thread_early and __libc_init_main_thread_late. The early function is called very early in the startup of the dynamic linker and static executables. It initializes the global auxv pointer and enough TLS memory to do system calls, access errno, and run -fstack-protector code (but with a zero cookie because the code for generating a cookie is complex). After the linker is relocated, __libc_init_main_thread_late finishes thread initialization. Bug: none Test: bionic unit tests Change-Id: I6fcd8d7587a380f8bd649c817b40a3a6cc1d2ee0 Merged-In: I6fcd8d7587a380f8bd649c817b40a3a6cc1d2ee0 (cherry picked from commit 39bc44bb0e03514e8d92f8c0ceb0b5901e27a485)
* Use shared globals to init __progname + environRyan Prichard2018-11-281-2/+3
| | | | | | | | | | | | | | | | | Initialize the __progname and environ global variables using libc_shared_globals rather than KernelArgumentBlock. Also: suppose the linker is invoked on an executable: linker prog [args...] The first argument passed to main() and constructor functions is "prog" rather than "linker". For consistency, this CL changes the BSD __progname global from "linker" to "prog". Bug: none Test: bionic unit tests Change-Id: I376d76953c9436706dbc53911ef6585c1acc1c31
* Move the abort message to libc_shared_globalsRyan Prichard2018-11-281-1/+3
| | | | | | | | | | | | | | | | | __libc_shared_globals() is available in dynamic modules as soon as relocation has finished (i.e. after ifuncs run). Before ifuncs have run, the android_set_abort_message() function already doesn't work because it calls public APIs via the PLT. (If this matters, we can use a static bool variable to enable android_set_abort_message after libc initialization). __libc_shared_globals() is hidden, so it's available in the linker immediately (i.e. before relocation). TLS memory (e.g. errno) currently isn't accessible until after relocation, but a later patch fixes that. Bug: none Test: bionic unit tests Change-Id: Ied4433758ed2da9ee404c6158e319cf502d05a53
* Expose libc_shared_globals to libc.so with symbolRyan Prichard2018-11-281-4/+5
| | | | | | | | | | | | | | | | | | Previously, the address of the global variable was communicated from the dynamic linker to libc.so using a field of KernelArgumentBlock, which is communicated using the TLS_SLOT_BIONIC_PREINIT slot. As long as this function isn't called during relocations (i.e. while executing an ifunc), it always return a non-NULL value. If it's called before its PLT entry is relocated, I expect a crash. I removed the __libc_init_shared_globals function. It's currently empty, and I don't think there's one point in libc's initialization where shared globals should be initialized. Bug: http://b/25751302 Test: bionic unit tests Change-Id: I614d25e7ef5e0d2ccc40d5c821dee10f1ec61c2e
* Make android_get_application_target_sdk_version available to the NDK.Elliott Hughes2018-11-151-4/+0
| | | | | | | | | | | Also move this and android_get_device_api_level into <android/api-level.h> so that they're always available. This involves cleaning up <sys/cdefs.h> slightly. Bug: N/A Test: builds Change-Id: I25435c55f3549cd0d827a7581bee75ea8228028b
* Move API levels from `uint32_t` to `int`.Elliott Hughes2018-11-131-4/+4
| | | | | | | | (cherrypick of a6c71a09670ca636cca5cfea9d74b03a951e2b5e.) Bug: N/A Test: builds Change-Id: I9c414e30e3c4fe2a4e16a2fe4ce18eae85fe4844
* [hwasan] Tweak process and thread initialization.Evgenii Stepanov2018-09-201-4/+9
| | | | | | | | | | | | | Make sure that TLS_SLOT_TSAN is always available and correctly set up in HWASan-instrumented functions by setting up the tls register and running hwasan initialization (__hwasan_init in the main thread and __hwasan_thread_enter in secondary) early enough. This is needed to accomodate a change in HWASan: https://reviews.llvm.org/D52249 Bug: 112438058 Test: boot with SANITIZE_TARGET=hwaddress, run bionic-tests Change-Id: Icd909a4ea0da6c6c1095522bcc28debef5f2c63d
* HWASan support in bionic.Evgenii Stepanov2018-08-211-5/+22
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | * Allow sanitization of libc (excluding existing global sanitizers) and disallow sanitization of linker. The latter has not been necessary before because HWASan is the first sanitizer to support static binaries (with the exception of CFI, which is not used globally). * Static binary startup: initialize HWASan shadow very early so that almost entire libc can be sanitized. The rest of initialization is done in a global constructor; until that is done sanitized code can run but can't report errors (will simply crash with SIGTRAP). * Switch malloc_common from je_* to __sanitizer_*. * Call hwasan functions when entering and leaving threads. We can not intercept pthread_create when libc depends on libclang_rt.hwasan. An alternative to this would be a callback interface like requested here: https://sourceware.org/glibc/wiki/ThreadPropertiesAPI All of the above is behind a compile-time check __has_feature(hwaddress_sanitizer). This means that HWASan actually requires libc to be instrumented, and would not work otherwise. It's an implementation choice that greatly reduces complexity of the tool. Instrumented libc also guarantees that hwasan is present and initialized in every process, which allows piecemeal sanitization (i.e. library w/o main executable, or even individual static libraries), unlike ASan. Change-Id: If44c46b79b15049d1745ba46ec910ae4f355d19c
* Modernize codebase by replacing NULL with nullptrYi Kong2018-08-021-2/+2
| | | | | | | | Fixes -Wzero-as-null-pointer-constant warning. Test: m Bug: 68236239 Change-Id: I5b4123bc6709641315120a191e36cc57541349b2
* Introduce api to track fd ownership in libc.Josh Gao2018-07-191-0/+5
| | | | | | | | | | | | | Add two functions to allow objects that own a file descriptor to enforce that only they can close their file descriptor. Use them in FILE* and DIR*. Bug: http://b/110100358 Test: bionic_unit_tests Test: aosp/master boots without errors Test: treehugger Change-Id: Iecd6e8b26c62217271e0822dc3d2d7888b091a45
* Initialize __libc_sysinfo early on.Ryan Prichard2018-05-301-0/+7
| | | | | | | | | | | | | | | | | | | | | | | | | __libc_sysinfo is hidden, so accessing it doesn't require a relocated GOT. It is important not to have a relocatable initializer on __libc_sysinfo, because if it did have one, and if we initialized it before relocating the linker, then on 32-bit x86 (which uses REL rather than RELA), the relocation step would calculate the wrong addend and overwrite __libc_sysinfo with garbage. Asides: * It'd be simpler to keep the __libc_sysinfo initializer for static executables, but the loader pulls in libc_init_static (even though it uses almost none of the code in that file, like __libc_init). * The loader has called __libc_init_sysinfo three times by the time it has relocated itself. A static executable calls it twice, while libc.so calls it only once. Bug: none Test: lunch aosp_x86-userdebug ; emulator Test: adb shell /data/nativetest/bionic-unit-tests/bionic-unit-tests Test: adb shell /data/nativetest/bionic-unit-tests-static/bionic-unit-tests-static Change-Id: I5944f57847db7191608f4f83dde22b49e279e6cb
* Clean up TLS_SLOT_BIONIC_PREINIT usage a bitRyan Prichard2018-05-301-2/+1
| | | | | | | | | | | | | | | | | | | | | - It is only needed for dynamic executables, so move the initialization out of __libc_init_main_thread and just before the solib constructor calls. For static executables, the slot was initialized, then never used or cleared. Instead, leave it clear. - For static executables, __libc_init_main_thread already initialized the stack guard, so remove the redundant __init_thread_stack_guard call. - Simplify the slot access/clearing a bit in __libc_preinit. - Remove the "__libc_init_common() will change the TLS area so the old one won't be accessible anyway." comment. AFAICT, it's incorrect -- the main thread's TLS area in a dynamic executable is initialized to a static pthread_internal_t object in the linker, then reused by libc.so. Test: adb shell /data/nativetest/bionic-unit-tests/bionic-unit-tests Test: adb shell /data/nativetest/bionic-unit-tests-static/bionic-unit-tests-static Change-Id: Ie2da6f5be3ad563fa65b38eaadf8ba6ecc6a64b6
* Support getting/setting API level in static binaries.Elliott Hughes2018-04-031-1/+11
| | | | | | Bug: http://b/27917272 Test: fixes static semaphore.sem_wait_no_EINTR_in_sdk_less_equal_than_23 test Change-Id: Ifeeff20772ff0308aab9417d48671b604a3e9665
* Add directives to force stop unwinding.Christopher Ferris2017-10-051-0/+3
| | | | | | | | | | | | | | | | | | | | | On aarch64/x86/x86_64 add a macro that inserts a cfi directive that will stop unwinding. For arm, clang doesn't allow emitting .cantunwind, so add a comment and leave it the same as it current is. Add this macro to __libc_init and __start_thread. Also, remove duplicate compilation of libc_init_static.cpp that already includes the static library that includes that file. Bug: 15469122 Test: Did unwinds using new unwinder tool (unwind) and debuggerd -b Test: and verified new unwinder works on aarch64/x86/x86_64. Test: Verified that it works on old unwinder for aarch64/x86, but Test: x86_64 doesn't work properly, but as well as before. Change-Id: I77302e8f6c7ba1549d98a4a164106ee82c9ecadc
* Only initialize the global stack protector once.Josh Gao2016-06-301-0/+2
| | | | | | | | | | | | Before, dynamic executables would initialize the global stack protector twice, once for the linker, and once for the executable. This worked because the result was the same for both initializations, because it used getauxval(AT_RANDOM), which won't be the case once arc4random gets used for it. Bug: http://b/29622562 Change-Id: I7718b1ba8ee8fac7127ab2360cb1088e510fef5c Test: ran the stack protector tests on angler (32/64bit, static/dynamic)
* Revert "Revert "Make sem_wait able to return errno EINTR for sdk > 23.""Yabin Cui2016-01-251-0/+5
| | | | | | | This reverts commit 6d5108520225fd2c4b10ca63565545ec120aab4a. And add missing bionic_sdk_version.h. Change-Id: I24cc738b1fd1d26234c52afbc787f5b3c4a9c9cb
* Revert "Make sem_wait able to return errno EINTR for sdk > 23."Dan Albert2016-01-261-5/+0
| | | | | | | | Broke the build. There's no such file as bionic_sdk_version.h anywhere in the tree. This reverts commit 892b61d3409e8cdf0e121c688737eb058d57f7ab. Change-Id: Iec3f4588edfb1d1524bb5f16451fd05dc6ebe44a
* Make sem_wait able to return errno EINTR for sdk > 23.Yabin Cui2016-01-251-0/+5
| | | | | | | | | | Posix standards says sem_wait is interruptible by the delivery of a signal. To keep compatiblity with old apps, only fix that in newer sdk versions. Bug: 26743454 Change-Id: I924cbb436658e3e0f397c922d866ece99b8241a3
* Defend against -fstack-protector in libc startup.Elliott Hughes2016-01-061-12/+8
| | | | | | | | | Exactly which functions get a stack protector is up to the compiler, so let's separate the code that sets up the environment stack protection requires and explicitly build it with -fno-stack-protector. Bug: http://b/26276517 Change-Id: I8719e23ead1f1e81715c32c1335da868f68369b5
* Move VDSO pointers to a shared globals struct.Josh Gao2015-10-091-0/+5
| | | | Change-Id: I01cbc9cf0917dc1fac52d9205bda2c68529d12ef
* Move PAGE_MASK into <sys/user.h>.Elliott Hughes2015-07-281-7/+1
| | | | | | | | | I'm removing the TODO on the assumption that being compatible with glibc is more useful than BSD. The new internal "bionic_page.h" header factors out some duplication between libc and the linker. Bug: http://b/22735893 Change-Id: I4aec4dcba5886fb6f6b9290a8f85660643261321