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=256"                                                          %run %t 2>&1 | FileCheck %s --check-prefix=CHECK-nolimit
4 // RUN: %env_scudo_opts="hard_rss_limit_mb=256"                                                          %run %t 2>&1 | FileCheck %s --check-prefix=CHECK-nolimit
5 // RUN: %env_scudo_opts="soft_rss_limit_mb=64: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=64: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=64: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=64: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=64: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=64: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=64: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=64: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 
25 static const size_t kNumAllocs = 128;
26 static const size_t kAllocSize = 1 << 20;  // 1MB.
27 
28 static void *allocs[kNumAllocs];
29 
main(int argc,char * argv[])30 int main(int argc, char *argv[]) {
31   int returned_null = 0;
32   for (int i = 0; i < kNumAllocs; i++) {
33     if ((i & 0xf) == 0)
34       usleep(50000);
35     allocs[i] = malloc(kAllocSize);
36     if (allocs[i])
37       memset(allocs[i], 0xff, kAllocSize);  // Dirty the pages.
38     else
39       returned_null++;
40   }
41   for (int i = 0; i < kNumAllocs; i++)
42     free(allocs[i]);
43   if (returned_null == 0)
44     printf("All malloc calls succeeded\n");
45   else
46     printf("%d malloc calls returned NULL\n", returned_null);
47   return 0;
48 }
49 
50 // CHECK-nolimit: All malloc calls succeeded
51 // CHECK-softlimit: soft RSS limit exhausted
52 // CHECK-softlimit-NOT: malloc calls
53 // CHECK-softlimit-returnnull: soft RSS limit exhausted
54 // CHECK-softlimit-returnnull: malloc calls returned NULL
55 // CHECK-hardlimit: hard RSS limit exhausted
56 // CHECK-hardlimit-NOT: malloc calls
57