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