101 lines · cpp
1// Test the behavior of malloc/calloc/realloc when the allocation causes OOM2// in the secondary allocator.3// By default (allocator_may_return_null=0) the process should crash.4// With allocator_may_return_null=1 the allocator should return 0.5// Set the limit to 20.5T on 64 bits to account for ASan shadow memory,6// allocator buffers etc. so that the test allocation of ~1T will trigger OOM.7// Limit this test to Linux since we're relying on allocator internal8// limits (shadow memory size, allocation limits etc.)9 10// TODO(boomanaiden154): Switch back to using env options instead of explicitly11// exporting when the underlying bug causing ulimits to not apply to run lines12// that use env options is fixed.13 14// RUN: %clangxx_asan -O0 %s -o %t15// RUN: ulimit -v 2202429030416// RUN: not %run %t malloc 2>&1 \17// RUN: | FileCheck %s --check-prefixes=CHECK-MALLOC,CHECK-CRASH18// RUN: %export_asan_opts=allocator_may_return_null=019// RUN: not %run %t malloc 2>&1 \20// RUN: | FileCheck %s --check-prefixes=CHECK-MALLOC,CHECK-CRASH21// RUN: %export_asan_opts=allocator_may_return_null=122// RUN: %run %t malloc 2>&1 \23// RUN: | FileCheck %s --check-prefixes=CHECK-MALLOC,CHECK-NULL24// RUN: %export_asan_opts=allocator_may_return_null=025// RUN: not %run %t calloc 2>&1 \26// RUN: | FileCheck %s --check-prefixes=CHECK-CALLOC,CHECK-CRASH27// RUN: %export_asan_opts=allocator_may_return_null=128// RUN: %run %t calloc 2>&1 \29// RUN: | FileCheck %s --check-prefixes=CHECK-CALLOC,CHECK-NULL30// RUN: %export_asan_opts=allocator_may_return_null=031// RUN: not %run %t realloc 2>&1 \32// RUN: | FileCheck %s --check-prefixes=CHECK-REALLOC,CHECK-CRASH33// RUN: %export_asan_opts=allocator_may_return_null=134// RUN: %run %t realloc 2>&1 \35// RUN: | FileCheck %s --check-prefixes=CHECK-REALLOC,CHECK-NULL36// RUN: %export_asan_opts=allocator_may_return_null=037// RUN: not %run %t realloc-after-malloc 2>&1 \38// RUN: | FileCheck %s --check-prefixes=CHECK-MALLOC-REALLOC,CHECK-CRASH39// RUN: %export_asan_opts=allocator_may_return_null=140// RUN: %run %t realloc-after-malloc 2>&1 \41// RUN: | FileCheck %s --check-prefixes=CHECK-MALLOC-REALLOC,CHECK-NULL42 43// ASan shadow memory on s390 is too large for this test.44// AArch64 bots fail on this test.45// TODO(alekseys): Android lit do not run ulimit on device.46// REQUIRES: shell, shadow-scale-347// UNSUPPORTED: android, target={{(s390|aarch64|powerpc64le).*}}48 49#include <stdlib.h>50#include <string.h>51#include <stdio.h>52#include <assert.h>53 54int main(int argc, char **argv) {55 assert(argc == 2);56 const char *action = argv[1];57 fprintf(stderr, "%s:\n", action);58 59 // Allocate just a bit less than max allocation size enforced by ASan's60 // allocator (currently 1T and 3G).61 const size_t size =62#if __LP64__63 (1ULL << 40) - (1ULL << 30);64#else65 (3ULL << 30) - (1ULL << 20);66#endif67 68 void *x = 0;69 70 if (!strcmp(action, "malloc")) {71 x = malloc(size);72 } else if (!strcmp(action, "calloc")) {73 x = calloc(size / 4, 4);74 } else if (!strcmp(action, "realloc")) {75 x = realloc(0, size);76 } else if (!strcmp(action, "realloc-after-malloc")) {77 char *t = (char*)malloc(100);78 *t = 42;79 x = realloc(t, size);80 assert(*t == 42);81 free(t);82 } else {83 assert(0);84 }85 86 // The NULL pointer is printed differently on different systems, while (long)087 // is always the same.88 fprintf(stderr, "x: %lx\n", (long)x);89 free(x);90 91 return x != 0;92}93 94// CHECK-MALLOC: malloc:95// CHECK-CALLOC: calloc:96// CHECK-REALLOC: realloc:97// CHECK-MALLOC-REALLOC: realloc-after-malloc:98 99// CHECK-CRASH: SUMMARY: AddressSanitizer: out-of-memory100// CHECK-NULL: x: 0101