asan_allocator.h revision b1971ca4a3057916ca90a733c672a08127d5fe67
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_allocator2.cc.
13//===----------------------------------------------------------------------===//
14
15#ifndef ASAN_ALLOCATOR_H
16#define ASAN_ALLOCATOR_H
17
18#include "asan_internal.h"
19#include "asan_interceptors.h"
20#include "sanitizer_common/sanitizer_list.h"
21
22namespace __asan {
23
24enum 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
30static const uptr kNumberOfSizeClasses = 255;
31struct AsanChunk;
32
33void InitializeAllocator();
34
35class 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);
46  bool AddrIsInside(uptr addr, uptr access_size, sptr *offset) {
47    if (addr >= Beg() && (addr + access_size) <= End()) {
48      *offset = addr - Beg();
49      return true;
50    }
51    return false;
52  }
53  bool AddrIsAtLeft(uptr addr, uptr access_size, sptr *offset) {
54    (void)access_size;
55    if (addr < Beg()) {
56      *offset = Beg() - addr;
57      return true;
58    }
59    return false;
60  }
61  bool AddrIsAtRight(uptr addr, uptr access_size, sptr *offset) {
62    if (addr + access_size > End()) {
63      *offset = addr - End();
64      return true;
65    }
66    return false;
67  }
68
69 private:
70  AsanChunk *const chunk_;
71};
72
73AsanChunkView FindHeapChunkByAddress(uptr address);
74
75// List of AsanChunks with total size.
76class AsanChunkFifoList: public IntrusiveList<AsanChunk> {
77 public:
78  explicit AsanChunkFifoList(LinkerInitialized) { }
79  AsanChunkFifoList() { clear(); }
80  void Push(AsanChunk *n);
81  void PushList(AsanChunkFifoList *q);
82  AsanChunk *Pop();
83  uptr size() { return size_; }
84  void clear() {
85    IntrusiveList<AsanChunk>::clear();
86    size_ = 0;
87  }
88 private:
89  uptr size_;
90};
91
92struct AsanThreadLocalMallocStorage {
93  explicit AsanThreadLocalMallocStorage(LinkerInitialized x)
94      { }
95  AsanThreadLocalMallocStorage() {
96    CHECK(REAL(memset));
97    REAL(memset)(this, 0, sizeof(AsanThreadLocalMallocStorage));
98  }
99
100  uptr quarantine_cache[16];
101  uptr allocator2_cache[96 * (512 * 8 + 16)];  // Opaque.
102  void CommitBack();
103};
104
105// Fake stack frame contains local variables of one function.
106// This struct should fit into a stack redzone (32 bytes).
107struct FakeFrame {
108  uptr magic;  // Modified by the instrumented code.
109  uptr descr;  // Modified by the instrumented code.
110  FakeFrame *next;
111  u64 real_stack     : 48;
112  u64 size_minus_one : 16;
113};
114
115struct FakeFrameFifo {
116 public:
117  void FifoPush(FakeFrame *node);
118  FakeFrame *FifoPop();
119 private:
120  FakeFrame *first_, *last_;
121};
122
123class FakeFrameLifo {
124 public:
125  void LifoPush(FakeFrame *node) {
126    node->next = top_;
127    top_ = node;
128  }
129  void LifoPop() {
130    CHECK(top_);
131    top_ = top_->next;
132  }
133  FakeFrame *top() { return top_; }
134 private:
135  FakeFrame *top_;
136};
137
138// For each thread we create a fake stack and place stack objects on this fake
139// stack instead of the real stack. The fake stack is not really a stack but
140// a fast malloc-like allocator so that when a function exits the fake stack
141// is not poped but remains there for quite some time until gets used again.
142// So, we poison the objects on the fake stack when function returns.
143// It helps us find use-after-return bugs.
144// We can not rely on __asan_stack_free being called on every function exit,
145// so we maintain a lifo list of all current fake frames and update it on every
146// call to __asan_stack_malloc.
147class FakeStack {
148 public:
149  FakeStack();
150  explicit FakeStack(LinkerInitialized) {}
151  void Init(uptr stack_size);
152  void StopUsingFakeStack() { alive_ = false; }
153  void Cleanup();
154  uptr AllocateStack(uptr size, uptr real_stack);
155  static void OnFree(uptr ptr, uptr size, uptr real_stack);
156  // Return the bottom of the maped region.
157  uptr AddrIsInFakeStack(uptr addr);
158  bool StackSize() { return stack_size_; }
159
160 private:
161  static const uptr kMinStackFrameSizeLog = 9;  // Min frame is 512B.
162  static const uptr kMaxStackFrameSizeLog = 16;  // Max stack frame is 64K.
163  static const uptr kMaxStackMallocSize = 1 << kMaxStackFrameSizeLog;
164  static const uptr kNumberOfSizeClasses =
165      kMaxStackFrameSizeLog - kMinStackFrameSizeLog + 1;
166
167  bool AddrIsInSizeClass(uptr addr, uptr size_class);
168
169  // Each size class should be large enough to hold all frames.
170  uptr ClassMmapSize(uptr size_class);
171
172  uptr ClassSize(uptr size_class) {
173    return 1UL << (size_class + kMinStackFrameSizeLog);
174  }
175
176  void DeallocateFrame(FakeFrame *fake_frame);
177
178  uptr ComputeSizeClass(uptr alloc_size);
179  void AllocateOneSizeClass(uptr size_class);
180
181  uptr stack_size_;
182  bool   alive_;
183
184  uptr allocated_size_classes_[kNumberOfSizeClasses];
185  FakeFrameFifo size_classes_[kNumberOfSizeClasses];
186  FakeFrameLifo call_stack_;
187};
188
189void *asan_memalign(uptr alignment, uptr size, StackTrace *stack,
190                    AllocType alloc_type);
191void asan_free(void *ptr, StackTrace *stack, AllocType alloc_type);
192
193void *asan_malloc(uptr size, StackTrace *stack);
194void *asan_calloc(uptr nmemb, uptr size, StackTrace *stack);
195void *asan_realloc(void *p, uptr size, StackTrace *stack);
196void *asan_valloc(uptr size, StackTrace *stack);
197void *asan_pvalloc(uptr size, StackTrace *stack);
198
199int asan_posix_memalign(void **memptr, uptr alignment, uptr size,
200                          StackTrace *stack);
201uptr asan_malloc_usable_size(void *ptr, StackTrace *stack);
202
203uptr asan_mz_size(const void *ptr);
204void asan_mz_force_lock();
205void asan_mz_force_unlock();
206
207void PrintInternalAllocatorStats();
208
209}  // namespace __asan
210#endif  // ASAN_ALLOCATOR_H
211