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: target=thumb{{.*}} && !asan
18
19// https://github.com/google/sanitizers/issues/981
20// UNSUPPORTED: android-26
21
22// Symbolizer needs to allocated memory when reporting.
23// UNSUPPORTED: internal_symbolizer
24
25#include <stdlib.h>
26#include <stdio.h>
27#include <string.h>
28#include <unistd.h>
29
30static const int kMaxNumAllocs = 1 << 9;
31static const int kAllocSize = 1 << 20; // Large enough to go via mmap.
32
33static char *allocs[kMaxNumAllocs];
34
35int main() {
36 int num_allocs = kMaxNumAllocs / 16;
37 for (int i = 0; num_allocs <= kMaxNumAllocs; i++, num_allocs *= 2) {
38 fprintf(stderr, format: "[%d] allocating %d times\n", i, num_allocs);
39 int zero_results = 0;
40 for (int j = 0; j < num_allocs; j++) {
41 if ((j % (num_allocs / 8)) == 0) {
42 usleep(useconds: 100000);
43 fprintf(stderr, format: " [%d]\n", j);
44 }
45 allocs[j] = (char*)malloc(size: kAllocSize);
46 if (allocs[j])
47 memset(s: allocs[j], c: -1, n: kAllocSize);
48 else
49 zero_results++;
50 }
51 if (zero_results)
52 fprintf(stderr, format: "Some of the malloc calls returned null: %d\n",
53 zero_results);
54 if (zero_results != num_allocs)
55 fprintf(stderr, format: "Some of the malloc calls returned non-null: %d\n",
56 num_allocs - zero_results);
57 for (int j = 0; j < num_allocs; j++) {
58 free(ptr: allocs[j]);
59 }
60 }
61}
62
63// CHECK_MAY_RETURN_1: allocating 32 times
64// CHECK_MAY_RETURN_1: Some of the malloc calls returned non-null:
65// CHECK_MAY_RETURN_1: allocating 256 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// CHECK_MAY_RETURN_1: allocating 512 times
69// CHECK_MAY_RETURN_1: Some of the malloc calls returned null:
70// CHECK_MAY_RETURN_1: Some of the malloc calls returned non-null:
71
72// CHECK_MAY_RETURN_0: allocating 32 times
73// CHECK_MAY_RETURN_0: Some of the malloc calls returned non-null:
74// CHECK_MAY_RETURN_0: {{SUMMARY: .*Sanitizer: rss-limit-exceeded}}
75

source code of compiler-rt/test/sanitizer_common/TestCases/Linux/soft_rss_limit_mb_test.cpp