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