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