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 | 41ffe3d | 2012-12-17 07:54:29 +0000 | [diff] [blame] | 20 | #include "sanitizer_common/sanitizer_list.h" |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 21 | |
| 22 | namespace __asan { |
| 23 | |
Kostya Serebryany | 3674c6b | 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 | 8d03204 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 30 | static const uptr kNumberOfSizeClasses = 255; |
Kostya Serebryany | 9d1eee9 | 2011-11-30 17:33:13 +0000 | [diff] [blame] | 31 | struct AsanChunk; |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 32 | |
Kostya Serebryany | 61761f1 | 2013-01-28 08:05:47 +0000 | [diff] [blame] | 33 | void InitializeAllocator(); |
| 34 | |
Alexey Samsonov | 8661465 | 2012-09-18 07:38:10 +0000 | [diff] [blame] | 35 | class AsanChunkView { |
| 36 | public: |
| 37 | explicit AsanChunkView(AsanChunk *chunk) : chunk_(chunk) {} |
| 38 | bool IsValid() { return chunk_ != 0; } |
| 39 | uptr Beg(); // first byte of user memory. |
| 40 | uptr End(); // last byte of user memory. |
| 41 | uptr UsedSize(); // size requested by the user. |
| 42 | uptr AllocTid(); |
| 43 | uptr FreeTid(); |
| 44 | void GetAllocStack(StackTrace *stack); |
| 45 | void GetFreeStack(StackTrace *stack); |
Evgeniy Stepanov | 1bc7298 | 2013-02-05 14:32:03 +0000 | [diff] [blame] | 46 | bool AddrIsInside(uptr addr, uptr access_size, sptr *offset) { |
Kostya Serebryany | 5e2a7ac | 2012-12-11 09:02:36 +0000 | [diff] [blame] | 47 | if (addr >= Beg() && (addr + access_size) <= End()) { |
| 48 | *offset = addr - Beg(); |
| 49 | return true; |
| 50 | } |
| 51 | return false; |
| 52 | } |
Evgeniy Stepanov | 1bc7298 | 2013-02-05 14:32:03 +0000 | [diff] [blame] | 53 | bool AddrIsAtLeft(uptr addr, uptr access_size, sptr *offset) { |
Alexander Potapenko | 602a09f | 2012-12-12 12:32:57 +0000 | [diff] [blame] | 54 | (void)access_size; |
Kostya Serebryany | 5e2a7ac | 2012-12-11 09:02:36 +0000 | [diff] [blame] | 55 | if (addr < Beg()) { |
| 56 | *offset = Beg() - addr; |
| 57 | return true; |
| 58 | } |
| 59 | return false; |
| 60 | } |
Evgeniy Stepanov | 1bc7298 | 2013-02-05 14:32:03 +0000 | [diff] [blame] | 61 | bool AddrIsAtRight(uptr addr, uptr access_size, sptr *offset) { |
Evgeniy Stepanov | 0b805cc | 2013-02-08 12:59:42 +0000 | [diff] [blame] | 62 | if (addr + access_size > End()) { |
Evgeniy Stepanov | 1bc7298 | 2013-02-05 14:32:03 +0000 | [diff] [blame] | 63 | *offset = addr - End(); |
Kostya Serebryany | 5e2a7ac | 2012-12-11 09:02:36 +0000 | [diff] [blame] | 64 | return true; |
| 65 | } |
| 66 | return false; |
| 67 | } |
| 68 | |
Alexey Samsonov | 8661465 | 2012-09-18 07:38:10 +0000 | [diff] [blame] | 69 | private: |
| 70 | AsanChunk *const chunk_; |
| 71 | }; |
| 72 | |
| 73 | AsanChunkView FindHeapChunkByAddress(uptr address); |
| 74 | |
Kostya Serebryany | 41ffe3d | 2012-12-17 07:54:29 +0000 | [diff] [blame] | 75 | // List of AsanChunks with total size. |
| 76 | class AsanChunkFifoList: public IntrusiveList<AsanChunk> { |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 77 | public: |
| 78 | explicit AsanChunkFifoList(LinkerInitialized) { } |
| 79 | AsanChunkFifoList() { clear(); } |
| 80 | void Push(AsanChunk *n); |
| 81 | void PushList(AsanChunkFifoList *q); |
| 82 | AsanChunk *Pop(); |
Kostya Serebryany | 8d03204 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 83 | uptr size() { return size_; } |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 84 | void clear() { |
Kostya Serebryany | 41ffe3d | 2012-12-17 07:54:29 +0000 | [diff] [blame] | 85 | IntrusiveList<AsanChunk>::clear(); |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 86 | size_ = 0; |
| 87 | } |
| 88 | private: |
Kostya Serebryany | 8d03204 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 89 | uptr size_; |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 90 | }; |
| 91 | |
| 92 | struct AsanThreadLocalMallocStorage { |
| 93 | explicit AsanThreadLocalMallocStorage(LinkerInitialized x) |
Dmitry Vyukov | db0cf87 | 2013-01-11 08:07:43 +0000 | [diff] [blame] | 94 | { } |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 95 | AsanThreadLocalMallocStorage() { |
Alexey Samsonov | e725478 | 2012-02-08 13:45:31 +0000 | [diff] [blame] | 96 | CHECK(REAL(memset)); |
| 97 | REAL(memset)(this, 0, sizeof(AsanThreadLocalMallocStorage)); |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 98 | } |
| 99 | |
Dmitry Vyukov | db0cf87 | 2013-01-11 08:07:43 +0000 | [diff] [blame] | 100 | uptr quarantine_cache[16]; |
Dmitry Vyukov | 0d46b2b | 2013-01-15 10:45:18 +0000 | [diff] [blame] | 101 | uptr allocator2_cache[96 * (512 * 8 + 16)]; // Opaque. |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 102 | void CommitBack(); |
| 103 | }; |
| 104 | |
Kostya Serebryany | 3674c6b | 2012-12-21 08:53:59 +0000 | [diff] [blame] | 105 | void *asan_memalign(uptr alignment, uptr size, StackTrace *stack, |
| 106 | AllocType alloc_type); |
| 107 | void asan_free(void *ptr, StackTrace *stack, AllocType alloc_type); |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 108 | |
Kostya Serebryany | 6b0d775 | 2012-08-28 11:54:30 +0000 | [diff] [blame] | 109 | void *asan_malloc(uptr size, StackTrace *stack); |
| 110 | void *asan_calloc(uptr nmemb, uptr size, StackTrace *stack); |
| 111 | void *asan_realloc(void *p, uptr size, StackTrace *stack); |
| 112 | void *asan_valloc(uptr size, StackTrace *stack); |
| 113 | void *asan_pvalloc(uptr size, StackTrace *stack); |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 114 | |
Kostya Serebryany | 8d03204 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 115 | int asan_posix_memalign(void **memptr, uptr alignment, uptr size, |
Kostya Serebryany | 6b0d775 | 2012-08-28 11:54:30 +0000 | [diff] [blame] | 116 | StackTrace *stack); |
| 117 | uptr asan_malloc_usable_size(void *ptr, StackTrace *stack); |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 118 | |
Kostya Serebryany | 8d03204 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 119 | uptr asan_mz_size(const void *ptr); |
Alexey Samsonov | 209c514 | 2012-01-17 06:39:10 +0000 | [diff] [blame] | 120 | void asan_mz_force_lock(); |
| 121 | void asan_mz_force_unlock(); |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 122 | |
Kostya Serebryany | 4a42cf6 | 2012-12-27 14:09:19 +0000 | [diff] [blame] | 123 | void PrintInternalAllocatorStats(); |
| 124 | |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 125 | } // namespace __asan |
| 126 | #endif // ASAN_ALLOCATOR_H |