blob: 145cc5d05769ee6827a68684cb81f3943fa682c6 [file] [log] [blame]
Stephen Hines86277eb2015-03-23 12:06:32 -07001// 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: %tool_options=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: %tool_options=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
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: %tool_options=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
11
12// FIXME: make it work for other sanitizers.
13// XFAIL: lsan
14// XFAIL: tsan
15// XFAIL: msan
16#include <stdlib.h>
17#include <stdio.h>
18#include <string.h>
19#include <unistd.h>
20
21static const int kMaxNumAllocs = 1 << 9;
22static const int kAllocSize = 1 << 20; // Large enough to go via mmap.
23
24static char *allocs[kMaxNumAllocs];
25
26int main() {
27 int num_allocs = kMaxNumAllocs / 4;
28 for (int i = 0; i < 3; i++, num_allocs *= 2) {
29 fprintf(stderr, "[%d] allocating %d times\n", i, num_allocs);
30 int zero_results = 0;
31 for (int j = 0; j < num_allocs; j++) {
32 if ((j % (num_allocs / 8)) == 0) {
33 usleep(100000);
34 fprintf(stderr, " [%d]\n", j);
35 }
36 allocs[j] = (char*)malloc(kAllocSize);
37 if (allocs[j])
38 memset(allocs[j], -1, kAllocSize);
39 else
40 zero_results++;
41 }
42 if (zero_results)
43 fprintf(stderr, "Some of the malloc calls returned null: %d\n",
44 zero_results);
45 if (zero_results != num_allocs)
46 fprintf(stderr, "Some of the malloc calls returned non-null: %d\n",
47 num_allocs - zero_results);
48 for (int j = 0; j < num_allocs; j++) {
49 free(allocs[j]);
50 }
51 }
52}
53
54// CHECK_MAY_RETURN_1: allocating 128 times
55// CHECK_MAY_RETURN_1: Some of the malloc calls returned non-null: 128
56// CHECK_MAY_RETURN_1: allocating 256 times
57// CHECK_MAY_RETURN_1: Some of the malloc calls returned null:
58// CHECK_MAY_RETURN_1: Some of the malloc calls returned non-null:
59// CHECK_MAY_RETURN_1: allocating 512 times
60// CHECK_MAY_RETURN_1: Some of the malloc calls returned null:
61// CHECK_MAY_RETURN_1: Some of the malloc calls returned non-null:
62
63// CHECK_MAY_RETURN_0: allocating 128 times
64// CHECK_MAY_RETURN_0: Some of the malloc calls returned non-null: 128
65// CHECK_MAY_RETURN_0: allocating 256 times
66// CHECK_MAY_RETURN_0: allocator is terminating the process instead of returning