summaryrefslogtreecommitdiff
path: root/lib/asan/lit_tests
diff options
context:
space:
mode:
authorAlexey Samsonov <samsonov@google.com>2013-09-02 08:39:07 +0000
committerAlexey Samsonov <samsonov@google.com>2013-09-02 08:39:07 +0000
commit717ece58e18190c4aef50bd16254db1d74036395 (patch)
treeba193b6dcc29b16a8a5ed88d98cdaf216dd4177e /lib/asan/lit_tests
parent6d40a0a2ffa6735e45bd1d62c94ff725fd3e8b71 (diff)
downloadcompiler-rt-717ece58e18190c4aef50bd16254db1d74036395.tar.gz
compiler-rt-717ece58e18190c4aef50bd16254db1d74036395.tar.bz2
compiler-rt-717ece58e18190c4aef50bd16254db1d74036395.tar.xz
Improve collecting malloc stats in ASan
Summary: This change makes races between updates of thread-local stats and merging all the thread-local stats together less harmful. Reviewers: kcc Reviewed By: kcc CC: dvyukov, llvm-commits Differential Revision: http://llvm-reviews.chandlerc.com/D1572 git-svn-id: https://llvm.org/svn/llvm-project/compiler-rt/trunk@189744 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/asan/lit_tests')
-rw-r--r--lib/asan/lit_tests/TestCases/current_allocated_bytes.cc43
1 files changed, 43 insertions, 0 deletions
diff --git a/lib/asan/lit_tests/TestCases/current_allocated_bytes.cc b/lib/asan/lit_tests/TestCases/current_allocated_bytes.cc
new file mode 100644
index 00000000..669cf150
--- /dev/null
+++ b/lib/asan/lit_tests/TestCases/current_allocated_bytes.cc
@@ -0,0 +1,43 @@
+// RUN: %clangxx_asan -O0 %s -o %t && %t
+// RUN: %clangxx_asan -O2 %s -o %t && %t
+
+#include <assert.h>
+#include <pthread.h>
+#include <sanitizer/asan_interface.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+const size_t kLargeAlloc = 1UL << 20;
+
+void* allocate(void *arg) {
+ volatile void *ptr = malloc(kLargeAlloc);
+ free((void*)ptr);
+ return 0;
+}
+
+void* check_stats(void *arg) {
+ assert(__asan_get_current_allocated_bytes() > 0);
+ return 0;
+}
+
+int main() {
+ size_t used_mem = __asan_get_current_allocated_bytes();
+ printf("Before: %zu\n", used_mem);
+ const int kNumIterations = 1000;
+ for (int iter = 0; iter < kNumIterations; iter++) {
+ pthread_t thr[4];
+ for (int j = 0; j < 4; j++) {
+ assert(0 ==
+ pthread_create(&thr[j], 0, (j < 2) ? allocate : check_stats, 0));
+ }
+ for (int j = 0; j < 4; j++)
+ assert(0 == pthread_join(thr[j], 0));
+ used_mem = __asan_get_current_allocated_bytes();
+ if (used_mem > kLargeAlloc) {
+ printf("After iteration %d: %zu\n", iter, used_mem);
+ return 1;
+ }
+ }
+ printf("Success after %d iterations\n", kNumIterations);
+ return 0;
+}