blob: 1c6c30b22880062f634836c39520ef8e7cdd764e [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//
12// ASan-private header for asan_allocator.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
21namespace __asan {
22
Kostya Serebryany8d032042012-05-31 14:35:53 +000023static const uptr kNumberOfSizeClasses = 255;
Kostya Serebryany9d1eee92011-11-30 17:33:13 +000024struct AsanChunk;
Kostya Serebryany019b76f2011-11-30 01:07:02 +000025
Alexey Samsonov86614652012-09-18 07:38:10 +000026class AsanChunkView {
27 public:
28 explicit AsanChunkView(AsanChunk *chunk) : chunk_(chunk) {}
29 bool IsValid() { return chunk_ != 0; }
30 uptr Beg(); // first byte of user memory.
31 uptr End(); // last byte of user memory.
32 uptr UsedSize(); // size requested by the user.
33 uptr AllocTid();
34 uptr FreeTid();
35 void GetAllocStack(StackTrace *stack);
36 void GetFreeStack(StackTrace *stack);
37 bool AddrIsInside(uptr addr, uptr access_size, uptr *offset);
38 bool AddrIsAtLeft(uptr addr, uptr access_size, uptr *offset);
39 bool AddrIsAtRight(uptr addr, uptr access_size, uptr *offset);
40 private:
41 AsanChunk *const chunk_;
42};
43
44AsanChunkView FindHeapChunkByAddress(uptr address);
45
Kostya Serebryany019b76f2011-11-30 01:07:02 +000046class AsanChunkFifoList {
47 public:
48 explicit AsanChunkFifoList(LinkerInitialized) { }
49 AsanChunkFifoList() { clear(); }
50 void Push(AsanChunk *n);
51 void PushList(AsanChunkFifoList *q);
52 AsanChunk *Pop();
Kostya Serebryany8d032042012-05-31 14:35:53 +000053 uptr size() { return size_; }
Kostya Serebryany019b76f2011-11-30 01:07:02 +000054 void clear() {
Kostya Serebryany8d032042012-05-31 14:35:53 +000055 first_ = last_ = 0;
Kostya Serebryany019b76f2011-11-30 01:07:02 +000056 size_ = 0;
57 }
58 private:
59 AsanChunk *first_;
60 AsanChunk *last_;
Kostya Serebryany8d032042012-05-31 14:35:53 +000061 uptr size_;
Kostya Serebryany019b76f2011-11-30 01:07:02 +000062};
63
64struct AsanThreadLocalMallocStorage {
65 explicit AsanThreadLocalMallocStorage(LinkerInitialized x)
66 : quarantine_(x) { }
67 AsanThreadLocalMallocStorage() {
Alexey Samsonove7254782012-02-08 13:45:31 +000068 CHECK(REAL(memset));
69 REAL(memset)(this, 0, sizeof(AsanThreadLocalMallocStorage));
Kostya Serebryany019b76f2011-11-30 01:07:02 +000070 }
71
72 AsanChunkFifoList quarantine_;
73 AsanChunk *free_lists_[kNumberOfSizeClasses];
74 void CommitBack();
75};
76
77// Fake stack frame contains local variables of one function.
78// This struct should fit into a stack redzone (32 bytes).
79struct FakeFrame {
Kostya Serebryany8d032042012-05-31 14:35:53 +000080 uptr magic; // Modified by the instrumented code.
81 uptr descr; // Modified by the instrumented code.
Kostya Serebryany019b76f2011-11-30 01:07:02 +000082 FakeFrame *next;
Kostya Serebryany1d35d152012-05-31 15:02:07 +000083 u64 real_stack : 48;
84 u64 size_minus_one : 16;
Kostya Serebryany019b76f2011-11-30 01:07:02 +000085};
86
87struct FakeFrameFifo {
88 public:
89 void FifoPush(FakeFrame *node);
90 FakeFrame *FifoPop();
91 private:
92 FakeFrame *first_, *last_;
93};
94
95class FakeFrameLifo {
96 public:
97 void LifoPush(FakeFrame *node) {
98 node->next = top_;
99 top_ = node;
100 }
101 void LifoPop() {
102 CHECK(top_);
103 top_ = top_->next;
104 }
105 FakeFrame *top() { return top_; }
106 private:
107 FakeFrame *top_;
108};
109
110// For each thread we create a fake stack and place stack objects on this fake
111// stack instead of the real stack. The fake stack is not really a stack but
112// a fast malloc-like allocator so that when a function exits the fake stack
113// is not poped but remains there for quite some time until gets used again.
114// So, we poison the objects on the fake stack when function returns.
115// It helps us find use-after-return bugs.
116// We can not rely on __asan_stack_free being called on every function exit,
117// so we maintain a lifo list of all current fake frames and update it on every
118// call to __asan_stack_malloc.
119class FakeStack {
120 public:
121 FakeStack();
122 explicit FakeStack(LinkerInitialized) {}
Kostya Serebryany8d032042012-05-31 14:35:53 +0000123 void Init(uptr stack_size);
Kostya Serebryany72fde372011-12-09 01:49:31 +0000124 void StopUsingFakeStack() { alive_ = false; }
Kostya Serebryany019b76f2011-11-30 01:07:02 +0000125 void Cleanup();
Kostya Serebryany8d032042012-05-31 14:35:53 +0000126 uptr AllocateStack(uptr size, uptr real_stack);
127 static void OnFree(uptr ptr, uptr size, uptr real_stack);
Kostya Serebryany019b76f2011-11-30 01:07:02 +0000128 // Return the bottom of the maped region.
Kostya Serebryany8d032042012-05-31 14:35:53 +0000129 uptr AddrIsInFakeStack(uptr addr);
Alexander Potapenko0be25d52012-02-21 08:45:41 +0000130 bool StackSize() { return stack_size_; }
Alexey Samsonovc3a81192012-08-30 14:22:21 +0000131
Kostya Serebryany019b76f2011-11-30 01:07:02 +0000132 private:
Kostya Serebryany8d032042012-05-31 14:35:53 +0000133 static const uptr kMinStackFrameSizeLog = 9; // Min frame is 512B.
134 static const uptr kMaxStackFrameSizeLog = 16; // Max stack frame is 64K.
135 static const uptr kMaxStackMallocSize = 1 << kMaxStackFrameSizeLog;
136 static const uptr kNumberOfSizeClasses =
Kostya Serebryany019b76f2011-11-30 01:07:02 +0000137 kMaxStackFrameSizeLog - kMinStackFrameSizeLog + 1;
138
Kostya Serebryany8d032042012-05-31 14:35:53 +0000139 bool AddrIsInSizeClass(uptr addr, uptr size_class);
Kostya Serebryany019b76f2011-11-30 01:07:02 +0000140
141 // Each size class should be large enough to hold all frames.
Kostya Serebryany8d032042012-05-31 14:35:53 +0000142 uptr ClassMmapSize(uptr size_class);
Kostya Serebryany019b76f2011-11-30 01:07:02 +0000143
Kostya Serebryany8d032042012-05-31 14:35:53 +0000144 uptr ClassSize(uptr size_class) {
Kostya Serebryany019b76f2011-11-30 01:07:02 +0000145 return 1UL << (size_class + kMinStackFrameSizeLog);
146 }
147
148 void DeallocateFrame(FakeFrame *fake_frame);
149
Kostya Serebryany8d032042012-05-31 14:35:53 +0000150 uptr ComputeSizeClass(uptr alloc_size);
151 void AllocateOneSizeClass(uptr size_class);
Kostya Serebryany019b76f2011-11-30 01:07:02 +0000152
Kostya Serebryany8d032042012-05-31 14:35:53 +0000153 uptr stack_size_;
Kostya Serebryany019b76f2011-11-30 01:07:02 +0000154 bool alive_;
155
Kostya Serebryany8d032042012-05-31 14:35:53 +0000156 uptr allocated_size_classes_[kNumberOfSizeClasses];
Kostya Serebryany019b76f2011-11-30 01:07:02 +0000157 FakeFrameFifo size_classes_[kNumberOfSizeClasses];
158 FakeFrameLifo call_stack_;
159};
160
Kostya Serebryany6b0d7752012-08-28 11:54:30 +0000161void *asan_memalign(uptr alignment, uptr size, StackTrace *stack);
162void asan_free(void *ptr, StackTrace *stack);
Kostya Serebryany019b76f2011-11-30 01:07:02 +0000163
Kostya Serebryany6b0d7752012-08-28 11:54:30 +0000164void *asan_malloc(uptr size, StackTrace *stack);
165void *asan_calloc(uptr nmemb, uptr size, StackTrace *stack);
166void *asan_realloc(void *p, uptr size, StackTrace *stack);
167void *asan_valloc(uptr size, StackTrace *stack);
168void *asan_pvalloc(uptr size, StackTrace *stack);
Kostya Serebryany019b76f2011-11-30 01:07:02 +0000169
Kostya Serebryany8d032042012-05-31 14:35:53 +0000170int asan_posix_memalign(void **memptr, uptr alignment, uptr size,
Kostya Serebryany6b0d7752012-08-28 11:54:30 +0000171 StackTrace *stack);
172uptr asan_malloc_usable_size(void *ptr, StackTrace *stack);
Kostya Serebryany019b76f2011-11-30 01:07:02 +0000173
Kostya Serebryany8d032042012-05-31 14:35:53 +0000174uptr asan_mz_size(const void *ptr);
Alexey Samsonov209c5142012-01-17 06:39:10 +0000175void asan_mz_force_lock();
176void asan_mz_force_unlock();
Kostya Serebryany019b76f2011-11-30 01:07:02 +0000177
178} // namespace __asan
179#endif // ASAN_ALLOCATOR_H