summaryrefslogtreecommitdiff
path: root/tests/stdlib_test.cpp
diff options
context:
space:
mode:
authorElliott Hughes <enh@google.com>2013-11-15 17:40:18 -0800
committerElliott Hughes <enh@google.com>2013-11-18 19:48:11 -0800
commit877ec6d90418ff1d6597147d355a2229fdffae7e (patch)
treee475221a7fbff1564ad37548e920333c49cc5873 /tests/stdlib_test.cpp
parentf246c589d66e5dc0e3cddc3c37261fb0e3fc67e9 (diff)
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
Diffstat (limited to 'tests/stdlib_test.cpp')
-rw-r--r--tests/stdlib_test.cpp25
1 files changed, 25 insertions, 0 deletions
diff --git a/tests/stdlib_test.cpp b/tests/stdlib_test.cpp
index e5d78122c..fa59c4105 100644
--- a/tests/stdlib_test.cpp
+++ b/tests/stdlib_test.cpp
@@ -19,6 +19,7 @@
#include <errno.h>
#include <libgen.h>
#include <limits.h>
+#include <pthread.h>
#include <stdint.h>
#include <stdlib.h>
@@ -132,3 +133,27 @@ TEST(stdlib, qsort) {
ASSERT_STREQ("bravo", entries[1].name);
ASSERT_STREQ("charlie", entries[2].name);
}
+
+static void* TestBug57421_child(void* arg) {
+ pthread_t main_thread = reinterpret_cast<pthread_t>(arg);
+ pthread_join(main_thread, NULL);
+ char* value = getenv("ENVIRONMENT_VARIABLE");
+ if (value == NULL) {
+ setenv("ENVIRONMENT_VARIABLE", "value", 1);
+ }
+ return NULL;
+}
+
+static void TestBug57421_main() {
+ pthread_t t;
+ ASSERT_EQ(0, pthread_create(&t, NULL, TestBug57421_child, reinterpret_cast<void*>(pthread_self())));
+ pthread_exit(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(stdlib_DeathTest, getenv_after_main_thread_exits) {
+ // https://code.google.com/p/android/issues/detail?id=57421
+ ::testing::FLAGS_gtest_death_test_style = "threadsafe";
+ ASSERT_EXIT(TestBug57421_main(), ::testing::ExitedWithCode(0), "");
+}