d: Fix testcase failure of gdc.dg/Wbuiltin_declaration_mismatch2.d.
[official-gcc.git] / libsanitizer / hwasan / hwasan_allocator.h
blobecf3f6816fc7e7a7b71e34269e5c9d02e927b7c0
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.
11 //===----------------------------------------------------------------------===//
13 #ifndef HWASAN_ALLOCATOR_H
14 #define HWASAN_ALLOCATOR_H
16 #include "hwasan.h"
17 #include "hwasan_interface_internal.h"
18 #include "hwasan_mapping.h"
19 #include "hwasan_poisoning.h"
20 #include "lsan/lsan_common.h"
21 #include "sanitizer_common/sanitizer_allocator.h"
22 #include "sanitizer_common/sanitizer_allocator_checks.h"
23 #include "sanitizer_common/sanitizer_allocator_interface.h"
24 #include "sanitizer_common/sanitizer_allocator_report.h"
25 #include "sanitizer_common/sanitizer_common.h"
26 #include "sanitizer_common/sanitizer_ring_buffer.h"
28 #if !defined(__aarch64__) && !defined(__x86_64__) && !(SANITIZER_RISCV64)
29 # error Unsupported platform
30 #endif
32 namespace __hwasan {
34 struct Metadata {
35 private:
36 atomic_uint64_t alloc_context_id;
37 u32 requested_size_low;
38 u16 requested_size_high;
39 atomic_uint8_t chunk_state;
40 u8 lsan_tag;
42 public:
43 inline void SetAllocated(u32 stack, u64 size);
44 inline void SetUnallocated();
46 inline bool IsAllocated() const;
47 inline u64 GetRequestedSize() const;
48 inline u32 GetAllocStackId() const;
49 inline u32 GetAllocThreadId() const;
50 inline void SetLsanTag(__lsan::ChunkTag tag);
51 inline __lsan::ChunkTag GetLsanTag() const;
53 static_assert(sizeof(Metadata) == 16);
55 struct HwasanMapUnmapCallback {
56 void OnMap(uptr p, uptr size) const { UpdateMemoryUsage(); }
57 void OnUnmap(uptr p, uptr size) const {
58 // We are about to unmap a chunk of user memory.
59 // It can return as user-requested mmap() or another thread stack.
60 // Make it accessible with zero-tagged pointer.
61 TagMemory(p, size, 0);
65 static const uptr kMaxAllowedMallocSize = 1UL << 40; // 1T
67 struct AP64 {
68 static const uptr kSpaceBeg = ~0ULL;
70 #if defined(HWASAN_ALIASING_MODE)
71 static const uptr kSpaceSize = 1ULL << kAddressTagShift;
72 typedef __sanitizer::DefaultSizeClassMap SizeClassMap;
73 #elif SANITIZER_LINUX && !SANITIZER_ANDROID
74 static const uptr kSpaceSize = 0x40000000000ULL; // 4T.
75 typedef __sanitizer::DefaultSizeClassMap SizeClassMap;
76 #else
77 static const uptr kSpaceSize = 0x2000000000ULL; // 128G.
78 typedef __sanitizer::VeryDenseSizeClassMap SizeClassMap;
79 #endif
81 static const uptr kMetadataSize = sizeof(Metadata);
82 using AddressSpaceView = LocalAddressSpaceView;
83 typedef HwasanMapUnmapCallback MapUnmapCallback;
84 static const uptr kFlags = 0;
87 typedef SizeClassAllocator64<AP64> PrimaryAllocator;
88 typedef CombinedAllocator<PrimaryAllocator> Allocator;
89 typedef Allocator::AllocatorCache AllocatorCache;
91 void AllocatorSwallowThreadLocalCache(AllocatorCache *cache);
93 class HwasanChunkView {
94 public:
95 HwasanChunkView() : block_(0), metadata_(nullptr) {}
96 HwasanChunkView(uptr block, Metadata *metadata)
97 : block_(block), metadata_(metadata) {}
98 bool IsAllocated() const; // Checks if the memory is currently allocated
99 uptr Beg() const; // First byte of user memory
100 uptr End() const; // Last byte of user memory
101 uptr UsedSize() const; // Size requested by the user
102 uptr ActualSize() const; // Size allocated by the allocator.
103 u32 GetAllocStackId() const;
104 u32 GetAllocThreadId() const;
105 bool FromSmallHeap() const;
106 bool AddrIsInside(uptr addr) const;
108 private:
109 friend class __lsan::LsanMetadata;
110 uptr block_;
111 Metadata *const metadata_;
114 HwasanChunkView FindHeapChunkByAddress(uptr address);
116 // Information about one (de)allocation that happened in the past.
117 // These are recorded in a thread-local ring buffer.
118 struct HeapAllocationRecord {
119 uptr tagged_addr;
120 u32 alloc_thread_id;
121 u32 alloc_context_id;
122 u32 free_context_id;
123 u32 requested_size;
126 typedef RingBuffer<HeapAllocationRecord> HeapAllocationsRingBuffer;
128 void GetAllocatorStats(AllocatorStatCounters s);
130 } // namespace __hwasan
132 #endif // HWASAN_ALLOCATOR_H