summaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorYabin Cui <yabinc@google.com>2015-04-04 19:31:54 +0000
committerGerrit Code Review <noreply-gerritcodereview@google.com>2015-04-04 19:31:55 +0000
commit4bd8f9637daaada333ff35945b00cfe6cb822376 (patch)
tree558ae0624aff54953ca601079a3560f7340467ff /tests
parente686df8d839216460f02cf7db6c4e26a13e49afd (diff)
parentf796985923e2d8308e00ed9567f36546dafb98d7 (diff)
downloadbionic-4bd8f9637daaada333ff35945b00cfe6cb822376.zip
bionic-4bd8f9637daaada333ff35945b00cfe6cb822376.tar.gz
bionic-4bd8f9637daaada333ff35945b00cfe6cb822376.tar.bz2
Merge "Fix bug for recursive/errorcheck mutex on 32-bit devices."
Diffstat (limited to 'tests')
-rw-r--r--tests/Android.mk10
-rw-r--r--tests/pthread_test.cpp53
-rw-r--r--tests/stack_protector_test.cpp6
3 files changed, 51 insertions, 18 deletions
diff --git a/tests/Android.mk b/tests/Android.mk
index 995877e..c942375 100644
--- a/tests/Android.mk
+++ b/tests/Android.mk
@@ -128,6 +128,9 @@ libBionicStandardTests_c_includes := \
bionic/libc \
external/tinyxml2 \
+libBionicStandardTests_static_libraries := \
+ libbase \
+
libBionicStandardTests_ldlibs_host := \
-lrt \
@@ -257,6 +260,7 @@ bionic-unit-tests_whole_static_libraries := \
bionic-unit-tests_static_libraries := \
libtinyxml2 \
liblog \
+ libbase \
# TODO: Include __cxa_thread_atexit_test.cpp to glibc tests once it is upgraded (glibc 2.18+)
bionic-unit-tests_src_files := \
@@ -317,6 +321,7 @@ bionic-unit-tests-static_static_libraries := \
libdl \
libtinyxml2 \
liblog \
+ libbase \
bionic-unit-tests-static_force_static_executable := true
@@ -355,6 +360,11 @@ bionic-unit-tests-glibc_whole_static_libraries := \
libBionicGtestMain \
$(fortify_libs) \
+bionic-unit-tests-glibc_static_libraries := \
+ libbase \
+ liblog \
+ libcutils \
+
bionic-unit-tests-glibc_ldlibs := \
-lrt -ldl -lutil \
diff --git a/tests/pthread_test.cpp b/tests/pthread_test.cpp
index 5ab1f11..f96ccf9 100644
--- a/tests/pthread_test.cpp
+++ b/tests/pthread_test.cpp
@@ -29,13 +29,19 @@
#include <unistd.h>
#include <atomic>
+#include <regex>
#include <vector>
+#include <base/file.h>
+#include <base/stringprintf.h>
+
#include "private/bionic_macros.h"
#include "private/ScopeGuard.h"
#include "BionicDeathTest.h"
#include "ScopedSignalHandler.h"
+extern "C" pid_t gettid();
+
TEST(pthread, pthread_key_create) {
pthread_key_t key;
ASSERT_EQ(0, pthread_key_create(&key, NULL));
@@ -704,6 +710,23 @@ TEST(pthread, pthread_rwlock_smoke) {
ASSERT_EQ(0, pthread_rwlock_destroy(&l));
}
+static void WaitUntilThreadSleep(std::atomic<pid_t>& pid) {
+ while (pid == 0) {
+ usleep(1000);
+ }
+ std::string filename = android::base::StringPrintf("/proc/%d/stat", pid.load());
+ std::regex regex {R"(\s+S\s+)"};
+
+ while (true) {
+ std::string content;
+ ASSERT_TRUE(android::base::ReadFileToString(filename, &content));
+ if (std::regex_search(content, regex)) {
+ break;
+ }
+ usleep(1000);
+ }
+}
+
struct RwlockWakeupHelperArg {
pthread_rwlock_t lock;
enum Progress {
@@ -713,9 +736,11 @@ struct RwlockWakeupHelperArg {
LOCK_ACCESSED
};
std::atomic<Progress> progress;
+ std::atomic<pid_t> tid;
};
static void pthread_rwlock_reader_wakeup_writer_helper(RwlockWakeupHelperArg* arg) {
+ arg->tid = gettid();
ASSERT_EQ(RwlockWakeupHelperArg::LOCK_INITIALIZED, arg->progress);
arg->progress = RwlockWakeupHelperArg::LOCK_WAITING;
@@ -732,14 +757,14 @@ TEST(pthread, pthread_rwlock_reader_wakeup_writer) {
ASSERT_EQ(0, pthread_rwlock_init(&wakeup_arg.lock, NULL));
ASSERT_EQ(0, pthread_rwlock_rdlock(&wakeup_arg.lock));
wakeup_arg.progress = RwlockWakeupHelperArg::LOCK_INITIALIZED;
+ wakeup_arg.tid = 0;
pthread_t thread;
ASSERT_EQ(0, pthread_create(&thread, NULL,
reinterpret_cast<void* (*)(void*)>(pthread_rwlock_reader_wakeup_writer_helper), &wakeup_arg));
- while (wakeup_arg.progress != RwlockWakeupHelperArg::LOCK_WAITING) {
- usleep(5000);
- }
- usleep(5000);
+ WaitUntilThreadSleep(wakeup_arg.tid);
+ ASSERT_EQ(RwlockWakeupHelperArg::LOCK_WAITING, wakeup_arg.progress);
+
wakeup_arg.progress = RwlockWakeupHelperArg::LOCK_RELEASED;
ASSERT_EQ(0, pthread_rwlock_unlock(&wakeup_arg.lock));
@@ -749,6 +774,7 @@ TEST(pthread, pthread_rwlock_reader_wakeup_writer) {
}
static void pthread_rwlock_writer_wakeup_reader_helper(RwlockWakeupHelperArg* arg) {
+ arg->tid = gettid();
ASSERT_EQ(RwlockWakeupHelperArg::LOCK_INITIALIZED, arg->progress);
arg->progress = RwlockWakeupHelperArg::LOCK_WAITING;
@@ -765,14 +791,14 @@ TEST(pthread, pthread_rwlock_writer_wakeup_reader) {
ASSERT_EQ(0, pthread_rwlock_init(&wakeup_arg.lock, NULL));
ASSERT_EQ(0, pthread_rwlock_wrlock(&wakeup_arg.lock));
wakeup_arg.progress = RwlockWakeupHelperArg::LOCK_INITIALIZED;
+ wakeup_arg.tid = 0;
pthread_t thread;
ASSERT_EQ(0, pthread_create(&thread, NULL,
reinterpret_cast<void* (*)(void*)>(pthread_rwlock_writer_wakeup_reader_helper), &wakeup_arg));
- while (wakeup_arg.progress != RwlockWakeupHelperArg::LOCK_WAITING) {
- usleep(5000);
- }
- usleep(5000);
+ WaitUntilThreadSleep(wakeup_arg.tid);
+ ASSERT_EQ(RwlockWakeupHelperArg::LOCK_WAITING, wakeup_arg.progress);
+
wakeup_arg.progress = RwlockWakeupHelperArg::LOCK_RELEASED;
ASSERT_EQ(0, pthread_rwlock_unlock(&wakeup_arg.lock));
@@ -1263,7 +1289,6 @@ TEST(pthread, pthread_mutex_init_same_as_static_initializers) {
ASSERT_EQ(0, memcmp(&lock_recursive, &m3.lock, sizeof(pthread_mutex_t)));
ASSERT_EQ(0, pthread_mutex_destroy(&lock_recursive));
}
-
class MutexWakeupHelper {
private:
PthreadMutex m;
@@ -1274,8 +1299,10 @@ class MutexWakeupHelper {
LOCK_ACCESSED
};
std::atomic<Progress> progress;
+ std::atomic<pid_t> tid;
static void thread_fn(MutexWakeupHelper* helper) {
+ helper->tid = gettid();
ASSERT_EQ(LOCK_INITIALIZED, helper->progress);
helper->progress = LOCK_WAITING;
@@ -1293,15 +1320,15 @@ class MutexWakeupHelper {
void test() {
ASSERT_EQ(0, pthread_mutex_lock(&m.lock));
progress = LOCK_INITIALIZED;
+ tid = 0;
pthread_t thread;
ASSERT_EQ(0, pthread_create(&thread, NULL,
reinterpret_cast<void* (*)(void*)>(MutexWakeupHelper::thread_fn), this));
- while (progress != LOCK_WAITING) {
- usleep(5000);
- }
- usleep(5000);
+ WaitUntilThreadSleep(tid);
+ ASSERT_EQ(LOCK_WAITING, progress);
+
progress = LOCK_RELEASED;
ASSERT_EQ(0, pthread_mutex_unlock(&m.lock));
diff --git a/tests/stack_protector_test.cpp b/tests/stack_protector_test.cpp
index 8007711..22285d1 100644
--- a/tests/stack_protector_test.cpp
+++ b/tests/stack_protector_test.cpp
@@ -24,14 +24,10 @@
#include <pthread.h>
#include <stdint.h>
#include <stdio.h>
-#include <sys/syscall.h>
#include <unistd.h>
#include <set>
-#if defined(__GLIBC__)
-// glibc doesn't expose gettid(2).
-pid_t gettid() { return syscall(__NR_gettid); }
-#endif // __GLIBC__
+extern "C" pid_t gettid();
// For x86, bionic and glibc have per-thread stack guard values (all identical).
#if defined(__i386__)