• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===-- sanitizer_allocator.cc --------------------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file is shared between AddressSanitizer and ThreadSanitizer
11 // run-time libraries.
12 // This allocator that is used inside run-times.
13 //===----------------------------------------------------------------------===//
14 #include "sanitizer_common.h"
15 
16 // FIXME: We should probably use more low-level allocator that would
17 // mmap some pages and split them into chunks to fulfill requests.
18 #if defined(__linux__) && !defined(__ANDROID__)
19 extern "C" void *__libc_malloc(__sanitizer::uptr size);
20 extern "C" void __libc_free(void *ptr);
21 # define LIBC_MALLOC __libc_malloc
22 # define LIBC_FREE __libc_free
23 #else  // __linux__ && !ANDROID
24 # include <stdlib.h>
25 # define LIBC_MALLOC malloc
26 # define LIBC_FREE free
27 #endif  // __linux__ && !ANDROID
28 
29 namespace __sanitizer {
30 
31 const u64 kBlockMagic = 0x6A6CB03ABCEBC041ull;
32 
InternalAlloc(uptr size)33 void *InternalAlloc(uptr size) {
34   if (size + sizeof(u64) < size)
35     return 0;
36   void *p = LIBC_MALLOC(size + sizeof(u64));
37   if (p == 0)
38     return 0;
39   ((u64*)p)[0] = kBlockMagic;
40   return (char*)p + sizeof(u64);
41 }
42 
InternalFree(void * addr)43 void InternalFree(void *addr) {
44   if (addr == 0)
45     return;
46   addr = (char*)addr - sizeof(u64);
47   CHECK_EQ(((u64*)addr)[0], kBlockMagic);
48   ((u64*)addr)[0] = 0;
49   LIBC_FREE(addr);
50 }
51 
52 // LowLevelAllocator
53 static LowLevelAllocateCallback low_level_alloc_callback;
54 
Allocate(uptr size)55 void *LowLevelAllocator::Allocate(uptr size) {
56   // Align allocation size.
57   size = RoundUpTo(size, 8);
58   if (allocated_end_ - allocated_current_ < (sptr)size) {
59     uptr size_to_allocate = Max(size, GetPageSizeCached());
60     allocated_current_ =
61         (char*)MmapOrDie(size_to_allocate, __FUNCTION__);
62     allocated_end_ = allocated_current_ + size_to_allocate;
63     if (low_level_alloc_callback) {
64       low_level_alloc_callback((uptr)allocated_current_,
65                                size_to_allocate);
66     }
67   }
68   CHECK(allocated_end_ - allocated_current_ >= (sptr)size);
69   void *res = allocated_current_;
70   allocated_current_ += size;
71   return res;
72 }
73 
SetLowLevelAllocateCallback(LowLevelAllocateCallback callback)74 void SetLowLevelAllocateCallback(LowLevelAllocateCallback callback) {
75   low_level_alloc_callback = callback;
76 }
77 
CallocShouldReturnNullDueToOverflow(uptr size,uptr n)78 bool CallocShouldReturnNullDueToOverflow(uptr size, uptr n) {
79   if (!size) return false;
80   uptr max = (uptr)-1L;
81   return (max / size) < n;
82 }
83 
84 }  // namespace __sanitizer
85