]> git.proxmox.com Git - rustc.git/blame - src/compiler-rt/test/asan/TestCases/current_allocated_bytes.cc
Imported Upstream version 1.5.0+dfsg1
[rustc.git] / src / compiler-rt / test / asan / TestCases / current_allocated_bytes.cc
CommitLineData
1a4d82fc
JJ
1// RUN: %clangxx_asan -O0 %s -lpthread -o %t && %run %t
2// RUN: %clangxx_asan -O2 %s -lpthread -o %t && %run %t
3
4#include <assert.h>
5#include <pthread.h>
6#include <sanitizer/asan_interface.h>
7#include <stdio.h>
8#include <stdlib.h>
9
10const size_t kLargeAlloc = 1UL << 20;
11
12void* allocate(void *arg) {
13 volatile void *ptr = malloc(kLargeAlloc);
14 free((void*)ptr);
15 return 0;
16}
17
18void* check_stats(void *arg) {
19 assert(__asan_get_current_allocated_bytes() > 0);
20 return 0;
21}
22
23int main() {
24 size_t used_mem = __asan_get_current_allocated_bytes();
25 printf("Before: %zu\n", used_mem);
26 const int kNumIterations = 1000;
27 for (int iter = 0; iter < kNumIterations; iter++) {
28 pthread_t thr[4];
29 for (int j = 0; j < 4; j++) {
30 assert(0 ==
31 pthread_create(&thr[j], 0, (j < 2) ? allocate : check_stats, 0));
32 }
33 for (int j = 0; j < 4; j++)
34 assert(0 == pthread_join(thr[j], 0));
35 used_mem = __asan_get_current_allocated_bytes();
36 if (used_mem > kLargeAlloc) {
37 printf("After iteration %d: %zu\n", iter, used_mem);
38 return 1;
39 }
40 }
41 printf("Success after %d iterations\n", kNumIterations);
42 return 0;
43}