xref: /llvm-project/compiler-rt/test/sanitizer_common/TestCases/hard_rss_limit_mb_test.cpp (revision 8e60061290138c2e66ce29a0c8f91722131c0941)
1 // Check hard_rss_limit_mb. Not all sanitizers implement it yet.
2 // RUN: %clangxx -O2 %s -o %t
3 //
4 // Run with limit should fail:
5 // RUN: %env_tool_opts=hard_rss_limit_mb=100                           not %run %t 2>&1 | FileCheck %s
6 // This run uses getrusage:
7 // RUN: %env_tool_opts=hard_rss_limit_mb=100:can_use_proc_maps_statm=0 not %run %t 2>&1 | FileCheck %s
8 //
9 // Run w/o limit or with a large enough limit should pass:
10 // RUN: %env_tool_opts=hard_rss_limit_mb=4000 %run %t
11 // RUN: %run %t
12 //
13 // Ubsan does not intercept pthread_create.
14 // XFAIL: ubsan
15 // UNSUPPORTED: target={{.*(freebsd|solaris).*}}, darwin
16 
17 // THUMB starts background thead only for Asan.
18 // XFAIL: target=thumb{{.*}} && !asan
19 
20 #include <string.h>
21 #include <stdio.h>
22 #include <unistd.h>
23 
24 const int kNumAllocs = 200 * 1000;
25 const int kAllocSize = 1000;
26 volatile char *sink[kNumAllocs];
27 
main(int argc,char ** argv)28 int main(int argc, char **argv) {
29   for (int i = 0; i < kNumAllocs; i++) {
30     if ((i % 1000) == 0) {
31       // Don't write to stderr! Doing that triggers a kernel race condition
32       // between this thread and the rss-limit thread, and may lose part of the
33       // output. See https://lkml.org/lkml/2014/2/17/324.
34       printf("[%d]\n", i);
35     }
36     char *x = new char[kAllocSize];
37     memset(x, 0, kAllocSize);
38     sink[i] = x;
39   }
40   sleep(1);  // Make sure the background thread has time to kill the process.
41 // CHECK: hard rss limit exhausted
42 }
43