• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Tests ASAN_OPTIONS=allocator_release_to_os=1
2 
3 // RUN: %clangxx_asan -std=c++11 %s -o %t
4 // RUN: %env_asan_opts=allocator_release_to_os_interval_ms=0 %run %t 2>&1 | FileCheck %s --check-prefix=RELEASE
5 // RUN: %env_asan_opts=allocator_release_to_os_interval_ms=-1 %run %t 2>&1 | FileCheck %s --check-prefix=NO_RELEASE
6 // RUN: %env_asan_opts=allocator_release_to_os_interval_ms=-1 %run %t force 2>&1 | FileCheck %s --check-prefix=FORCE_RELEASE
7 
8 // REQUIRES: x86_64-target-arch
9 
10 #include <algorithm>
11 #include <assert.h>
12 #include <random>
13 #include <stdint.h>
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <string.h>
17 
18 #include <sanitizer/allocator_interface.h>
19 #include <sanitizer/asan_interface.h>
20 
MallocReleaseStress()21 void MallocReleaseStress() {
22   const size_t kNumChunks = 10000;
23   const size_t kAllocSize = 100;
24   const size_t kNumIter = 100;
25   uintptr_t *chunks[kNumChunks] = {0};
26   std::mt19937 r;
27 
28   for (size_t iter = 0; iter < kNumIter; iter++) {
29     std::shuffle(chunks, chunks + kNumChunks, r);
30     size_t to_replace = rand() % kNumChunks;
31     for (size_t i = 0; i < kNumChunks; i++) {
32       if (chunks[i])
33         assert(chunks[i][0] == (uintptr_t)chunks[i]);
34       if (i < to_replace) {
35         delete [] chunks[i];
36         chunks[i] = new uintptr_t[kAllocSize];
37         chunks[i][0] = (uintptr_t)chunks[i];
38       }
39     }
40   }
41   for (auto p : chunks)
42     delete[] p;
43 }
44 
main(int argc,char ** argv)45 int main(int argc, char **argv) {
46   MallocReleaseStress();
47   if (argc > 1 && !strcmp("force", argv[1]))
48     __sanitizer_purge_allocator();
49   __asan_print_accumulated_stats();
50 }
51 
52 // RELEASE: mapped:{{.*}}releases: {{[1-9]}}
53 // NO_RELEASE: mapped:{{.*}}releases: 0
54 // FORCE_RELEASE: mapped:{{.*}}releases: {{[1-9]}}
55