forked from OSchip/llvm-project
				
			[sanitizer] make the allocator crash instead of returning 0 on huge size (controlled by the allocator_may_return_null flag)
llvm-svn: 190127
This commit is contained in:
		
							parent
							
								
									36c28ce38d
								
							
						
					
					
						commit
						ada5a7b7ef
					
				| 
						 | 
					@ -345,7 +345,7 @@ static void *Allocate(uptr size, uptr alignment, StackTrace *stack,
 | 
				
			||||||
  if (size > kMaxAllowedMallocSize || needed_size > kMaxAllowedMallocSize) {
 | 
					  if (size > kMaxAllowedMallocSize || needed_size > kMaxAllowedMallocSize) {
 | 
				
			||||||
    Report("WARNING: AddressSanitizer failed to allocate %p bytes\n",
 | 
					    Report("WARNING: AddressSanitizer failed to allocate %p bytes\n",
 | 
				
			||||||
           (void*)size);
 | 
					           (void*)size);
 | 
				
			||||||
    return 0;
 | 
					    return AllocatorReturnNull();
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  AsanThread *t = GetCurrentThread();
 | 
					  AsanThread *t = GetCurrentThread();
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -0,0 +1,65 @@
 | 
				
			||||||
 | 
					// Test the behavior of malloc/calloc/realloc when the allocation size is huge.
 | 
				
			||||||
 | 
					// By default (allocator_may_return_null=0) the process shoudl crash.
 | 
				
			||||||
 | 
					// With allocator_may_return_null=1 the allocator should return 0.
 | 
				
			||||||
 | 
					//
 | 
				
			||||||
 | 
					// RUN: %clangxx_asan -O0 %s -o %t
 | 
				
			||||||
 | 
					// RUN: not %t malloc 2>&1 | FileCheck %s --check-prefix=CHECK-mCRASH
 | 
				
			||||||
 | 
					// RUN: ASAN_OPTIONS=allocator_may_return_null=0 not %t malloc 2>&1 | FileCheck %s --check-prefix=CHECK-mCRASH
 | 
				
			||||||
 | 
					// RUN: ASAN_OPTIONS=allocator_may_return_null=1     %t malloc 2>&1 | FileCheck %s --check-prefix=CHECK-mNULL
 | 
				
			||||||
 | 
					// RUN: ASAN_OPTIONS=allocator_may_return_null=0 not %t calloc 2>&1 | FileCheck %s --check-prefix=CHECK-cCRASH
 | 
				
			||||||
 | 
					// RUN: ASAN_OPTIONS=allocator_may_return_null=1     %t calloc 2>&1 | FileCheck %s --check-prefix=CHECK-cNULL
 | 
				
			||||||
 | 
					// RUN: ASAN_OPTIONS=allocator_may_return_null=0 not %t realloc 2>&1 | FileCheck %s --check-prefix=CHECK-rCRASH
 | 
				
			||||||
 | 
					// RUN: ASAN_OPTIONS=allocator_may_return_null=1     %t realloc 2>&1 | FileCheck %s --check-prefix=CHECK-rNULL
 | 
				
			||||||
 | 
					// RUN: ASAN_OPTIONS=allocator_may_return_null=0 not %t realloc-after-malloc 2>&1 | FileCheck %s --check-prefix=CHECK-mrCRASH
 | 
				
			||||||
 | 
					// RUN: ASAN_OPTIONS=allocator_may_return_null=1     %t realloc-after-malloc 2>&1 | FileCheck %s --check-prefix=CHECK-mrNULL
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#include <limits.h>
 | 
				
			||||||
 | 
					#include <stdlib.h>
 | 
				
			||||||
 | 
					#include <string.h>
 | 
				
			||||||
 | 
					#include <stdio.h>
 | 
				
			||||||
 | 
					#include <assert.h>
 | 
				
			||||||
 | 
					#include <limits>
 | 
				
			||||||
 | 
					int main(int argc, char **argv) {
 | 
				
			||||||
 | 
					  volatile size_t size = std::numeric_limits<size_t>::max() - 10000;
 | 
				
			||||||
 | 
					  assert(argc == 2);
 | 
				
			||||||
 | 
					  char *x = 0;
 | 
				
			||||||
 | 
					  if (!strcmp(argv[1], "malloc")) {
 | 
				
			||||||
 | 
					    fprintf(stderr, "malloc:\n");
 | 
				
			||||||
 | 
					    x = (char*)malloc(size);
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					  if (!strcmp(argv[1], "calloc")) {
 | 
				
			||||||
 | 
					    fprintf(stderr, "calloc:\n");
 | 
				
			||||||
 | 
					    x = (char*)calloc(size / 4, 4);
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  if (!strcmp(argv[1], "realloc")) {
 | 
				
			||||||
 | 
					    fprintf(stderr, "realloc:\n");
 | 
				
			||||||
 | 
					    x = (char*)realloc(0, size);
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					  if (!strcmp(argv[1], "realloc-after-malloc")) {
 | 
				
			||||||
 | 
					    fprintf(stderr, "realloc-after-malloc:\n");
 | 
				
			||||||
 | 
					    char *t = (char*)malloc(100);
 | 
				
			||||||
 | 
					    *t = 42;
 | 
				
			||||||
 | 
					    x = (char*)realloc(t, size);
 | 
				
			||||||
 | 
					    assert(*t == 42);
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					  fprintf(stderr, "x: %p\n", x);
 | 
				
			||||||
 | 
					  return x != 0;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					// CHECK-mCRASH: malloc:
 | 
				
			||||||
 | 
					// CHECK-mCRASH: AddressSanitizer's allocator is terminating the process
 | 
				
			||||||
 | 
					// CHECK-cCRASH: calloc:
 | 
				
			||||||
 | 
					// CHECK-cCRASH: AddressSanitizer's allocator is terminating the process
 | 
				
			||||||
 | 
					// CHECK-rCRASH: realloc:
 | 
				
			||||||
 | 
					// CHECK-rCRASH: AddressSanitizer's allocator is terminating the process
 | 
				
			||||||
 | 
					// CHECK-mrCRASH: realloc-after-malloc:
 | 
				
			||||||
 | 
					// CHECK-mrCRASH: AddressSanitizer's allocator is terminating the process
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// CHECK-mNULL: malloc:
 | 
				
			||||||
 | 
					// CHECK-mNULL: x: (nil)
 | 
				
			||||||
 | 
					// CHECK-cNULL: calloc:
 | 
				
			||||||
 | 
					// CHECK-cNULL: x: (nil)
 | 
				
			||||||
 | 
					// CHECK-rNULL: realloc:
 | 
				
			||||||
 | 
					// CHECK-rNULL: x: (nil)
 | 
				
			||||||
 | 
					// CHECK-mrNULL: realloc-after-malloc:
 | 
				
			||||||
 | 
					// CHECK-mrNULL: x: (nil)
 | 
				
			||||||
| 
						 | 
					@ -227,26 +227,6 @@ TEST(AddressSanitizer, BitFieldNegativeTest) {
 | 
				
			||||||
  delete Ident(x);
 | 
					  delete Ident(x);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
static size_t kOOMAllocationSize =
 | 
					 | 
				
			||||||
  SANITIZER_WORDSIZE == 64 ? (size_t)(1ULL << 48) : (0xf0000000);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
TEST(AddressSanitizer, OutOfMemoryTest) {
 | 
					 | 
				
			||||||
  EXPECT_EQ(0, realloc(0, kOOMAllocationSize));
 | 
					 | 
				
			||||||
  EXPECT_EQ(0, realloc(0, ~Ident(0)));
 | 
					 | 
				
			||||||
  EXPECT_EQ(0, malloc(kOOMAllocationSize));
 | 
					 | 
				
			||||||
  EXPECT_EQ(0, malloc(~Ident(0)));
 | 
					 | 
				
			||||||
  EXPECT_EQ(0, calloc(1, kOOMAllocationSize));
 | 
					 | 
				
			||||||
  EXPECT_EQ(0, calloc(1, ~Ident(0)));
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
TEST(AddressSanitizer, BadReallocTest) {
 | 
					 | 
				
			||||||
  int *a = (int*)Ident(malloc(100));
 | 
					 | 
				
			||||||
  a[0] = 42;
 | 
					 | 
				
			||||||
  EXPECT_EQ(0, realloc(a, kOOMAllocationSize));
 | 
					 | 
				
			||||||
  EXPECT_EQ(42, a[0]);
 | 
					 | 
				
			||||||
  free(a);
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
#if ASAN_NEEDS_SEGV
 | 
					#if ASAN_NEEDS_SEGV
 | 
				
			||||||
namespace {
 | 
					namespace {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -14,6 +14,7 @@
 | 
				
			||||||
#include "sanitizer_allocator.h"
 | 
					#include "sanitizer_allocator.h"
 | 
				
			||||||
#include "sanitizer_allocator_internal.h"
 | 
					#include "sanitizer_allocator_internal.h"
 | 
				
			||||||
#include "sanitizer_common.h"
 | 
					#include "sanitizer_common.h"
 | 
				
			||||||
 | 
					#include "sanitizer_flags.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
namespace __sanitizer {
 | 
					namespace __sanitizer {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -139,4 +140,14 @@ bool CallocShouldReturnNullDueToOverflow(uptr size, uptr n) {
 | 
				
			||||||
  return (max / size) < n;
 | 
					  return (max / size) < n;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					void *AllocatorReturnNull() {
 | 
				
			||||||
 | 
					  if (common_flags()->allocator_may_return_null)
 | 
				
			||||||
 | 
					    return 0;
 | 
				
			||||||
 | 
					  Report("%s's allocator is terminating the process instead of returning 0\n",
 | 
				
			||||||
 | 
					         SanitizerToolName);
 | 
				
			||||||
 | 
					  Report("If you don't like this behavior set allocator_may_return_null=1\n");
 | 
				
			||||||
 | 
					  CHECK(0);
 | 
				
			||||||
 | 
					  return 0;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
}  // namespace __sanitizer
 | 
					}  // namespace __sanitizer
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -23,6 +23,9 @@
 | 
				
			||||||
 | 
					
 | 
				
			||||||
namespace __sanitizer {
 | 
					namespace __sanitizer {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// Depending on allocator_may_return_null either return 0 or crash.
 | 
				
			||||||
 | 
					void *AllocatorReturnNull();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// SizeClassMap maps allocation sizes into size classes and back.
 | 
					// SizeClassMap maps allocation sizes into size classes and back.
 | 
				
			||||||
// Class 0 corresponds to size 0.
 | 
					// Class 0 corresponds to size 0.
 | 
				
			||||||
// Classes 1 - 16 correspond to sizes 16 to 256 (size = class_id * 16).
 | 
					// Classes 1 - 16 correspond to sizes 16 to 256 (size = class_id * 16).
 | 
				
			||||||
| 
						 | 
					@ -941,7 +944,7 @@ class LargeMmapAllocator {
 | 
				
			||||||
    uptr map_size = RoundUpMapSize(size);
 | 
					    uptr map_size = RoundUpMapSize(size);
 | 
				
			||||||
    if (alignment > page_size_)
 | 
					    if (alignment > page_size_)
 | 
				
			||||||
      map_size += alignment;
 | 
					      map_size += alignment;
 | 
				
			||||||
    if (map_size < size) return 0;  // Overflow.
 | 
					    if (map_size < size) return AllocatorReturnNull();  // Overflow.
 | 
				
			||||||
    uptr map_beg = reinterpret_cast<uptr>(
 | 
					    uptr map_beg = reinterpret_cast<uptr>(
 | 
				
			||||||
        MmapOrDie(map_size, "LargeMmapAllocator"));
 | 
					        MmapOrDie(map_size, "LargeMmapAllocator"));
 | 
				
			||||||
    MapUnmapCallback().OnMap(map_beg, map_size);
 | 
					    MapUnmapCallback().OnMap(map_beg, map_size);
 | 
				
			||||||
| 
						 | 
					@ -1176,7 +1179,7 @@ class CombinedAllocator {
 | 
				
			||||||
    if (size == 0)
 | 
					    if (size == 0)
 | 
				
			||||||
      size = 1;
 | 
					      size = 1;
 | 
				
			||||||
    if (size + alignment < size)
 | 
					    if (size + alignment < size)
 | 
				
			||||||
      return 0;
 | 
					      return AllocatorReturnNull();
 | 
				
			||||||
    if (alignment > 8)
 | 
					    if (alignment > 8)
 | 
				
			||||||
      size = RoundUpTo(size, alignment);
 | 
					      size = RoundUpTo(size, alignment);
 | 
				
			||||||
    void *res;
 | 
					    void *res;
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -31,6 +31,7 @@ void ParseCommonFlagsFromString(const char *str) {
 | 
				
			||||||
  ParseFlag(str, &f->log_path, "log_path");
 | 
					  ParseFlag(str, &f->log_path, "log_path");
 | 
				
			||||||
  ParseFlag(str, &f->detect_leaks, "detect_leaks");
 | 
					  ParseFlag(str, &f->detect_leaks, "detect_leaks");
 | 
				
			||||||
  ParseFlag(str, &f->leak_check_at_exit, "leak_check_at_exit");
 | 
					  ParseFlag(str, &f->leak_check_at_exit, "leak_check_at_exit");
 | 
				
			||||||
 | 
					  ParseFlag(str, &f->allocator_may_return_null, "allocator_may_return_null");
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
static bool GetFlagValue(const char *env, const char *name,
 | 
					static bool GetFlagValue(const char *env, const char *name,
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -45,6 +45,8 @@ struct CommonFlags {
 | 
				
			||||||
  // detect_leaks=false, or if __lsan_do_leak_check() is called before the
 | 
					  // detect_leaks=false, or if __lsan_do_leak_check() is called before the
 | 
				
			||||||
  // handler has a chance to run.
 | 
					  // handler has a chance to run.
 | 
				
			||||||
  bool leak_check_at_exit;
 | 
					  bool leak_check_at_exit;
 | 
				
			||||||
 | 
					  // If false, the allocator will crash instead of returning 0 on out-of-memory.
 | 
				
			||||||
 | 
					  bool allocator_may_return_null;
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
extern CommonFlags common_flags_dont_use_directly;
 | 
					extern CommonFlags common_flags_dont_use_directly;
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -14,6 +14,7 @@
 | 
				
			||||||
#include "sanitizer_common/sanitizer_allocator.h"
 | 
					#include "sanitizer_common/sanitizer_allocator.h"
 | 
				
			||||||
#include "sanitizer_common/sanitizer_allocator_internal.h"
 | 
					#include "sanitizer_common/sanitizer_allocator_internal.h"
 | 
				
			||||||
#include "sanitizer_common/sanitizer_common.h"
 | 
					#include "sanitizer_common/sanitizer_common.h"
 | 
				
			||||||
 | 
					#include "sanitizer_common/sanitizer_flags.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#include "sanitizer_test_utils.h"
 | 
					#include "sanitizer_test_utils.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -416,12 +417,20 @@ void TestCombinedAllocator() {
 | 
				
			||||||
  memset(&cache, 0, sizeof(cache));
 | 
					  memset(&cache, 0, sizeof(cache));
 | 
				
			||||||
  a->InitCache(&cache);
 | 
					  a->InitCache(&cache);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  bool allocator_may_return_null = common_flags()->allocator_may_return_null;
 | 
				
			||||||
 | 
					  common_flags()->allocator_may_return_null = true;
 | 
				
			||||||
  EXPECT_EQ(a->Allocate(&cache, -1, 1), (void*)0);
 | 
					  EXPECT_EQ(a->Allocate(&cache, -1, 1), (void*)0);
 | 
				
			||||||
  EXPECT_EQ(a->Allocate(&cache, -1, 1024), (void*)0);
 | 
					  EXPECT_EQ(a->Allocate(&cache, -1, 1024), (void*)0);
 | 
				
			||||||
  EXPECT_EQ(a->Allocate(&cache, (uptr)-1 - 1024, 1), (void*)0);
 | 
					  EXPECT_EQ(a->Allocate(&cache, (uptr)-1 - 1024, 1), (void*)0);
 | 
				
			||||||
  EXPECT_EQ(a->Allocate(&cache, (uptr)-1 - 1024, 1024), (void*)0);
 | 
					  EXPECT_EQ(a->Allocate(&cache, (uptr)-1 - 1024, 1024), (void*)0);
 | 
				
			||||||
  EXPECT_EQ(a->Allocate(&cache, (uptr)-1 - 1023, 1024), (void*)0);
 | 
					  EXPECT_EQ(a->Allocate(&cache, (uptr)-1 - 1023, 1024), (void*)0);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  common_flags()->allocator_may_return_null = false;
 | 
				
			||||||
 | 
					  EXPECT_DEATH(a->Allocate(&cache, -1, 1),
 | 
				
			||||||
 | 
					               "allocator is terminating the process");
 | 
				
			||||||
 | 
					  // Restore the original value.
 | 
				
			||||||
 | 
					  common_flags()->allocator_may_return_null = allocator_may_return_null;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  const uptr kNumAllocs = 100000;
 | 
					  const uptr kNumAllocs = 100000;
 | 
				
			||||||
  const uptr kNumIter = 10;
 | 
					  const uptr kNumIter = 10;
 | 
				
			||||||
  for (uptr iter = 0; iter < kNumIter; iter++) {
 | 
					  for (uptr iter = 0; iter < kNumIter; iter++) {
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue