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