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