asan_allocator.h revision 1b17f5b79d58c5aff291dde05727ad0b215b81c6
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.
11//
12// ASan-private header for asan_allocator2.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#include "sanitizer_common/sanitizer_list.h"
21
22namespace __asan {
23
24enum 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
30static const uptr kNumberOfSizeClasses = 255;
31struct AsanChunk;
32
33void InitializeAllocator();
34
35class AsanChunkView {
36 public:
37  explicit AsanChunkView(AsanChunk *chunk) : chunk_(chunk) {}
38  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.
43  uptr AllocTid();
44  uptr FreeTid();
45  void GetAllocStack(StackTrace *stack);
46  void GetFreeStack(StackTrace *stack);
47  bool AddrIsInside(uptr addr, uptr access_size, sptr *offset) {
48    if (addr >= Beg() && (addr + access_size) <= End()) {
49      *offset = addr - Beg();
50      return true;
51    }
52    return false;
53  }
54  bool AddrIsAtLeft(uptr addr, uptr access_size, sptr *offset) {
55    (void)access_size;
56    if (addr < Beg()) {
57      *offset = Beg() - addr;
58      return true;
59    }
60    return false;
61  }
62  bool AddrIsAtRight(uptr addr, uptr access_size, sptr *offset) {
63    if (addr + access_size > End()) {
64      *offset = addr - End();
65      return true;
66    }
67    return false;
68  }
69
70 private:
71  AsanChunk *const chunk_;
72};
73
74AsanChunkView FindHeapChunkByAddress(uptr address);
75
76// List of AsanChunks with total size.
77class AsanChunkFifoList: public IntrusiveList<AsanChunk> {
78 public:
79  explicit AsanChunkFifoList(LinkerInitialized) { }
80  AsanChunkFifoList() { clear(); }
81  void Push(AsanChunk *n);
82  void PushList(AsanChunkFifoList *q);
83  AsanChunk *Pop();
84  uptr size() { return size_; }
85  void clear() {
86    IntrusiveList<AsanChunk>::clear();
87    size_ = 0;
88  }
89 private:
90  uptr size_;
91};
92
93struct AsanThreadLocalMallocStorage {
94  explicit AsanThreadLocalMallocStorage(LinkerInitialized x)
95      { }
96  AsanThreadLocalMallocStorage() {
97    CHECK(REAL(memset));
98    REAL(memset)(this, 0, sizeof(AsanThreadLocalMallocStorage));
99  }
100
101  uptr quarantine_cache[16];
102  uptr allocator2_cache[96 * (512 * 8 + 16)];  // Opaque.
103  void CommitBack();
104};
105
106void *asan_memalign(uptr alignment, uptr size, StackTrace *stack,
107                    AllocType alloc_type);
108void asan_free(void *ptr, StackTrace *stack, AllocType alloc_type);
109
110void *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);
115
116int asan_posix_memalign(void **memptr, uptr alignment, uptr size,
117                          StackTrace *stack);
118uptr asan_malloc_usable_size(void *ptr, uptr pc, uptr bp);
119
120uptr asan_mz_size(const void *ptr);
121void asan_mz_force_lock();
122void asan_mz_force_unlock();
123
124void PrintInternalAllocatorStats();
125
126}  // namespace __asan
127#endif  // ASAN_ALLOCATOR_H
128