History log of /bionic/libc/bionic/pthread_create.cpp
Revision Date Author Comments
38f01e05ef7f7ef18a43339436fba645d123b838 28-Oct-2017 Elliott Hughes <enh@google.com> Preserve historical pthread_create scheduler behavior better.

At the cost of two flag bits for what POSIX thinks should be a boolean
choice, plus somewhat confusing behavior from pthread_attr_getinheritsched
depending on when you call it/what specific scheduler attributes you've
set in the pthread_attr_t, we can emulate the old behavior exactly and
prevent annoying SELinux denial spam caused by calls to sched_setscheduler.

Bug: http://b/68391226
Test: adb logcat on boot contains no sys_nice avc denials
Change-Id: I4f759c2c4fd1d80cceb0912d7da09d35902e2e5e
fa432524a66e5797874ef50e4ede95ded4cee199 25-Oct-2017 dimitry <dimitry@google.com> Mark __BIONIC_WEAK_FOR_NATIVE_BRIDGE symbols

To make it easier for Native Bridge implementations
to override these symbols.

Bug: http://b/67993967
Test: make
Change-Id: I4c53e53af494bca365dd2b3305ab0ccc2b23ba44
8aecba7aa6b7f7b92f69c0d3febef59fdb135f87 18-Oct-2017 Elliott Hughes <enh@google.com> Implement pthread_attr_getinheritsched/pthread_attr_setinheritsched.

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
a613d0df5c682617380728a7d151483de05a4f92 06-Oct-2017 Dan Albert <danalbert@google.com> Add a legacy inline for mmap64.

While this was never an inline, this function alone has caused most of
the bug reports related to _FILE_OFFSET_BITS=64. Providing an inline
for it should allow a lot more code to build with _FILE_OFFSET_BITS=64
when targeting pre-L.

Test: make checkbuild
Test: built trivial cc_binary for LP32 against API 14 with
_FILE_OFFSET_BITS=64 set
Bug: lots
Change-Id: I8479d34af4da358c11423bee43d45b59e9d4143e
854556c41e20c37b210498b0374415b640104785 19-Sep-2017 Elliott Hughes <enh@google.com> Improve pthread_create failure handling.

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).

(cherry picked from commit 53dc9dd70155fd75af744cbebecc563658c69818)

Bug: http://b/65608572
Test: ran tests
Change-Id: I881029d9fd368b8556843e70fdb278fbe51dbfee
Merged-In: I881029d9fd368b8556843e70fdb278fbe51dbfee
f2a1b12f2e689c2ef116c955f42c640f5c344487 26-Sep-2017 Elliott Hughes <enh@google.com> Include strerror(errno) in __init_tls abort messages.

Bug: http://b/66911122
Bug: 64709603 (presubmit balking at the line above)
Test: ran tests
Change-Id: I068a388f8ea31012e7e07e8bded1c113796a8440
Merged-In: I068a388f8ea31012e7e07e8bded1c113796a8440
53dc9dd70155fd75af744cbebecc563658c69818 19-Sep-2017 Elliott Hughes <enh@google.com> Improve pthread_create failure handling.

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
d6c678ca90d6f7843a9186515fa38d9eec467ff6 28-Jun-2017 Elliott Hughes <enh@google.com> Support larger guard regions.

This also fixes a long-standing bug where the guard region would be taken
out of the stack itself, rather than being -- as POSIX demands -- additional
space after the stack. Historically a 128KiB stack with a 256KiB guard would
have given you an immediate crash.

Bug: http://b/38413813
Test: builds, boots
Change-Id: Idd12a3899be1d92fea3d3e0fa6882ca2216bd79c
7b0af7ad82fcf88e800d1a553d81fda29dc064bd 16-Sep-2017 Elliott Hughes <enh@google.com> Always log errno when aborting.

(Where errno is relevant.)

Also consistently use -1 as the fd for anonymous mmaps. (It doesn't matter,
but it's more common, and potentially more intention-revealing.)

Bug: http://b/65608572
Test: ran tests
Change-Id: Ie9a207632d8242f42086ba3ca862519014c3c102
6de60874aa4b2525df6acf93dbd00c048b464799 14-Aug-2017 dimitry <dimitry@google.com> Report correct errno on clone failure

Test: make
Change-Id: Id0af3678627c06167a6d434d8616c4a304e1fbc0
dfcb82d92d00c7f1516477b19da97c1d2b07f0b4 12-May-2017 Elliott Hughes <enh@google.com> Clean up __isthreaded.

__isthreaded is annoying for ARC++ and useless for everyone. Just hard-code
the value in ndk_cruft for LP32 and be done with it.

Bug: N/A
Test: builds
Change-Id: I08f11a404bbec55ed57cb1e18b5116163c7d7d13
7a3681e5b6c39bc2b3b62031ca5941dbf7bc4e63 25-Apr-2017 Christopher Ferris <cferris@google.com> Move libc_log code into libasync_safe.

This library is used by a number of different libraries in the system.
Make it easy for platform libraries to use this library and create
an actual exported include file.

Change the names of the functions to reflect the new name of the library.

Run clang_format on the async_safe_log.cpp file since the formatting is
all over the place.

Bug: 31919199

Test: Compiled for angler/bullhead, and booted.
Test: Ran bionic unit tests.
Test: Ran the malloc debug tests.
Change-Id: I8071bf690c17b0ea3bc8dc5749cdd5b6ad58478a
5450f86b31287173278513eb8b4c8e73b9fdd3ae 08-Mar-2017 Josh Gao <jmgao@google.com> Fix leak of bionic TLS when threads are detached.

__pthread_internal_free doesn't happen on threads that are detached,
causing the bionic TLS allocation (and guard pages) to be leaked.

Fix the leak, and name the allocations to make things apparent if this
ever happens again.

Bug: http://b/36045112
Test: manually ran a program that detached empty threads
Change-Id: Id1c7852b7384474244f7bf5a0f7da54ff962e0a1
5e2285d3ccdbb64a49ad2e5e521f50c897a3954d 22-Feb-2017 Josh Gao <jmgao@google.com> Allocate thread local buffers in __init_tls.

Thread local buffers were using pthread_setspecific for storage with
lazy initialization. pthread_setspecific shares TLS slots between the
linker and libc.so, so thread local buffers being initialized in a
different order between libc.so and the linker meant that bad things
would happen (manifesting as snprintf not working because the
locale was mangled)

Bug: http://b/20464031
Test: /data/nativetest64/bionic-unit-tests/bionic-unit-tests
everything passes
Test: /data/nativetest/bionic-unit-tests/bionic-unit-tests
thread_local tests are failing both before and after (KUSER_HELPERS?)
Test: /data/nativetest64/bionic-unit-tests-static/bionic-unit-tests-static
no additional failures
Change-Id: I9f445a77c6e86979f3fa49c4a5feecf6ec2b0c3f
11859d467c035081f8acafeaf0184b670cd41c48 14-Feb-2017 Elliott Hughes <enh@google.com> Be more strict about using invalid `pthread_t`s.

Another release, another attempt to remove the global thread list.

But this time, let's admit that it's not going away. We can switch to using
a read/write lock for the global thread list, and to aborting rather than
quietly returning ESRCH if we're given an invalid pthread_t.

This change affects pthread_detach, pthread_getcpuclockid,
pthread_getschedparam/pthread_setschedparam, pthread_join, and pthread_kill:
instead of returning ESRCH when passed an invalid pthread_t, if you're
targeting O or above, they'll abort with the message "attempt to use
invalid pthread_t".

Note that this doesn't change behavior as much as you might think: the old
lookup only held the global thread list lock for the duration of the lookup,
so there was still a race between that and the dereference in the caller,
given that callers actually need the tid to pass to some syscall or other,
and sometimes update fields in the pthread_internal_t struct too.

(This patch replaces such users with calls to pthread_gettid_np, which
at least makes the TOCTOU window smaller.)

We can't check thread->tid against 0 to see whether a pthread_t is still
valid because a dead thread gets its thread struct unmapped along with its
stack, so the dereference isn't safe.

Taking the affected functions one by one:

* pthread_getcpuclockid and pthread_getschedparam/pthread_setschedparam
should be fine. Unsafe calls to those seem highly unlikely.

* Unsafe pthread_detach callers probably want to switch to
pthread_attr_setdetachstate instead, or using
pthread_detach(pthread_self()) from the new thread's start routine
rather than doing the detach in the parent.

* pthread_join calls should be safe anyway, because a joinable thread
won't actually exit and unmap until it's joined. If you're joining an
unjoinable thread, the fix is to stop marking it detached. If you're
joining an already-joined thread, you need to rethink your design.

* Unsafe pthread_kill calls aren't portably fixable. (And are obviously
inherently non-portable as-is.) The best alternative on Android is to
use pthread_gettid_np at some point that you know the thread to be
alive, and then call kill/tgkill directly.

That's still not completely safe because if you're too late, the tid
may have been reused, but then your code is inherently unsafe anyway.

Bug: http://b/19636317
Test: ran tests
Change-Id: I0372c4428e8a7f1c3af5c9334f5d9c25f2c73f21
7484c21c4c352a2200d94939fabc10d1bd3f0723 02-Feb-2017 Elliott Hughes <enh@google.com> Revert "Remove the global thread list."

This reverts commit b0e8c565a622b5519e03d4416b0b5b1a5f20d7f5.

Breaks swiftshader (http:/b/34883464).

Change-Id: I7b21193ba8a78f07d7ac65e41d0fe8516940a83b
b0e8c565a622b5519e03d4416b0b5b1a5f20d7f5 04-Jan-2017 Elliott Hughes <enh@google.com> Remove the global thread list.

Another release, another attempt to fix this bug.

This change affects pthread_detach, pthread_getcpuclockid,
pthread_getschedparam/pthread_setschedparam, pthread_join, and pthread_kill:
instead of returning ESRCH when passed an invalid pthread_t, they'll now SEGV.

Note that this doesn't change behavior as much as you might think: the old
lookup only held the global thread list lock for the duration of the lookup,
so there was still a race between that and the dereference in the caller,
given that callers actually need the tid to pass to some syscall or other,
and sometimes update fields in the pthread_internal_t struct too.

We can't check thread->tid against 0 to see whether a pthread_t is still
valid because a dead thread gets its thread struct unmapped along with its
stack, so the dereference isn't safe.

Taking the affected functions one by one:

* pthread_getcpuclockid and pthread_getschedparam/pthread_setschedparam
should be fine. Unsafe calls to those seem highly unlikely.

* Unsafe pthread_detach callers probably want to switch to
pthread_attr_setdetachstate instead, or using pthread_detach(pthread_self())
from the new thread's start routine rather than doing the detach in the
parent.

* pthread_join calls should be safe anyway, because a joinable thread won't
actually exit and unmap until it's joined. If you're joining an
unjoinable thread, the fix is to stop marking it detached. If you're
joining an already-joined thread, you need to rethink your design.

* Unsafe pthread_kill calls aren't portably fixable. (And are obviously
inherently non-portable as-is.) The best alternative on Android is to
use pthread_gettid_np at some point that you know the thread to be alive,
and then call kill/tgkill directly. That's still not completely safe
because if you're too late, the tid may have been reused, but then your
code is inherently unsafe anyway.

If we find too much code is still broken, we can come back and disable
the global thread list lookups for anything targeting >= O and then have
another go at really removing this in P...

Bug: http://b/19636317
Test: N6P boots, bionic tests pass
Change-Id: Ia92641212f509344b99ee2a9bfab5383147fcba6
cb728e6f63e90405223e2a72f119967368f78b7a 15-Sep-2016 Josh Gao <jmgao@google.com> Fix instances of '#if __i386__'.

Triggers -Wundef, which is on in -Weverything.

Bug: http://b/31496165
Change-Id: Ie2241b19abd6257bebf68baa3ecc4de8066c722e
b36efa4343d79e3fb548d12d039193850246b892 15-Sep-2016 Josh Gao <jmgao@google.com> Fix instances of '#if __LP64__'.

Triggers -Wundef, which is on in -Weverything.

Bug: http://b/31496165
Change-Id: Ib06107073f7dd1d584c19c222d0430da9d35630b
4159e866ab7b2397e714d7a1b309c5e6719c4620 02-Sep-2016 Josh Gao <jmgao@google.com> Initialize main thread TLS before the global stack guard.

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
b6453c52ac55f85d7f88f04db6e320825cea9bf7 30-Jun-2016 Josh Gao <jmgao@google.com> Only initialize the global stack protector once.

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)
01b85d5df3744449471b327d7afd6c1d5854bd11 10-Feb-2016 Elliott Hughes <enh@google.com> Set x86 TLS limit to 0xfffff, not PAGE_SIZE.

Not least because we set limit_in_pages to 1. PAGE_SIZE pages was never
anyone's intention.

Change-Id: Ide867f44a2fb20d4d5d0cd67ced468e8665a0193
304348af197f30b3bf0e0764b97eb9699a376c68 03-Dec-2015 Yabin Cui <yabinc@google.com> Clear pthread_internal_t allocated on user provided stack.

Several parts in pthread_internal_t should be initialized
to zero, like tls, key_data and thread_local_dtors. So
just clear the whole pthread_internal_t is more convenient.

Bug: 25990348
Change-Id: Ibb6d1200ea5e6e1afbc77971f179197e8239f6ea
d26e780df66b9add4cf7e7ebb2f6c6749d1c5050 23-Oct-2015 Yabin Cui <yabinc@google.com> Use bionic lock in pthread_internal_t.

It removes calling to pthread_mutex_lock() at the beginning of new
thread, which helps to support thread sanitizer.

Change-Id: Ia3601c476de7976a9177b792bd74bb200cee0e13
eeb6b57e750220edc0b489fae11837bc73fa7a45 02-Jul-2015 Elliott Hughes <enh@google.com> Name the thread stack and signal stack guard pages.

Bug: http://b/22228722
Change-Id: I1dae672e386e404fb304a34496a29fe21134c784
ef115003012f61cf5539fdfeb201b98e4a92f610 31-Mar-2015 Yabin Cui <yabinc@google.com> Revert "Revert "add guard pages to the internal signal stacks""

This reverts commit a3125fd1396a09a7fc4872dc4653f342150a3deb.
And Fix the prctl() problem that cause system crash.

Change-Id: Icc8d12d848cfba881a7984ca2827fd81be41f9fd
a3125fd1396a09a7fc4872dc4653f342150a3deb 31-Mar-2015 Elliott Hughes <enh@google.com> Revert "add guard pages to the internal signal stacks"

This reverts commit 595752f623ae88f7e4193a6e531a0805f1c6c4dc.

Change-Id: Iefa66e9049ca0424e53cd5fc320d161b93556dcb
3925f32ffb1ac93fb20b0e4ec86aa600eb942645 31-Mar-2015 Elliott Hughes <enh@google.com> Revert "Fix clang build breakage ("arithmetic on a pointer to void")."

This reverts commit 16c77212792808b9e4d8229e64c5b42f4327b6dc.

Change-Id: I568dee5400599693b1585ce6d4be7a0b5f37dc74
16c77212792808b9e4d8229e64c5b42f4327b6dc 30-Mar-2015 Elliott Hughes <enh@google.com> Fix clang build breakage ("arithmetic on a pointer to void").

Change-Id: Ia0953fc1cd0f8ea2d4423b3c6e34f6dc7a9f31e9
595752f623ae88f7e4193a6e531a0805f1c6c4dc 29-Mar-2015 Daniel Micay <danielmicay@gmail.com> add guard pages to the internal signal stacks

Signal handlers tend to be lean, but can still overflow the (tiny)
stack.

Change-Id: Ia21c6453d92a9f8d1536ad01ff26a1a84c05f8fb
799cb35f45a161de96b272de38724f77e988f5f3 26-Mar-2015 Yabin Cui <yabinc@google.com> Fix in error handling in pthread_create.cpp.

It is due to a previous change "Let g_thread_list_lock only protect g_thread_list".
We need to add the newly created thread to thread_list even if
__init_thread fails, so the thread can exit successfully.

Change-Id: I0332df11acfdd181350bcc092b12d90d679057a4
673b15e4ee2c6d99b150aedddc0f389e29f98e1b 19-Mar-2015 Yabin Cui <yabinc@google.com> Let g_thread_list_lock only protect g_thread_list.

As glibc/netbsd don't protect access to thread struct members by a global
lock, we don't want to do it either. This change reduces the
responsibility of g_thread_list_lock to only protect g_thread_list.

Bug: 19636317
Change-Id: I897890710653dac165d8fa4452c7ecf74abdbf2b
a2db50d5d7fa67b297eddd1c0549f08ea4b6a950 20-Mar-2015 Yabin Cui <yabinc@google.com> Fix alignment error for pthread_internal_t/pthread stack.

aligned attribute can only control compiler's behavior, but we
are manually allocating pthread_internal_t. So we need to make
sure of alignment manually.

Change-Id: Iea4c46eadf10dfd15dc955c5f41cf6063cfd8536
58cf31b50699ed9f523de38c8e943f3bbd1ced9e 07-Mar-2015 Yabin Cui <yabinc@google.com> Make pthread join_state not protected by g_thread_list_lock.

1. Move the representation of thread join_state from pthread.attr.flag
to pthread.join_state. This clarifies thread state change.
2. Use atomic operations for pthread.join_state. So we don't need to
protect it by g_thread_list_lock. g_thread_list_lock will be reduced
to only protect g_thread_list or even removed in further changes.

Bug: 19636317
Change-Id: I31fb143a7c69508c7287307dd3b0776993ec0f43
5e2bd719d7dd19afe55f8d4f24366c0230e0e6c7 21-Feb-2015 Yabin Cui <yabinc@google.com> Refactor pthread_key.cpp to be lock-free.

Change-Id: I20dfb9d3cdc40eed10ea12ac34f03caaa94f7a49
05fc1d7050d5451aea08dc5f504d2670287b2d43 29-Jan-2015 Elliott Hughes <enh@google.com> Add missing includes.

Change-Id: Ibf549266a19a67eb9158d341a69dddfb654be669
8b5df3920f2843c9cdf04160517c1e8b77c992f5 22-Jan-2015 Elliott Hughes <enh@google.com> Turn on -Wold-style-cast and fix the errors.

A couple of dodgy cases where we cast away const, but otherwise pretty boring.

Change-Id: Ibc39ebd525377792b5911464be842121c20f03b9
917d390510e442b9b030d54992ebf41cc1e7f853 08-Jan-2015 Yabin Cui <yabinc@google.com> Make pthread stack size match real range.

Bug: 18908062
Change-Id: I7037ac8273ebe54dd19b1561c7a376819049124c
ba8dfc2669d658dc340eb8f9c9b40ca074f05047 06-Jan-2015 Yabin Cui <yabinc@google.com> Remove PTHREAD_ATTR_FLAG_USER_ALLOCATED_STACK.

Patch for https://android-review.googlesource.com/#/c/120844/.

Change-Id: Idca5ccd7b28e8f07f1d2d1b6e3bba6781b62f0e0
6a7aaf46759db32c6ed0eb953a4a230dc96af0d9 23-Dec-2014 Yabin Cui <yabinc@google.com> Reserve enough user request stack space in pthread_create.

Bug: 18830897
Change-Id: I1ba4aaeaf66a7ff99c5d82ad45469011171b0a3b
8cf1b305670123aed7638d984ca39bfd22388440 04-Dec-2014 Yabin Cui <yabinc@google.com> Use mmap to create the pthread_internal_t

Add name to mmaped regions.
Add pthread benchmark code.
Allocate pthread_internal_t on regular stack.

Bug: 16847284
Change-Id: Id60835163bb0d68092241f1a118015b5a8f85069
6c238f2926e69a950f0671ae5519584c20d84196 12-Dec-2014 Yabin Cui <yabinc@google.com> Fix pthread key num calculation.

Bug: 18723085
Change-Id: Iba2c834b350e4cdba0b2d771b221560a3e5df952
8574a0670bf698b8d2f817098f9d825c4be03a68 02-Dec-2014 Yabin Cui <yabinc@google.com> Use mmap to create the pthread_internal_t.

Bug: 16847284
Change-Id: I488fa236f57aa3acb29b4ffbbab2fab51b0653be
5b8ceff5f87889e781c13305767e140afd28eb76 26-Nov-2014 Nicolas Geoffray <ngeoffray@google.com> Revert "Use mmap to create the pthread_internal_t."

Unfortunately, this change provokes random crashes for ART, and
I have seen libc crashes on the device that might be related to it.

Reverting it fixes the ART crashes. there is unfortunately no
stack trace for the crashes, but just a "Segmentation fault" message.


This reverts commit cc5f6543e3f91385b9a912438965b7e8265df54a.

Change-Id: I68dca8e1e9b9edcce7eb84596e8db619e40e8052
cc5f6543e3f91385b9a912438965b7e8265df54a 25-Nov-2014 Yabin Cui <yabinc@google.com> Use mmap to create the pthread_internal_t.

Bug: 16847284
Change-Id: Ic8c85f95afac1d8422ecb69674c688d1fecb6a44
11bf8a3025a7b5aee891c521255a7db1860e5b12 30-Jul-2014 Elliott Hughes <enh@google.com> Only wipe TLS for user-supplied stacks.

Bug: 16667988

(cherry picked from commit 40a521744825b6060960c296d5fb3da4c6593d94)

Change-Id: I7550fa47b76e643323aa3e2a53529e393c829e47
40a521744825b6060960c296d5fb3da4c6593d94 30-Jul-2014 Elliott Hughes <enh@google.com> Only wipe TLS for user-supplied stacks.

Bug: 16667988
Change-Id: Id180ab2bc6713e1612386120a306db5bbf1d6046
e959a3b3157e38ebda4ce6f86efb5644faa4988b 10-Jul-2014 Elliott Hughes <enh@google.com> Remove the global lock around thread stack creation.

This lock has been here since the original commits, but as far as I can tell
it never served any purpose. We've never had a free list of cached stacks or
anything like that.

Change-Id: I9d665c7eaa9c699ce0659ffb111402a0239fe1f5
e0f25dda3f38a70e5689e785597c5cf2b68bdcf8 10-Jul-2014 Elliott Hughes <enh@google.com> Remove the obsolete _thread_created_hook.

gdb won't even try to use this on Android because it knows we don't
support old enough kernels to need it.

Bug: 15470251
Change-Id: Ia6d54585d888bbab8ee0490a148a1586b25437b9
7086ad6919feb2415c6027163f5c63323bcca27c 20-Jun-2014 Elliott Hughes <enh@google.com> Cache getpid.

In practice, with this implementation we never need to make a system call.
We get the main thread's tid (which is the same as our pid) back from
the set_tid_address system call we have to make during initialization.
A new pthread will have the same pid as its parent, and a fork child's
main (and only) thread will have a pid equal to its tid, which we get for
free from the kernel before clone returns.

The only time we'd actually have to make a getpid system call now is if
we take a signal during fork and the signal handler calls getpid. (That,
or we call getpid in the dynamic linker while it's still dealing with its
own relocations and hasn't even set up the main thread yet.)

Bug: 15387103
Change-Id: I6d4718ed0a5c912fc75b5f738c49a023dbed5189
03eebcb6e8762e668a0d3af6bb303cccb88c5b81 13-Jun-2014 Christopher Ferris <cferris@google.com> Move common macros into bionic_macros.h.

Bug: 15590152
Change-Id: I730636613ef3653f68c5ab1d43b53beaf8e0dc25
b30aff405a220495941f1673b0a5e66c4fa8b84c 28-May-2014 Elliott Hughes <enh@google.com> Revert "Revert "Lose the hand-written futex assembler.""

The problem with the original patch was that using syscall(3) means that
errno can be set, but pthread_create(3) was abusing the TLS errno slot as
a pthread_mutex_t for the thread startup handshake.

There was also a mistake in the check for syscall failures --- it should
have checked against -1 instead of 0 (not just because that's the default
idiom, but also here because futex(2) can legitimately return values > 0).

This patch stops abusing the TLS errno slot and adds a pthread_mutex_t to
pthread_internal_t instead. (Note that for LP64 sizeof(pthread_mutex_t) >
sizeof(uintptr_t), so we could potentially clobber other TLS slots too.)

I've also rewritten the LP32 compatibility stubs to directly reuse the
code from the .h file.

This reverts commit 75c55ff84ebfa686c7ae2cc8ee431c6a33bd46b4.

Bug: 15195455
Change-Id: I6ffb13e5cf6a35d8f59f692d94192aae9ab4593d
cd46104cf81aef14f9554bb4d9bced534a121471 14-May-2014 Elliott Hughes <enh@google.com> Fix a typo in the big g_ search/replace.

Change-Id: I79261de70d225236d0eadff288220258d697437f
1728b2396591853345507a063ed6075dfd251706 14-May-2014 Elliott Hughes <enh@google.com> Switch to g_ for globals.

That's what the Google style guide recommends, and we're starting
to get a mix.

Change-Id: Ib0c53a890bb5deed5c679e887541a715faea91fc
0d236aa3f1e6d31b0c729448ae9d3ed1cad23fb4 09-May-2014 Elliott Hughes <enh@google.com> Align the child stack in clone(2).

Also let clone(2) set the TLS for x86.

Also ensure we initialize the TLS before we clone(2) for all architectures.

Change-Id: Ie5fa4466e1c9ee116a281dfedef574c5ba60c0b5
b676aafad04f88e36658d4468054375158dbd3c1 11-Apr-2014 Elliott Hughes <enh@google.com> Remove unnecessary #includes.

Change-Id: Ie7e0c9ea03f35517c7dcf09fc808c12e55262bc1
f2cea021ab2c6d7d7feeb40cca098aa132605876 13-Mar-2014 Elliott Hughes <enh@google.com> Clean up <stdio.h> macros.

Also neuter __isthreaded.

We should come back to try to hide struct FILE's internals for LP64.

Bug: 3453512
Bug: 3453550
Change-Id: I7e115329fb4579246a72fea367b9fc8cb6055d18
af8aebebb52d73ea38c604525a6a5857618861cf 15-Jan-2014 Elliott Hughes <enh@google.com> Move _thread_created_hook to where it belongs.

Change-Id: I643d761c78ccaae25270aeffa2afb811c4e2fcd7
80906141f79be8be63fc915bfab467029b442ca1 26-Nov-2013 Elliott Hughes <enh@google.com> Work around CLONE_SETTLS being weird on x86.

Unlike other architectures, on x86 (but not x86-64), CLONE_SETTLS
takes a pointer to a struct user_desc instead of a pointer to the
TLS itself. Rather than have to deal with this here, let's just use
the old __set_tls mechanism we used to use (and still use for the
main thread on all architectures, so it's not going away any time
soon).

Bug: 11826724
Change-Id: I02a27939a73ae6cea1134a3f4c1dd7eafea479da
cef3faec0ea40fdfe58e425fd0be64f00de6a26d 20-Nov-2013 Elliott Hughes <enh@google.com> Clean up pthread_internal_t.

Bug: 11755300
Change-Id: Ib509e8c5ec6b23513aa78b5ac5141d7c34ce2dc8
877ec6d90418ff1d6597147d355a2229fdffae7e 16-Nov-2013 Elliott Hughes <enh@google.com> Fix pthread_join.

Let the kernel keep pthread_internal_t::tid updated, including
across forks and for the main thread. This then lets us fix
pthread_join to only return after the thread has really exited.

Also fix the thread attributes of the main thread so we don't
unmap the main thread's stack (which is really owned by the
dynamic linker and contains things like environment variables),
which fixes crashes when joining with an exited main thread
and also fixes problems reported publicly with accessing environment
variables after the main thread exits (for which I've added a new
unit test).

In passing I also fixed a bug where if the clone(2) inside
pthread_create(3) fails, we'd unmap the child's stack and TLS (which
contains the mutex) and then try to unlock the mutex. Boom! It wasn't
until after I'd uploaded the fix for this that I came across a new
public bug reporting this exact failure.

Bug: 8206355
Bug: 11693195
Bug: https://code.google.com/p/android/issues/detail?id=57421
Bug: https://code.google.com/p/android/issues/detail?id=62392
Change-Id: I2af9cf6e8ae510a67256ad93cad891794ed0580b
e48b68570d872ef7ece1d873c0ea298ea76393f3 15-Nov-2013 Elliott Hughes <enh@google.com> Clean up the pthread_create trampoline.

Bug: 8206355
Bug: 11693195
Change-Id: I35cc024d5b6ebd19d1d2e45610db185addaf45df
70b24b1cc2a1a4436b1fea3f8b76616fdcb27224 15-Nov-2013 Elliott Hughes <enh@google.com> Switch pthread_create over to __bionic_clone.

Bug: 8206355
Bug: 11693195
Change-Id: I04aadbc36c87e1b7e33324b9a930a1e441fbfed6
c3f114037dbf028896310609fd28cf2b3da99c4d 30-Oct-2013 Elliott Hughes <enh@google.com> <pthread.h> fixes and pthread cleanup.

<pthread.h> was missing nonnull attributes, noreturn on pthread_exit,
and had incorrect cv qualifiers for several standard functions.

I've also marked the non-standard stuff (where I count glibc rather
than POSIX as "standard") so we can revisit this cruft for LP64 and
try to ensure we're compatible with glibc.

I've also broken out the pthread_cond* functions into a new file.

I've made the remaining pthread files (plus ptrace) part of the bionic code
and fixed all the warnings.

I've added a few more smoke tests for chunks of untested pthread functionality.

We no longer need the libc_static_common_src_files hack for any of the
pthread implementation because we long since stripped out the rest of
the armv5 support, and this hack was just to ensure that __get_tls in libc.a
went via the kernel if necessary.

This patch also finishes the job of breaking up the pthread.c monolith, and
adds a handful of new tests.

Change-Id: Idc0ae7f5d8aa65989598acd4c01a874fe21582c7
2b6e43e00ece68b3aba26d8f95f07cd9d9294ab4 30-Oct-2013 Elliott Hughes <enh@google.com> Explain the sigprocmask in pthread_exit.

Also remove the SIGSEGV special case, which was probably because
hand-written __exit_with_stack_teardown stubs used to try to cause
SIGSEGV if the exit system call returned (which it never does, so
that dead code disappeared).

Also move the sigprocmask into the only case where it's necessary ---
the one where we unmap the stack that would be used by a signal
handler.

Change-Id: Ie40d20c1ae2f5e7125131b6b492cba7a2c6d08e9
98624c374646a050556bdc402b55b792fefa7e55 16-Oct-2013 Elliott Hughes <enh@google.com> Make pthread_create report sched_setscheduler failures on LP64.

We couldn't fix this for 32-bit because there's too much broken
code out there. (Pretty much everyone asks for real-time
scheduling for all their threads, and the kernel says "don't be
stupid".)

Change-Id: I43c5271e6b6bb91278b9a19eec08cbf05391e3c4
eb847bc8666842a3cfc9c06e8458ad1abebebaf0 10-Oct-2013 Elliott Hughes <enh@google.com> Fix x86_64 build, clean up intermediate libraries.

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
2a0b873065edb304fa2d1c54f8de663ea638b8ab 09-Oct-2013 Elliott Hughes <enh@google.com> Fix __errno for LP64 and clean up __get_tls.

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
84114c8dd5b17efecf7988f263ce431208d7be5a 17-Jul-2013 Elliott Hughes <enh@google.com> Improve stack overflow diagnostics (take 2).

This reverts commits eb1b07469f2b5a392dc1bfd8adc211aea8c72bc5 and
d14dc3b87fbf80553f1cafa453816b7f11366627, and fixes the bug where
we were calling mmap (which might cause errno to be set) before
__set_tls (which is required to implement errno).

Bug: 8557703
Change-Id: I2c36d00240c56e156e1bb430d8c22a73a068b70c
d14dc3b87fbf80553f1cafa453816b7f11366627 17-Jul-2013 Guang Zhu <guangzhu@google.com> Revert "Improve stack overflow diagnostics."

This reverts commit aa754dca90487356cabf07ade0e8d88c2630b784.

Change-Id: Ifa76eee31f7f44075eb3a48554315b2693062f44
eb1b07469f2b5a392dc1bfd8adc211aea8c72bc5 17-Jul-2013 Guang Zhu <guangzhu@google.com> Revert "Clean up our alternate signal stacks."

This reverts commit 5cf87951abd23b0b3ebf159e8aa06b02e39527ca.

Change-Id: Idd6ca7d80a018755da3bd315d91193723ce7f3bf
5cf87951abd23b0b3ebf159e8aa06b02e39527ca 16-Jul-2013 Elliott Hughes <enh@google.com> Clean up our alternate signal stacks.

Bug: 8557703
Change-Id: Ie93901dd1c29e9d3bf795b0f0400616d9ef08f75
aa754dca90487356cabf07ade0e8d88c2630b784 16-Jul-2013 Elliott Hughes <enh@google.com> Improve stack overflow diagnostics.

We notify debuggerd of problems by installing signal handlers. That's
fine except for when the signal is caused by us running off the end of
a thread's stack and into the guard page.

Bug: 8557703
Change-Id: I1ef65b4bb3bbca7e9a9743056177094921e60ed3
b95cf0d23a1db3b7c37bd98b0c86196796c9b029 15-Jul-2013 Elliott Hughes <enh@google.com> Fix pthread_getattr_np, pthread_attr_setguardsize, and pthread_attr_setstacksize.

pthread_getattr_np was reporting the values supplied to us, not the values we
actually used, which is kinda the whole point of pthread_getattr_np.

pthread_attr_setguardsize and pthread_attr_setstacksize were reporting EINVAL
for any size that wasn't a multiple of the system page size. This is
unnecessary. We can just round like POSIX suggests and glibc already does.

Also improve the error reporting for pthread_create failures.

Change-Id: I7ebc518628a8a1161ec72e111def911d500bba71
0f020d18b138e24b1fe34074808e07ac412f35a4 06-Jun-2013 msg555 <msg555@gmail.com> Handles spurious wake-ups in pthread_join()

Removed 'join_count' from pthread_internal_t and switched to using the flag
PTHREAD_ATTR_FLAG_JOINED to indicate if a thread is being joined. Combined with
a switch to a while loop in pthread_join, this fixes spurious wake-ups but
prevents a thread from being joined multiple times. This is fine for
two reasons:

1) The pthread_join specification allows for undefined behavior when multiple
threads try to join a single thread.

2) There is no thread safe way to allow multiple threads to join a single
thread with the pthread interface. The second thread calling pthread_join
could be pre-empted until the thread is destroyed and its handle reused for
a different thread. Therefore multi-join is always an error.

Bug: https://code.google.com/p/android/issues/detail?id=52255
Change-Id: I8b6784d47620ffdcdbfb14524e7402e21d46c5f7
96449b3dc16d64ea91df83c9d7942f3e22e76b52 30-Mar-2013 Elliott Hughes <enh@google.com> Extra logging in pthread_create.

pthread_create returns EAGAIN when it can't allocate a pthread_internal_t,
when it can't allocate a stack for the new thread, or when clone(2) fails
because there are too many threads. It's useful to be able to know why your
pthread_create just failed, so add some logging.

Bug: 8470684

(cherry picked from commit cfa089df23ff50fcd5ed3854c54991d30be5fc7e)

Change-Id: Ibfc98a84c1817a931f9ae4c2b88762f0edfb6b79
cfa089df23ff50fcd5ed3854c54991d30be5fc7e 30-Mar-2013 Elliott Hughes <enh@google.com> Extra logging in pthread_create.

pthread_create returns EAGAIN when it can't allocate a pthread_internal_t,
when it can't allocate a stack for the new thread, or when clone(2) fails
because there are too many threads. It's useful to be able to know why your
pthread_create just failed, so add some logging.

Bug: 8470684
Change-Id: I1bb4497d4f7528eacce0db35c2014771cba64569
8f2a5a0b40fc82126c691d5c30131d908772aab7 15-Mar-2013 Elliott Hughes <enh@google.com> Clean up internal libc logging.

We only need one logging API, and I prefer the one that does no
allocation and is thus safe to use in any context.

Also use O_CLOEXEC when opening the /dev/log files.

Move everything logging-related into one header file.

Change-Id: Ic1e3ea8e9b910dc29df351bff6c0aa4db26fbb58
40eabe24e4e3ae8ebe437f1f4e43cf39cbba2e9e 15-Feb-2013 Elliott Hughes <enh@google.com> Fix the pthread_setname_np test.

Fix the pthread_setname_np test to take into account that emulator kernels are
so old that they don't support setting the name of other threads.

The CLONE_DETACHED thread is obsolete since 2.5 kernels.

Rename kernel_id to tid.

Fix the signature of __pthread_clone.

Clean up the clone and pthread_setname_np implementations slightly.

Change-Id: I16c2ff8845b67530544bbda9aa6618058603066d
4b4a8824289c48c823cd38bc63289d121aae3d67 13-Feb-2013 Elliott Hughes <enh@google.com> Clean up pthread_create.

Bug: 3461078
Change-Id: I082122a86d7692cd58f4145539241be026258ee0