1bd33d3ab168fcbcfe09a3fd6a971994b481e89d0Kostya Serebryany// Test the behavior of malloc/calloc/realloc when the allocation size is huge. 22d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines// By default (allocator_may_return_null=0) the process should crash. 3bd33d3ab168fcbcfe09a3fd6a971994b481e89d0Kostya Serebryany// With allocator_may_return_null=1 the allocator should return 0. 4bd33d3ab168fcbcfe09a3fd6a971994b481e89d0Kostya Serebryany// 5bd33d3ab168fcbcfe09a3fd6a971994b481e89d0Kostya Serebryany// RUN: %clangxx_tsan -O0 %s -o %t 62d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines// RUN: not %run %t malloc 2>&1 | FileCheck %s --check-prefix=CHECK-mCRASH 72d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines// RUN: TSAN_OPTIONS=allocator_may_return_null=0 not %run %t malloc 2>&1 | FileCheck %s --check-prefix=CHECK-mCRASH 82d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines// RUN: TSAN_OPTIONS=allocator_may_return_null=0 not %run %t calloc 2>&1 | FileCheck %s --check-prefix=CHECK-cCRASH 92d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines// RUN: TSAN_OPTIONS=allocator_may_return_null=0 not %run %t calloc-overflow 2>&1 | FileCheck %s --check-prefix=CHECK-coCRASH 102d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines// RUN: TSAN_OPTIONS=allocator_may_return_null=0 not %run %t realloc 2>&1 | FileCheck %s --check-prefix=CHECK-rCRASH 112d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines// RUN: TSAN_OPTIONS=allocator_may_return_null=0 not %run %t realloc-after-malloc 2>&1 | FileCheck %s --check-prefix=CHECK-mrCRASH 12bd33d3ab168fcbcfe09a3fd6a971994b481e89d0Kostya Serebryany 13bd33d3ab168fcbcfe09a3fd6a971994b481e89d0Kostya Serebryany#include <limits.h> 14bd33d3ab168fcbcfe09a3fd6a971994b481e89d0Kostya Serebryany#include <stdlib.h> 15bd33d3ab168fcbcfe09a3fd6a971994b481e89d0Kostya Serebryany#include <string.h> 16bd33d3ab168fcbcfe09a3fd6a971994b481e89d0Kostya Serebryany#include <stdio.h> 17bd33d3ab168fcbcfe09a3fd6a971994b481e89d0Kostya Serebryany#include <assert.h> 18bd33d3ab168fcbcfe09a3fd6a971994b481e89d0Kostya Serebryany#include <limits> 19bd33d3ab168fcbcfe09a3fd6a971994b481e89d0Kostya Serebryanyint main(int argc, char **argv) { 20bd33d3ab168fcbcfe09a3fd6a971994b481e89d0Kostya Serebryany volatile size_t size = std::numeric_limits<size_t>::max() - 10000; 21bd33d3ab168fcbcfe09a3fd6a971994b481e89d0Kostya Serebryany assert(argc == 2); 22bd33d3ab168fcbcfe09a3fd6a971994b481e89d0Kostya Serebryany char *x = 0; 23bd33d3ab168fcbcfe09a3fd6a971994b481e89d0Kostya Serebryany if (!strcmp(argv[1], "malloc")) { 24bd33d3ab168fcbcfe09a3fd6a971994b481e89d0Kostya Serebryany fprintf(stderr, "malloc:\n"); 25bd33d3ab168fcbcfe09a3fd6a971994b481e89d0Kostya Serebryany x = (char*)malloc(size); 26bd33d3ab168fcbcfe09a3fd6a971994b481e89d0Kostya Serebryany } 27bd33d3ab168fcbcfe09a3fd6a971994b481e89d0Kostya Serebryany if (!strcmp(argv[1], "calloc")) { 28bd33d3ab168fcbcfe09a3fd6a971994b481e89d0Kostya Serebryany fprintf(stderr, "calloc:\n"); 29bd33d3ab168fcbcfe09a3fd6a971994b481e89d0Kostya Serebryany x = (char*)calloc(size / 4, 4); 30bd33d3ab168fcbcfe09a3fd6a971994b481e89d0Kostya Serebryany } 31bd33d3ab168fcbcfe09a3fd6a971994b481e89d0Kostya Serebryany 32bd33d3ab168fcbcfe09a3fd6a971994b481e89d0Kostya Serebryany if (!strcmp(argv[1], "calloc-overflow")) { 33bd33d3ab168fcbcfe09a3fd6a971994b481e89d0Kostya Serebryany fprintf(stderr, "calloc-overflow:\n"); 34bd33d3ab168fcbcfe09a3fd6a971994b481e89d0Kostya Serebryany volatile size_t kMaxSizeT = std::numeric_limits<size_t>::max(); 35bd33d3ab168fcbcfe09a3fd6a971994b481e89d0Kostya Serebryany size_t kArraySize = 4096; 36bd33d3ab168fcbcfe09a3fd6a971994b481e89d0Kostya Serebryany volatile size_t kArraySize2 = kMaxSizeT / kArraySize + 10; 37bd33d3ab168fcbcfe09a3fd6a971994b481e89d0Kostya Serebryany x = (char*)calloc(kArraySize, kArraySize2); 38bd33d3ab168fcbcfe09a3fd6a971994b481e89d0Kostya Serebryany } 39bd33d3ab168fcbcfe09a3fd6a971994b481e89d0Kostya Serebryany 40bd33d3ab168fcbcfe09a3fd6a971994b481e89d0Kostya Serebryany if (!strcmp(argv[1], "realloc")) { 41bd33d3ab168fcbcfe09a3fd6a971994b481e89d0Kostya Serebryany fprintf(stderr, "realloc:\n"); 42bd33d3ab168fcbcfe09a3fd6a971994b481e89d0Kostya Serebryany x = (char*)realloc(0, size); 43bd33d3ab168fcbcfe09a3fd6a971994b481e89d0Kostya Serebryany } 44bd33d3ab168fcbcfe09a3fd6a971994b481e89d0Kostya Serebryany if (!strcmp(argv[1], "realloc-after-malloc")) { 45bd33d3ab168fcbcfe09a3fd6a971994b481e89d0Kostya Serebryany fprintf(stderr, "realloc-after-malloc:\n"); 46bd33d3ab168fcbcfe09a3fd6a971994b481e89d0Kostya Serebryany char *t = (char*)malloc(100); 47bd33d3ab168fcbcfe09a3fd6a971994b481e89d0Kostya Serebryany *t = 42; 48bd33d3ab168fcbcfe09a3fd6a971994b481e89d0Kostya Serebryany x = (char*)realloc(t, size); 49bd33d3ab168fcbcfe09a3fd6a971994b481e89d0Kostya Serebryany assert(*t == 42); 50bd33d3ab168fcbcfe09a3fd6a971994b481e89d0Kostya Serebryany } 51bd33d3ab168fcbcfe09a3fd6a971994b481e89d0Kostya Serebryany fprintf(stderr, "x: %p\n", x); 52bd33d3ab168fcbcfe09a3fd6a971994b481e89d0Kostya Serebryany return x != 0; 53bd33d3ab168fcbcfe09a3fd6a971994b481e89d0Kostya Serebryany} 54bd33d3ab168fcbcfe09a3fd6a971994b481e89d0Kostya Serebryany// CHECK-mCRASH: malloc: 55bd33d3ab168fcbcfe09a3fd6a971994b481e89d0Kostya Serebryany// CHECK-mCRASH: ThreadSanitizer's allocator is terminating the process 56bd33d3ab168fcbcfe09a3fd6a971994b481e89d0Kostya Serebryany// CHECK-cCRASH: calloc: 57bd33d3ab168fcbcfe09a3fd6a971994b481e89d0Kostya Serebryany// CHECK-cCRASH: ThreadSanitizer's allocator is terminating the process 58bd33d3ab168fcbcfe09a3fd6a971994b481e89d0Kostya Serebryany// CHECK-coCRASH: calloc-overflow: 59bd33d3ab168fcbcfe09a3fd6a971994b481e89d0Kostya Serebryany// CHECK-coCRASH: ThreadSanitizer's allocator is terminating the process 60bd33d3ab168fcbcfe09a3fd6a971994b481e89d0Kostya Serebryany// CHECK-rCRASH: realloc: 61bd33d3ab168fcbcfe09a3fd6a971994b481e89d0Kostya Serebryany// CHECK-rCRASH: ThreadSanitizer's allocator is terminating the process 62bd33d3ab168fcbcfe09a3fd6a971994b481e89d0Kostya Serebryany// CHECK-mrCRASH: realloc-after-malloc: 63bd33d3ab168fcbcfe09a3fd6a971994b481e89d0Kostya Serebryany// CHECK-mrCRASH: ThreadSanitizer's allocator is terminating the process 64bd33d3ab168fcbcfe09a3fd6a971994b481e89d0Kostya Serebryany 65