aboutsummaryrefslogtreecommitdiff
path: root/libc/bionic/libc_init_dynamic.cpp
Commit message (Collapse)AuthorAgeFilesLines
* Fix HWASan crashes in heapprofd initialization.Florian Mayer2022-02-011-7/+9
| | | | | | | Test: atest perfetto_integrationtests on HWASan build Bug: 210588740 Change-Id: Iddaa4a7d0dedee541f97bbf0a13958de023bdf59
* Add some slack at the end of large allocations when target SDK level < S.Peter Collingbourne2021-03-051-0/+3
| | | | | | | | | | | | | | 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
* Use ELF notes to set the desired memory tagging level.Evgenii Stepanov2021-01-061-0/+1
| | | | | | | | | | | 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
* Update a few comments.Elliott Hughes2020-02-101-6/+1
| | | | | Test: N/A Change-Id: Id5e21eaff9bc43256b8f54e708dfc862ee7e763a
* allow for heapprofd's signal to be multiplexedRyan Savitski2020-01-151-0/+3
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | This patch refactors heapprofd_malloc to make it easier to reuse the reserved signal for multiple purposes. We define a new generic signal handler for profilers, which dispatches to more specific logic based on the signal's payload (si_value). The profiler signal handler is installed during libc preinit, after malloc initialization (so races against synchronous heapprofd initialization need not be considered). In terms of code organization, I copied the existing approach with a loosely referenced function in bionic_globals.h. Do tell if you'd rather a different approach here. The profileability of a process is quite tied to the malloc files/interfaces in bionic - in particular, it's set through android_mallopt. I do not change that, but instead introduce a new android_mallopt option to be able to query profileability of the process (which is now used by the new profiler signal handler). As part of that, gZygoteChildProfileable is moved from heapprofd_malloc to common (alongside gZygoteChild). I've removed the masking and reraising of the heapprofd signal when racing against malloc_limit init. We're ok with taking a simpler approach and dropping the heapprofd signal in such an unlikely race. Note: this requires a corresponding change in heapprofd to use sigqueue() instead of kill(), as the latter leaves the si_value uninitialized(?) on the receiving side. Bug: 144281346 Change-Id: I93bb2e82cff5870e5ca499cf86439860aca9dfa5
* Move bionic_macros.h from private to platform.Josh Gao2020-01-021-1/+1
| | | | | Test: treehugger Change-Id: Ie473914f4c8924c7240b3ac22093a9daf42fc948
* Init fork handler after __libc_init_malloc.Mitch Phillips2019-11-141-0/+2
| | | | | | | | | | 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
* linker: Add library load and unload hooks for HWASan.Peter Collingbourne2019-08-051-0/+15
| | | | | | | | | These hooks notify the HWASan runtime library whenever a library is loaded or unloaded so that it can update its shadow memory. Bug: 138159093 Test: walleye_hwasan-userdebug boots with+without https://reviews.llvm.org/D65770 Change-Id: I6caf2a6540ed2c0d94db444e806a3c7ba504cabb
* Fix linker self-exec detectionRyan Prichard2019-02-071-3/+0
| | | | | | | | | | | | | | | | | When the linker is invoked on itself, (`linker64 /system/bin/linker64`), the linker prints an error, because self-invocation isn't allowed. The current method for detecting self-invocation fails because the second linker instance can crash in a constructor function before reaching __linker_init. Fix the problem by moving the error check into a constructor function, which finishes initializing libc sufficiently to call async_safe_fatal. The only important thing missing is __libc_sysinfo on 32-bit x86. The aux vector isn't readily accessible, so use the fallback int 0x80. Bug: http://b/123637025 Test: bionic unit tests (32-bit x86) Change-Id: I8be6369e8be3938906628ae1f82be13e6c510119
* Implement dynamic TLS accesses and allocationRyan Prichard2019-01-251-0/+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
* Replace TLS_SLOT_BIONIC_PREINIT w/ shared globalsRyan Prichard2018-12-041-11/+5
| | | | | | | | | | | | | | | 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)
* Use shared globals so getauxval works earlierRyan Prichard2018-12-041-2/+0
| | | | | | | | | | | | | | | | Make getauxval() work in .preinit_array. It still won't be usable for ifuncs unless we can guarantee that the __loader_shared_globals relocation is resolved before the ifunc calls [__bionic_]getauxval. Define __bionic_getauxval for use in replacing calls to KernelArgumentBlock::getauxval, which doesn't (and sometimes isn't allowed to) access TLS variables like errno. Bug: http://b/25751302 Test: bionic unit tests Change-Id: I461feeaed7f43cfa2a2b6c34147194f0df82b516 Merged-In: I461feeaed7f43cfa2a2b6c34147194f0df82b516 (cherry picked from commit bdab4a2b97c53af0205788875342ec08e6901376)
* Use shared globals to init __progname + environRyan Prichard2018-11-281-1/+1
| | | | | | | | | | | | | | | | | 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
* Expose libc_shared_globals to libc.so with symbolRyan Prichard2018-11-281-3/+8
| | | | | | | | | | | | | | | | | | 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
* Cleanup: __libc_init_AT_SECURE, auxv, sysinfoRyan Prichard2018-11-261-1/+6
| | | | | | | | | | | | | | | | | | | | | __sanitize_environment_variables is only called when getauxval(AT_SECURE) is true. Instead of scanning __libc_auxv, reuse getauxval. If the entry is missing, getauxval will set errno to ENOENT. Reduce the number of times that __libc_sysinfo and __libc_auxv are initialized. (Previously, __libc_sysinfo was initialized 3 times for the linker's copy). The two variables are initialized in these places: - __libc_init_main_thread for libc.a (including the linker copy) - __libc_preinit_impl for libc.so - __linker_init: the linker's copy of __libc_sysinfo is still initialized twice, because __libc_init_main_thread runs after relocation. A later CL consolidates the linker's two initializations. Bug: none Test: bionic unit tests Change-Id: I196f4c9011b0d803ee85c07afb415fcb146f4d65
* Make android_get_application_target_sdk_version available to the NDK.Elliott Hughes2018-11-151-6/+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-2/+2
| | | | | | | | (cherrypick of a6c71a09670ca636cca5cfea9d74b03a951e2b5e.) Bug: N/A Test: builds Change-Id: I9c414e30e3c4fe2a4e16a2fe4ce18eae85fe4844
* Allow invoking the linker on an executable.Ryan Prichard2018-10-101-1/+3
| | | | | | | | | | | | | | | | | | | | | | | | | The executable can be inside a zip file using the same syntax used for shared objects: path.zip!/libentry.so. The linker currently requires an absolute path. This restriction could be loosened, but it didn't seem important? If it allowed non-absolute paths, we'd need to decide how to handle: - foo/bar (relative to CWD?) - foo (search PATH / LD_LIBRARY_PATH, or also relative to CWD?) - foo.zip!/bar (normalize_path() requires an absolute path) The linker adjusts the argc/argv passed to main() and to constructor functions to hide the initial linker argument, but doesn't adjust the auxv vector or files like /proc/self/{exe,cmdline,auxv,stat}. Those files will report that the kernel loaded the linker as an executable. I think the linker_logger.cpp change guarding against (g_argv == NULL) isn't actually necessary, but it seemed like a good idea given that I'm delaying initialization of g_argv until after C++ constructors have run. Bug: http://b/112050209 Test: bionic unit tests Change-Id: I846faf98b16fd34218946f6167e8b451897debe5
* Modernize codebase by replacing NULL with nullptrYi Kong2018-08-021-1/+1
| | | | | | | | 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/+2
| | | | | | | | | | | | | 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
* Remove valgrind workarounds.Elliott Hughes2018-06-121-0/+3
| | | | | | Bug: http://b/77856586 Test: ran 32-bit x86 tests on host Change-Id: I98f6262bb1363ed5c8dd533b1f89b59b2b188525
* Initialize __libc_sysinfo early on.Ryan Prichard2018-05-301-0/+6
| | | | | | | | | | | | | | | | | | | | | | | | | __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-10/+6
| | | | | | | | | | | | | | | | | | | | | - 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
* Fix LP32 large pid detection.Elliott Hughes2017-10-231-5/+8
| | | | | | Bug: http://b/68046352 Test: ran tests Change-Id: I89cb99173ca77e9457e677187430b61cedb55c04
* Add directives to force stop unwinding.Christopher Ferris2017-10-051-0/+2
| | | | | | | | | | | | | | | | | | | | | 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
* Disable stack protector for __libc_preinitStephen Crane2017-06-211-6/+16
| | | | | | | | | | | | | __libc_preinit sets up the stack protector global cookie value, and thus cannot intialize a stack protector cookie for itself in the function prologue. LTO compilation can inline functions requiring a stack protector into __libc_preinit. This patch disables stack protection for __libc_preinit and forces all potentially inlined functions into a helper that can have a stack protector. Test: run bionic-unit-tests Change-Id: I45911611190f216c91eb6feff722967214c5f99f
* Only initialize the global stack protector once.Josh Gao2016-06-301-0/+5
| | | | | | | | | | | | 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/+6
| | | | | | | This reverts commit 6d5108520225fd2c4b10ca63565545ec120aab4a. And add missing bionic_sdk_version.h. Change-Id: I24cc738b1fd1d26234c52afbc787f5b3c4a9c9cb
* Merge "Revert "Make sem_wait able to return errno EINTR for sdk > 23.""Elliott Hughes2016-01-261-6/+0
|\
| * Revert "Make sem_wait able to return errno EINTR for sdk > 23."Dan Albert2016-01-261-6/+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
* | Merge "Make sem_wait able to return errno EINTR for sdk > 23."Yabin Cui2016-01-251-0/+6
|\|
| * Make sem_wait able to return errno EINTR for sdk > 23.Yabin Cui2016-01-251-0/+6
| | | | | | | | | | | | | | | | | | | | 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
* | Malloc debug rewrite.Christopher Ferris2016-01-251-6/+0
|/ | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | The major components of the rewrite: - Completely remove the qemu shared library code. Nobody was using it and it appears to have broken at some point. - Adds the ability to enable/disable different options independently. - Adds a new option that can enable the backtrace on alloc/free when a process gets a specific signal. - Adds a new way to enable malloc debug. If a special property is set, and the process has an environment variable set, then debug malloc will be enabled. This allows something that might be a derivative of app_process to be started with an environment variable being enabled. - get_malloc_leak_info() used to return one element for each pointer that had the exact same backtrace. The new version returns information for every one of the pointers with same backtrace. It turns out ddms already automatically coalesces these, so the old method simply hid the fact that there where multiple pointers with the same amount of backtrace. - Moved all of the malloc debug specific code into the library. Nothing related to the malloc debug data structures remains in libc. - Removed the calls to the debug malloc cleanup routine. Instead, I added an atexit call with the debug malloc cleanup routine. This gets around most problems related to the timing of doing the cleanup. The new properties and environment variables: libc.debug.malloc.options Set by option name (such as "backtrace"). Setting this to a bad value will cause a usage statement to be printed to the log. libc.debug.malloc.program Same as before. If this is set, then only the program named will be launched with malloc debug enabled. This is not a complete match, but if any part of the property is in the program name, malloc debug is enabled. libc.debug.malloc.env_enabled If set, then malloc debug is only enabled if the running process has the environment variable LIBC_DEBUG_MALLOC_ENABLE set. Bug: 19145921 Change-Id: I7b0e58cc85cc6d4118173fe1f8627a391b64c0d7
* Defend against -fstack-protector in libc startup.Elliott Hughes2016-01-061-0/+1
| | | | | | | | | 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 malloc dispatch table to __libc_globals.Josh Gao2015-10-121-2/+2
| | | | Change-Id: Ic20b980d1e8b6c2d4b773ebe336658fd17c737cb
* Move VDSO pointers to a shared globals struct.Josh Gao2015-10-091-0/+1
| | | | Change-Id: I01cbc9cf0917dc1fac52d9205bda2c68529d12ef
* Revert "Register __libc_fini as early as possible."Dmitriy Ivanov2014-09-041-9/+9
| | | | | | This reverts commit e880c736d6c1d947f6309d5f1f63c74e8345c6a6. Change-Id: Ide83e442eb5dbfef5298a15bc602c3fe1dda1862
* Register __libc_fini as early as possible.Dmitriy Ivanov2014-09-031-9/+9
| | | | | | | We want __libc_fini to be called after all the destructors. Bug: 14611536 Change-Id: Ibb83a94436795ec178fd605fa531ac29608f4a3e
* Upstream atexitDmitriy Ivanov2014-07-141-1/+1
| | | | Change-Id: Ia454a2181b5058ed9783dc02b6b1805d0e4d2715
* Turn on -Wunused and fix the mistakes it uncovers.Elliott Hughes2014-06-031-1/+1
| | | | Change-Id: I023d2d8b547fbc21d4124bb7510d42b06a0dc501
* Remove the broken pthread deadlock prediction.Elliott Hughes2014-05-141-5/+1
| | | | | | | This hasn't built in over one release cycle and no one even noticed. art does this the right way and other projects should do the same. Change-Id: I7d1fb84c4080e008f329ee73e209ce85a36e6d55
* Introduce netd_client, a dynamic library that talks to netd.Sreeram Ramachandran2014-05-131-0/+4
| | | | | | | | | | | The library exists outside bionic. It is dynamically loaded, to replace selected standard socket syscalls with versions that talk to netd. Change connect() to use the library if available. (cherry picked from commit 3a6b627a14df8111b03e452f2df4b5f4938e0e49) Change-Id: Ib6198e19dbc306521a26fcecfdf6e8424d163fc9
* Fix x86_64 build, clean up intermediate libraries.Elliott Hughes2013-10-091-2/+3
| | | | | | | | | | | | | | | | | | | | | | The x86_64 build was failing because clone.S had a call to __thread_entry which was being added to a different intermediate .a on the way to making libc.so, and the linker couldn't guarantee statically that such a relocation would be possible. ld: error: out/target/product/generic_x86_64/obj/STATIC_LIBRARIES/libc_common_intermediates/libc_common.a(clone.o): requires dynamic R_X86_64_PC32 reloc against '__thread_entry' which may overflow at runtime; recompile with -fPIC This patch addresses that by ensuring that the caller and callee end up in the same intermediate .a. While I'm here, I've tried to clean up some of the mess that led to this situation too. In particular, this removes libc/private/ from the default include path (except for the DNS code), and splits out the DNS code into its own library (since it's a weird special case of upstream NetBSD code that's diverged so heavily it's unlikely ever to get back in sync). There's more cleanup of the DNS situation possible, but this is definitely a step in the right direction, and it's more than enough to get x86_64 building cleanly. Change-Id: I00425a7245b7a2573df16cc38798187d0729e7c4
* Fix __errno for LP64 and clean up __get_tls.Elliott Hughes2013-10-091-1/+1
| | | | | | | | | | | If __get_tls has the right type, a lot of confusing casting can disappear. It was probably a mistake that __get_tls was exposed as a function for mips and x86 (but not arm), so let's (a) ensure that the __get_tls function always matches the macro, (b) that we have the function for arm too, and (c) that we don't have the function for any 64-bit architecture. Change-Id: Ie9cb989b66e2006524ad7733eb6e1a65055463be
* Hide various symbols that shouldn't be exposed.Elliott Hughes2013-03-151-4/+2
| | | | | | | A mangled symbol in libc.so is a symbol that shouldn't be exported by libc.so. Change-Id: Id92d1e1968b3d11d111a5d9ef692adb1ac7694a1
* Clean up the argc/argv/envp/auxv handling.Elliott Hughes2013-02-071-0/+115
There's now only one place where we deal with this stuff, it only needs to be parsed once by the dynamic linker (rather than by each recipient), and it's now easier for us to get hold of auxv data early on. Change-Id: I6314224257c736547aac2e2a650e66f2ea53bef5