* include/bits/allocator.h (operator==, operator!=): Add exception
[official-gcc.git] / libsanitizer / asan / asan_allocator.h
blob763f4a58ef9f3c0e31f0ac96aa477c4eca7b7f3c
1 //===-- asan_allocator.h ----------------------------------------*- C++ -*-===//
2 //
3 // This file is distributed under the University of Illinois Open Source
4 // License. See LICENSE.TXT for details.
5 //
6 //===----------------------------------------------------------------------===//
7 //
8 // This file is a part of AddressSanitizer, an address sanity checker.
9 //
10 // ASan-private header for asan_allocator2.cc.
11 //===----------------------------------------------------------------------===//
13 #ifndef ASAN_ALLOCATOR_H
14 #define ASAN_ALLOCATOR_H
16 #include "asan_internal.h"
17 #include "asan_interceptors.h"
18 #include "sanitizer_common/sanitizer_list.h"
20 namespace __asan {
22 enum AllocType {
23 FROM_MALLOC = 1, // Memory block came from malloc, calloc, realloc, etc.
24 FROM_NEW = 2, // Memory block came from operator new.
25 FROM_NEW_BR = 3 // Memory block came from operator new [ ]
28 static const uptr kNumberOfSizeClasses = 255;
29 struct AsanChunk;
31 void InitializeAllocator();
33 class AsanChunkView {
34 public:
35 explicit AsanChunkView(AsanChunk *chunk) : chunk_(chunk) {}
36 bool IsValid(); // Checks if AsanChunkView points to a valid allocated
37 // or quarantined chunk.
38 uptr Beg(); // First byte of user memory.
39 uptr End(); // Last byte of user memory.
40 uptr UsedSize(); // Size requested by the user.
41 uptr AllocTid();
42 uptr FreeTid();
43 void GetAllocStack(StackTrace *stack);
44 void GetFreeStack(StackTrace *stack);
45 bool AddrIsInside(uptr addr, uptr access_size, sptr *offset) {
46 if (addr >= Beg() && (addr + access_size) <= End()) {
47 *offset = addr - Beg();
48 return true;
50 return false;
52 bool AddrIsAtLeft(uptr addr, uptr access_size, sptr *offset) {
53 (void)access_size;
54 if (addr < Beg()) {
55 *offset = Beg() - addr;
56 return true;
58 return false;
60 bool AddrIsAtRight(uptr addr, uptr access_size, sptr *offset) {
61 if (addr + access_size > End()) {
62 *offset = addr - End();
63 return true;
65 return false;
68 private:
69 AsanChunk *const chunk_;
72 AsanChunkView FindHeapChunkByAddress(uptr address);
74 // List of AsanChunks with total size.
75 class AsanChunkFifoList: public IntrusiveList<AsanChunk> {
76 public:
77 explicit AsanChunkFifoList(LinkerInitialized) { }
78 AsanChunkFifoList() { clear(); }
79 void Push(AsanChunk *n);
80 void PushList(AsanChunkFifoList *q);
81 AsanChunk *Pop();
82 uptr size() { return size_; }
83 void clear() {
84 IntrusiveList<AsanChunk>::clear();
85 size_ = 0;
87 private:
88 uptr size_;
91 struct AsanThreadLocalMallocStorage {
92 uptr quarantine_cache[16];
93 uptr allocator2_cache[96 * (512 * 8 + 16)]; // Opaque.
94 void CommitBack();
95 private:
96 // These objects are allocated via mmap() and are zero-initialized.
97 AsanThreadLocalMallocStorage() {}
100 void *asan_memalign(uptr alignment, uptr size, StackTrace *stack,
101 AllocType alloc_type);
102 void asan_free(void *ptr, StackTrace *stack, AllocType alloc_type);
104 void *asan_malloc(uptr size, StackTrace *stack);
105 void *asan_calloc(uptr nmemb, uptr size, StackTrace *stack);
106 void *asan_realloc(void *p, uptr size, StackTrace *stack);
107 void *asan_valloc(uptr size, StackTrace *stack);
108 void *asan_pvalloc(uptr size, StackTrace *stack);
110 int asan_posix_memalign(void **memptr, uptr alignment, uptr size,
111 StackTrace *stack);
112 uptr asan_malloc_usable_size(void *ptr, uptr pc, uptr bp);
114 uptr asan_mz_size(const void *ptr);
115 void asan_mz_force_lock();
116 void asan_mz_force_unlock();
118 void PrintInternalAllocatorStats();
120 } // namespace __asan
121 #endif // ASAN_ALLOCATOR_H