blob: ee28ecf98cab76e331dec2330da32e4306e6ba19 [file] [log] [blame]
Kostya Serebryany019b76f2011-11-30 01:07:02 +00001//===-- 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 Samsonov2c31cc32014-12-17 00:26:50 +000012// ASan-private header for asan_allocator.cc.
Kostya Serebryany019b76f2011-11-30 01:07:02 +000013//===----------------------------------------------------------------------===//
14
15#ifndef ASAN_ALLOCATOR_H
16#define ASAN_ALLOCATOR_H
17
Alexey Samsonovd6b24fa2014-12-19 19:35:11 +000018#include "asan_flags.h"
Kostya Serebryany019b76f2011-11-30 01:07:02 +000019#include "asan_internal.h"
20#include "asan_interceptors.h"
Kostya Serebryanyd4b1b202014-04-15 13:30:32 +000021#include "sanitizer_common/sanitizer_allocator.h"
Kostya Serebryany41ffe3d2012-12-17 07:54:29 +000022#include "sanitizer_common/sanitizer_list.h"
Kostya Serebryany019b76f2011-11-30 01:07:02 +000023
24namespace __asan {
25
Kostya Serebryany3674c6b2012-12-21 08:53:59 +000026enum 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 Serebryany9d1eee92011-11-30 17:33:13 +000032struct AsanChunk;
Kostya Serebryany019b76f2011-11-30 01:07:02 +000033
Alexey Samsonovd6b24fa2014-12-19 19:35:11 +000034struct AllocatorOptions {
35 u32 quarantine_size_mb;
Evgeniy Stepanov37db58e2016-12-22 21:43:22 +000036 u32 thread_local_quarantine_size_kb;
Alexey Samsonovd6b24fa2014-12-19 19:35:11 +000037 u16 min_redzone;
38 u16 max_redzone;
39 u8 may_return_null;
40 u8 alloc_dealloc_mismatch;
Evgeniy Stepanovd3305af2016-11-29 00:22:50 +000041 s32 release_to_os_interval_ms;
Alexey Samsonovd6b24fa2014-12-19 19:35:11 +000042
43 void SetFrom(const Flags *f, const CommonFlags *cf);
Alexey Samsonov04eeec32014-12-19 20:35:53 +000044 void CopyTo(Flags *f, CommonFlags *cf);
Alexey Samsonovd6b24fa2014-12-19 19:35:11 +000045};
46
47void InitializeAllocator(const AllocatorOptions &options);
48void ReInitializeAllocator(const AllocatorOptions &options);
Alexey Samsonov04eeec32014-12-19 20:35:53 +000049void GetAllocatorOptions(AllocatorOptions *options);
Kostya Serebryany61761f12013-01-28 08:05:47 +000050
Alexey Samsonov86614652012-09-18 07:38:10 +000051class AsanChunkView {
52 public:
53 explicit AsanChunkView(AsanChunk *chunk) : chunk_(chunk) {}
Evgeniy Stepanov04cfed92017-01-05 22:17:53 +000054 bool IsValid() const; // Checks if AsanChunkView points to a valid
55 // allocated or quarantined chunk.
56 bool IsAllocated() const; // Checks if the memory is currently allocated.
57 bool IsQuarantined() const; // Checks if the memory is currently quarantined.
58 uptr Beg() const; // First byte of user memory.
59 uptr End() const; // Last byte of user memory.
60 uptr UsedSize() const; // Size requested by the user.
61 uptr AllocTid() const;
62 uptr FreeTid() const;
Kostya Serebryany796f6552014-02-27 12:45:36 +000063 bool Eq(const AsanChunkView &c) const { return chunk_ == c.chunk_; }
Evgeniy Stepanov04cfed92017-01-05 22:17:53 +000064 u32 GetAllocStackId() const;
65 u32 GetFreeStackId() const;
66 StackTrace GetAllocStack() const;
67 StackTrace GetFreeStack() const;
68 AllocType GetAllocType() const;
69 bool AddrIsInside(uptr addr, uptr access_size, sptr *offset) const {
Kostya Serebryany5e2a7ac2012-12-11 09:02:36 +000070 if (addr >= Beg() && (addr + access_size) <= End()) {
71 *offset = addr - Beg();
72 return true;
73 }
74 return false;
75 }
Evgeniy Stepanov04cfed92017-01-05 22:17:53 +000076 bool AddrIsAtLeft(uptr addr, uptr access_size, sptr *offset) const {
Alexander Potapenko602a09f2012-12-12 12:32:57 +000077 (void)access_size;
Kostya Serebryany5e2a7ac2012-12-11 09:02:36 +000078 if (addr < Beg()) {
79 *offset = Beg() - addr;
80 return true;
81 }
82 return false;
83 }
Evgeniy Stepanov04cfed92017-01-05 22:17:53 +000084 bool AddrIsAtRight(uptr addr, uptr access_size, sptr *offset) const {
Evgeniy Stepanov0b805cc2013-02-08 12:59:42 +000085 if (addr + access_size > End()) {
Evgeniy Stepanov1bc72982013-02-05 14:32:03 +000086 *offset = addr - End();
Kostya Serebryany5e2a7ac2012-12-11 09:02:36 +000087 return true;
88 }
89 return false;
90 }
91
Alexey Samsonov86614652012-09-18 07:38:10 +000092 private:
93 AsanChunk *const chunk_;
94};
95
96AsanChunkView FindHeapChunkByAddress(uptr address);
Kostya Serebryany6e3cda42016-08-23 18:13:51 +000097AsanChunkView FindHeapChunkByAllocBeg(uptr address);
Alexey Samsonov86614652012-09-18 07:38:10 +000098
Kostya Serebryany41ffe3d2012-12-17 07:54:29 +000099// List of AsanChunks with total size.
100class AsanChunkFifoList: public IntrusiveList<AsanChunk> {
Kostya Serebryany019b76f2011-11-30 01:07:02 +0000101 public:
102 explicit AsanChunkFifoList(LinkerInitialized) { }
103 AsanChunkFifoList() { clear(); }
104 void Push(AsanChunk *n);
105 void PushList(AsanChunkFifoList *q);
106 AsanChunk *Pop();
Kostya Serebryany8d032042012-05-31 14:35:53 +0000107 uptr size() { return size_; }
Kostya Serebryany019b76f2011-11-30 01:07:02 +0000108 void clear() {
Kostya Serebryany41ffe3d2012-12-17 07:54:29 +0000109 IntrusiveList<AsanChunk>::clear();
Kostya Serebryany019b76f2011-11-30 01:07:02 +0000110 size_ = 0;
111 }
112 private:
Kostya Serebryany8d032042012-05-31 14:35:53 +0000113 uptr size_;
Kostya Serebryany019b76f2011-11-30 01:07:02 +0000114};
115
Kostya Serebryanyd4b1b202014-04-15 13:30:32 +0000116struct AsanMapUnmapCallback {
117 void OnMap(uptr p, uptr size) const;
118 void OnUnmap(uptr p, uptr size) const;
119};
120
121#if SANITIZER_CAN_USE_ALLOCATOR64
122# if defined(__powerpc64__)
123const uptr kAllocatorSpace = 0xa0000000000ULL;
124const uptr kAllocatorSize = 0x20000000000ULL; // 2T.
Evgeniy Stepanovec3e4362016-09-13 19:05:33 +0000125typedef DefaultSizeClassMap SizeClassMap;
126# elif defined(__aarch64__) && SANITIZER_ANDROID
127const uptr kAllocatorSpace = 0x3000000000ULL;
128const uptr kAllocatorSize = 0x2000000000ULL; // 128G.
129typedef VeryCompactSizeClassMap SizeClassMap;
Adhemerval Zanellaeccc9392015-08-20 18:49:40 +0000130# elif defined(__aarch64__)
Evgeniy Stepanovec3e4362016-09-13 19:05:33 +0000131// AArch64/SANITIZER_CAN_USER_ALLOCATOR64 is only for 42-bit VMA
Adhemerval Zanellaeccc9392015-08-20 18:49:40 +0000132// so no need to different values for different VMA.
133const uptr kAllocatorSpace = 0x10000000000ULL;
134const uptr kAllocatorSize = 0x10000000000ULL; // 3T.
Evgeniy Stepanovec3e4362016-09-13 19:05:33 +0000135typedef DefaultSizeClassMap SizeClassMap;
Etienne Bergeron780507c2016-09-14 19:23:21 +0000136# elif SANITIZER_WINDOWS
137const uptr kAllocatorSpace = ~(uptr)0;
138const uptr kAllocatorSize = 0x8000000000ULL; // 500G
139typedef DefaultSizeClassMap SizeClassMap;
Kostya Serebryanyd4b1b202014-04-15 13:30:32 +0000140# else
141const uptr kAllocatorSpace = 0x600000000000ULL;
142const uptr kAllocatorSize = 0x40000000000ULL; // 4T.
Kostya Serebryanyd4b1b202014-04-15 13:30:32 +0000143typedef DefaultSizeClassMap SizeClassMap;
Evgeniy Stepanovec3e4362016-09-13 19:05:33 +0000144# endif
Kostya Serebryany15647b12016-08-25 20:23:08 +0000145struct AP64 { // Allocator64 parameters. Deliberately using a short name.
146 static const uptr kSpaceBeg = kAllocatorSpace;
147 static const uptr kSpaceSize = kAllocatorSize;
148 static const uptr kMetadataSize = 0;
149 typedef __asan::SizeClassMap SizeClassMap;
150 typedef AsanMapUnmapCallback MapUnmapCallback;
Kostya Serebryany7c5ae7c2016-08-26 00:06:03 +0000151 static const uptr kFlags = 0;
Kostya Serebryany15647b12016-08-25 20:23:08 +0000152};
153
154typedef SizeClassAllocator64<AP64> PrimaryAllocator;
Kostya Serebryanyd4b1b202014-04-15 13:30:32 +0000155#else // Fallback to SizeClassAllocator32.
156static const uptr kRegionSizeLog = 20;
157static const uptr kNumRegions = SANITIZER_MMAP_RANGE_SIZE >> kRegionSizeLog;
158# if SANITIZER_WORDSIZE == 32
159typedef FlatByteMap<kNumRegions> ByteMap;
160# elif SANITIZER_WORDSIZE == 64
161typedef TwoLevelByteMap<(kNumRegions >> 12), 1 << 12> ByteMap;
162# endif
163typedef CompactSizeClassMap SizeClassMap;
164typedef SizeClassAllocator32<0, SANITIZER_MMAP_RANGE_SIZE, 16,
165 SizeClassMap, kRegionSizeLog,
166 ByteMap,
167 AsanMapUnmapCallback> PrimaryAllocator;
168#endif // SANITIZER_CAN_USE_ALLOCATOR64
169
Alexey Samsonov1ac144b2015-06-26 19:18:02 +0000170static const uptr kNumberOfSizeClasses = SizeClassMap::kNumClasses;
Kostya Serebryanyd4b1b202014-04-15 13:30:32 +0000171typedef SizeClassAllocatorLocalCache<PrimaryAllocator> AllocatorCache;
172typedef LargeMmapAllocator<AsanMapUnmapCallback> SecondaryAllocator;
173typedef CombinedAllocator<PrimaryAllocator, AllocatorCache,
Alexey Samsonovb2dcac02014-12-17 01:55:03 +0000174 SecondaryAllocator> AsanAllocator;
Kostya Serebryanyd4b1b202014-04-15 13:30:32 +0000175
176
Kostya Serebryany019b76f2011-11-30 01:07:02 +0000177struct AsanThreadLocalMallocStorage {
Dmitry Vyukovdb0cf872013-01-11 08:07:43 +0000178 uptr quarantine_cache[16];
Alexey Samsonovb2dcac02014-12-17 01:55:03 +0000179 AllocatorCache allocator_cache;
Kostya Serebryany019b76f2011-11-30 01:07:02 +0000180 void CommitBack();
Alexey Samsonove0e31c42013-11-27 13:22:21 +0000181 private:
182 // These objects are allocated via mmap() and are zero-initialized.
183 AsanThreadLocalMallocStorage() {}
Kostya Serebryany019b76f2011-11-30 01:07:02 +0000184};
185
Alexey Samsonov9c859272014-10-26 03:35:14 +0000186void *asan_memalign(uptr alignment, uptr size, BufferedStackTrace *stack,
Kostya Serebryany3674c6b2012-12-21 08:53:59 +0000187 AllocType alloc_type);
Alexey Samsonov9c859272014-10-26 03:35:14 +0000188void asan_free(void *ptr, BufferedStackTrace *stack, AllocType alloc_type);
189void asan_sized_free(void *ptr, uptr size, BufferedStackTrace *stack,
Kostya Serebryany69852a82014-07-30 09:48:23 +0000190 AllocType alloc_type);
Kostya Serebryany019b76f2011-11-30 01:07:02 +0000191
Alexey Samsonov9c859272014-10-26 03:35:14 +0000192void *asan_malloc(uptr size, BufferedStackTrace *stack);
193void *asan_calloc(uptr nmemb, uptr size, BufferedStackTrace *stack);
194void *asan_realloc(void *p, uptr size, BufferedStackTrace *stack);
195void *asan_valloc(uptr size, BufferedStackTrace *stack);
196void *asan_pvalloc(uptr size, BufferedStackTrace *stack);
Kostya Serebryany019b76f2011-11-30 01:07:02 +0000197
Kostya Serebryany8d032042012-05-31 14:35:53 +0000198int asan_posix_memalign(void **memptr, uptr alignment, uptr size,
Alexey Samsonov9c859272014-10-26 03:35:14 +0000199 BufferedStackTrace *stack);
Reid Kleckner3b029052016-03-24 20:19:48 +0000200uptr asan_malloc_usable_size(const void *ptr, uptr pc, uptr bp);
Kostya Serebryany019b76f2011-11-30 01:07:02 +0000201
Kostya Serebryany8d032042012-05-31 14:35:53 +0000202uptr asan_mz_size(const void *ptr);
Alexey Samsonov209c5142012-01-17 06:39:10 +0000203void asan_mz_force_lock();
204void asan_mz_force_unlock();
Kostya Serebryany019b76f2011-11-30 01:07:02 +0000205
Kostya Serebryany4a42cf62012-12-27 14:09:19 +0000206void PrintInternalAllocatorStats();
Kostya Serebryany7e85a922015-01-06 23:53:32 +0000207void AsanSoftRssLimitExceededCallback(bool exceeded);
Kostya Serebryany4a42cf62012-12-27 14:09:19 +0000208
Kostya Serebryany019b76f2011-11-30 01:07:02 +0000209} // namespace __asan
210#endif // ASAN_ALLOCATOR_H