]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/compiler-rt/lib/hwasan/hwasan_allocator.h
Merge llvm, clang, compiler-rt, libc++, lld, and lldb release_80 branch
[FreeBSD/FreeBSD.git] / contrib / compiler-rt / lib / hwasan / hwasan_allocator.h
1 //===-- hwasan_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 HWAddressSanitizer.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef HWASAN_ALLOCATOR_H
15 #define HWASAN_ALLOCATOR_H
16
17 #include "interception/interception.h"
18 #include "sanitizer_common/sanitizer_allocator.h"
19 #include "sanitizer_common/sanitizer_allocator_checks.h"
20 #include "sanitizer_common/sanitizer_allocator_interface.h"
21 #include "sanitizer_common/sanitizer_allocator_report.h"
22 #include "sanitizer_common/sanitizer_common.h"
23 #include "sanitizer_common/sanitizer_ring_buffer.h"
24 #include "hwasan_poisoning.h"
25
26 #if !defined(__aarch64__) && !defined(__x86_64__)
27 #error Unsupported platform
28 #endif
29
30 #if HWASAN_WITH_INTERCEPTORS
31 DECLARE_REAL(void *, realloc, void *ptr, uptr size)
32 DECLARE_REAL(void, free, void *ptr)
33 #endif
34
35 namespace __hwasan {
36
37 struct Metadata {
38   u32 requested_size : 31;  // sizes are < 2G.
39   u32 right_aligned  : 1;
40   u32 alloc_context_id;
41 };
42
43 struct HwasanMapUnmapCallback {
44   void OnMap(uptr p, uptr size) const { UpdateMemoryUsage(); }
45   void OnUnmap(uptr p, uptr size) const {
46     // We are about to unmap a chunk of user memory.
47     // It can return as user-requested mmap() or another thread stack.
48     // Make it accessible with zero-tagged pointer.
49     TagMemory(p, size, 0);
50   }
51 };
52
53 static const uptr kMaxAllowedMallocSize = 2UL << 30;  // 2G
54
55 struct AP64 {
56   static const uptr kSpaceBeg = ~0ULL;
57   static const uptr kSpaceSize = 0x2000000000ULL;
58   static const uptr kMetadataSize = sizeof(Metadata);
59   typedef __sanitizer::VeryDenseSizeClassMap SizeClassMap;
60   using AddressSpaceView = LocalAddressSpaceView;
61   typedef HwasanMapUnmapCallback MapUnmapCallback;
62   static const uptr kFlags = 0;
63 };
64 typedef SizeClassAllocator64<AP64> PrimaryAllocator;
65 typedef SizeClassAllocatorLocalCache<PrimaryAllocator> AllocatorCache;
66 typedef LargeMmapAllocator<HwasanMapUnmapCallback> SecondaryAllocator;
67 typedef CombinedAllocator<PrimaryAllocator, AllocatorCache,
68                           SecondaryAllocator> Allocator;
69
70 void AllocatorSwallowThreadLocalCache(AllocatorCache *cache);
71
72 class HwasanChunkView {
73  public:
74   HwasanChunkView() : block_(0), metadata_(nullptr) {}
75   HwasanChunkView(uptr block, Metadata *metadata)
76       : block_(block), metadata_(metadata) {}
77   bool IsAllocated() const;    // Checks if the memory is currently allocated
78   uptr Beg() const;            // First byte of user memory
79   uptr End() const;            // Last byte of user memory
80   uptr UsedSize() const;       // Size requested by the user
81   uptr ActualSize() const;     // Size allocated by the allocator.
82   u32 GetAllocStackId() const;
83   bool FromSmallHeap() const;
84  private:
85   uptr block_;
86   Metadata *const metadata_;
87 };
88
89 HwasanChunkView FindHeapChunkByAddress(uptr address);
90
91 // Information about one (de)allocation that happened in the past.
92 // These are recorded in a thread-local ring buffer.
93 // TODO: this is currently 24 bytes (20 bytes + alignment).
94 // Compress it to 16 bytes or extend it to be more useful.
95 struct HeapAllocationRecord {
96   uptr tagged_addr;
97   u32  alloc_context_id;
98   u32  free_context_id;
99   u32  requested_size;
100 };
101
102 typedef RingBuffer<HeapAllocationRecord> HeapAllocationsRingBuffer;
103
104 void GetAllocatorStats(AllocatorStatCounters s);
105
106 } // namespace __hwasan
107
108 #endif // HWASAN_ALLOCATOR_H