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 | // |
Kostya Serebryany | b1971ca | 2013-04-04 11:32:49 +0000 | [diff] [blame] | 12 | // ASan-private header for asan_allocator2.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 | |
| 18 | #include "asan_internal.h" |
| 19 | #include "asan_interceptors.h" |
Kostya Serebryany | c88059c | 2012-12-17 07:54:29 +0000 | [diff] [blame] | 20 | #include "sanitizer_common/sanitizer_list.h" |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 21 | |
| 22 | namespace __asan { |
| 23 | |
Kostya Serebryany | fe6d916 | 2012-12-21 08:53:59 +0000 | [diff] [blame] | 24 | enum AllocType { |
| 25 | FROM_MALLOC = 1, // Memory block came from malloc, calloc, realloc, etc. |
| 26 | FROM_NEW = 2, // Memory block came from operator new. |
| 27 | FROM_NEW_BR = 3 // Memory block came from operator new [ ] |
| 28 | }; |
| 29 | |
Kostya Serebryany | 3f4c387 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 30 | static const uptr kNumberOfSizeClasses = 255; |
Kostya Serebryany | cbab911 | 2011-11-30 17:33:13 +0000 | [diff] [blame] | 31 | struct AsanChunk; |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 32 | |
Kostya Serebryany | b478260 | 2013-01-28 08:05:47 +0000 | [diff] [blame] | 33 | void InitializeAllocator(); |
| 34 | |
Alexey Samsonov | 5c153fa | 2012-09-18 07:38:10 +0000 | [diff] [blame] | 35 | class AsanChunkView { |
| 36 | public: |
| 37 | explicit AsanChunkView(AsanChunk *chunk) : chunk_(chunk) {} |
Alexey Samsonov | d9def29 | 2013-10-14 11:13:54 +0000 | [diff] [blame] | 38 | bool IsValid(); // Checks if AsanChunkView points to a valid allocated |
| 39 | // or quarantined chunk. |
| 40 | uptr Beg(); // First byte of user memory. |
| 41 | uptr End(); // Last byte of user memory. |
| 42 | uptr UsedSize(); // Size requested by the user. |
Alexey Samsonov | 5c153fa | 2012-09-18 07:38:10 +0000 | [diff] [blame] | 43 | uptr AllocTid(); |
| 44 | uptr FreeTid(); |
| 45 | void GetAllocStack(StackTrace *stack); |
| 46 | void GetFreeStack(StackTrace *stack); |
Evgeniy Stepanov | 589dcda | 2013-02-05 14:32:03 +0000 | [diff] [blame] | 47 | bool AddrIsInside(uptr addr, uptr access_size, sptr *offset) { |
Kostya Serebryany | 321e125 | 2012-12-11 09:02:36 +0000 | [diff] [blame] | 48 | if (addr >= Beg() && (addr + access_size) <= End()) { |
| 49 | *offset = addr - Beg(); |
| 50 | return true; |
| 51 | } |
| 52 | return false; |
| 53 | } |
Evgeniy Stepanov | 589dcda | 2013-02-05 14:32:03 +0000 | [diff] [blame] | 54 | bool AddrIsAtLeft(uptr addr, uptr access_size, sptr *offset) { |
Alexander Potapenko | 2ca1222 | 2012-12-12 12:32:57 +0000 | [diff] [blame] | 55 | (void)access_size; |
Kostya Serebryany | 321e125 | 2012-12-11 09:02:36 +0000 | [diff] [blame] | 56 | if (addr < Beg()) { |
| 57 | *offset = Beg() - addr; |
| 58 | return true; |
| 59 | } |
| 60 | return false; |
| 61 | } |
Evgeniy Stepanov | 589dcda | 2013-02-05 14:32:03 +0000 | [diff] [blame] | 62 | bool AddrIsAtRight(uptr addr, uptr access_size, sptr *offset) { |
Evgeniy Stepanov | ed83584 | 2013-02-08 12:59:42 +0000 | [diff] [blame] | 63 | if (addr + access_size > End()) { |
Evgeniy Stepanov | 589dcda | 2013-02-05 14:32:03 +0000 | [diff] [blame] | 64 | *offset = addr - End(); |
Kostya Serebryany | 321e125 | 2012-12-11 09:02:36 +0000 | [diff] [blame] | 65 | return true; |
| 66 | } |
| 67 | return false; |
| 68 | } |
| 69 | |
Alexey Samsonov | 5c153fa | 2012-09-18 07:38:10 +0000 | [diff] [blame] | 70 | private: |
| 71 | AsanChunk *const chunk_; |
| 72 | }; |
| 73 | |
| 74 | AsanChunkView FindHeapChunkByAddress(uptr address); |
| 75 | |
Kostya Serebryany | c88059c | 2012-12-17 07:54:29 +0000 | [diff] [blame] | 76 | // List of AsanChunks with total size. |
| 77 | class AsanChunkFifoList: public IntrusiveList<AsanChunk> { |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 78 | public: |
| 79 | explicit AsanChunkFifoList(LinkerInitialized) { } |
| 80 | AsanChunkFifoList() { clear(); } |
| 81 | void Push(AsanChunk *n); |
| 82 | void PushList(AsanChunkFifoList *q); |
| 83 | AsanChunk *Pop(); |
Kostya Serebryany | 3f4c387 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 84 | uptr size() { return size_; } |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 85 | void clear() { |
Kostya Serebryany | c88059c | 2012-12-17 07:54:29 +0000 | [diff] [blame] | 86 | IntrusiveList<AsanChunk>::clear(); |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 87 | size_ = 0; |
| 88 | } |
| 89 | private: |
Kostya Serebryany | 3f4c387 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 90 | uptr size_; |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 91 | }; |
| 92 | |
| 93 | struct AsanThreadLocalMallocStorage { |
| 94 | explicit AsanThreadLocalMallocStorage(LinkerInitialized x) |
Dmitry Vyukov | 9fc0df8 | 2013-01-11 08:07:43 +0000 | [diff] [blame] | 95 | { } |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 96 | AsanThreadLocalMallocStorage() { |
Alexey Samsonov | 09672ca | 2012-02-08 13:45:31 +0000 | [diff] [blame] | 97 | CHECK(REAL(memset)); |
| 98 | REAL(memset)(this, 0, sizeof(AsanThreadLocalMallocStorage)); |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 99 | } |
| 100 | |
Dmitry Vyukov | 9fc0df8 | 2013-01-11 08:07:43 +0000 | [diff] [blame] | 101 | uptr quarantine_cache[16]; |
Dmitry Vyukov | 40265e8 | 2013-01-15 10:45:18 +0000 | [diff] [blame] | 102 | uptr allocator2_cache[96 * (512 * 8 + 16)]; // Opaque. |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 103 | void CommitBack(); |
| 104 | }; |
| 105 | |
Kostya Serebryany | fe6d916 | 2012-12-21 08:53:59 +0000 | [diff] [blame] | 106 | void *asan_memalign(uptr alignment, uptr size, StackTrace *stack, |
| 107 | AllocType alloc_type); |
| 108 | void asan_free(void *ptr, StackTrace *stack, AllocType alloc_type); |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 109 | |
Kostya Serebryany | c3390df | 2012-08-28 11:54:30 +0000 | [diff] [blame] | 110 | void *asan_malloc(uptr size, StackTrace *stack); |
| 111 | void *asan_calloc(uptr nmemb, uptr size, StackTrace *stack); |
| 112 | void *asan_realloc(void *p, uptr size, StackTrace *stack); |
| 113 | void *asan_valloc(uptr size, StackTrace *stack); |
| 114 | void *asan_pvalloc(uptr size, StackTrace *stack); |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 115 | |
Kostya Serebryany | 3f4c387 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 116 | int asan_posix_memalign(void **memptr, uptr alignment, uptr size, |
Kostya Serebryany | c3390df | 2012-08-28 11:54:30 +0000 | [diff] [blame] | 117 | StackTrace *stack); |
Alexey Samsonov | 1b17f5b | 2013-11-13 14:46:58 +0000 | [diff] [blame] | 118 | uptr asan_malloc_usable_size(void *ptr, uptr pc, uptr bp); |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 119 | |
Kostya Serebryany | 3f4c387 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 120 | uptr asan_mz_size(const void *ptr); |
Alexey Samsonov | 4fd95f1 | 2012-01-17 06:39:10 +0000 | [diff] [blame] | 121 | void asan_mz_force_lock(); |
| 122 | void asan_mz_force_unlock(); |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 123 | |
Kostya Serebryany | 4b48f45 | 2012-12-27 14:09:19 +0000 | [diff] [blame] | 124 | void PrintInternalAllocatorStats(); |
| 125 | |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 126 | } // namespace __asan |
| 127 | #endif // ASAN_ALLOCATOR_H |