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
This commit is contained in:
@@ -150,22 +150,48 @@ TEST(pthread, pthread_join_self) {
|
||||
ASSERT_EQ(EDEADLK, pthread_join(pthread_self(), &result));
|
||||
}
|
||||
|
||||
#if __BIONIC__ // For some reason, gtest on bionic can cope with this but gtest on glibc can't.
|
||||
struct TestBug37410 {
|
||||
pthread_t main_thread;
|
||||
pthread_mutex_t mutex;
|
||||
|
||||
static void TestBug37410() {
|
||||
pthread_t t1;
|
||||
ASSERT_EQ(0, pthread_create(&t1, NULL, JoinFn, reinterpret_cast<void*>(pthread_self())));
|
||||
pthread_exit(NULL);
|
||||
}
|
||||
static void main() {
|
||||
TestBug37410 data;
|
||||
data.main_thread = pthread_self();
|
||||
ASSERT_EQ(0, pthread_mutex_init(&data.mutex, NULL));
|
||||
ASSERT_EQ(0, pthread_mutex_lock(&data.mutex));
|
||||
|
||||
pthread_t t;
|
||||
ASSERT_EQ(0, pthread_create(&t, NULL, TestBug37410::thread_fn, reinterpret_cast<void*>(&data)));
|
||||
|
||||
// Wait for the thread to be running...
|
||||
ASSERT_EQ(0, pthread_mutex_lock(&data.mutex));
|
||||
ASSERT_EQ(0, pthread_mutex_unlock(&data.mutex));
|
||||
|
||||
// ...and exit.
|
||||
pthread_exit(NULL);
|
||||
}
|
||||
|
||||
private:
|
||||
static void* thread_fn(void* arg) {
|
||||
TestBug37410* data = reinterpret_cast<TestBug37410*>(arg);
|
||||
|
||||
// Let the main thread know we're running.
|
||||
pthread_mutex_unlock(&data->mutex);
|
||||
|
||||
// And wait for the main thread to exit.
|
||||
pthread_join(data->main_thread, NULL);
|
||||
|
||||
return NULL;
|
||||
}
|
||||
};
|
||||
|
||||
// Even though this isn't really a death test, we have to say "DeathTest" here so gtest knows to
|
||||
// run this test (which exits normally) in its own process.
|
||||
TEST(pthread_DeathTest, pthread_bug_37410) {
|
||||
// http://code.google.com/p/android/issues/detail?id=37410
|
||||
::testing::FLAGS_gtest_death_test_style = "threadsafe";
|
||||
ASSERT_EXIT(TestBug37410(), ::testing::ExitedWithCode(0), "");
|
||||
ASSERT_EXIT(TestBug37410::main(), ::testing::ExitedWithCode(0), "");
|
||||
}
|
||||
#endif
|
||||
|
||||
static void* SignalHandlerFn(void* arg) {
|
||||
sigset_t wait_set;
|
||||
|
Reference in New Issue
Block a user