summaryrefslogtreecommitdiffstats
path: root/compiler-rt/lib/lsan/lit_tests
diff options
context:
space:
mode:
authorSergey Matveev <earthdok@google.com>2013-07-08 12:57:24 +0000
committerSergey Matveev <earthdok@google.com>2013-07-08 12:57:24 +0000
commitbdeff959a166594abf76206d0eae397e4cc17747 (patch)
treeaf6b1dc3d1afef208ba86f9bddca36067e428d77 /compiler-rt/lib/lsan/lit_tests
parent86142e02df74b8a4b3cb696818c31ff064f1bc45 (diff)
downloadbcm5719-llvm-bdeff959a166594abf76206d0eae397e4cc17747.tar.gz
bcm5719-llvm-bdeff959a166594abf76206d0eae397e4cc17747.zip
[lsan] Handle fork() correctly.
Update the main thread's os_id on every pthread_create, and before initiating leak checking. This ensures that we have the correct os_id even if we have forked after Init(). llvm-svn: 185815
Diffstat (limited to 'compiler-rt/lib/lsan/lit_tests')
-rw-r--r--compiler-rt/lib/lsan/lit_tests/TestCases/fork.cc24
-rw-r--r--compiler-rt/lib/lsan/lit_tests/TestCases/fork_threaded.cc44
2 files changed, 68 insertions, 0 deletions
diff --git a/compiler-rt/lib/lsan/lit_tests/TestCases/fork.cc b/compiler-rt/lib/lsan/lit_tests/TestCases/fork.cc
new file mode 100644
index 00000000000..69258d9a0c7
--- /dev/null
+++ b/compiler-rt/lib/lsan/lit_tests/TestCases/fork.cc
@@ -0,0 +1,24 @@
+// Test that thread local data is handled correctly after forking without exec().
+// RUN: %clangxx_lsan %s -o %t
+// RUN: %t 2>&1
+
+#include <assert.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/wait.h>
+#include <unistd.h>
+
+__thread void *thread_local_var;
+
+int main() {
+ int status = 0;
+ thread_local_var = malloc(1337);
+ pid_t pid = fork();
+ assert(pid >= 0);
+ if (pid > 0) {
+ waitpid(pid, &status, 0);
+ assert(WIFEXITED(status));
+ return WEXITSTATUS(status);
+ }
+ return 0;
+}
diff --git a/compiler-rt/lib/lsan/lit_tests/TestCases/fork_threaded.cc b/compiler-rt/lib/lsan/lit_tests/TestCases/fork_threaded.cc
new file mode 100644
index 00000000000..b162de458b0
--- /dev/null
+++ b/compiler-rt/lib/lsan/lit_tests/TestCases/fork_threaded.cc
@@ -0,0 +1,44 @@
+// Test that thread local data is handled correctly after forking without
+// exec(). In this test leak checking is initiated from a non-main thread.
+// RUN: %clangxx_lsan %s -o %t
+// RUN: %t 2>&1
+
+#include <assert.h>
+#include <pthread.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/wait.h>
+#include <unistd.h>
+
+__thread void *thread_local_var;
+
+void *exit_thread_func(void *arg) {
+ exit(0);
+}
+
+void ExitFromThread() {
+ pthread_t tid;
+ int res;
+ res = pthread_create(&tid, 0, exit_thread_func, 0);
+ assert(res == 0);
+ res = pthread_join(tid, 0);
+ assert(res == 0);
+}
+
+int main() {
+ int status = 0;
+ thread_local_var = malloc(1337);
+ pid_t pid = fork();
+ assert(pid >= 0);
+ if (pid > 0) {
+ waitpid(pid, &status, 0);
+ assert(WIFEXITED(status));
+ return WEXITSTATUS(status);
+ } else {
+ // Spawn a thread and call exit() from there, to check that we track main
+ // thread's pid correctly even if leak checking is initiated from another
+ // thread.
+ ExitFromThread();
+ }
+ return 0;
+}
OpenPOWER on IntegriCloud