[CMake] Respect CMAKE_CXX_FLAGS in custom clang_compile commands
[blocksruntime.git] / lib / asan / asan_allocator.h
blob10c9eee8da5a12db4e5aedca53fddba6ebd0b658
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.
12 // ASan-private header for asan_allocator2.cc.
13 //===----------------------------------------------------------------------===//
15 #ifndef ASAN_ALLOCATOR_H
16 #define ASAN_ALLOCATOR_H
18 #include "asan_internal.h"
19 #include "asan_interceptors.h"
20 #include "sanitizer_common/sanitizer_list.h"
22 namespace __asan {
24 enum 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 [ ]
30 static const uptr kNumberOfSizeClasses = 255;
31 struct AsanChunk;
33 void InitializeAllocator();
34 void ReInitializeAllocator();
36 class AsanChunkView {
37 public:
38 explicit AsanChunkView(AsanChunk *chunk) : chunk_(chunk) {}
39 bool IsValid(); // Checks if AsanChunkView points to a valid allocated
40 // or quarantined chunk.
41 uptr Beg(); // First byte of user memory.
42 uptr End(); // Last byte of user memory.
43 uptr UsedSize(); // Size requested by the user.
44 uptr AllocTid();
45 uptr FreeTid();
46 bool Eq(const AsanChunkView &c) const { return chunk_ == c.chunk_; }
47 void GetAllocStack(StackTrace *stack);
48 void GetFreeStack(StackTrace *stack);
49 bool AddrIsInside(uptr addr, uptr access_size, sptr *offset) {
50 if (addr >= Beg() && (addr + access_size) <= End()) {
51 *offset = addr - Beg();
52 return true;
54 return false;
56 bool AddrIsAtLeft(uptr addr, uptr access_size, sptr *offset) {
57 (void)access_size;
58 if (addr < Beg()) {
59 *offset = Beg() - addr;
60 return true;
62 return false;
64 bool AddrIsAtRight(uptr addr, uptr access_size, sptr *offset) {
65 if (addr + access_size > End()) {
66 *offset = addr - End();
67 return true;
69 return false;
72 private:
73 AsanChunk *const chunk_;
76 AsanChunkView FindHeapChunkByAddress(uptr address);
78 // List of AsanChunks with total size.
79 class AsanChunkFifoList: public IntrusiveList<AsanChunk> {
80 public:
81 explicit AsanChunkFifoList(LinkerInitialized) { }
82 AsanChunkFifoList() { clear(); }
83 void Push(AsanChunk *n);
84 void PushList(AsanChunkFifoList *q);
85 AsanChunk *Pop();
86 uptr size() { return size_; }
87 void clear() {
88 IntrusiveList<AsanChunk>::clear();
89 size_ = 0;
91 private:
92 uptr size_;
95 struct AsanThreadLocalMallocStorage {
96 uptr quarantine_cache[16];
97 // Allocator cache contains atomic_uint64_t which must be 8-byte aligned.
98 ALIGNED(8) uptr allocator2_cache[96 * (512 * 8 + 16)]; // Opaque.
99 void CommitBack();
100 private:
101 // These objects are allocated via mmap() and are zero-initialized.
102 AsanThreadLocalMallocStorage() {}
105 void *asan_memalign(uptr alignment, uptr size, StackTrace *stack,
106 AllocType alloc_type);
107 void asan_free(void *ptr, StackTrace *stack, AllocType alloc_type);
109 void *asan_malloc(uptr size, StackTrace *stack);
110 void *asan_calloc(uptr nmemb, uptr size, StackTrace *stack);
111 void *asan_realloc(void *p, uptr size, StackTrace *stack);
112 void *asan_valloc(uptr size, StackTrace *stack);
113 void *asan_pvalloc(uptr size, StackTrace *stack);
115 int asan_posix_memalign(void **memptr, uptr alignment, uptr size,
116 StackTrace *stack);
117 uptr asan_malloc_usable_size(void *ptr, uptr pc, uptr bp);
119 uptr asan_mz_size(const void *ptr);
120 void asan_mz_force_lock();
121 void asan_mz_force_unlock();
123 void PrintInternalAllocatorStats();
125 } // namespace __asan
126 #endif // ASAN_ALLOCATOR_H