1// Test the mmap_limit_mb flag.
2//
3// RUN: %clangxx_asan -O2 %s -o %t
4// RUN: %run %t 20 16
5// RUN: %run %t 30 1000000
6// RUN: %env_asan_opts=mmap_limit_mb=300 %run %t 20 16
7// RUN: %env_asan_opts=mmap_limit_mb=300 %run %t 20 1000000
8// RUN: %env_asan_opts=mmap_limit_mb=300 not %run %t 500 16 2>&1 | FileCheck %s
9// RUN: %env_asan_opts=mmap_limit_mb=300 not %run %t 500 1000000 2>&1 | FileCheck %s
10//
11// FIXME: Windows doesn't implement mmap_limit_mb.
12// XFAIL: target={{.*windows-msvc.*}}
13
14#include <assert.h>
15#include <stdlib.h>
16#include <stdio.h>
17
18#include <algorithm>
19#include <vector>
20
21int main(int argc, char **argv) {
22 assert(argc == 3);
23 long total_mb = atoi(nptr: argv[1]);
24 long allocation_size = atoi(nptr: argv[2]);
25 fprintf(stderr, format: "total_mb: %zd allocation_size: %zd\n", total_mb,
26 allocation_size);
27 std::vector<char *> v;
28 for (long total = total_mb << 20; total > 0; total -= allocation_size)
29 v.push_back(x: new char[allocation_size]);
30 for (std::vector<char *>::const_iterator it = v.begin(); it != v.end(); ++it)
31 delete[](*it);
32 fprintf(stderr, format: "PASS\n");
33 // CHECK: total_mmaped{{.*}}mmap_limit_mb
34 return 0;
35}
36

source code of compiler-rt/test/asan/TestCases/Posix/mmap_limit_mb.cpp