aboutsummaryrefslogtreecommitdiff
path: root/test/asan/TestCases/Linux/release_to_os_test.cc
blob: 26402167d6b118968091687216687439ed37b0d3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// Tests ASAN_OPTIONS=allocator_release_to_os=1
//

// RUN: %clangxx_asan -std=c++11 %s -o %t
// RUN: %env_asan_opts=allocator_release_to_os_interval_ms=0 %run %t 2>&1 | FileCheck %s --check-prefix=RELEASE
// RUN: %env_asan_opts=allocator_release_to_os_interval_ms=-1 %run %t 2>&1 | FileCheck %s --check-prefix=NO_RELEASE
//
// REQUIRES: x86_64-target-arch
#include <stdlib.h>
#include <stdio.h>
#include <algorithm>
#include <stdint.h>
#include <assert.h>

#include <sanitizer/asan_interface.h>

void MallocReleaseStress() {
  const size_t kNumChunks = 10000;
  const size_t kAllocSize = 100;
  const size_t kNumIter = 100;
  uintptr_t *chunks[kNumChunks] = {0};

  for (size_t iter = 0; iter < kNumIter; iter++) {
    std::random_shuffle(chunks, chunks + kNumChunks);
    size_t to_replace = rand() % kNumChunks;
    for (size_t i = 0; i < kNumChunks; i++) {
      if (chunks[i])
        assert(chunks[i][0] == (uintptr_t)chunks[i]);
      if (i < to_replace) {
        delete [] chunks[i];
        chunks[i] = new uintptr_t[kAllocSize];
        chunks[i][0] = (uintptr_t)chunks[i];
      }
    }
  }
  for (auto p : chunks)
    delete[] p;
}

int main() {
  MallocReleaseStress();
  __asan_print_accumulated_stats();
}

// RELEASE: mapped:{{.*}}releases: {{[1-9]}}
// NO_RELEASE: mapped:{{.*}}releases: 0