aboutsummaryrefslogtreecommitdiff
path: root/libc/bionic/__libc_init_main_thread.cpp
Commit message (Collapse)AuthorAgeFilesLines
* Expose a function to reset the stack protector for the zygote.Elliott Hughes2020-09-111-5/+14
| | | | | | | | | This is already covered by the existing test by virtue of being used for all threads. Bug: http://b/168258494 Test: treehugger Change-Id: I5c872fd7f30a4c79de1d70e7702f4b12d4e94cd3
* Add an android_unsafe_frame_pointer_chase function.Peter Collingbourne2020-02-031-0/+1
| | | | | | | | | | This function will be used by Scudo and GWP-ASan to efficiently collect stack traces for frames built with frame pointers. Bug: 135634846 Bug: 135772972 Change-Id: Ic63efdbafe11dfbb1226b5b4b403d53c4dbf28f3 Merged-In: Ic63efdbafe11dfbb1226b5b4b403d53c4dbf28f3
* Use ifuncs in the linkerRyan Prichard2019-11-051-2/+22
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | 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)
* Name stack+tls VMAs with PR_SET_VMA_ANON_NAMERyan Prichard2019-07-231-0/+3
| | | | | | | | | | | | | | | | | | | Bionic creates a single thread mapping to hold a thread's stack and static TLS memory. Use PR_SET_VMA_ANON_NAME to name this region "stack_and_tls:tid". dumpsys meminfo can report this region as "Stack" memory. The main thread's memory is instead named stack_and_tls:main, and the VMA is renamed to stack_and_tls:main in a post-fork child. For the main thread, and threads using pthread_attr_setstack, Bionic still creates the stack_and_tls mapping, but it only has TLS memory in it. Bug: http://b/134795155 Test: run "dumpsys meminfo" and verify that this CL increases the reported stack memory usage from about 4MB to 21MB. Change-Id: Id1f39ff083329e83426130b4ef94222ffacb90ae Merged-In: Id1f39ff083329e83426130b4ef94222ffacb90ae
* Implement dynamic TLS accesses and allocationRyan Prichard2019-01-251-0/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | 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
* Initialize static TLS memory using module listRyan Prichard2019-01-161-0/+1
| | | | | | | | | This implementation simply iterates over each static TLS module and copies its initialization image into a new thread's static TLS block. Bug: http://b/78026329 Test: bionic unit tests Change-Id: Ib7edb665271a07010bc68e306feb5df422f2f9e6
* Reorganize static TLS memory for ELF TLSRyan Prichard2019-01-111-22/+44
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | 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)
* Replace TLS_SLOT_BIONIC_PREINIT w/ shared globalsRyan Prichard2018-12-041-3/+3
| | | | | | | | | | | | | | | 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/+1
| | | | | | | | | | | | | | | | 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)
* Split main thread init into early+late functionsRyan Prichard2018-12-041-10/+24
| | | | | | | | | | | | | | | | | | 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)
* bionic: Allocate a shadow call stack for each thread.Peter Collingbourne2018-11-121-1/+1
| | | | | | | | | | | | | Instead of allocating the stack within a 16MB guard region as we were doing before, just allocate the stack on its own. This isn't as secure as with the guard region (since it means that an attacker who can read the pthread_internal_t can determine the address of the SCS), but it will at least allow us to discover more blockers until a solution to b/118642754 is decided on. Bug: 112907825 Bug: 118642754 Change-Id: Ibe5dffbad1b4700eaa0e24177eea792e7c329a61
* Revert "bionic: Allocate a shadow call stack for each thread."Peter Collingbourne2018-10-291-1/+1
| | | | | | | | | | This reverts commit da1bc79f937225b1a048d9e5a03eca81680a17fd. Reason for revert: Caused OOM in media process Bug: 112907825 Bug: 118593766 Change-Id: I545663871d75889b209b9fd2131cdaa97166478f
* bionic: Allocate a shadow call stack for each thread.Peter Collingbourne2018-10-291-1/+1
| | | | | | Bug: 112907825 Change-Id: I7c1479a0cd68696739bf6aa5e0700ba4f2a137ec Merged-In: I7c1479a0cd68696739bf6aa5e0700ba4f2a137ec
* [hwasan] Tweak process and thread initialization.Evgenii Stepanov2018-09-201-3/+7
| | | | | | | | | | | | | 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
* Inline __libc_init_global_stack_chk_guard.Ryan Prichard2018-05-301-5/+1
| | | | | | | | Prior to b6453c52ac55f85d7f88f04db6e320825cea9bf7, this function was called from two C++ files. Test: boot hikey Change-Id: Ieeae6ea617c5b19e2cd76c1b37c9511a72d01597
* Clean up TLS_SLOT_BIONIC_PREINIT usage a bitRyan Prichard2018-05-301-6/+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
* The main thread should just INHERIT its scheduler attributes.Elliott Hughes2017-10-261-2/+7
| | | | | | | | | | | | | | Explicitly setting SCHED_OTHER/0 was causing SELinux denials: 02-08 09:58:05.430 661 661 I auditd : type=1400 audit(0.0:20): avc: denied { sys_nice } for comm="grep" capability=23 scontext=u:r:init-qcom-devstart-sh:s0 tcontext=u:r:init-qcom-devstart-sh:s0 tclass=capability permissive=0 02-08 09:58:05.430 662 662 I auditd : type=1400 audit(0.0:21): avc: denied { sys_nice } for comm="sed" capability=23 scontext=u:r:init-qcom-devstart-sh:s0 tcontext=u:r:init-qcom-devstart-sh:s0 tclass=capability permissive=0 Also use public pthread API rather than modifying the main thread's pthread_attr_t directly. Bug: http://b/68328561 Test: strace -f -e true Change-Id: I65b7ab3ce285a2901a6eaacb243000c780883c3a
* Merge "Implement pthread_attr_getinheritsched/pthread_attr_setinheritsched."Elliott Hughes2017-10-261-1/+0
|\
| * Implement pthread_attr_getinheritsched/pthread_attr_setinheritsched.Elliott Hughes2017-10-251-1/+0
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Historically, Android defaulted to EXPLICIT but with a special case because SCHED_NORMAL/priority 0 was awkward. Because the code couldn't actually tell whether SCHED_NORMAL/priority 0 was a genuine attempt to explicitly set those attributes (because the parent thread is SCHED_FIFO, say) or just because the pthread_attr_t was left at its defaults. Now we support INHERIT, we could call sched_getscheduler to see whether we actually need to call sched_setscheduler, but since the major cost is the fixed syscall overhead, we may as well just conservatively call sched_setscheduler and let the kernel decide whether it's a no-op. (Especially because we'd then have to add both sched_getscheduler and sched_setscheduler to any seccomp filter.) Platform code (or app code that only needs to support >= P) can actually add a call to pthread_attr_setinheritsched to say that they just want to inherit (if they know that none of their threads actually mess with scheduler attributes at all), which will save them a sched_setscheduler call except in the doubly-special case of SCHED_RESET_ON_FORK (which we do handle). An alternative would be "make pthread_attr_setschedparams and pthread_attr_setschedprio set EXPLICIT and change the platform default to INHERIT", but even though I can only think of weird pathological examples where anyone would notice that change, that behavior -- of pthread_attr_setschedparams/pthread_attr_setschedprio overriding an earlier call to pthread_attr_setinheritsched -- isn't allowed by POSIX (whereas defaulting to EXPLICIT is). If we have a lot of trouble with this change in the app compatibility testing phase, though, we'll want to reconsider this decision! -*- This change also removes a comment about setting the scheduler attributes in main_thread because we'd have to actually keep them up to date, and it's not clear that doing so would be worth the trouble. Also make async_safe_format_log preserve errno so we don't have to be so careful around it. Bug: http://b/67471710 Test: ran tests Change-Id: Idd026c4ce78a536656adcb57aa2e7b2c616eeddf
* | Make __libc_init_main_thread and android_set_abort_message weakdimitry2017-10-191-0/+2
|/ | | | | | | | Bug: http://b/67672033 Bug: http://b/67934730 Test: make && flash and boot angler. Test: run bionic-unit-tests Change-Id: Ibf47f00dd7fae19014f56c857eb9712711363029
* Improve pthread_create failure handling.Elliott Hughes2017-09-191-1/+1
| | | | | | | | | | | | | | | | | | Return EAGAIN rather than aborting if we fail to set up the TLS for a new thread. Add a test that uses all the VMAs so we can properly test these edge cases. Add an explicit test for pthread_attr_setdetachstate, which we use in the previous test, but other than that has no tests. Remove support for ro.logd.timestamp/persist.logd.timestamp, which doesn't seem to be used, and which prevents us from logging failures in cases where mmap fails (because we need to mmap in the system property implementation). Bug: http://b/65608572 Test: ran tests Change-Id: I9009f06546e1c2cc55eff996d08b55eff3482343
* Revert "Revert "Attempt to use arc4random where getauxval(AT_RANDOM) is used.""Josh Gao2016-11-101-3/+2
| | | | | | | This reverts commit 60d052dda600f26fdb6180ac1a2c53e97df5a58a. Bug: http://b/27729263 Change-Id: I7f32dc9f9a1f21fda3213f10da940821e1bdd63f
* Initialize main thread TLS before the global stack guard.Josh Gao2016-09-061-1/+3
| | | | | | | | | | | The GCE breakage caused by 78a52f19bb207d1c736f1d5362e01f338d78645 was due to TLS_SLOT_SELF being uninitialized before the use of errno by syscall. Separate stack guard initialization from TLS initialization so that stack guard initialization can make syscalls. Bug: http://b/29622562 Bug: http://b/31251721 Change-Id: Id0e4379e0efb7194a2df7bd16211ff11c6598033
* Revert "Attempt to use arc4random where getauxval(AT_RANDOM) is used."Josh Gao2016-09-021-2/+3
| | | | | | This reverts commit 78a52f19bb207d1c736f1d5362e01f338d78645f. Change-Id: Ia72501ad0b81e894d67e5ce59b597397c11acfa5
* Attempt to use arc4random where getauxval(AT_RANDOM) is used.Josh Gao2016-08-311-3/+2
| | | | | | | | | When arc4random can get entropy (which is true for basically everyone but init on kernels that don't support getrandom), use it instead of AT_RANDOM. Bug: http://b/29622562 Change-Id: I6932803af2c477e65562ff531bd959f199fad1df
* Only initialize the global stack protector once.Josh Gao2016-06-301-2/+13
| | | | | | | | | | | | 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)
* libc: implement kernel vdso syscalls for i386Mingwei Shi2016-03-251-0/+3
| | | | | | | | | | This patch uses __kernel_vsyscall instead of "int 0x80" as the syscall entry point. AT_SYSINFO points to an adapter to mask the arch specific difference and gives a performance boost on i386 architecture. Change-ID: Ib340c604d02c6c25714a95793737e3cfdc3fc5d7 Signed-off-by: Mingwei Shi <mingwei.shi@intel.com>
* Improve and fix the stack-protector tests.Elliott Hughes2016-03-091-0/+4
| | | | | Bug: http://b/26888853 Change-Id: I505dbf7d5934f7247fb639f55dd6a9341df3947b
* Defend against -fstack-protector in libc startup.Elliott Hughes2016-01-061-0/+85
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