1// RUN: %clang_scudo %s -o %t
2// RUN: %run %t 2>&1 | FileCheck %s --check-prefix=CHECK-nolimit
3// RUN: %env_scudo_opts="soft_rss_limit_mb=128" %run %t 2>&1 | FileCheck %s --check-prefix=CHECK-nolimit
4// RUN: %env_scudo_opts="hard_rss_limit_mb=128" %run %t 2>&1 | FileCheck %s --check-prefix=CHECK-nolimit
5// RUN: %env_scudo_opts="soft_rss_limit_mb=32:allocator_may_return_null=0" not %run %t 2>&1 | FileCheck %s --check-prefix=CHECK-softlimit
6// RUN: %env_scudo_opts="soft_rss_limit_mb=32:allocator_may_return_null=1" %run %t 2>&1 | FileCheck %s --check-prefix=CHECK-softlimit-returnnull
7// RUN: %env_scudo_opts="soft_rss_limit_mb=32:allocator_may_return_null=0:can_use_proc_maps_statm=0" not %run %t 2>&1 | FileCheck %s --check-prefix=CHECK-softlimit
8// RUN: %env_scudo_opts="soft_rss_limit_mb=32:allocator_may_return_null=1:can_use_proc_maps_statm=0" %run %t 2>&1 | FileCheck %s --check-prefix=CHECK-softlimit-returnnull
9// RUN: %env_scudo_opts="hard_rss_limit_mb=32:allocator_may_return_null=0" not %run %t 2>&1 | FileCheck %s --check-prefix=CHECK-hardlimit
10// RUN: %env_scudo_opts="hard_rss_limit_mb=32:allocator_may_return_null=1" not %run %t 2>&1 | FileCheck %s --check-prefix=CHECK-hardlimit
11// RUN: %env_scudo_opts="hard_rss_limit_mb=32:allocator_may_return_null=0:can_use_proc_maps_statm=0" not %run %t 2>&1 | FileCheck %s --check-prefix=CHECK-hardlimit
12// RUN: %env_scudo_opts="hard_rss_limit_mb=32:allocator_may_return_null=1:can_use_proc_maps_statm=0" not %run %t 2>&1 | FileCheck %s --check-prefix=CHECK-hardlimit
13
14// Tests that the soft and hard RSS limits work as intended. Without limit or
15// with a high limit, the test should pass without any malloc returning NULL or
16// the program dying.
17// If a limit is specified, it should return some NULL or die depending on
18// allocator_may_return_null. This should also work without statm.
19
20#include <stdio.h>
21#include <stdlib.h>
22#include <string.h>
23#include <unistd.h>
24
25static const size_t kNumAllocs = 64;
26static const size_t kAllocSize = 1 << 20; // 1MB.
27
28static void *allocs[kNumAllocs];
29
30int main(int argc, char *argv[]) {
31 int returned_null = 0;
32 for (int i = 0; i < kNumAllocs; i++) {
33 // sleep for 100ms every 8 allocations, to allow the RSS check to catch up.
34 if (i != 0 && (i & 0x7) == 0)
35 usleep(100000);
36 allocs[i] = malloc(kAllocSize);
37 if (allocs[i])
38 memset(allocs[i], 0xff, kAllocSize); // Dirty the pages.
39 else
40 returned_null++;
41 }
42 for (int i = 0; i < kNumAllocs; i++)
43 free(allocs[i]);
44 if (returned_null == 0)
45 printf("All malloc calls succeeded\n");
46 else
47 printf("%d malloc calls returned NULL\n", returned_null);
48 return 0;
49}
50
51// CHECK-nolimit: All malloc calls succeeded
52// CHECK-softlimit: soft RSS limit exhausted
53// CHECK-softlimit-NOT: malloc calls
54// CHECK-softlimit-returnnull: soft RSS limit exhausted
55// CHECK-softlimit-returnnull: malloc calls returned NULL
56// CHECK-hardlimit: hard RSS limit exhausted
57// CHECK-hardlimit-NOT: malloc calls
58