blob: c5fcbbb5d64bb0d3a74e822fe2fda2e27eedf52a [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//
Kostya Serebryanyb1971ca2013-04-04 11:32:49 +000012// ASan-private header for asan_allocator2.cc.
Kostya Serebryany1e172b42011-11-30 01:07:02 +000013//===----------------------------------------------------------------------===//
14
15#ifndef ASAN_ALLOCATOR_H
16#define ASAN_ALLOCATOR_H
17
18#include "asan_internal.h"
19#include "asan_interceptors.h"
Kostya Serebryanyc88059c2012-12-17 07:54:29 +000020#include "sanitizer_common/sanitizer_list.h"
Kostya Serebryany1e172b42011-11-30 01:07:02 +000021
22namespace __asan {
23
Kostya Serebryanyfe6d9162012-12-21 08:53:59 +000024enum 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
Kostya Serebryany3f4c3872012-05-31 14:35:53 +000030static const uptr kNumberOfSizeClasses = 255;
Kostya Serebryanycbab9112011-11-30 17:33:13 +000031struct AsanChunk;
Kostya Serebryany1e172b42011-11-30 01:07:02 +000032
Kostya Serebryanyb4782602013-01-28 08:05:47 +000033void InitializeAllocator();
34
Alexey Samsonov5c153fa2012-09-18 07:38:10 +000035class AsanChunkView {
36 public:
37 explicit AsanChunkView(AsanChunk *chunk) : chunk_(chunk) {}
Alexey Samsonovd9def292013-10-14 11:13:54 +000038 bool IsValid(); // Checks if AsanChunkView points to a valid allocated
39 // or quarantined chunk.
40 uptr Beg(); // First byte of user memory.
41 uptr End(); // Last byte of user memory.
42 uptr UsedSize(); // Size requested by the user.
Alexey Samsonov5c153fa2012-09-18 07:38:10 +000043 uptr AllocTid();
44 uptr FreeTid();
45 void GetAllocStack(StackTrace *stack);
46 void GetFreeStack(StackTrace *stack);
Evgeniy Stepanov589dcda2013-02-05 14:32:03 +000047 bool AddrIsInside(uptr addr, uptr access_size, sptr *offset) {
Kostya Serebryany321e1252012-12-11 09:02:36 +000048 if (addr >= Beg() && (addr + access_size) <= End()) {
49 *offset = addr - Beg();
50 return true;
51 }
52 return false;
53 }
Evgeniy Stepanov589dcda2013-02-05 14:32:03 +000054 bool AddrIsAtLeft(uptr addr, uptr access_size, sptr *offset) {
Alexander Potapenko2ca12222012-12-12 12:32:57 +000055 (void)access_size;
Kostya Serebryany321e1252012-12-11 09:02:36 +000056 if (addr < Beg()) {
57 *offset = Beg() - addr;
58 return true;
59 }
60 return false;
61 }
Evgeniy Stepanov589dcda2013-02-05 14:32:03 +000062 bool AddrIsAtRight(uptr addr, uptr access_size, sptr *offset) {
Evgeniy Stepanoved835842013-02-08 12:59:42 +000063 if (addr + access_size > End()) {
Evgeniy Stepanov589dcda2013-02-05 14:32:03 +000064 *offset = addr - End();
Kostya Serebryany321e1252012-12-11 09:02:36 +000065 return true;
66 }
67 return false;
68 }
69
Alexey Samsonov5c153fa2012-09-18 07:38:10 +000070 private:
71 AsanChunk *const chunk_;
72};
73
74AsanChunkView FindHeapChunkByAddress(uptr address);
75
Kostya Serebryanyc88059c2012-12-17 07:54:29 +000076// List of AsanChunks with total size.
77class AsanChunkFifoList: public IntrusiveList<AsanChunk> {
Kostya Serebryany1e172b42011-11-30 01:07:02 +000078 public:
79 explicit AsanChunkFifoList(LinkerInitialized) { }
80 AsanChunkFifoList() { clear(); }
81 void Push(AsanChunk *n);
82 void PushList(AsanChunkFifoList *q);
83 AsanChunk *Pop();
Kostya Serebryany3f4c3872012-05-31 14:35:53 +000084 uptr size() { return size_; }
Kostya Serebryany1e172b42011-11-30 01:07:02 +000085 void clear() {
Kostya Serebryanyc88059c2012-12-17 07:54:29 +000086 IntrusiveList<AsanChunk>::clear();
Kostya Serebryany1e172b42011-11-30 01:07:02 +000087 size_ = 0;
88 }
89 private:
Kostya Serebryany3f4c3872012-05-31 14:35:53 +000090 uptr size_;
Kostya Serebryany1e172b42011-11-30 01:07:02 +000091};
92
93struct AsanThreadLocalMallocStorage {
94 explicit AsanThreadLocalMallocStorage(LinkerInitialized x)
Dmitry Vyukov9fc0df82013-01-11 08:07:43 +000095 { }
Kostya Serebryany1e172b42011-11-30 01:07:02 +000096 AsanThreadLocalMallocStorage() {
Alexey Samsonov09672ca2012-02-08 13:45:31 +000097 CHECK(REAL(memset));
98 REAL(memset)(this, 0, sizeof(AsanThreadLocalMallocStorage));
Kostya Serebryany1e172b42011-11-30 01:07:02 +000099 }
100
Dmitry Vyukov9fc0df82013-01-11 08:07:43 +0000101 uptr quarantine_cache[16];
Dmitry Vyukov40265e82013-01-15 10:45:18 +0000102 uptr allocator2_cache[96 * (512 * 8 + 16)]; // Opaque.
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000103 void CommitBack();
104};
105
Kostya Serebryanyfe6d9162012-12-21 08:53:59 +0000106void *asan_memalign(uptr alignment, uptr size, StackTrace *stack,
107 AllocType alloc_type);
108void asan_free(void *ptr, StackTrace *stack, AllocType alloc_type);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000109
Kostya Serebryanyc3390df2012-08-28 11:54:30 +0000110void *asan_malloc(uptr size, StackTrace *stack);
111void *asan_calloc(uptr nmemb, uptr size, StackTrace *stack);
112void *asan_realloc(void *p, uptr size, StackTrace *stack);
113void *asan_valloc(uptr size, StackTrace *stack);
114void *asan_pvalloc(uptr size, StackTrace *stack);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000115
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000116int asan_posix_memalign(void **memptr, uptr alignment, uptr size,
Kostya Serebryanyc3390df2012-08-28 11:54:30 +0000117 StackTrace *stack);
Alexey Samsonov1b17f5b2013-11-13 14:46:58 +0000118uptr asan_malloc_usable_size(void *ptr, uptr pc, uptr bp);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000119
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000120uptr asan_mz_size(const void *ptr);
Alexey Samsonov4fd95f12012-01-17 06:39:10 +0000121void asan_mz_force_lock();
122void asan_mz_force_unlock();
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000123
Kostya Serebryany4b48f452012-12-27 14:09:19 +0000124void PrintInternalAllocatorStats();
125
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000126} // namespace __asan
127#endif // ASAN_ALLOCATOR_H