Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 1 | //===-- asan_allocator.h ----------------------------------------*- C++ -*-===// |
| 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 a part of AddressSanitizer, an address sanity checker. |
| 11 | // |
Kostya Serebryany | eef8bd4 | 2013-04-04 11:32:49 +0000 | [diff] [blame] | 12 | // ASan-private header for asan_allocator2.cc. |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #ifndef ASAN_ALLOCATOR_H |
| 16 | #define ASAN_ALLOCATOR_H |
| 17 | |
| 18 | #include "asan_internal.h" |
| 19 | #include "asan_interceptors.h" |
Kostya Serebryany | d4b1b20 | 2014-04-15 13:30:32 +0000 | [diff] [blame^] | 20 | #include "sanitizer_common/sanitizer_allocator.h" |
Kostya Serebryany | 41ffe3d | 2012-12-17 07:54:29 +0000 | [diff] [blame] | 21 | #include "sanitizer_common/sanitizer_list.h" |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 22 | |
| 23 | namespace __asan { |
| 24 | |
Kostya Serebryany | 3674c6b | 2012-12-21 08:53:59 +0000 | [diff] [blame] | 25 | enum AllocType { |
| 26 | FROM_MALLOC = 1, // Memory block came from malloc, calloc, realloc, etc. |
| 27 | FROM_NEW = 2, // Memory block came from operator new. |
| 28 | FROM_NEW_BR = 3 // Memory block came from operator new [ ] |
| 29 | }; |
| 30 | |
Kostya Serebryany | 8d03204 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 31 | static const uptr kNumberOfSizeClasses = 255; |
Kostya Serebryany | 9d1eee9 | 2011-11-30 17:33:13 +0000 | [diff] [blame] | 32 | struct AsanChunk; |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 33 | |
Kostya Serebryany | 61761f1 | 2013-01-28 08:05:47 +0000 | [diff] [blame] | 34 | void InitializeAllocator(); |
Evgeniy Stepanov | 756e1c1 | 2014-02-03 14:19:08 +0000 | [diff] [blame] | 35 | void ReInitializeAllocator(); |
Kostya Serebryany | 61761f1 | 2013-01-28 08:05:47 +0000 | [diff] [blame] | 36 | |
Alexey Samsonov | 8661465 | 2012-09-18 07:38:10 +0000 | [diff] [blame] | 37 | class AsanChunkView { |
| 38 | public: |
| 39 | explicit AsanChunkView(AsanChunk *chunk) : chunk_(chunk) {} |
Alexey Samsonov | df6e656 | 2013-10-14 11:13:54 +0000 | [diff] [blame] | 40 | bool IsValid(); // Checks if AsanChunkView points to a valid allocated |
| 41 | // or quarantined chunk. |
| 42 | uptr Beg(); // First byte of user memory. |
| 43 | uptr End(); // Last byte of user memory. |
| 44 | uptr UsedSize(); // Size requested by the user. |
Alexey Samsonov | 8661465 | 2012-09-18 07:38:10 +0000 | [diff] [blame] | 45 | uptr AllocTid(); |
| 46 | uptr FreeTid(); |
Kostya Serebryany | 796f655 | 2014-02-27 12:45:36 +0000 | [diff] [blame] | 47 | bool Eq(const AsanChunkView &c) const { return chunk_ == c.chunk_; } |
Alexey Samsonov | 8661465 | 2012-09-18 07:38:10 +0000 | [diff] [blame] | 48 | void GetAllocStack(StackTrace *stack); |
| 49 | void GetFreeStack(StackTrace *stack); |
Evgeniy Stepanov | 1bc7298 | 2013-02-05 14:32:03 +0000 | [diff] [blame] | 50 | bool AddrIsInside(uptr addr, uptr access_size, sptr *offset) { |
Kostya Serebryany | 5e2a7ac | 2012-12-11 09:02:36 +0000 | [diff] [blame] | 51 | if (addr >= Beg() && (addr + access_size) <= End()) { |
| 52 | *offset = addr - Beg(); |
| 53 | return true; |
| 54 | } |
| 55 | return false; |
| 56 | } |
Evgeniy Stepanov | 1bc7298 | 2013-02-05 14:32:03 +0000 | [diff] [blame] | 57 | bool AddrIsAtLeft(uptr addr, uptr access_size, sptr *offset) { |
Alexander Potapenko | 602a09f | 2012-12-12 12:32:57 +0000 | [diff] [blame] | 58 | (void)access_size; |
Kostya Serebryany | 5e2a7ac | 2012-12-11 09:02:36 +0000 | [diff] [blame] | 59 | if (addr < Beg()) { |
| 60 | *offset = Beg() - addr; |
| 61 | return true; |
| 62 | } |
| 63 | return false; |
| 64 | } |
Evgeniy Stepanov | 1bc7298 | 2013-02-05 14:32:03 +0000 | [diff] [blame] | 65 | bool AddrIsAtRight(uptr addr, uptr access_size, sptr *offset) { |
Evgeniy Stepanov | 0b805cc | 2013-02-08 12:59:42 +0000 | [diff] [blame] | 66 | if (addr + access_size > End()) { |
Evgeniy Stepanov | 1bc7298 | 2013-02-05 14:32:03 +0000 | [diff] [blame] | 67 | *offset = addr - End(); |
Kostya Serebryany | 5e2a7ac | 2012-12-11 09:02:36 +0000 | [diff] [blame] | 68 | return true; |
| 69 | } |
| 70 | return false; |
| 71 | } |
| 72 | |
Alexey Samsonov | 8661465 | 2012-09-18 07:38:10 +0000 | [diff] [blame] | 73 | private: |
| 74 | AsanChunk *const chunk_; |
| 75 | }; |
| 76 | |
| 77 | AsanChunkView FindHeapChunkByAddress(uptr address); |
| 78 | |
Kostya Serebryany | 41ffe3d | 2012-12-17 07:54:29 +0000 | [diff] [blame] | 79 | // List of AsanChunks with total size. |
| 80 | class AsanChunkFifoList: public IntrusiveList<AsanChunk> { |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 81 | public: |
| 82 | explicit AsanChunkFifoList(LinkerInitialized) { } |
| 83 | AsanChunkFifoList() { clear(); } |
| 84 | void Push(AsanChunk *n); |
| 85 | void PushList(AsanChunkFifoList *q); |
| 86 | AsanChunk *Pop(); |
Kostya Serebryany | 8d03204 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 87 | uptr size() { return size_; } |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 88 | void clear() { |
Kostya Serebryany | 41ffe3d | 2012-12-17 07:54:29 +0000 | [diff] [blame] | 89 | IntrusiveList<AsanChunk>::clear(); |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 90 | size_ = 0; |
| 91 | } |
| 92 | private: |
Kostya Serebryany | 8d03204 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 93 | uptr size_; |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 94 | }; |
| 95 | |
Kostya Serebryany | d4b1b20 | 2014-04-15 13:30:32 +0000 | [diff] [blame^] | 96 | struct AsanMapUnmapCallback { |
| 97 | void OnMap(uptr p, uptr size) const; |
| 98 | void OnUnmap(uptr p, uptr size) const; |
| 99 | }; |
| 100 | |
| 101 | #if SANITIZER_CAN_USE_ALLOCATOR64 |
| 102 | # if defined(__powerpc64__) |
| 103 | const uptr kAllocatorSpace = 0xa0000000000ULL; |
| 104 | const uptr kAllocatorSize = 0x20000000000ULL; // 2T. |
| 105 | # else |
| 106 | const uptr kAllocatorSpace = 0x600000000000ULL; |
| 107 | const uptr kAllocatorSize = 0x40000000000ULL; // 4T. |
| 108 | # endif |
| 109 | typedef DefaultSizeClassMap SizeClassMap; |
| 110 | typedef SizeClassAllocator64<kAllocatorSpace, kAllocatorSize, 0 /*metadata*/, |
| 111 | SizeClassMap, AsanMapUnmapCallback> PrimaryAllocator; |
| 112 | #else // Fallback to SizeClassAllocator32. |
| 113 | static const uptr kRegionSizeLog = 20; |
| 114 | static const uptr kNumRegions = SANITIZER_MMAP_RANGE_SIZE >> kRegionSizeLog; |
| 115 | # if SANITIZER_WORDSIZE == 32 |
| 116 | typedef FlatByteMap<kNumRegions> ByteMap; |
| 117 | # elif SANITIZER_WORDSIZE == 64 |
| 118 | typedef TwoLevelByteMap<(kNumRegions >> 12), 1 << 12> ByteMap; |
| 119 | # endif |
| 120 | typedef CompactSizeClassMap SizeClassMap; |
| 121 | typedef SizeClassAllocator32<0, SANITIZER_MMAP_RANGE_SIZE, 16, |
| 122 | SizeClassMap, kRegionSizeLog, |
| 123 | ByteMap, |
| 124 | AsanMapUnmapCallback> PrimaryAllocator; |
| 125 | #endif // SANITIZER_CAN_USE_ALLOCATOR64 |
| 126 | |
| 127 | typedef SizeClassAllocatorLocalCache<PrimaryAllocator> AllocatorCache; |
| 128 | typedef LargeMmapAllocator<AsanMapUnmapCallback> SecondaryAllocator; |
| 129 | typedef CombinedAllocator<PrimaryAllocator, AllocatorCache, |
| 130 | SecondaryAllocator> Allocator; |
| 131 | |
| 132 | |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 133 | struct AsanThreadLocalMallocStorage { |
Dmitry Vyukov | db0cf87 | 2013-01-11 08:07:43 +0000 | [diff] [blame] | 134 | uptr quarantine_cache[16]; |
Kostya Serebryany | d4b1b20 | 2014-04-15 13:30:32 +0000 | [diff] [blame^] | 135 | AllocatorCache allocator2_cache; |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 136 | void CommitBack(); |
Alexey Samsonov | e0e31c4 | 2013-11-27 13:22:21 +0000 | [diff] [blame] | 137 | private: |
| 138 | // These objects are allocated via mmap() and are zero-initialized. |
| 139 | AsanThreadLocalMallocStorage() {} |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 140 | }; |
| 141 | |
Kostya Serebryany | 3674c6b | 2012-12-21 08:53:59 +0000 | [diff] [blame] | 142 | void *asan_memalign(uptr alignment, uptr size, StackTrace *stack, |
| 143 | AllocType alloc_type); |
| 144 | void asan_free(void *ptr, StackTrace *stack, AllocType alloc_type); |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 145 | |
Kostya Serebryany | 6b0d775 | 2012-08-28 11:54:30 +0000 | [diff] [blame] | 146 | void *asan_malloc(uptr size, StackTrace *stack); |
| 147 | void *asan_calloc(uptr nmemb, uptr size, StackTrace *stack); |
| 148 | void *asan_realloc(void *p, uptr size, StackTrace *stack); |
| 149 | void *asan_valloc(uptr size, StackTrace *stack); |
| 150 | void *asan_pvalloc(uptr size, StackTrace *stack); |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 151 | |
Kostya Serebryany | 8d03204 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 152 | int asan_posix_memalign(void **memptr, uptr alignment, uptr size, |
Kostya Serebryany | 6b0d775 | 2012-08-28 11:54:30 +0000 | [diff] [blame] | 153 | StackTrace *stack); |
Alexey Samsonov | 9ff4598 | 2013-11-13 14:46:58 +0000 | [diff] [blame] | 154 | uptr asan_malloc_usable_size(void *ptr, uptr pc, uptr bp); |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 155 | |
Kostya Serebryany | 8d03204 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 156 | uptr asan_mz_size(const void *ptr); |
Alexey Samsonov | 209c514 | 2012-01-17 06:39:10 +0000 | [diff] [blame] | 157 | void asan_mz_force_lock(); |
| 158 | void asan_mz_force_unlock(); |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 159 | |
Kostya Serebryany | 4a42cf6 | 2012-12-27 14:09:19 +0000 | [diff] [blame] | 160 | void PrintInternalAllocatorStats(); |
| 161 | |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 162 | } // namespace __asan |
| 163 | #endif // ASAN_ALLOCATOR_H |