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