blob: 6b2324ae34a9451d8efd0fc4717e29da48ea1781 [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//
Kostya Serebryanyeef8bd42013-04-04 11:32:49 +000012// ASan-private header for asan_allocator2.cc.
Kostya Serebryany019b76f2011-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 Serebryanyd4b1b202014-04-15 13:30:32 +000020#include "sanitizer_common/sanitizer_allocator.h"
Kostya Serebryany41ffe3d2012-12-17 07:54:29 +000021#include "sanitizer_common/sanitizer_list.h"
Kostya Serebryany019b76f2011-11-30 01:07:02 +000022
23namespace __asan {
24
Kostya Serebryany3674c6b2012-12-21 08:53:59 +000025enum AllocType {
26 FROM_MALLOC = 1, // Memory block came from malloc, calloc, realloc, etc.
27 FROM_NEW = 2, // Memory block came from operator new.
28 FROM_NEW_BR = 3 // Memory block came from operator new [ ]
29};
30
Kostya Serebryany8d032042012-05-31 14:35:53 +000031static const uptr kNumberOfSizeClasses = 255;
Kostya Serebryany9d1eee92011-11-30 17:33:13 +000032struct AsanChunk;
Kostya Serebryany019b76f2011-11-30 01:07:02 +000033
Kostya Serebryany61761f12013-01-28 08:05:47 +000034void InitializeAllocator();
Evgeniy Stepanov756e1c12014-02-03 14:19:08 +000035void ReInitializeAllocator();
Kostya Serebryany61761f12013-01-28 08:05:47 +000036
Alexey Samsonov86614652012-09-18 07:38:10 +000037class AsanChunkView {
38 public:
39 explicit AsanChunkView(AsanChunk *chunk) : chunk_(chunk) {}
Alexey Samsonovdf6e6562013-10-14 11:13:54 +000040 bool IsValid(); // Checks if AsanChunkView points to a valid allocated
41 // or quarantined chunk.
42 uptr Beg(); // First byte of user memory.
43 uptr End(); // Last byte of user memory.
44 uptr UsedSize(); // Size requested by the user.
Alexey Samsonov86614652012-09-18 07:38:10 +000045 uptr AllocTid();
46 uptr FreeTid();
Kostya Serebryany796f6552014-02-27 12:45:36 +000047 bool Eq(const AsanChunkView &c) const { return chunk_ == c.chunk_; }
Alexey Samsonov86614652012-09-18 07:38:10 +000048 void GetAllocStack(StackTrace *stack);
49 void GetFreeStack(StackTrace *stack);
Evgeniy Stepanov1bc72982013-02-05 14:32:03 +000050 bool AddrIsInside(uptr addr, uptr access_size, sptr *offset) {
Kostya Serebryany5e2a7ac2012-12-11 09:02:36 +000051 if (addr >= Beg() && (addr + access_size) <= End()) {
52 *offset = addr - Beg();
53 return true;
54 }
55 return false;
56 }
Evgeniy Stepanov1bc72982013-02-05 14:32:03 +000057 bool AddrIsAtLeft(uptr addr, uptr access_size, sptr *offset) {
Alexander Potapenko602a09f2012-12-12 12:32:57 +000058 (void)access_size;
Kostya Serebryany5e2a7ac2012-12-11 09:02:36 +000059 if (addr < Beg()) {
60 *offset = Beg() - addr;
61 return true;
62 }
63 return false;
64 }
Evgeniy Stepanov1bc72982013-02-05 14:32:03 +000065 bool AddrIsAtRight(uptr addr, uptr access_size, sptr *offset) {
Evgeniy Stepanov0b805cc2013-02-08 12:59:42 +000066 if (addr + access_size > End()) {
Evgeniy Stepanov1bc72982013-02-05 14:32:03 +000067 *offset = addr - End();
Kostya Serebryany5e2a7ac2012-12-11 09:02:36 +000068 return true;
69 }
70 return false;
71 }
72
Alexey Samsonov86614652012-09-18 07:38:10 +000073 private:
74 AsanChunk *const chunk_;
75};
76
77AsanChunkView FindHeapChunkByAddress(uptr address);
78
Kostya Serebryany41ffe3d2012-12-17 07:54:29 +000079// List of AsanChunks with total size.
80class AsanChunkFifoList: public IntrusiveList<AsanChunk> {
Kostya Serebryany019b76f2011-11-30 01:07:02 +000081 public:
82 explicit AsanChunkFifoList(LinkerInitialized) { }
83 AsanChunkFifoList() { clear(); }
84 void Push(AsanChunk *n);
85 void PushList(AsanChunkFifoList *q);
86 AsanChunk *Pop();
Kostya Serebryany8d032042012-05-31 14:35:53 +000087 uptr size() { return size_; }
Kostya Serebryany019b76f2011-11-30 01:07:02 +000088 void clear() {
Kostya Serebryany41ffe3d2012-12-17 07:54:29 +000089 IntrusiveList<AsanChunk>::clear();
Kostya Serebryany019b76f2011-11-30 01:07:02 +000090 size_ = 0;
91 }
92 private:
Kostya Serebryany8d032042012-05-31 14:35:53 +000093 uptr size_;
Kostya Serebryany019b76f2011-11-30 01:07:02 +000094};
95
Kostya Serebryanyd4b1b202014-04-15 13:30:32 +000096struct AsanMapUnmapCallback {
97 void OnMap(uptr p, uptr size) const;
98 void OnUnmap(uptr p, uptr size) const;
99};
100
101#if SANITIZER_CAN_USE_ALLOCATOR64
102# if defined(__powerpc64__)
103const uptr kAllocatorSpace = 0xa0000000000ULL;
104const uptr kAllocatorSize = 0x20000000000ULL; // 2T.
105# else
106const uptr kAllocatorSpace = 0x600000000000ULL;
107const uptr kAllocatorSize = 0x40000000000ULL; // 4T.
108# endif
109typedef DefaultSizeClassMap SizeClassMap;
110typedef SizeClassAllocator64<kAllocatorSpace, kAllocatorSize, 0 /*metadata*/,
111 SizeClassMap, AsanMapUnmapCallback> PrimaryAllocator;
112#else // Fallback to SizeClassAllocator32.
113static const uptr kRegionSizeLog = 20;
114static const uptr kNumRegions = SANITIZER_MMAP_RANGE_SIZE >> kRegionSizeLog;
115# if SANITIZER_WORDSIZE == 32
116typedef FlatByteMap<kNumRegions> ByteMap;
117# elif SANITIZER_WORDSIZE == 64
118typedef TwoLevelByteMap<(kNumRegions >> 12), 1 << 12> ByteMap;
119# endif
120typedef CompactSizeClassMap SizeClassMap;
121typedef SizeClassAllocator32<0, SANITIZER_MMAP_RANGE_SIZE, 16,
122 SizeClassMap, kRegionSizeLog,
123 ByteMap,
124 AsanMapUnmapCallback> PrimaryAllocator;
125#endif // SANITIZER_CAN_USE_ALLOCATOR64
126
127typedef SizeClassAllocatorLocalCache<PrimaryAllocator> AllocatorCache;
128typedef LargeMmapAllocator<AsanMapUnmapCallback> SecondaryAllocator;
129typedef CombinedAllocator<PrimaryAllocator, AllocatorCache,
130 SecondaryAllocator> Allocator;
131
132
Kostya Serebryany019b76f2011-11-30 01:07:02 +0000133struct AsanThreadLocalMallocStorage {
Dmitry Vyukovdb0cf872013-01-11 08:07:43 +0000134 uptr quarantine_cache[16];
Kostya Serebryanyd4b1b202014-04-15 13:30:32 +0000135 AllocatorCache allocator2_cache;
Kostya Serebryany019b76f2011-11-30 01:07:02 +0000136 void CommitBack();
Alexey Samsonove0e31c42013-11-27 13:22:21 +0000137 private:
138 // These objects are allocated via mmap() and are zero-initialized.
139 AsanThreadLocalMallocStorage() {}
Kostya Serebryany019b76f2011-11-30 01:07:02 +0000140};
141
Kostya Serebryany3674c6b2012-12-21 08:53:59 +0000142void *asan_memalign(uptr alignment, uptr size, StackTrace *stack,
143 AllocType alloc_type);
144void asan_free(void *ptr, StackTrace *stack, AllocType alloc_type);
Kostya Serebryany019b76f2011-11-30 01:07:02 +0000145
Kostya Serebryany6b0d7752012-08-28 11:54:30 +0000146void *asan_malloc(uptr size, StackTrace *stack);
147void *asan_calloc(uptr nmemb, uptr size, StackTrace *stack);
148void *asan_realloc(void *p, uptr size, StackTrace *stack);
149void *asan_valloc(uptr size, StackTrace *stack);
150void *asan_pvalloc(uptr size, StackTrace *stack);
Kostya Serebryany019b76f2011-11-30 01:07:02 +0000151
Kostya Serebryany8d032042012-05-31 14:35:53 +0000152int asan_posix_memalign(void **memptr, uptr alignment, uptr size,
Kostya Serebryany6b0d7752012-08-28 11:54:30 +0000153 StackTrace *stack);
Alexey Samsonov9ff45982013-11-13 14:46:58 +0000154uptr asan_malloc_usable_size(void *ptr, uptr pc, uptr bp);
Kostya Serebryany019b76f2011-11-30 01:07:02 +0000155
Kostya Serebryany8d032042012-05-31 14:35:53 +0000156uptr asan_mz_size(const void *ptr);
Alexey Samsonov209c5142012-01-17 06:39:10 +0000157void asan_mz_force_lock();
158void asan_mz_force_unlock();
Kostya Serebryany019b76f2011-11-30 01:07:02 +0000159
Kostya Serebryany4a42cf62012-12-27 14:09:19 +0000160void PrintInternalAllocatorStats();
161
Kostya Serebryany019b76f2011-11-30 01:07:02 +0000162} // namespace __asan
163#endif // ASAN_ALLOCATOR_H