Home | History | Annotate | Download | only in asan
      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 
     22 namespace __asan {
     23 
     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 [ ]
     28 };
     29 
     30 static const uptr kNumberOfSizeClasses = 255;
     31 struct AsanChunk;
     32 
     33 void InitializeAllocator();
     34 
     35 class AsanChunkView {
     36  public:
     37   explicit AsanChunkView(AsanChunk *chunk) : chunk_(chunk) {}
     38   bool IsValid() { return chunk_ != 0; }
     39   uptr Beg();       // first byte of user memory.
     40   uptr End();       // last byte of user memory.
     41   uptr UsedSize();  // size requested by the user.
     42   uptr AllocTid();
     43   uptr FreeTid();
     44   void GetAllocStack(StackTrace *stack);
     45   void GetFreeStack(StackTrace *stack);
     46   bool AddrIsInside(uptr addr, uptr access_size, sptr *offset) {
     47     if (addr >= Beg() && (addr + access_size) <= End()) {
     48       *offset = addr - Beg();
     49       return true;
     50     }
     51     return false;
     52   }
     53   bool AddrIsAtLeft(uptr addr, uptr access_size, sptr *offset) {
     54     (void)access_size;
     55     if (addr < Beg()) {
     56       *offset = Beg() - addr;
     57       return true;
     58     }
     59     return false;
     60   }
     61   bool AddrIsAtRight(uptr addr, uptr access_size, sptr *offset) {
     62     if (addr + access_size > End()) {
     63       *offset = addr - End();
     64       return true;
     65     }
     66     return false;
     67   }
     68 
     69  private:
     70   AsanChunk *const chunk_;
     71 };
     72 
     73 AsanChunkView FindHeapChunkByAddress(uptr address);
     74 
     75 // List of AsanChunks with total size.
     76 class AsanChunkFifoList: public IntrusiveList<AsanChunk> {
     77  public:
     78   explicit AsanChunkFifoList(LinkerInitialized) { }
     79   AsanChunkFifoList() { clear(); }
     80   void Push(AsanChunk *n);
     81   void PushList(AsanChunkFifoList *q);
     82   AsanChunk *Pop();
     83   uptr size() { return size_; }
     84   void clear() {
     85     IntrusiveList<AsanChunk>::clear();
     86     size_ = 0;
     87   }
     88  private:
     89   uptr size_;
     90 };
     91 
     92 struct AsanThreadLocalMallocStorage {
     93   explicit AsanThreadLocalMallocStorage(LinkerInitialized x)
     94       { }
     95   AsanThreadLocalMallocStorage() {
     96     CHECK(REAL(memset));
     97     REAL(memset)(this, 0, sizeof(AsanThreadLocalMallocStorage));
     98   }
     99 
    100   uptr quarantine_cache[16];
    101   uptr allocator2_cache[96 * (512 * 8 + 16)];  // Opaque.
    102   void CommitBack();
    103 };
    104 
    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);
    108 
    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);
    114 
    115 int asan_posix_memalign(void **memptr, uptr alignment, uptr size,
    116                           StackTrace *stack);
    117 uptr asan_malloc_usable_size(void *ptr, StackTrace *stack);
    118 
    119 uptr asan_mz_size(const void *ptr);
    120 void asan_mz_force_lock();
    121 void asan_mz_force_unlock();
    122 
    123 void PrintInternalAllocatorStats();
    124 
    125 }  // namespace __asan
    126 #endif  // ASAN_ALLOCATOR_H
    127