58 lines · c
1// RUN: %clang_scudo %s -o %t2// RUN: %run %t 2>&1 | FileCheck %s --check-prefix=CHECK-nolimit3// RUN: %env_scudo_opts="soft_rss_limit_mb=128" %run %t 2>&1 | FileCheck %s --check-prefix=CHECK-nolimit4// RUN: %env_scudo_opts="hard_rss_limit_mb=128" %run %t 2>&1 | FileCheck %s --check-prefix=CHECK-nolimit5// RUN: %env_scudo_opts="soft_rss_limit_mb=32:allocator_may_return_null=0" not %run %t 2>&1 | FileCheck %s --check-prefix=CHECK-softlimit6// RUN: %env_scudo_opts="soft_rss_limit_mb=32:allocator_may_return_null=1" %run %t 2>&1 | FileCheck %s --check-prefix=CHECK-softlimit-returnnull7// 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-softlimit8// 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-returnnull9// RUN: %env_scudo_opts="hard_rss_limit_mb=32:allocator_may_return_null=0" not %run %t 2>&1 | FileCheck %s --check-prefix=CHECK-hardlimit10// RUN: %env_scudo_opts="hard_rss_limit_mb=32:allocator_may_return_null=1" not %run %t 2>&1 | FileCheck %s --check-prefix=CHECK-hardlimit11// 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-hardlimit12// 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-hardlimit13 14// Tests that the soft and hard RSS limits work as intended. Without limit or15// with a high limit, the test should pass without any malloc returning NULL or16// the program dying.17// If a limit is specified, it should return some NULL or die depending on18// 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 else40 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 else47 printf("%d malloc calls returned NULL\n", returned_null);48 return 0;49}50 51// CHECK-nolimit: All malloc calls succeeded52// CHECK-softlimit: soft RSS limit exhausted53// CHECK-softlimit-NOT: malloc calls54// CHECK-softlimit-returnnull: soft RSS limit exhausted55// CHECK-softlimit-returnnull: malloc calls returned NULL56// CHECK-hardlimit: hard RSS limit exhausted57// CHECK-hardlimit-NOT: malloc calls58