blob: 3208d1f950cdcf07725a36ec42caef9e55e4dccc [file] [log] [blame]
Kostya Serebryany1e172b42011-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//
Stephen Hines86277eb2015-03-23 12:06:32 -070012// ASan-private header for asan_allocator.cc.
Kostya Serebryany1e172b42011-11-30 01:07:02 +000013//===----------------------------------------------------------------------===//
14
15#ifndef ASAN_ALLOCATOR_H
16#define ASAN_ALLOCATOR_H
17
Stephen Hines86277eb2015-03-23 12:06:32 -070018#include "asan_flags.h"
Kostya Serebryany1e172b42011-11-30 01:07:02 +000019#include "asan_internal.h"
20#include "asan_interceptors.h"
Stephen Hines2d1fdb22014-05-28 23:58:16 -070021#include "sanitizer_common/sanitizer_allocator.h"
Kostya Serebryanyc88059c2012-12-17 07:54:29 +000022#include "sanitizer_common/sanitizer_list.h"
Kostya Serebryany1e172b42011-11-30 01:07:02 +000023
24namespace __asan {
25
Kostya Serebryanyfe6d9162012-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 Serebryany3f4c3872012-05-31 14:35:53 +000032static const uptr kNumberOfSizeClasses = 255;
Kostya Serebryanycbab9112011-11-30 17:33:13 +000033struct AsanChunk;
Kostya Serebryany1e172b42011-11-30 01:07:02 +000034
Stephen Hines86277eb2015-03-23 12:06:32 -070035struct 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
46void InitializeAllocator(const AllocatorOptions &options);
47void ReInitializeAllocator(const AllocatorOptions &options);
48void GetAllocatorOptions(AllocatorOptions *options);
Kostya Serebryanyb4782602013-01-28 08:05:47 +000049
Alexey Samsonov5c153fa2012-09-18 07:38:10 +000050class AsanChunkView {
51 public:
52 explicit AsanChunkView(AsanChunk *chunk) : chunk_(chunk) {}
Alexey Samsonovd9def292013-10-14 11:13:54 +000053 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 Samsonov5c153fa2012-09-18 07:38:10 +000058 uptr AllocTid();
59 uptr FreeTid();
Stephen Hines2d1fdb22014-05-28 23:58:16 -070060 bool Eq(const AsanChunkView &c) const { return chunk_ == c.chunk_; }
Stephen Hines6d186232014-11-26 17:56:19 -080061 StackTrace GetAllocStack();
62 StackTrace GetFreeStack();
Evgeniy Stepanov589dcda2013-02-05 14:32:03 +000063 bool AddrIsInside(uptr addr, uptr access_size, sptr *offset) {
Kostya Serebryany321e1252012-12-11 09:02:36 +000064 if (addr >= Beg() && (addr + access_size) <= End()) {
65 *offset = addr - Beg();
66 return true;
67 }
68 return false;
69 }
Evgeniy Stepanov589dcda2013-02-05 14:32:03 +000070 bool AddrIsAtLeft(uptr addr, uptr access_size, sptr *offset) {
Alexander Potapenko2ca12222012-12-12 12:32:57 +000071 (void)access_size;
Kostya Serebryany321e1252012-12-11 09:02:36 +000072 if (addr < Beg()) {
73 *offset = Beg() - addr;
74 return true;
75 }
76 return false;
77 }
Evgeniy Stepanov589dcda2013-02-05 14:32:03 +000078 bool AddrIsAtRight(uptr addr, uptr access_size, sptr *offset) {
Evgeniy Stepanoved835842013-02-08 12:59:42 +000079 if (addr + access_size > End()) {
Evgeniy Stepanov589dcda2013-02-05 14:32:03 +000080 *offset = addr - End();
Kostya Serebryany321e1252012-12-11 09:02:36 +000081 return true;
82 }
83 return false;
84 }
85
Alexey Samsonov5c153fa2012-09-18 07:38:10 +000086 private:
87 AsanChunk *const chunk_;
88};
89
90AsanChunkView FindHeapChunkByAddress(uptr address);
91
Kostya Serebryanyc88059c2012-12-17 07:54:29 +000092// List of AsanChunks with total size.
93class AsanChunkFifoList: public IntrusiveList<AsanChunk> {
Kostya Serebryany1e172b42011-11-30 01:07:02 +000094 public:
95 explicit AsanChunkFifoList(LinkerInitialized) { }
96 AsanChunkFifoList() { clear(); }
97 void Push(AsanChunk *n);
98 void PushList(AsanChunkFifoList *q);
99 AsanChunk *Pop();
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000100 uptr size() { return size_; }
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000101 void clear() {
Kostya Serebryanyc88059c2012-12-17 07:54:29 +0000102 IntrusiveList<AsanChunk>::clear();
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000103 size_ = 0;
104 }
105 private:
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000106 uptr size_;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000107};
108
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700109struct AsanMapUnmapCallback {
110 void OnMap(uptr p, uptr size) const;
111 void OnUnmap(uptr p, uptr size) const;
112};
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000113
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700114#if SANITIZER_CAN_USE_ALLOCATOR64
115# if defined(__powerpc64__)
116const uptr kAllocatorSpace = 0xa0000000000ULL;
117const uptr kAllocatorSize = 0x20000000000ULL; // 2T.
118# else
119const uptr kAllocatorSpace = 0x600000000000ULL;
120const uptr kAllocatorSize = 0x40000000000ULL; // 4T.
121# endif
122typedef DefaultSizeClassMap SizeClassMap;
123typedef SizeClassAllocator64<kAllocatorSpace, kAllocatorSize, 0 /*metadata*/,
124 SizeClassMap, AsanMapUnmapCallback> PrimaryAllocator;
125#else // Fallback to SizeClassAllocator32.
126static const uptr kRegionSizeLog = 20;
127static const uptr kNumRegions = SANITIZER_MMAP_RANGE_SIZE >> kRegionSizeLog;
128# if SANITIZER_WORDSIZE == 32
129typedef FlatByteMap<kNumRegions> ByteMap;
130# elif SANITIZER_WORDSIZE == 64
131typedef TwoLevelByteMap<(kNumRegions >> 12), 1 << 12> ByteMap;
132# endif
133typedef CompactSizeClassMap SizeClassMap;
134typedef SizeClassAllocator32<0, SANITIZER_MMAP_RANGE_SIZE, 16,
135 SizeClassMap, kRegionSizeLog,
136 ByteMap,
137 AsanMapUnmapCallback> PrimaryAllocator;
138#endif // SANITIZER_CAN_USE_ALLOCATOR64
139
140typedef SizeClassAllocatorLocalCache<PrimaryAllocator> AllocatorCache;
141typedef LargeMmapAllocator<AsanMapUnmapCallback> SecondaryAllocator;
142typedef CombinedAllocator<PrimaryAllocator, AllocatorCache,
Stephen Hines86277eb2015-03-23 12:06:32 -0700143 SecondaryAllocator> AsanAllocator;
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700144
145
146struct AsanThreadLocalMallocStorage {
Dmitry Vyukov9fc0df82013-01-11 08:07:43 +0000147 uptr quarantine_cache[16];
Stephen Hines86277eb2015-03-23 12:06:32 -0700148 AllocatorCache allocator_cache;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000149 void CommitBack();
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700150 private:
151 // These objects are allocated via mmap() and are zero-initialized.
152 AsanThreadLocalMallocStorage() {}
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000153};
154
Stephen Hines6d186232014-11-26 17:56:19 -0800155void *asan_memalign(uptr alignment, uptr size, BufferedStackTrace *stack,
Kostya Serebryanyfe6d9162012-12-21 08:53:59 +0000156 AllocType alloc_type);
Stephen Hines6d186232014-11-26 17:56:19 -0800157void asan_free(void *ptr, BufferedStackTrace *stack, AllocType alloc_type);
158void asan_sized_free(void *ptr, uptr size, BufferedStackTrace *stack,
159 AllocType alloc_type);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000160
Stephen Hines6d186232014-11-26 17:56:19 -0800161void *asan_malloc(uptr size, BufferedStackTrace *stack);
162void *asan_calloc(uptr nmemb, uptr size, BufferedStackTrace *stack);
163void *asan_realloc(void *p, uptr size, BufferedStackTrace *stack);
164void *asan_valloc(uptr size, BufferedStackTrace *stack);
165void *asan_pvalloc(uptr size, BufferedStackTrace *stack);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000166
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000167int asan_posix_memalign(void **memptr, uptr alignment, uptr size,
Stephen Hines6d186232014-11-26 17:56:19 -0800168 BufferedStackTrace *stack);
Alexey Samsonov1b17f5b2013-11-13 14:46:58 +0000169uptr asan_malloc_usable_size(void *ptr, uptr pc, uptr bp);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000170
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000171uptr asan_mz_size(const void *ptr);
Alexey Samsonov4fd95f12012-01-17 06:39:10 +0000172void asan_mz_force_lock();
173void asan_mz_force_unlock();
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000174
Kostya Serebryany4b48f452012-12-27 14:09:19 +0000175void PrintInternalAllocatorStats();
Stephen Hines86277eb2015-03-23 12:06:32 -0700176void AsanSoftRssLimitExceededCallback(bool exceeded);
Kostya Serebryany4b48f452012-12-27 14:09:19 +0000177
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000178} // namespace __asan
179#endif // ASAN_ALLOCATOR_H