Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 1 | //===-- scudo_allocator.cpp -------------------------------------*- 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 | /// Scudo Hardened Allocator implementation. |
| 11 | /// It uses the sanitizer_common allocator as a base and aims at mitigating |
| 12 | /// heap corruption vulnerabilities. It provides a checksum-guarded chunk |
| 13 | /// header, a delayed free list, and additional sanity checks. |
| 14 | /// |
| 15 | //===----------------------------------------------------------------------===// |
| 16 | |
| 17 | #include "scudo_allocator.h" |
Kostya Kortchinsky | b0e96eb | 2017-05-09 15:12:38 +0000 | [diff] [blame] | 18 | #include "scudo_crc32.h" |
Kostya Kortchinsky | 36b3434 | 2017-04-27 20:21:16 +0000 | [diff] [blame] | 19 | #include "scudo_tls.h" |
Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 20 | #include "scudo_utils.h" |
| 21 | |
Alex Shlyapnikov | 42bea01 | 2017-07-18 19:11:04 +0000 | [diff] [blame^] | 22 | #include "sanitizer_common/sanitizer_allocator_checks.h" |
Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 23 | #include "sanitizer_common/sanitizer_allocator_interface.h" |
Alex Shlyapnikov | df18cbb | 2017-07-14 21:17:16 +0000 | [diff] [blame] | 24 | #include "sanitizer_common/sanitizer_errno.h" |
Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 25 | #include "sanitizer_common/sanitizer_quarantine.h" |
| 26 | |
Kostya Kortchinsky | 006805d | 2017-04-20 15:11:00 +0000 | [diff] [blame] | 27 | #include <string.h> |
Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 28 | |
| 29 | namespace __scudo { |
| 30 | |
Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 31 | // Global static cookie, initialized at start-up. |
Kostya Kortchinsky | 1148dc5 | 2016-11-30 17:32:20 +0000 | [diff] [blame] | 32 | static uptr Cookie; |
| 33 | |
Kostya Kortchinsky | 1148dc5 | 2016-11-30 17:32:20 +0000 | [diff] [blame] | 34 | // We default to software CRC32 if the alternatives are not supported, either |
| 35 | // at compilation or at runtime. |
| 36 | static atomic_uint8_t HashAlgorithm = { CRC32Software }; |
| 37 | |
Kostya Kortchinsky | b0e96eb | 2017-05-09 15:12:38 +0000 | [diff] [blame] | 38 | INLINE u32 computeCRC32(uptr Crc, uptr Value, uptr *Array, uptr ArraySize) { |
| 39 | // If the hardware CRC32 feature is defined here, it was enabled everywhere, |
| 40 | // as opposed to only for scudo_crc32.cpp. This means that other hardware |
| 41 | // specific instructions were likely emitted at other places, and as a |
| 42 | // result there is no reason to not use it here. |
Kostya Kortchinsky | b39dff4 | 2017-01-18 17:11:17 +0000 | [diff] [blame] | 43 | #if defined(__SSE4_2__) || defined(__ARM_FEATURE_CRC32) |
Kostya Kortchinsky | b0e96eb | 2017-05-09 15:12:38 +0000 | [diff] [blame] | 44 | Crc = CRC32_INTRINSIC(Crc, Value); |
| 45 | for (uptr i = 0; i < ArraySize; i++) |
| 46 | Crc = CRC32_INTRINSIC(Crc, Array[i]); |
| 47 | return Crc; |
Kostya Kortchinsky | b39dff4 | 2017-01-18 17:11:17 +0000 | [diff] [blame] | 48 | #else |
Kostya Kortchinsky | b0e96eb | 2017-05-09 15:12:38 +0000 | [diff] [blame] | 49 | if (atomic_load_relaxed(&HashAlgorithm) == CRC32Hardware) { |
| 50 | Crc = computeHardwareCRC32(Crc, Value); |
| 51 | for (uptr i = 0; i < ArraySize; i++) |
| 52 | Crc = computeHardwareCRC32(Crc, Array[i]); |
| 53 | return Crc; |
| 54 | } |
| 55 | Crc = computeSoftwareCRC32(Crc, Value); |
| 56 | for (uptr i = 0; i < ArraySize; i++) |
| 57 | Crc = computeSoftwareCRC32(Crc, Array[i]); |
| 58 | return Crc; |
| 59 | #endif // defined(__SSE4_2__) || defined(__ARM_FEATURE_CRC32) |
Kostya Kortchinsky | 1148dc5 | 2016-11-30 17:32:20 +0000 | [diff] [blame] | 60 | } |
Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 61 | |
Kostya Kortchinsky | 36b3434 | 2017-04-27 20:21:16 +0000 | [diff] [blame] | 62 | static ScudoBackendAllocator &getBackendAllocator(); |
| 63 | |
Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 64 | struct ScudoChunk : UnpackedHeader { |
| 65 | // We can't use the offset member of the chunk itself, as we would double |
| 66 | // fetch it without any warranty that it wouldn't have been tampered. To |
| 67 | // prevent this, we work with a local copy of the header. |
Kostya Kortchinsky | 71dcc33 | 2016-10-26 16:16:58 +0000 | [diff] [blame] | 68 | void *getAllocBeg(UnpackedHeader *Header) { |
Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 69 | return reinterpret_cast<void *>( |
| 70 | reinterpret_cast<uptr>(this) - (Header->Offset << MinAlignmentLog)); |
| 71 | } |
| 72 | |
Kostya Kortchinsky | 1148dc5 | 2016-11-30 17:32:20 +0000 | [diff] [blame] | 73 | // Returns the usable size for a chunk, meaning the amount of bytes from the |
| 74 | // beginning of the user data to the end of the backend allocated chunk. |
| 75 | uptr getUsableSize(UnpackedHeader *Header) { |
Kostya Kortchinsky | 01a66fc | 2017-05-11 21:40:45 +0000 | [diff] [blame] | 76 | uptr Size = |
Kostya Kortchinsky | b44364d | 2017-07-13 21:01:19 +0000 | [diff] [blame] | 77 | getBackendAllocator().getActuallyAllocatedSize(getAllocBeg(Header), |
Kostya Kortchinsky | 01a66fc | 2017-05-11 21:40:45 +0000 | [diff] [blame] | 78 | Header->FromPrimary); |
Kostya Kortchinsky | 1148dc5 | 2016-11-30 17:32:20 +0000 | [diff] [blame] | 79 | if (Size == 0) |
Kostya Kortchinsky | 006805d | 2017-04-20 15:11:00 +0000 | [diff] [blame] | 80 | return 0; |
Kostya Kortchinsky | 1148dc5 | 2016-11-30 17:32:20 +0000 | [diff] [blame] | 81 | return Size - AlignedChunkHeaderSize - (Header->Offset << MinAlignmentLog); |
| 82 | } |
| 83 | |
| 84 | // Compute the checksum of the Chunk pointer and its ChunkHeader. |
Kostya Kortchinsky | 71dcc33 | 2016-10-26 16:16:58 +0000 | [diff] [blame] | 85 | u16 computeChecksum(UnpackedHeader *Header) const { |
Kostya Kortchinsky | 1148dc5 | 2016-11-30 17:32:20 +0000 | [diff] [blame] | 86 | UnpackedHeader ZeroChecksumHeader = *Header; |
| 87 | ZeroChecksumHeader.Checksum = 0; |
| 88 | uptr HeaderHolder[sizeof(UnpackedHeader) / sizeof(uptr)]; |
| 89 | memcpy(&HeaderHolder, &ZeroChecksumHeader, sizeof(HeaderHolder)); |
Kostya Kortchinsky | b0e96eb | 2017-05-09 15:12:38 +0000 | [diff] [blame] | 90 | u32 Crc = computeCRC32(Cookie, reinterpret_cast<uptr>(this), HeaderHolder, |
| 91 | ARRAY_SIZE(HeaderHolder)); |
Kostya Kortchinsky | b39dff4 | 2017-01-18 17:11:17 +0000 | [diff] [blame] | 92 | return static_cast<u16>(Crc); |
Kostya Kortchinsky | 1148dc5 | 2016-11-30 17:32:20 +0000 | [diff] [blame] | 93 | } |
| 94 | |
Kostya Kortchinsky | 006805d | 2017-04-20 15:11:00 +0000 | [diff] [blame] | 95 | // Checks the validity of a chunk by verifying its checksum. It doesn't |
| 96 | // incur termination in the event of an invalid chunk. |
Kostya Kortchinsky | 1148dc5 | 2016-11-30 17:32:20 +0000 | [diff] [blame] | 97 | bool isValid() { |
| 98 | UnpackedHeader NewUnpackedHeader; |
| 99 | const AtomicPackedHeader *AtomicHeader = |
| 100 | reinterpret_cast<const AtomicPackedHeader *>(this); |
Kostya Kortchinsky | a00b922 | 2017-01-20 18:32:18 +0000 | [diff] [blame] | 101 | PackedHeader NewPackedHeader = atomic_load_relaxed(AtomicHeader); |
Kostya Kortchinsky | 1148dc5 | 2016-11-30 17:32:20 +0000 | [diff] [blame] | 102 | NewUnpackedHeader = bit_cast<UnpackedHeader>(NewPackedHeader); |
| 103 | return (NewUnpackedHeader.Checksum == computeChecksum(&NewUnpackedHeader)); |
Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 104 | } |
| 105 | |
Kostya Kortchinsky | 006805d | 2017-04-20 15:11:00 +0000 | [diff] [blame] | 106 | // Nulls out a chunk header. When returning the chunk to the backend, there |
| 107 | // is no need to store a valid ChunkAvailable header, as this would be |
| 108 | // computationally expensive. Zeroing out serves the same purpose by making |
| 109 | // the header invalid. In the extremely rare event where 0 would be a valid |
| 110 | // checksum for the chunk, the state of the chunk is ChunkAvailable anyway. |
| 111 | COMPILER_CHECK(ChunkAvailable == 0); |
| 112 | void eraseHeader() { |
| 113 | PackedHeader NullPackedHeader = 0; |
| 114 | AtomicPackedHeader *AtomicHeader = |
| 115 | reinterpret_cast<AtomicPackedHeader *>(this); |
| 116 | atomic_store_relaxed(AtomicHeader, NullPackedHeader); |
| 117 | } |
| 118 | |
Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 119 | // Loads and unpacks the header, verifying the checksum in the process. |
| 120 | void loadHeader(UnpackedHeader *NewUnpackedHeader) const { |
| 121 | const AtomicPackedHeader *AtomicHeader = |
| 122 | reinterpret_cast<const AtomicPackedHeader *>(this); |
Kostya Kortchinsky | a00b922 | 2017-01-20 18:32:18 +0000 | [diff] [blame] | 123 | PackedHeader NewPackedHeader = atomic_load_relaxed(AtomicHeader); |
Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 124 | *NewUnpackedHeader = bit_cast<UnpackedHeader>(NewPackedHeader); |
Kostya Kortchinsky | 006805d | 2017-04-20 15:11:00 +0000 | [diff] [blame] | 125 | if (UNLIKELY(NewUnpackedHeader->Checksum != |
| 126 | computeChecksum(NewUnpackedHeader))) { |
Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 127 | dieWithMessage("ERROR: corrupted chunk header at address %p\n", this); |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | // Packs and stores the header, computing the checksum in the process. |
| 132 | void storeHeader(UnpackedHeader *NewUnpackedHeader) { |
Kostya Kortchinsky | 71dcc33 | 2016-10-26 16:16:58 +0000 | [diff] [blame] | 133 | NewUnpackedHeader->Checksum = computeChecksum(NewUnpackedHeader); |
Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 134 | PackedHeader NewPackedHeader = bit_cast<PackedHeader>(*NewUnpackedHeader); |
| 135 | AtomicPackedHeader *AtomicHeader = |
| 136 | reinterpret_cast<AtomicPackedHeader *>(this); |
Kostya Kortchinsky | a00b922 | 2017-01-20 18:32:18 +0000 | [diff] [blame] | 137 | atomic_store_relaxed(AtomicHeader, NewPackedHeader); |
Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | // Packs and stores the header, computing the checksum in the process. We |
| 141 | // compare the current header with the expected provided one to ensure that |
| 142 | // we are not being raced by a corruption occurring in another thread. |
| 143 | void compareExchangeHeader(UnpackedHeader *NewUnpackedHeader, |
| 144 | UnpackedHeader *OldUnpackedHeader) { |
Kostya Kortchinsky | 71dcc33 | 2016-10-26 16:16:58 +0000 | [diff] [blame] | 145 | NewUnpackedHeader->Checksum = computeChecksum(NewUnpackedHeader); |
Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 146 | PackedHeader NewPackedHeader = bit_cast<PackedHeader>(*NewUnpackedHeader); |
| 147 | PackedHeader OldPackedHeader = bit_cast<PackedHeader>(*OldUnpackedHeader); |
| 148 | AtomicPackedHeader *AtomicHeader = |
| 149 | reinterpret_cast<AtomicPackedHeader *>(this); |
Kostya Kortchinsky | 006805d | 2017-04-20 15:11:00 +0000 | [diff] [blame] | 150 | if (UNLIKELY(!atomic_compare_exchange_strong(AtomicHeader, |
| 151 | &OldPackedHeader, |
| 152 | NewPackedHeader, |
| 153 | memory_order_relaxed))) { |
Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 154 | dieWithMessage("ERROR: race on chunk header at address %p\n", this); |
| 155 | } |
| 156 | } |
| 157 | }; |
| 158 | |
Kostya Kortchinsky | 006805d | 2017-04-20 15:11:00 +0000 | [diff] [blame] | 159 | ScudoChunk *getScudoChunk(uptr UserBeg) { |
| 160 | return reinterpret_cast<ScudoChunk *>(UserBeg - AlignedChunkHeaderSize); |
| 161 | } |
| 162 | |
Kostya Kortchinsky | 36b3434 | 2017-04-27 20:21:16 +0000 | [diff] [blame] | 163 | struct AllocatorOptions { |
| 164 | u32 QuarantineSizeMb; |
| 165 | u32 ThreadLocalQuarantineSizeKb; |
| 166 | bool MayReturnNull; |
| 167 | s32 ReleaseToOSIntervalMs; |
| 168 | bool DeallocationTypeMismatch; |
| 169 | bool DeleteSizeMismatch; |
| 170 | bool ZeroContents; |
Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 171 | |
Kostya Kortchinsky | 36b3434 | 2017-04-27 20:21:16 +0000 | [diff] [blame] | 172 | void setFrom(const Flags *f, const CommonFlags *cf); |
| 173 | void copyTo(Flags *f, CommonFlags *cf) const; |
| 174 | }; |
Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 175 | |
Kostya Kortchinsky | 36b3434 | 2017-04-27 20:21:16 +0000 | [diff] [blame] | 176 | void AllocatorOptions::setFrom(const Flags *f, const CommonFlags *cf) { |
| 177 | MayReturnNull = cf->allocator_may_return_null; |
| 178 | ReleaseToOSIntervalMs = cf->allocator_release_to_os_interval_ms; |
| 179 | QuarantineSizeMb = f->QuarantineSizeMb; |
| 180 | ThreadLocalQuarantineSizeKb = f->ThreadLocalQuarantineSizeKb; |
| 181 | DeallocationTypeMismatch = f->DeallocationTypeMismatch; |
| 182 | DeleteSizeMismatch = f->DeleteSizeMismatch; |
| 183 | ZeroContents = f->ZeroContents; |
Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 184 | } |
| 185 | |
Kostya Kortchinsky | 36b3434 | 2017-04-27 20:21:16 +0000 | [diff] [blame] | 186 | void AllocatorOptions::copyTo(Flags *f, CommonFlags *cf) const { |
| 187 | cf->allocator_may_return_null = MayReturnNull; |
| 188 | cf->allocator_release_to_os_interval_ms = ReleaseToOSIntervalMs; |
| 189 | f->QuarantineSizeMb = QuarantineSizeMb; |
| 190 | f->ThreadLocalQuarantineSizeKb = ThreadLocalQuarantineSizeKb; |
| 191 | f->DeallocationTypeMismatch = DeallocationTypeMismatch; |
| 192 | f->DeleteSizeMismatch = DeleteSizeMismatch; |
| 193 | f->ZeroContents = ZeroContents; |
| 194 | } |
| 195 | |
| 196 | static void initScudoInternal(const AllocatorOptions &Options); |
| 197 | |
| 198 | static bool ScudoInitIsRunning = false; |
| 199 | |
| 200 | void initScudo() { |
Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 201 | SanitizerToolName = "Scudo"; |
| 202 | CHECK(!ScudoInitIsRunning && "Scudo init calls itself!"); |
| 203 | ScudoInitIsRunning = true; |
| 204 | |
Kostya Kortchinsky | b0e96eb | 2017-05-09 15:12:38 +0000 | [diff] [blame] | 205 | // Check if hardware CRC32 is supported in the binary and by the platform, if |
| 206 | // so, opt for the CRC32 hardware version of the checksum. |
| 207 | if (computeHardwareCRC32 && testCPUFeature(CRC32CPUFeature)) |
Kostya Kortchinsky | 1148dc5 | 2016-11-30 17:32:20 +0000 | [diff] [blame] | 208 | atomic_store_relaxed(&HashAlgorithm, CRC32Hardware); |
Kostya Kortchinsky | 1148dc5 | 2016-11-30 17:32:20 +0000 | [diff] [blame] | 209 | |
Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 210 | initFlags(); |
| 211 | |
| 212 | AllocatorOptions Options; |
| 213 | Options.setFrom(getFlags(), common_flags()); |
Kostya Kortchinsky | 36b3434 | 2017-04-27 20:21:16 +0000 | [diff] [blame] | 214 | initScudoInternal(Options); |
Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 215 | |
Kostya Kortchinsky | 36b3434 | 2017-04-27 20:21:16 +0000 | [diff] [blame] | 216 | // TODO(kostyak): determine if MaybeStartBackgroudThread could be of some use. |
Kostya Kortchinsky | 71dcc33 | 2016-10-26 16:16:58 +0000 | [diff] [blame] | 217 | |
Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 218 | ScudoInitIsRunning = false; |
| 219 | } |
| 220 | |
Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 221 | struct QuarantineCallback { |
| 222 | explicit QuarantineCallback(AllocatorCache *Cache) |
| 223 | : Cache_(Cache) {} |
| 224 | |
Kostya Kortchinsky | 01a66fc | 2017-05-11 21:40:45 +0000 | [diff] [blame] | 225 | // Chunk recycling function, returns a quarantined chunk to the backend, |
| 226 | // first making sure it hasn't been tampered with. |
Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 227 | void Recycle(ScudoChunk *Chunk) { |
| 228 | UnpackedHeader Header; |
| 229 | Chunk->loadHeader(&Header); |
Kostya Kortchinsky | 006805d | 2017-04-20 15:11:00 +0000 | [diff] [blame] | 230 | if (UNLIKELY(Header.State != ChunkQuarantine)) { |
Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 231 | dieWithMessage("ERROR: invalid chunk state when recycling address %p\n", |
| 232 | Chunk); |
| 233 | } |
Kostya Kortchinsky | 006805d | 2017-04-20 15:11:00 +0000 | [diff] [blame] | 234 | Chunk->eraseHeader(); |
Kostya Kortchinsky | 71dcc33 | 2016-10-26 16:16:58 +0000 | [diff] [blame] | 235 | void *Ptr = Chunk->getAllocBeg(&Header); |
Kostya Kortchinsky | b44364d | 2017-07-13 21:01:19 +0000 | [diff] [blame] | 236 | if (Header.FromPrimary) |
| 237 | getBackendAllocator().deallocatePrimary(Cache_, Ptr); |
| 238 | else |
| 239 | getBackendAllocator().deallocateSecondary(Ptr); |
Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 240 | } |
| 241 | |
Kostya Kortchinsky | 01a66fc | 2017-05-11 21:40:45 +0000 | [diff] [blame] | 242 | // Internal quarantine allocation and deallocation functions. We first check |
| 243 | // that the batches are indeed serviced by the Primary. |
| 244 | // TODO(kostyak): figure out the best way to protect the batches. |
| 245 | COMPILER_CHECK(sizeof(QuarantineBatch) < SizeClassMap::kMaxSize); |
Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 246 | void *Allocate(uptr Size) { |
Kostya Kortchinsky | b44364d | 2017-07-13 21:01:19 +0000 | [diff] [blame] | 247 | return getBackendAllocator().allocatePrimary(Cache_, Size); |
Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | void Deallocate(void *Ptr) { |
Kostya Kortchinsky | b44364d | 2017-07-13 21:01:19 +0000 | [diff] [blame] | 251 | getBackendAllocator().deallocatePrimary(Cache_, Ptr); |
Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | AllocatorCache *Cache_; |
| 255 | }; |
| 256 | |
| 257 | typedef Quarantine<QuarantineCallback, ScudoChunk> ScudoQuarantine; |
Kostya Kortchinsky | 006805d | 2017-04-20 15:11:00 +0000 | [diff] [blame] | 258 | typedef ScudoQuarantine::Cache ScudoQuarantineCache; |
Kostya Kortchinsky | 36b3434 | 2017-04-27 20:21:16 +0000 | [diff] [blame] | 259 | COMPILER_CHECK(sizeof(ScudoQuarantineCache) <= |
| 260 | sizeof(ScudoThreadContext::QuarantineCachePlaceHolder)); |
Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 261 | |
Kostya Kortchinsky | 36b3434 | 2017-04-27 20:21:16 +0000 | [diff] [blame] | 262 | AllocatorCache *getAllocatorCache(ScudoThreadContext *ThreadContext) { |
| 263 | return &ThreadContext->Cache; |
Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 264 | } |
| 265 | |
Kostya Kortchinsky | 36b3434 | 2017-04-27 20:21:16 +0000 | [diff] [blame] | 266 | ScudoQuarantineCache *getQuarantineCache(ScudoThreadContext *ThreadContext) { |
| 267 | return reinterpret_cast< |
| 268 | ScudoQuarantineCache *>(ThreadContext->QuarantineCachePlaceHolder); |
| 269 | } |
| 270 | |
Kostya Kortchinsky | 0058256 | 2017-07-12 15:29:08 +0000 | [diff] [blame] | 271 | ScudoPrng *getPrng(ScudoThreadContext *ThreadContext) { |
Kostya Kortchinsky | 36b3434 | 2017-04-27 20:21:16 +0000 | [diff] [blame] | 272 | return &ThreadContext->Prng; |
Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 273 | } |
| 274 | |
Kostya Kortchinsky | 006805d | 2017-04-20 15:11:00 +0000 | [diff] [blame] | 275 | struct ScudoAllocator { |
Kostya Kortchinsky | 71dcc33 | 2016-10-26 16:16:58 +0000 | [diff] [blame] | 276 | static const uptr MaxAllowedMallocSize = |
| 277 | FIRST_32_SECOND_64(2UL << 30, 1ULL << 40); |
Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 278 | |
Alex Shlyapnikov | ccab11b | 2017-06-20 21:23:02 +0000 | [diff] [blame] | 279 | typedef ReturnNullOrDieOnFailure FailureHandler; |
| 280 | |
Kostya Kortchinsky | 006805d | 2017-04-20 15:11:00 +0000 | [diff] [blame] | 281 | ScudoBackendAllocator BackendAllocator; |
Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 282 | ScudoQuarantine AllocatorQuarantine; |
| 283 | |
Kostya Kortchinsky | b44364d | 2017-07-13 21:01:19 +0000 | [diff] [blame] | 284 | StaticSpinMutex GlobalPrngMutex; |
| 285 | ScudoPrng GlobalPrng; |
| 286 | |
Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 287 | // The fallback caches are used when the thread local caches have been |
| 288 | // 'detroyed' on thread tear-down. They are protected by a Mutex as they can |
| 289 | // be accessed by different threads. |
| 290 | StaticSpinMutex FallbackMutex; |
| 291 | AllocatorCache FallbackAllocatorCache; |
Kostya Kortchinsky | 006805d | 2017-04-20 15:11:00 +0000 | [diff] [blame] | 292 | ScudoQuarantineCache FallbackQuarantineCache; |
Kostya Kortchinsky | 0058256 | 2017-07-12 15:29:08 +0000 | [diff] [blame] | 293 | ScudoPrng FallbackPrng; |
Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 294 | |
| 295 | bool DeallocationTypeMismatch; |
| 296 | bool ZeroContents; |
| 297 | bool DeleteSizeMismatch; |
| 298 | |
Kostya Kortchinsky | 006805d | 2017-04-20 15:11:00 +0000 | [diff] [blame] | 299 | explicit ScudoAllocator(LinkerInitialized) |
Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 300 | : AllocatorQuarantine(LINKER_INITIALIZED), |
| 301 | FallbackQuarantineCache(LINKER_INITIALIZED) {} |
| 302 | |
| 303 | void init(const AllocatorOptions &Options) { |
Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 304 | // Verify that the header offset field can hold the maximum offset. In the |
Kostya Kortchinsky | 71dcc33 | 2016-10-26 16:16:58 +0000 | [diff] [blame] | 305 | // case of the Secondary allocator, it takes care of alignment and the |
| 306 | // offset will always be 0. In the case of the Primary, the worst case |
| 307 | // scenario happens in the last size class, when the backend allocation |
| 308 | // would already be aligned on the requested alignment, which would happen |
| 309 | // to be the maximum alignment that would fit in that size class. As a |
| 310 | // result, the maximum offset will be at most the maximum alignment for the |
| 311 | // last size class minus the header size, in multiples of MinAlignment. |
Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 312 | UnpackedHeader Header = {}; |
Kostya Kortchinsky | b44364d | 2017-07-13 21:01:19 +0000 | [diff] [blame] | 313 | uptr MaxPrimaryAlignment = |
| 314 | 1 << MostSignificantSetBitIndex(SizeClassMap::kMaxSize - MinAlignment); |
| 315 | uptr MaxOffset = |
| 316 | (MaxPrimaryAlignment - AlignedChunkHeaderSize) >> MinAlignmentLog; |
Kostya Kortchinsky | 1148dc5 | 2016-11-30 17:32:20 +0000 | [diff] [blame] | 317 | Header.Offset = MaxOffset; |
| 318 | if (Header.Offset != MaxOffset) { |
Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 319 | dieWithMessage("ERROR: the maximum possible offset doesn't fit in the " |
| 320 | "header\n"); |
| 321 | } |
Kostya Kortchinsky | fff8e06 | 2017-04-20 18:07:17 +0000 | [diff] [blame] | 322 | // Verify that we can fit the maximum size or amount of unused bytes in the |
| 323 | // header. Given that the Secondary fits the allocation to a page, the worst |
| 324 | // case scenario happens in the Primary. It will depend on the second to |
| 325 | // last and last class sizes, as well as the dynamic base for the Primary. |
| 326 | // The following is an over-approximation that works for our needs. |
| 327 | uptr MaxSizeOrUnusedBytes = SizeClassMap::kMaxSize - 1; |
| 328 | Header.SizeOrUnusedBytes = MaxSizeOrUnusedBytes; |
| 329 | if (Header.SizeOrUnusedBytes != MaxSizeOrUnusedBytes) { |
Kostya Kortchinsky | 1148dc5 | 2016-11-30 17:32:20 +0000 | [diff] [blame] | 330 | dieWithMessage("ERROR: the maximum possible unused bytes doesn't fit in " |
| 331 | "the header\n"); |
| 332 | } |
Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 333 | |
| 334 | DeallocationTypeMismatch = Options.DeallocationTypeMismatch; |
| 335 | DeleteSizeMismatch = Options.DeleteSizeMismatch; |
| 336 | ZeroContents = Options.ZeroContents; |
Alex Shlyapnikov | ccab11b | 2017-06-20 21:23:02 +0000 | [diff] [blame] | 337 | SetAllocatorMayReturnNull(Options.MayReturnNull); |
Kostya Kortchinsky | b44364d | 2017-07-13 21:01:19 +0000 | [diff] [blame] | 338 | BackendAllocator.init(Options.ReleaseToOSIntervalMs); |
Kostya Kortchinsky | 71dcc33 | 2016-10-26 16:16:58 +0000 | [diff] [blame] | 339 | AllocatorQuarantine.Init( |
| 340 | static_cast<uptr>(Options.QuarantineSizeMb) << 20, |
| 341 | static_cast<uptr>(Options.ThreadLocalQuarantineSizeKb) << 10); |
Kostya Kortchinsky | b44364d | 2017-07-13 21:01:19 +0000 | [diff] [blame] | 342 | GlobalPrng.init(); |
| 343 | Cookie = GlobalPrng.getU64(); |
| 344 | BackendAllocator.initCache(&FallbackAllocatorCache); |
Kostya Kortchinsky | 0058256 | 2017-07-12 15:29:08 +0000 | [diff] [blame] | 345 | FallbackPrng.init(); |
Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 346 | } |
| 347 | |
Kostya Kortchinsky | 006805d | 2017-04-20 15:11:00 +0000 | [diff] [blame] | 348 | // Helper function that checks for a valid Scudo chunk. nullptr isn't. |
Kostya Kortchinsky | 1148dc5 | 2016-11-30 17:32:20 +0000 | [diff] [blame] | 349 | bool isValidPointer(const void *UserPtr) { |
Kostya Kortchinsky | 36b3434 | 2017-04-27 20:21:16 +0000 | [diff] [blame] | 350 | initThreadMaybe(); |
Kostya Kortchinsky | 0ce4999 | 2017-06-29 16:45:20 +0000 | [diff] [blame] | 351 | if (UNLIKELY(!UserPtr)) |
Kostya Kortchinsky | 1148dc5 | 2016-11-30 17:32:20 +0000 | [diff] [blame] | 352 | return false; |
Kostya Kortchinsky | 006805d | 2017-04-20 15:11:00 +0000 | [diff] [blame] | 353 | uptr UserBeg = reinterpret_cast<uptr>(UserPtr); |
| 354 | if (!IsAligned(UserBeg, MinAlignment)) |
| 355 | return false; |
| 356 | return getScudoChunk(UserBeg)->isValid(); |
Kostya Kortchinsky | 1148dc5 | 2016-11-30 17:32:20 +0000 | [diff] [blame] | 357 | } |
| 358 | |
Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 359 | // Allocates a chunk. |
Kostya Kortchinsky | 006805d | 2017-04-20 15:11:00 +0000 | [diff] [blame] | 360 | void *allocate(uptr Size, uptr Alignment, AllocType Type, |
| 361 | bool ForceZeroContents = false) { |
Kostya Kortchinsky | 36b3434 | 2017-04-27 20:21:16 +0000 | [diff] [blame] | 362 | initThreadMaybe(); |
Kostya Kortchinsky | 0ce4999 | 2017-06-29 16:45:20 +0000 | [diff] [blame] | 363 | if (UNLIKELY(Alignment > MaxAlignment)) |
Alex Shlyapnikov | ccab11b | 2017-06-20 21:23:02 +0000 | [diff] [blame] | 364 | return FailureHandler::OnBadRequest(); |
Kostya Kortchinsky | 0ce4999 | 2017-06-29 16:45:20 +0000 | [diff] [blame] | 365 | if (UNLIKELY(Alignment < MinAlignment)) |
Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 366 | Alignment = MinAlignment; |
Kostya Kortchinsky | 0ce4999 | 2017-06-29 16:45:20 +0000 | [diff] [blame] | 367 | if (UNLIKELY(Size >= MaxAllowedMallocSize)) |
Alex Shlyapnikov | ccab11b | 2017-06-20 21:23:02 +0000 | [diff] [blame] | 368 | return FailureHandler::OnBadRequest(); |
Kostya Kortchinsky | 0ce4999 | 2017-06-29 16:45:20 +0000 | [diff] [blame] | 369 | if (UNLIKELY(Size == 0)) |
Kostya Kortchinsky | 006805d | 2017-04-20 15:11:00 +0000 | [diff] [blame] | 370 | Size = 1; |
Kostya Kortchinsky | c74da7c | 2016-12-13 19:31:54 +0000 | [diff] [blame] | 371 | |
| 372 | uptr NeededSize = RoundUpTo(Size, MinAlignment) + AlignedChunkHeaderSize; |
Kostya Kortchinsky | 01a66fc | 2017-05-11 21:40:45 +0000 | [diff] [blame] | 373 | uptr AlignedSize = (Alignment > MinAlignment) ? |
| 374 | NeededSize + (Alignment - AlignedChunkHeaderSize) : NeededSize; |
Kostya Kortchinsky | 0ce4999 | 2017-06-29 16:45:20 +0000 | [diff] [blame] | 375 | if (UNLIKELY(AlignedSize >= MaxAllowedMallocSize)) |
Alex Shlyapnikov | ccab11b | 2017-06-20 21:23:02 +0000 | [diff] [blame] | 376 | return FailureHandler::OnBadRequest(); |
Kostya Kortchinsky | c74da7c | 2016-12-13 19:31:54 +0000 | [diff] [blame] | 377 | |
Kostya Kortchinsky | 01a66fc | 2017-05-11 21:40:45 +0000 | [diff] [blame] | 378 | // Primary and Secondary backed allocations have a different treatment. We |
| 379 | // deal with alignment requirements of Primary serviced allocations here, |
| 380 | // but the Secondary will take care of its own alignment needs. |
| 381 | bool FromPrimary = PrimaryAllocator::CanAllocate(AlignedSize, MinAlignment); |
Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 382 | |
| 383 | void *Ptr; |
Kostya Kortchinsky | 0058256 | 2017-07-12 15:29:08 +0000 | [diff] [blame] | 384 | u8 Salt; |
Kostya Kortchinsky | b44364d | 2017-07-13 21:01:19 +0000 | [diff] [blame] | 385 | uptr AllocSize; |
| 386 | if (FromPrimary) { |
| 387 | AllocSize = AlignedSize; |
| 388 | ScudoThreadContext *ThreadContext = getThreadContextAndLock(); |
| 389 | if (LIKELY(ThreadContext)) { |
| 390 | Salt = getPrng(ThreadContext)->getU8(); |
| 391 | Ptr = BackendAllocator.allocatePrimary(getAllocatorCache(ThreadContext), |
| 392 | AllocSize); |
| 393 | ThreadContext->unlock(); |
| 394 | } else { |
| 395 | SpinMutexLock l(&FallbackMutex); |
| 396 | Salt = FallbackPrng.getU8(); |
| 397 | Ptr = BackendAllocator.allocatePrimary(&FallbackAllocatorCache, |
| 398 | AllocSize); |
| 399 | } |
Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 400 | } else { |
Kostya Kortchinsky | b44364d | 2017-07-13 21:01:19 +0000 | [diff] [blame] | 401 | { |
| 402 | SpinMutexLock l(&GlobalPrngMutex); |
| 403 | Salt = GlobalPrng.getU8(); |
| 404 | } |
| 405 | AllocSize = NeededSize; |
| 406 | Ptr = BackendAllocator.allocateSecondary(AllocSize, Alignment); |
Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 407 | } |
Kostya Kortchinsky | 0ce4999 | 2017-06-29 16:45:20 +0000 | [diff] [blame] | 408 | if (UNLIKELY(!Ptr)) |
Alex Shlyapnikov | ccab11b | 2017-06-20 21:23:02 +0000 | [diff] [blame] | 409 | return FailureHandler::OnOOM(); |
Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 410 | |
Kostya Kortchinsky | 1148dc5 | 2016-11-30 17:32:20 +0000 | [diff] [blame] | 411 | // If requested, we will zero out the entire contents of the returned chunk. |
Kostya Kortchinsky | 006805d | 2017-04-20 15:11:00 +0000 | [diff] [blame] | 412 | if ((ForceZeroContents || ZeroContents) && FromPrimary) |
Kostya Kortchinsky | b44364d | 2017-07-13 21:01:19 +0000 | [diff] [blame] | 413 | memset(Ptr, 0, BackendAllocator.getActuallyAllocatedSize( |
| 414 | Ptr, /*FromPrimary=*/true)); |
Kostya Kortchinsky | 1148dc5 | 2016-11-30 17:32:20 +0000 | [diff] [blame] | 415 | |
Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 416 | UnpackedHeader Header = {}; |
Kostya Kortchinsky | 01a66fc | 2017-05-11 21:40:45 +0000 | [diff] [blame] | 417 | uptr AllocBeg = reinterpret_cast<uptr>(Ptr); |
| 418 | uptr UserBeg = AllocBeg + AlignedChunkHeaderSize; |
Kostya Kortchinsky | 0ce4999 | 2017-06-29 16:45:20 +0000 | [diff] [blame] | 419 | if (UNLIKELY(!IsAligned(UserBeg, Alignment))) { |
Kostya Kortchinsky | 01a66fc | 2017-05-11 21:40:45 +0000 | [diff] [blame] | 420 | // Since the Secondary takes care of alignment, a non-aligned pointer |
| 421 | // means it is from the Primary. It is also the only case where the offset |
| 422 | // field of the header would be non-zero. |
| 423 | CHECK(FromPrimary); |
| 424 | UserBeg = RoundUpTo(UserBeg, Alignment); |
| 425 | uptr Offset = UserBeg - AlignedChunkHeaderSize - AllocBeg; |
| 426 | Header.Offset = Offset >> MinAlignmentLog; |
| 427 | } |
Kostya Kortchinsky | b44364d | 2017-07-13 21:01:19 +0000 | [diff] [blame] | 428 | CHECK_LE(UserBeg + Size, AllocBeg + AllocSize); |
Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 429 | Header.State = ChunkAllocated; |
Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 430 | Header.AllocType = Type; |
Kostya Kortchinsky | fff8e06 | 2017-04-20 18:07:17 +0000 | [diff] [blame] | 431 | if (FromPrimary) { |
Kostya Kortchinsky | b44364d | 2017-07-13 21:01:19 +0000 | [diff] [blame] | 432 | Header.FromPrimary = 1; |
Kostya Kortchinsky | fff8e06 | 2017-04-20 18:07:17 +0000 | [diff] [blame] | 433 | Header.SizeOrUnusedBytes = Size; |
| 434 | } else { |
| 435 | // The secondary fits the allocations to a page, so the amount of unused |
| 436 | // bytes is the difference between the end of the user allocation and the |
| 437 | // next page boundary. |
| 438 | uptr PageSize = GetPageSizeCached(); |
| 439 | uptr TrailingBytes = (UserBeg + Size) & (PageSize - 1); |
| 440 | if (TrailingBytes) |
| 441 | Header.SizeOrUnusedBytes = PageSize - TrailingBytes; |
| 442 | } |
Kostya Kortchinsky | b44364d | 2017-07-13 21:01:19 +0000 | [diff] [blame] | 443 | Header.Salt = Salt; |
Kostya Kortchinsky | 006805d | 2017-04-20 15:11:00 +0000 | [diff] [blame] | 444 | getScudoChunk(UserBeg)->storeHeader(&Header); |
| 445 | void *UserPtr = reinterpret_cast<void *>(UserBeg); |
Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 446 | // if (&__sanitizer_malloc_hook) __sanitizer_malloc_hook(UserPtr, Size); |
| 447 | return UserPtr; |
| 448 | } |
| 449 | |
Kostya Kortchinsky | f1a54fd | 2017-04-21 18:10:53 +0000 | [diff] [blame] | 450 | // Place a chunk in the quarantine. In the event of a zero-sized quarantine, |
| 451 | // we directly deallocate the chunk, otherwise the flow would lead to the |
Kostya Kortchinsky | 36b3434 | 2017-04-27 20:21:16 +0000 | [diff] [blame] | 452 | // chunk being loaded (and checked) twice, and stored (and checksummed) once, |
| 453 | // with no additional security value. |
Kostya Kortchinsky | f1a54fd | 2017-04-21 18:10:53 +0000 | [diff] [blame] | 454 | void quarantineOrDeallocateChunk(ScudoChunk *Chunk, UnpackedHeader *Header, |
| 455 | uptr Size) { |
Kostya Kortchinsky | 01a66fc | 2017-05-11 21:40:45 +0000 | [diff] [blame] | 456 | bool FromPrimary = Header->FromPrimary; |
Kostya Kortchinsky | f1a54fd | 2017-04-21 18:10:53 +0000 | [diff] [blame] | 457 | bool BypassQuarantine = (AllocatorQuarantine.GetCacheSize() == 0); |
| 458 | if (BypassQuarantine) { |
| 459 | Chunk->eraseHeader(); |
| 460 | void *Ptr = Chunk->getAllocBeg(Header); |
Kostya Kortchinsky | b44364d | 2017-07-13 21:01:19 +0000 | [diff] [blame] | 461 | if (FromPrimary) { |
| 462 | ScudoThreadContext *ThreadContext = getThreadContextAndLock(); |
| 463 | if (LIKELY(ThreadContext)) { |
| 464 | getBackendAllocator().deallocatePrimary( |
| 465 | getAllocatorCache(ThreadContext), Ptr); |
| 466 | ThreadContext->unlock(); |
| 467 | } else { |
| 468 | SpinMutexLock Lock(&FallbackMutex); |
| 469 | getBackendAllocator().deallocatePrimary(&FallbackAllocatorCache, Ptr); |
| 470 | } |
Kostya Kortchinsky | f1a54fd | 2017-04-21 18:10:53 +0000 | [diff] [blame] | 471 | } else { |
Kostya Kortchinsky | b44364d | 2017-07-13 21:01:19 +0000 | [diff] [blame] | 472 | getBackendAllocator().deallocateSecondary(Ptr); |
Kostya Kortchinsky | f1a54fd | 2017-04-21 18:10:53 +0000 | [diff] [blame] | 473 | } |
| 474 | } else { |
| 475 | UnpackedHeader NewHeader = *Header; |
| 476 | NewHeader.State = ChunkQuarantine; |
| 477 | Chunk->compareExchangeHeader(&NewHeader, Header); |
Kostya Kortchinsky | ee069576 | 2017-05-05 21:38:22 +0000 | [diff] [blame] | 478 | ScudoThreadContext *ThreadContext = getThreadContextAndLock(); |
Kostya Kortchinsky | 36b3434 | 2017-04-27 20:21:16 +0000 | [diff] [blame] | 479 | if (LIKELY(ThreadContext)) { |
| 480 | AllocatorQuarantine.Put(getQuarantineCache(ThreadContext), |
| 481 | QuarantineCallback( |
| 482 | getAllocatorCache(ThreadContext)), |
| 483 | Chunk, Size); |
Kostya Kortchinsky | ee069576 | 2017-05-05 21:38:22 +0000 | [diff] [blame] | 484 | ThreadContext->unlock(); |
Kostya Kortchinsky | f1a54fd | 2017-04-21 18:10:53 +0000 | [diff] [blame] | 485 | } else { |
| 486 | SpinMutexLock l(&FallbackMutex); |
| 487 | AllocatorQuarantine.Put(&FallbackQuarantineCache, |
| 488 | QuarantineCallback(&FallbackAllocatorCache), |
| 489 | Chunk, Size); |
| 490 | } |
| 491 | } |
| 492 | } |
| 493 | |
Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 494 | // Deallocates a Chunk, which means adding it to the delayed free list (or |
| 495 | // Quarantine). |
| 496 | void deallocate(void *UserPtr, uptr DeleteSize, AllocType Type) { |
Kostya Kortchinsky | 36b3434 | 2017-04-27 20:21:16 +0000 | [diff] [blame] | 497 | initThreadMaybe(); |
Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 498 | // if (&__sanitizer_free_hook) __sanitizer_free_hook(UserPtr); |
Kostya Kortchinsky | 0ce4999 | 2017-06-29 16:45:20 +0000 | [diff] [blame] | 499 | if (UNLIKELY(!UserPtr)) |
Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 500 | return; |
Kostya Kortchinsky | 006805d | 2017-04-20 15:11:00 +0000 | [diff] [blame] | 501 | uptr UserBeg = reinterpret_cast<uptr>(UserPtr); |
| 502 | if (UNLIKELY(!IsAligned(UserBeg, MinAlignment))) { |
Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 503 | dieWithMessage("ERROR: attempted to deallocate a chunk not properly " |
| 504 | "aligned at address %p\n", UserPtr); |
| 505 | } |
Kostya Kortchinsky | 006805d | 2017-04-20 15:11:00 +0000 | [diff] [blame] | 506 | ScudoChunk *Chunk = getScudoChunk(UserBeg); |
Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 507 | UnpackedHeader OldHeader; |
| 508 | Chunk->loadHeader(&OldHeader); |
Kostya Kortchinsky | 006805d | 2017-04-20 15:11:00 +0000 | [diff] [blame] | 509 | if (UNLIKELY(OldHeader.State != ChunkAllocated)) { |
Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 510 | dieWithMessage("ERROR: invalid chunk state when deallocating address " |
Kostya Kortchinsky | 1148dc5 | 2016-11-30 17:32:20 +0000 | [diff] [blame] | 511 | "%p\n", UserPtr); |
Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 512 | } |
Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 513 | if (DeallocationTypeMismatch) { |
| 514 | // The deallocation type has to match the allocation one. |
Kostya Kortchinsky | 006805d | 2017-04-20 15:11:00 +0000 | [diff] [blame] | 515 | if (OldHeader.AllocType != Type) { |
Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 516 | // With the exception of memalign'd Chunks, that can be still be free'd. |
Kostya Kortchinsky | 006805d | 2017-04-20 15:11:00 +0000 | [diff] [blame] | 517 | if (OldHeader.AllocType != FromMemalign || Type != FromMalloc) { |
Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 518 | dieWithMessage("ERROR: allocation type mismatch on address %p\n", |
Kostya Kortchinsky | 006805d | 2017-04-20 15:11:00 +0000 | [diff] [blame] | 519 | UserPtr); |
Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 520 | } |
| 521 | } |
| 522 | } |
Kostya Kortchinsky | fff8e06 | 2017-04-20 18:07:17 +0000 | [diff] [blame] | 523 | uptr Size = OldHeader.FromPrimary ? OldHeader.SizeOrUnusedBytes : |
| 524 | Chunk->getUsableSize(&OldHeader) - OldHeader.SizeOrUnusedBytes; |
Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 525 | if (DeleteSizeMismatch) { |
| 526 | if (DeleteSize && DeleteSize != Size) { |
| 527 | dieWithMessage("ERROR: invalid sized delete on chunk at address %p\n", |
Kostya Kortchinsky | 006805d | 2017-04-20 15:11:00 +0000 | [diff] [blame] | 528 | UserPtr); |
Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 529 | } |
| 530 | } |
Kostya Kortchinsky | 1148dc5 | 2016-11-30 17:32:20 +0000 | [diff] [blame] | 531 | |
Kostya Kortchinsky | fff8e06 | 2017-04-20 18:07:17 +0000 | [diff] [blame] | 532 | // If a small memory amount was allocated with a larger alignment, we want |
| 533 | // to take that into account. Otherwise the Quarantine would be filled with |
Kostya Kortchinsky | f1a54fd | 2017-04-21 18:10:53 +0000 | [diff] [blame] | 534 | // tiny chunks, taking a lot of VA memory. This is an approximation of the |
Kostya Kortchinsky | fff8e06 | 2017-04-20 18:07:17 +0000 | [diff] [blame] | 535 | // usable size, that allows us to not call GetActuallyAllocatedSize. |
| 536 | uptr LiableSize = Size + (OldHeader.Offset << MinAlignment); |
Kostya Kortchinsky | f1a54fd | 2017-04-21 18:10:53 +0000 | [diff] [blame] | 537 | quarantineOrDeallocateChunk(Chunk, &OldHeader, LiableSize); |
Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 538 | } |
| 539 | |
Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 540 | // Reallocates a chunk. We can save on a new allocation if the new requested |
| 541 | // size still fits in the chunk. |
| 542 | void *reallocate(void *OldPtr, uptr NewSize) { |
Kostya Kortchinsky | 36b3434 | 2017-04-27 20:21:16 +0000 | [diff] [blame] | 543 | initThreadMaybe(); |
Kostya Kortchinsky | 006805d | 2017-04-20 15:11:00 +0000 | [diff] [blame] | 544 | uptr UserBeg = reinterpret_cast<uptr>(OldPtr); |
| 545 | if (UNLIKELY(!IsAligned(UserBeg, MinAlignment))) { |
| 546 | dieWithMessage("ERROR: attempted to reallocate a chunk not properly " |
| 547 | "aligned at address %p\n", OldPtr); |
| 548 | } |
| 549 | ScudoChunk *Chunk = getScudoChunk(UserBeg); |
Kostya Kortchinsky | 1148dc5 | 2016-11-30 17:32:20 +0000 | [diff] [blame] | 550 | UnpackedHeader OldHeader; |
| 551 | Chunk->loadHeader(&OldHeader); |
Kostya Kortchinsky | 006805d | 2017-04-20 15:11:00 +0000 | [diff] [blame] | 552 | if (UNLIKELY(OldHeader.State != ChunkAllocated)) { |
Kostya Kortchinsky | 1148dc5 | 2016-11-30 17:32:20 +0000 | [diff] [blame] | 553 | dieWithMessage("ERROR: invalid chunk state when reallocating address " |
| 554 | "%p\n", OldPtr); |
| 555 | } |
Kostya Kortchinsky | 006805d | 2017-04-20 15:11:00 +0000 | [diff] [blame] | 556 | if (UNLIKELY(OldHeader.AllocType != FromMalloc)) { |
Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 557 | dieWithMessage("ERROR: invalid chunk type when reallocating address %p\n", |
Kostya Kortchinsky | 006805d | 2017-04-20 15:11:00 +0000 | [diff] [blame] | 558 | OldPtr); |
Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 559 | } |
Kostya Kortchinsky | 006805d | 2017-04-20 15:11:00 +0000 | [diff] [blame] | 560 | uptr UsableSize = Chunk->getUsableSize(&OldHeader); |
Kostya Kortchinsky | fff8e06 | 2017-04-20 18:07:17 +0000 | [diff] [blame] | 561 | // The new size still fits in the current chunk, and the size difference |
| 562 | // is reasonable. |
| 563 | if (NewSize <= UsableSize && |
| 564 | (UsableSize - NewSize) < (SizeClassMap::kMaxSize / 2)) { |
Kostya Kortchinsky | f1a54fd | 2017-04-21 18:10:53 +0000 | [diff] [blame] | 565 | UnpackedHeader NewHeader = OldHeader; |
Kostya Kortchinsky | fff8e06 | 2017-04-20 18:07:17 +0000 | [diff] [blame] | 566 | NewHeader.SizeOrUnusedBytes = |
| 567 | OldHeader.FromPrimary ? NewSize : UsableSize - NewSize; |
Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 568 | Chunk->compareExchangeHeader(&NewHeader, &OldHeader); |
| 569 | return OldPtr; |
| 570 | } |
| 571 | // Otherwise, we have to allocate a new chunk and copy the contents of the |
| 572 | // old one. |
| 573 | void *NewPtr = allocate(NewSize, MinAlignment, FromMalloc); |
| 574 | if (NewPtr) { |
Kostya Kortchinsky | fff8e06 | 2017-04-20 18:07:17 +0000 | [diff] [blame] | 575 | uptr OldSize = OldHeader.FromPrimary ? OldHeader.SizeOrUnusedBytes : |
| 576 | UsableSize - OldHeader.SizeOrUnusedBytes; |
Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 577 | memcpy(NewPtr, OldPtr, Min(NewSize, OldSize)); |
Kostya Kortchinsky | f1a54fd | 2017-04-21 18:10:53 +0000 | [diff] [blame] | 578 | quarantineOrDeallocateChunk(Chunk, &OldHeader, UsableSize); |
Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 579 | } |
| 580 | return NewPtr; |
| 581 | } |
| 582 | |
Kostya Kortchinsky | 1148dc5 | 2016-11-30 17:32:20 +0000 | [diff] [blame] | 583 | // Helper function that returns the actual usable size of a chunk. |
| 584 | uptr getUsableSize(const void *Ptr) { |
Kostya Kortchinsky | 36b3434 | 2017-04-27 20:21:16 +0000 | [diff] [blame] | 585 | initThreadMaybe(); |
Kostya Kortchinsky | 0ce4999 | 2017-06-29 16:45:20 +0000 | [diff] [blame] | 586 | if (UNLIKELY(!Ptr)) |
Kostya Kortchinsky | 1148dc5 | 2016-11-30 17:32:20 +0000 | [diff] [blame] | 587 | return 0; |
Kostya Kortchinsky | 006805d | 2017-04-20 15:11:00 +0000 | [diff] [blame] | 588 | uptr UserBeg = reinterpret_cast<uptr>(Ptr); |
| 589 | ScudoChunk *Chunk = getScudoChunk(UserBeg); |
Kostya Kortchinsky | 1148dc5 | 2016-11-30 17:32:20 +0000 | [diff] [blame] | 590 | UnpackedHeader Header; |
| 591 | Chunk->loadHeader(&Header); |
| 592 | // Getting the usable size of a chunk only makes sense if it's allocated. |
Kostya Kortchinsky | 006805d | 2017-04-20 15:11:00 +0000 | [diff] [blame] | 593 | if (UNLIKELY(Header.State != ChunkAllocated)) { |
Kostya Kortchinsky | 1148dc5 | 2016-11-30 17:32:20 +0000 | [diff] [blame] | 594 | dieWithMessage("ERROR: invalid chunk state when sizing address %p\n", |
| 595 | Ptr); |
| 596 | } |
| 597 | return Chunk->getUsableSize(&Header); |
| 598 | } |
| 599 | |
Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 600 | void *calloc(uptr NMemB, uptr Size) { |
Kostya Kortchinsky | 36b3434 | 2017-04-27 20:21:16 +0000 | [diff] [blame] | 601 | initThreadMaybe(); |
Kostya Kortchinsky | b44364d | 2017-07-13 21:01:19 +0000 | [diff] [blame] | 602 | if (UNLIKELY(CheckForCallocOverflow(NMemB, Size))) |
Alex Shlyapnikov | ccab11b | 2017-06-20 21:23:02 +0000 | [diff] [blame] | 603 | return FailureHandler::OnBadRequest(); |
Kostya Kortchinsky | 0ce4999 | 2017-06-29 16:45:20 +0000 | [diff] [blame] | 604 | return allocate(NMemB * Size, MinAlignment, FromMalloc, true); |
Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 605 | } |
| 606 | |
Kostya Kortchinsky | 36b3434 | 2017-04-27 20:21:16 +0000 | [diff] [blame] | 607 | void commitBack(ScudoThreadContext *ThreadContext) { |
| 608 | AllocatorCache *Cache = getAllocatorCache(ThreadContext); |
| 609 | AllocatorQuarantine.Drain(getQuarantineCache(ThreadContext), |
| 610 | QuarantineCallback(Cache)); |
Kostya Kortchinsky | b44364d | 2017-07-13 21:01:19 +0000 | [diff] [blame] | 611 | BackendAllocator.destroyCache(Cache); |
Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 612 | } |
Kostya Kortchinsky | 8d6257b | 2017-02-03 20:49:42 +0000 | [diff] [blame] | 613 | |
| 614 | uptr getStats(AllocatorStat StatType) { |
Kostya Kortchinsky | 36b3434 | 2017-04-27 20:21:16 +0000 | [diff] [blame] | 615 | initThreadMaybe(); |
Kostya Kortchinsky | 8d6257b | 2017-02-03 20:49:42 +0000 | [diff] [blame] | 616 | uptr stats[AllocatorStatCount]; |
Kostya Kortchinsky | b44364d | 2017-07-13 21:01:19 +0000 | [diff] [blame] | 617 | BackendAllocator.getStats(stats); |
Kostya Kortchinsky | 8d6257b | 2017-02-03 20:49:42 +0000 | [diff] [blame] | 618 | return stats[StatType]; |
| 619 | } |
Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 620 | }; |
| 621 | |
Kostya Kortchinsky | 006805d | 2017-04-20 15:11:00 +0000 | [diff] [blame] | 622 | static ScudoAllocator Instance(LINKER_INITIALIZED); |
Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 623 | |
Kostya Kortchinsky | 006805d | 2017-04-20 15:11:00 +0000 | [diff] [blame] | 624 | static ScudoBackendAllocator &getBackendAllocator() { |
Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 625 | return Instance.BackendAllocator; |
| 626 | } |
| 627 | |
Kostya Kortchinsky | 36b3434 | 2017-04-27 20:21:16 +0000 | [diff] [blame] | 628 | static void initScudoInternal(const AllocatorOptions &Options) { |
Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 629 | Instance.init(Options); |
| 630 | } |
| 631 | |
Kostya Kortchinsky | 36b3434 | 2017-04-27 20:21:16 +0000 | [diff] [blame] | 632 | void ScudoThreadContext::init() { |
Kostya Kortchinsky | b44364d | 2017-07-13 21:01:19 +0000 | [diff] [blame] | 633 | getBackendAllocator().initCache(&Cache); |
Kostya Kortchinsky | 0058256 | 2017-07-12 15:29:08 +0000 | [diff] [blame] | 634 | Prng.init(); |
Kostya Kortchinsky | 36b3434 | 2017-04-27 20:21:16 +0000 | [diff] [blame] | 635 | memset(QuarantineCachePlaceHolder, 0, sizeof(QuarantineCachePlaceHolder)); |
| 636 | } |
| 637 | |
| 638 | void ScudoThreadContext::commitBack() { |
| 639 | Instance.commitBack(this); |
Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 640 | } |
| 641 | |
| 642 | void *scudoMalloc(uptr Size, AllocType Type) { |
Alex Shlyapnikov | 42bea01 | 2017-07-18 19:11:04 +0000 | [diff] [blame^] | 643 | return SetErrnoOnNull(Instance.allocate(Size, MinAlignment, Type)); |
Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 644 | } |
| 645 | |
| 646 | void scudoFree(void *Ptr, AllocType Type) { |
| 647 | Instance.deallocate(Ptr, 0, Type); |
| 648 | } |
| 649 | |
| 650 | void scudoSizedFree(void *Ptr, uptr Size, AllocType Type) { |
| 651 | Instance.deallocate(Ptr, Size, Type); |
| 652 | } |
| 653 | |
| 654 | void *scudoRealloc(void *Ptr, uptr Size) { |
| 655 | if (!Ptr) |
Alex Shlyapnikov | 42bea01 | 2017-07-18 19:11:04 +0000 | [diff] [blame^] | 656 | return SetErrnoOnNull(Instance.allocate(Size, MinAlignment, FromMalloc)); |
Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 657 | if (Size == 0) { |
| 658 | Instance.deallocate(Ptr, 0, FromMalloc); |
| 659 | return nullptr; |
| 660 | } |
Alex Shlyapnikov | 42bea01 | 2017-07-18 19:11:04 +0000 | [diff] [blame^] | 661 | return SetErrnoOnNull(Instance.reallocate(Ptr, Size)); |
Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 662 | } |
| 663 | |
| 664 | void *scudoCalloc(uptr NMemB, uptr Size) { |
Alex Shlyapnikov | 42bea01 | 2017-07-18 19:11:04 +0000 | [diff] [blame^] | 665 | return SetErrnoOnNull(Instance.calloc(NMemB, Size)); |
Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 666 | } |
| 667 | |
| 668 | void *scudoValloc(uptr Size) { |
Alex Shlyapnikov | 42bea01 | 2017-07-18 19:11:04 +0000 | [diff] [blame^] | 669 | return SetErrnoOnNull( |
| 670 | Instance.allocate(Size, GetPageSizeCached(), FromMemalign)); |
Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 671 | } |
| 672 | |
Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 673 | void *scudoPvalloc(uptr Size) { |
| 674 | uptr PageSize = GetPageSizeCached(); |
Alex Shlyapnikov | df18cbb | 2017-07-14 21:17:16 +0000 | [diff] [blame] | 675 | // pvalloc(0) should allocate one page. |
| 676 | Size = Size ? RoundUpTo(Size, PageSize) : PageSize; |
Alex Shlyapnikov | 42bea01 | 2017-07-18 19:11:04 +0000 | [diff] [blame^] | 677 | return SetErrnoOnNull(Instance.allocate(Size, PageSize, FromMemalign)); |
Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 678 | } |
| 679 | |
Kostya Kortchinsky | 0ce4999 | 2017-06-29 16:45:20 +0000 | [diff] [blame] | 680 | void *scudoMemalign(uptr Alignment, uptr Size) { |
Alex Shlyapnikov | df18cbb | 2017-07-14 21:17:16 +0000 | [diff] [blame] | 681 | if (UNLIKELY(!IsPowerOfTwo(Alignment))) { |
| 682 | errno = errno_EINVAL; |
Kostya Kortchinsky | 0ce4999 | 2017-06-29 16:45:20 +0000 | [diff] [blame] | 683 | return ScudoAllocator::FailureHandler::OnBadRequest(); |
Alex Shlyapnikov | df18cbb | 2017-07-14 21:17:16 +0000 | [diff] [blame] | 684 | } |
Alex Shlyapnikov | 42bea01 | 2017-07-18 19:11:04 +0000 | [diff] [blame^] | 685 | return SetErrnoOnNull(Instance.allocate(Size, Alignment, FromMemalign)); |
Kostya Kortchinsky | 0ce4999 | 2017-06-29 16:45:20 +0000 | [diff] [blame] | 686 | } |
| 687 | |
Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 688 | int scudoPosixMemalign(void **MemPtr, uptr Alignment, uptr Size) { |
Alex Shlyapnikov | 42bea01 | 2017-07-18 19:11:04 +0000 | [diff] [blame^] | 689 | if (UNLIKELY(!CheckPosixMemalignAlignment(Alignment))) { |
Alex Shlyapnikov | df18cbb | 2017-07-14 21:17:16 +0000 | [diff] [blame] | 690 | ScudoAllocator::FailureHandler::OnBadRequest(); |
| 691 | return errno_EINVAL; |
Kostya Kortchinsky | 0ce4999 | 2017-06-29 16:45:20 +0000 | [diff] [blame] | 692 | } |
Alex Shlyapnikov | df18cbb | 2017-07-14 21:17:16 +0000 | [diff] [blame] | 693 | void *Ptr = Instance.allocate(Size, Alignment, FromMemalign); |
Alex Shlyapnikov | 42bea01 | 2017-07-18 19:11:04 +0000 | [diff] [blame^] | 694 | if (UNLIKELY(!Ptr)) |
Alex Shlyapnikov | df18cbb | 2017-07-14 21:17:16 +0000 | [diff] [blame] | 695 | return errno_ENOMEM; |
| 696 | *MemPtr = Ptr; |
Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 697 | return 0; |
| 698 | } |
| 699 | |
| 700 | void *scudoAlignedAlloc(uptr Alignment, uptr Size) { |
Alex Shlyapnikov | 42bea01 | 2017-07-18 19:11:04 +0000 | [diff] [blame^] | 701 | if (UNLIKELY(!CheckAlignedAllocAlignmentAndSize(Alignment, Size))) { |
Alex Shlyapnikov | df18cbb | 2017-07-14 21:17:16 +0000 | [diff] [blame] | 702 | errno = errno_EINVAL; |
Kostya Kortchinsky | 0ce4999 | 2017-06-29 16:45:20 +0000 | [diff] [blame] | 703 | return ScudoAllocator::FailureHandler::OnBadRequest(); |
Alex Shlyapnikov | df18cbb | 2017-07-14 21:17:16 +0000 | [diff] [blame] | 704 | } |
Alex Shlyapnikov | 42bea01 | 2017-07-18 19:11:04 +0000 | [diff] [blame^] | 705 | return SetErrnoOnNull(Instance.allocate(Size, Alignment, FromMalloc)); |
Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 706 | } |
| 707 | |
| 708 | uptr scudoMallocUsableSize(void *Ptr) { |
| 709 | return Instance.getUsableSize(Ptr); |
| 710 | } |
| 711 | |
Kostya Kortchinsky | 1148dc5 | 2016-11-30 17:32:20 +0000 | [diff] [blame] | 712 | } // namespace __scudo |
Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 713 | |
| 714 | using namespace __scudo; |
| 715 | |
| 716 | // MallocExtension helper functions |
| 717 | |
| 718 | uptr __sanitizer_get_current_allocated_bytes() { |
Kostya Kortchinsky | 8d6257b | 2017-02-03 20:49:42 +0000 | [diff] [blame] | 719 | return Instance.getStats(AllocatorStatAllocated); |
Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 720 | } |
| 721 | |
| 722 | uptr __sanitizer_get_heap_size() { |
Kostya Kortchinsky | 8d6257b | 2017-02-03 20:49:42 +0000 | [diff] [blame] | 723 | return Instance.getStats(AllocatorStatMapped); |
Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 724 | } |
| 725 | |
| 726 | uptr __sanitizer_get_free_bytes() { |
| 727 | return 1; |
| 728 | } |
| 729 | |
| 730 | uptr __sanitizer_get_unmapped_bytes() { |
| 731 | return 1; |
| 732 | } |
| 733 | |
| 734 | uptr __sanitizer_get_estimated_allocated_size(uptr size) { |
| 735 | return size; |
| 736 | } |
| 737 | |
Kostya Kortchinsky | 1148dc5 | 2016-11-30 17:32:20 +0000 | [diff] [blame] | 738 | int __sanitizer_get_ownership(const void *Ptr) { |
| 739 | return Instance.isValidPointer(Ptr); |
Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 740 | } |
| 741 | |
Kostya Kortchinsky | 1148dc5 | 2016-11-30 17:32:20 +0000 | [diff] [blame] | 742 | uptr __sanitizer_get_allocated_size(const void *Ptr) { |
| 743 | return Instance.getUsableSize(Ptr); |
Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 744 | } |