1 // Check soft_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=soft_rss_limit_mb=220:quarantine_size=1:allocator_may_return_null=1     %run %t 2>&1 | FileCheck %s -check-prefix=CHECK_MAY_RETURN_1
6 // RUN: %env_tool_opts=soft_rss_limit_mb=220:quarantine_size=1:allocator_may_return_null=0 not %run %t 2>&1 | FileCheck %s -check-prefix=CHECK_MAY_RETURN_0 --implicit-check-not="returned null"
7 
8 // This run uses getrusage. We can only test getrusage when allocator_may_return_null=0
9 // because getrusage gives us max-rss, not current-rss.
10 // RUN: %env_tool_opts=soft_rss_limit_mb=220:quarantine_size=1:allocator_may_return_null=0:can_use_proc_maps_statm=0 not %run %t 2>&1 | FileCheck %s -check-prefix=CHECK_MAY_RETURN_0 --implicit-check-not="returned null"
11 // REQUIRES: stable-runtime
12 
13 // Ubsan does not intercept pthread_create.
14 // XFAIL: ubsan
15 
16 // THUMB starts background thead only for Asan.
17 // XFAIL: thumb && !asan
18 
19 // https://github.com/google/sanitizers/issues/981
20 // UNSUPPORTED: android-26
21 
22 #include <stdlib.h>
23 #include <stdio.h>
24 #include <string.h>
25 #include <unistd.h>
26 
27 static const int kMaxNumAllocs = 1 << 9;
28 static const int kAllocSize = 1 << 20;  // Large enough to go via mmap.
29 
30 static char *allocs[kMaxNumAllocs];
31 
main()32 int main() {
33   int num_allocs = kMaxNumAllocs / 16;
34   for (int i = 0; num_allocs <= kMaxNumAllocs; i++, num_allocs *= 2) {
35     fprintf(stderr, "[%d] allocating %d times\n", i, num_allocs);
36     int zero_results = 0;
37     for (int j = 0; j < num_allocs; j++) {
38       if ((j % (num_allocs / 8)) == 0) {
39         usleep(100000);
40         fprintf(stderr, "  [%d]\n", j);
41       }
42       allocs[j] = (char*)malloc(kAllocSize);
43       if (allocs[j])
44         memset(allocs[j], -1, kAllocSize);
45       else
46         zero_results++;
47     }
48     if (zero_results)
49       fprintf(stderr, "Some of the malloc calls returned null: %d\n",
50               zero_results);
51     if (zero_results != num_allocs)
52       fprintf(stderr, "Some of the malloc calls returned non-null: %d\n",
53               num_allocs - zero_results);
54     for (int j = 0; j < num_allocs; j++) {
55       free(allocs[j]);
56     }
57   }
58 }
59 
60 // CHECK_MAY_RETURN_1: allocating 32 times
61 // CHECK_MAY_RETURN_1: Some of the malloc calls returned non-null:
62 // CHECK_MAY_RETURN_1: allocating 256 times
63 // CHECK_MAY_RETURN_1: Some of the malloc calls returned null:
64 // CHECK_MAY_RETURN_1: Some of the malloc calls returned non-null:
65 // CHECK_MAY_RETURN_1: allocating 512 times
66 // CHECK_MAY_RETURN_1: Some of the malloc calls returned null:
67 // CHECK_MAY_RETURN_1: Some of the malloc calls returned non-null:
68 
69 // CHECK_MAY_RETURN_0: allocating 32 times
70 // CHECK_MAY_RETURN_0: Some of the malloc calls returned non-null:
71 // CHECK_MAY_RETURN_0: {{SUMMARY: .*Sanitizer: rss-limit-exceeded}}
72