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" |
| 23 | #include "sanitizer_common/sanitizer_quarantine.h" |
| 24 | |
Kostya Kortchinsky | 0ce4999 | 2017-06-29 16:45:20 +0000 | [diff] [blame] | 25 | #include <errno.h> |
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 = |
| 76 | getBackendAllocator().GetActuallyAllocatedSize(getAllocBeg(Header), |
| 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 | 01a66fc | 2017-05-11 21:40:45 +0000 | [diff] [blame] | 235 | getBackendAllocator().Deallocate(Cache_, Ptr, Header.FromPrimary); |
Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 236 | } |
| 237 | |
Kostya Kortchinsky | 01a66fc | 2017-05-11 21:40:45 +0000 | [diff] [blame] | 238 | // Internal quarantine allocation and deallocation functions. We first check |
| 239 | // that the batches are indeed serviced by the Primary. |
| 240 | // TODO(kostyak): figure out the best way to protect the batches. |
| 241 | COMPILER_CHECK(sizeof(QuarantineBatch) < SizeClassMap::kMaxSize); |
Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 242 | void *Allocate(uptr Size) { |
Kostya Kortchinsky | 01a66fc | 2017-05-11 21:40:45 +0000 | [diff] [blame] | 243 | return getBackendAllocator().Allocate(Cache_, Size, MinAlignment, true); |
Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | void Deallocate(void *Ptr) { |
Kostya Kortchinsky | 01a66fc | 2017-05-11 21:40:45 +0000 | [diff] [blame] | 247 | getBackendAllocator().Deallocate(Cache_, Ptr, true); |
Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | AllocatorCache *Cache_; |
| 251 | }; |
| 252 | |
| 253 | typedef Quarantine<QuarantineCallback, ScudoChunk> ScudoQuarantine; |
Kostya Kortchinsky | 006805d | 2017-04-20 15:11:00 +0000 | [diff] [blame] | 254 | typedef ScudoQuarantine::Cache ScudoQuarantineCache; |
Kostya Kortchinsky | 36b3434 | 2017-04-27 20:21:16 +0000 | [diff] [blame] | 255 | COMPILER_CHECK(sizeof(ScudoQuarantineCache) <= |
| 256 | sizeof(ScudoThreadContext::QuarantineCachePlaceHolder)); |
Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 257 | |
Kostya Kortchinsky | 36b3434 | 2017-04-27 20:21:16 +0000 | [diff] [blame] | 258 | AllocatorCache *getAllocatorCache(ScudoThreadContext *ThreadContext) { |
| 259 | return &ThreadContext->Cache; |
Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 260 | } |
| 261 | |
Kostya Kortchinsky | 36b3434 | 2017-04-27 20:21:16 +0000 | [diff] [blame] | 262 | ScudoQuarantineCache *getQuarantineCache(ScudoThreadContext *ThreadContext) { |
| 263 | return reinterpret_cast< |
| 264 | ScudoQuarantineCache *>(ThreadContext->QuarantineCachePlaceHolder); |
| 265 | } |
| 266 | |
Kostya Kortchinsky | 0058256 | 2017-07-12 15:29:08 +0000 | [diff] [blame] | 267 | ScudoPrng *getPrng(ScudoThreadContext *ThreadContext) { |
Kostya Kortchinsky | 36b3434 | 2017-04-27 20:21:16 +0000 | [diff] [blame] | 268 | return &ThreadContext->Prng; |
Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 269 | } |
| 270 | |
Kostya Kortchinsky | 006805d | 2017-04-20 15:11:00 +0000 | [diff] [blame] | 271 | struct ScudoAllocator { |
Kostya Kortchinsky | 71dcc33 | 2016-10-26 16:16:58 +0000 | [diff] [blame] | 272 | static const uptr MaxAllowedMallocSize = |
| 273 | FIRST_32_SECOND_64(2UL << 30, 1ULL << 40); |
Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 274 | |
Alex Shlyapnikov | ccab11b | 2017-06-20 21:23:02 +0000 | [diff] [blame] | 275 | typedef ReturnNullOrDieOnFailure FailureHandler; |
| 276 | |
Kostya Kortchinsky | 006805d | 2017-04-20 15:11:00 +0000 | [diff] [blame] | 277 | ScudoBackendAllocator BackendAllocator; |
Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 278 | ScudoQuarantine AllocatorQuarantine; |
| 279 | |
| 280 | // The fallback caches are used when the thread local caches have been |
| 281 | // 'detroyed' on thread tear-down. They are protected by a Mutex as they can |
| 282 | // be accessed by different threads. |
| 283 | StaticSpinMutex FallbackMutex; |
| 284 | AllocatorCache FallbackAllocatorCache; |
Kostya Kortchinsky | 006805d | 2017-04-20 15:11:00 +0000 | [diff] [blame] | 285 | ScudoQuarantineCache FallbackQuarantineCache; |
Kostya Kortchinsky | 0058256 | 2017-07-12 15:29:08 +0000 | [diff] [blame] | 286 | ScudoPrng FallbackPrng; |
Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 287 | |
| 288 | bool DeallocationTypeMismatch; |
| 289 | bool ZeroContents; |
| 290 | bool DeleteSizeMismatch; |
| 291 | |
Kostya Kortchinsky | 006805d | 2017-04-20 15:11:00 +0000 | [diff] [blame] | 292 | explicit ScudoAllocator(LinkerInitialized) |
Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 293 | : AllocatorQuarantine(LINKER_INITIALIZED), |
| 294 | FallbackQuarantineCache(LINKER_INITIALIZED) {} |
| 295 | |
| 296 | void init(const AllocatorOptions &Options) { |
Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 297 | // 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] | 298 | // case of the Secondary allocator, it takes care of alignment and the |
| 299 | // offset will always be 0. In the case of the Primary, the worst case |
| 300 | // scenario happens in the last size class, when the backend allocation |
| 301 | // would already be aligned on the requested alignment, which would happen |
| 302 | // to be the maximum alignment that would fit in that size class. As a |
| 303 | // result, the maximum offset will be at most the maximum alignment for the |
| 304 | // last size class minus the header size, in multiples of MinAlignment. |
Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 305 | UnpackedHeader Header = {}; |
Kostya Kortchinsky | 71dcc33 | 2016-10-26 16:16:58 +0000 | [diff] [blame] | 306 | uptr MaxPrimaryAlignment = 1 << MostSignificantSetBitIndex( |
Kostya Kortchinsky | 1148dc5 | 2016-11-30 17:32:20 +0000 | [diff] [blame] | 307 | SizeClassMap::kMaxSize - MinAlignment); |
| 308 | uptr MaxOffset = (MaxPrimaryAlignment - AlignedChunkHeaderSize) >> |
Kostya Kortchinsky | 71dcc33 | 2016-10-26 16:16:58 +0000 | [diff] [blame] | 309 | MinAlignmentLog; |
Kostya Kortchinsky | 1148dc5 | 2016-11-30 17:32:20 +0000 | [diff] [blame] | 310 | Header.Offset = MaxOffset; |
| 311 | if (Header.Offset != MaxOffset) { |
Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 312 | dieWithMessage("ERROR: the maximum possible offset doesn't fit in the " |
| 313 | "header\n"); |
| 314 | } |
Kostya Kortchinsky | fff8e06 | 2017-04-20 18:07:17 +0000 | [diff] [blame] | 315 | // Verify that we can fit the maximum size or amount of unused bytes in the |
| 316 | // header. Given that the Secondary fits the allocation to a page, the worst |
| 317 | // case scenario happens in the Primary. It will depend on the second to |
| 318 | // last and last class sizes, as well as the dynamic base for the Primary. |
| 319 | // The following is an over-approximation that works for our needs. |
| 320 | uptr MaxSizeOrUnusedBytes = SizeClassMap::kMaxSize - 1; |
| 321 | Header.SizeOrUnusedBytes = MaxSizeOrUnusedBytes; |
| 322 | if (Header.SizeOrUnusedBytes != MaxSizeOrUnusedBytes) { |
Kostya Kortchinsky | 1148dc5 | 2016-11-30 17:32:20 +0000 | [diff] [blame] | 323 | dieWithMessage("ERROR: the maximum possible unused bytes doesn't fit in " |
| 324 | "the header\n"); |
| 325 | } |
Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 326 | |
| 327 | DeallocationTypeMismatch = Options.DeallocationTypeMismatch; |
| 328 | DeleteSizeMismatch = Options.DeleteSizeMismatch; |
| 329 | ZeroContents = Options.ZeroContents; |
Alex Shlyapnikov | ccab11b | 2017-06-20 21:23:02 +0000 | [diff] [blame] | 330 | SetAllocatorMayReturnNull(Options.MayReturnNull); |
| 331 | BackendAllocator.Init(Options.ReleaseToOSIntervalMs); |
Kostya Kortchinsky | 71dcc33 | 2016-10-26 16:16:58 +0000 | [diff] [blame] | 332 | AllocatorQuarantine.Init( |
| 333 | static_cast<uptr>(Options.QuarantineSizeMb) << 20, |
| 334 | static_cast<uptr>(Options.ThreadLocalQuarantineSizeKb) << 10); |
Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 335 | BackendAllocator.InitCache(&FallbackAllocatorCache); |
Kostya Kortchinsky | 0058256 | 2017-07-12 15:29:08 +0000 | [diff] [blame] | 336 | FallbackPrng.init(); |
| 337 | Cookie = FallbackPrng.getU64(); |
Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 338 | } |
| 339 | |
Kostya Kortchinsky | 006805d | 2017-04-20 15:11:00 +0000 | [diff] [blame] | 340 | // Helper function that checks for a valid Scudo chunk. nullptr isn't. |
Kostya Kortchinsky | 1148dc5 | 2016-11-30 17:32:20 +0000 | [diff] [blame] | 341 | bool isValidPointer(const void *UserPtr) { |
Kostya Kortchinsky | 36b3434 | 2017-04-27 20:21:16 +0000 | [diff] [blame] | 342 | initThreadMaybe(); |
Kostya Kortchinsky | 0ce4999 | 2017-06-29 16:45:20 +0000 | [diff] [blame] | 343 | if (UNLIKELY(!UserPtr)) |
Kostya Kortchinsky | 1148dc5 | 2016-11-30 17:32:20 +0000 | [diff] [blame] | 344 | return false; |
Kostya Kortchinsky | 006805d | 2017-04-20 15:11:00 +0000 | [diff] [blame] | 345 | uptr UserBeg = reinterpret_cast<uptr>(UserPtr); |
| 346 | if (!IsAligned(UserBeg, MinAlignment)) |
| 347 | return false; |
| 348 | return getScudoChunk(UserBeg)->isValid(); |
Kostya Kortchinsky | 1148dc5 | 2016-11-30 17:32:20 +0000 | [diff] [blame] | 349 | } |
| 350 | |
Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 351 | // Allocates a chunk. |
Kostya Kortchinsky | 006805d | 2017-04-20 15:11:00 +0000 | [diff] [blame] | 352 | void *allocate(uptr Size, uptr Alignment, AllocType Type, |
| 353 | bool ForceZeroContents = false) { |
Kostya Kortchinsky | 36b3434 | 2017-04-27 20:21:16 +0000 | [diff] [blame] | 354 | initThreadMaybe(); |
Kostya Kortchinsky | 0ce4999 | 2017-06-29 16:45:20 +0000 | [diff] [blame] | 355 | if (UNLIKELY(Alignment > MaxAlignment)) |
Alex Shlyapnikov | ccab11b | 2017-06-20 21:23:02 +0000 | [diff] [blame] | 356 | return FailureHandler::OnBadRequest(); |
Kostya Kortchinsky | 0ce4999 | 2017-06-29 16:45:20 +0000 | [diff] [blame] | 357 | if (UNLIKELY(Alignment < MinAlignment)) |
Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 358 | Alignment = MinAlignment; |
Kostya Kortchinsky | 0ce4999 | 2017-06-29 16:45:20 +0000 | [diff] [blame] | 359 | if (UNLIKELY(Size >= MaxAllowedMallocSize)) |
Alex Shlyapnikov | ccab11b | 2017-06-20 21:23:02 +0000 | [diff] [blame] | 360 | return FailureHandler::OnBadRequest(); |
Kostya Kortchinsky | 0ce4999 | 2017-06-29 16:45:20 +0000 | [diff] [blame] | 361 | if (UNLIKELY(Size == 0)) |
Kostya Kortchinsky | 006805d | 2017-04-20 15:11:00 +0000 | [diff] [blame] | 362 | Size = 1; |
Kostya Kortchinsky | c74da7c | 2016-12-13 19:31:54 +0000 | [diff] [blame] | 363 | |
| 364 | uptr NeededSize = RoundUpTo(Size, MinAlignment) + AlignedChunkHeaderSize; |
Kostya Kortchinsky | 01a66fc | 2017-05-11 21:40:45 +0000 | [diff] [blame] | 365 | uptr AlignedSize = (Alignment > MinAlignment) ? |
| 366 | NeededSize + (Alignment - AlignedChunkHeaderSize) : NeededSize; |
Kostya Kortchinsky | 0ce4999 | 2017-06-29 16:45:20 +0000 | [diff] [blame] | 367 | if (UNLIKELY(AlignedSize >= MaxAllowedMallocSize)) |
Alex Shlyapnikov | ccab11b | 2017-06-20 21:23:02 +0000 | [diff] [blame] | 368 | return FailureHandler::OnBadRequest(); |
Kostya Kortchinsky | c74da7c | 2016-12-13 19:31:54 +0000 | [diff] [blame] | 369 | |
Kostya Kortchinsky | 01a66fc | 2017-05-11 21:40:45 +0000 | [diff] [blame] | 370 | // Primary and Secondary backed allocations have a different treatment. We |
| 371 | // deal with alignment requirements of Primary serviced allocations here, |
| 372 | // but the Secondary will take care of its own alignment needs. |
| 373 | bool FromPrimary = PrimaryAllocator::CanAllocate(AlignedSize, MinAlignment); |
Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 374 | |
| 375 | void *Ptr; |
Kostya Kortchinsky | 0058256 | 2017-07-12 15:29:08 +0000 | [diff] [blame] | 376 | u8 Salt; |
Kostya Kortchinsky | 01a66fc | 2017-05-11 21:40:45 +0000 | [diff] [blame] | 377 | uptr AllocationSize = FromPrimary ? AlignedSize : NeededSize; |
Kostya Kortchinsky | 006805d | 2017-04-20 15:11:00 +0000 | [diff] [blame] | 378 | uptr AllocationAlignment = FromPrimary ? MinAlignment : Alignment; |
Kostya Kortchinsky | ee069576 | 2017-05-05 21:38:22 +0000 | [diff] [blame] | 379 | ScudoThreadContext *ThreadContext = getThreadContextAndLock(); |
Kostya Kortchinsky | 36b3434 | 2017-04-27 20:21:16 +0000 | [diff] [blame] | 380 | if (LIKELY(ThreadContext)) { |
Kostya Kortchinsky | 0058256 | 2017-07-12 15:29:08 +0000 | [diff] [blame] | 381 | Salt = getPrng(ThreadContext)->getU8(); |
Kostya Kortchinsky | 36b3434 | 2017-04-27 20:21:16 +0000 | [diff] [blame] | 382 | Ptr = BackendAllocator.Allocate(getAllocatorCache(ThreadContext), |
Kostya Kortchinsky | 01a66fc | 2017-05-11 21:40:45 +0000 | [diff] [blame] | 383 | AllocationSize, AllocationAlignment, |
| 384 | FromPrimary); |
Kostya Kortchinsky | ee069576 | 2017-05-05 21:38:22 +0000 | [diff] [blame] | 385 | ThreadContext->unlock(); |
Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 386 | } else { |
| 387 | SpinMutexLock l(&FallbackMutex); |
Kostya Kortchinsky | 0058256 | 2017-07-12 15:29:08 +0000 | [diff] [blame] | 388 | Salt = FallbackPrng.getU8(); |
Kostya Kortchinsky | 01a66fc | 2017-05-11 21:40:45 +0000 | [diff] [blame] | 389 | Ptr = BackendAllocator.Allocate(&FallbackAllocatorCache, AllocationSize, |
| 390 | AllocationAlignment, FromPrimary); |
Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 391 | } |
Kostya Kortchinsky | 0ce4999 | 2017-06-29 16:45:20 +0000 | [diff] [blame] | 392 | if (UNLIKELY(!Ptr)) |
Alex Shlyapnikov | ccab11b | 2017-06-20 21:23:02 +0000 | [diff] [blame] | 393 | return FailureHandler::OnOOM(); |
Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 394 | |
Kostya Kortchinsky | 1148dc5 | 2016-11-30 17:32:20 +0000 | [diff] [blame] | 395 | // 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] | 396 | if ((ForceZeroContents || ZeroContents) && FromPrimary) |
Kostya Kortchinsky | 01a66fc | 2017-05-11 21:40:45 +0000 | [diff] [blame] | 397 | memset(Ptr, 0, |
| 398 | BackendAllocator.GetActuallyAllocatedSize(Ptr, FromPrimary)); |
Kostya Kortchinsky | 1148dc5 | 2016-11-30 17:32:20 +0000 | [diff] [blame] | 399 | |
Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 400 | UnpackedHeader Header = {}; |
Kostya Kortchinsky | 01a66fc | 2017-05-11 21:40:45 +0000 | [diff] [blame] | 401 | uptr AllocBeg = reinterpret_cast<uptr>(Ptr); |
| 402 | uptr UserBeg = AllocBeg + AlignedChunkHeaderSize; |
Kostya Kortchinsky | 0ce4999 | 2017-06-29 16:45:20 +0000 | [diff] [blame] | 403 | if (UNLIKELY(!IsAligned(UserBeg, Alignment))) { |
Kostya Kortchinsky | 01a66fc | 2017-05-11 21:40:45 +0000 | [diff] [blame] | 404 | // Since the Secondary takes care of alignment, a non-aligned pointer |
| 405 | // means it is from the Primary. It is also the only case where the offset |
| 406 | // field of the header would be non-zero. |
| 407 | CHECK(FromPrimary); |
| 408 | UserBeg = RoundUpTo(UserBeg, Alignment); |
| 409 | uptr Offset = UserBeg - AlignedChunkHeaderSize - AllocBeg; |
| 410 | Header.Offset = Offset >> MinAlignmentLog; |
| 411 | } |
| 412 | CHECK_LE(UserBeg + Size, AllocBeg + AllocationSize); |
Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 413 | Header.State = ChunkAllocated; |
Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 414 | Header.AllocType = Type; |
Kostya Kortchinsky | fff8e06 | 2017-04-20 18:07:17 +0000 | [diff] [blame] | 415 | if (FromPrimary) { |
| 416 | Header.FromPrimary = FromPrimary; |
| 417 | Header.SizeOrUnusedBytes = Size; |
| 418 | } else { |
| 419 | // The secondary fits the allocations to a page, so the amount of unused |
| 420 | // bytes is the difference between the end of the user allocation and the |
| 421 | // next page boundary. |
| 422 | uptr PageSize = GetPageSizeCached(); |
| 423 | uptr TrailingBytes = (UserBeg + Size) & (PageSize - 1); |
| 424 | if (TrailingBytes) |
| 425 | Header.SizeOrUnusedBytes = PageSize - TrailingBytes; |
| 426 | } |
Kostya Kortchinsky | 36b3434 | 2017-04-27 20:21:16 +0000 | [diff] [blame] | 427 | Header.Salt = static_cast<u8>(Salt); |
Kostya Kortchinsky | 006805d | 2017-04-20 15:11:00 +0000 | [diff] [blame] | 428 | getScudoChunk(UserBeg)->storeHeader(&Header); |
| 429 | void *UserPtr = reinterpret_cast<void *>(UserBeg); |
Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 430 | // if (&__sanitizer_malloc_hook) __sanitizer_malloc_hook(UserPtr, Size); |
| 431 | return UserPtr; |
| 432 | } |
| 433 | |
Kostya Kortchinsky | f1a54fd | 2017-04-21 18:10:53 +0000 | [diff] [blame] | 434 | // Place a chunk in the quarantine. In the event of a zero-sized quarantine, |
| 435 | // we directly deallocate the chunk, otherwise the flow would lead to the |
Kostya Kortchinsky | 36b3434 | 2017-04-27 20:21:16 +0000 | [diff] [blame] | 436 | // chunk being loaded (and checked) twice, and stored (and checksummed) once, |
| 437 | // with no additional security value. |
Kostya Kortchinsky | f1a54fd | 2017-04-21 18:10:53 +0000 | [diff] [blame] | 438 | void quarantineOrDeallocateChunk(ScudoChunk *Chunk, UnpackedHeader *Header, |
| 439 | uptr Size) { |
Kostya Kortchinsky | 01a66fc | 2017-05-11 21:40:45 +0000 | [diff] [blame] | 440 | bool FromPrimary = Header->FromPrimary; |
Kostya Kortchinsky | f1a54fd | 2017-04-21 18:10:53 +0000 | [diff] [blame] | 441 | bool BypassQuarantine = (AllocatorQuarantine.GetCacheSize() == 0); |
| 442 | if (BypassQuarantine) { |
| 443 | Chunk->eraseHeader(); |
| 444 | void *Ptr = Chunk->getAllocBeg(Header); |
Kostya Kortchinsky | ee069576 | 2017-05-05 21:38:22 +0000 | [diff] [blame] | 445 | ScudoThreadContext *ThreadContext = getThreadContextAndLock(); |
Kostya Kortchinsky | 36b3434 | 2017-04-27 20:21:16 +0000 | [diff] [blame] | 446 | if (LIKELY(ThreadContext)) { |
Kostya Kortchinsky | 01a66fc | 2017-05-11 21:40:45 +0000 | [diff] [blame] | 447 | getBackendAllocator().Deallocate(getAllocatorCache(ThreadContext), Ptr, |
| 448 | FromPrimary); |
Kostya Kortchinsky | ee069576 | 2017-05-05 21:38:22 +0000 | [diff] [blame] | 449 | ThreadContext->unlock(); |
Kostya Kortchinsky | f1a54fd | 2017-04-21 18:10:53 +0000 | [diff] [blame] | 450 | } else { |
| 451 | SpinMutexLock Lock(&FallbackMutex); |
Kostya Kortchinsky | 01a66fc | 2017-05-11 21:40:45 +0000 | [diff] [blame] | 452 | getBackendAllocator().Deallocate(&FallbackAllocatorCache, Ptr, |
| 453 | FromPrimary); |
Kostya Kortchinsky | f1a54fd | 2017-04-21 18:10:53 +0000 | [diff] [blame] | 454 | } |
| 455 | } else { |
| 456 | UnpackedHeader NewHeader = *Header; |
| 457 | NewHeader.State = ChunkQuarantine; |
| 458 | Chunk->compareExchangeHeader(&NewHeader, Header); |
Kostya Kortchinsky | ee069576 | 2017-05-05 21:38:22 +0000 | [diff] [blame] | 459 | ScudoThreadContext *ThreadContext = getThreadContextAndLock(); |
Kostya Kortchinsky | 36b3434 | 2017-04-27 20:21:16 +0000 | [diff] [blame] | 460 | if (LIKELY(ThreadContext)) { |
| 461 | AllocatorQuarantine.Put(getQuarantineCache(ThreadContext), |
| 462 | QuarantineCallback( |
| 463 | getAllocatorCache(ThreadContext)), |
| 464 | Chunk, Size); |
Kostya Kortchinsky | ee069576 | 2017-05-05 21:38:22 +0000 | [diff] [blame] | 465 | ThreadContext->unlock(); |
Kostya Kortchinsky | f1a54fd | 2017-04-21 18:10:53 +0000 | [diff] [blame] | 466 | } else { |
| 467 | SpinMutexLock l(&FallbackMutex); |
| 468 | AllocatorQuarantine.Put(&FallbackQuarantineCache, |
| 469 | QuarantineCallback(&FallbackAllocatorCache), |
| 470 | Chunk, Size); |
| 471 | } |
| 472 | } |
| 473 | } |
| 474 | |
Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 475 | // Deallocates a Chunk, which means adding it to the delayed free list (or |
| 476 | // Quarantine). |
| 477 | void deallocate(void *UserPtr, uptr DeleteSize, AllocType Type) { |
Kostya Kortchinsky | 36b3434 | 2017-04-27 20:21:16 +0000 | [diff] [blame] | 478 | initThreadMaybe(); |
Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 479 | // if (&__sanitizer_free_hook) __sanitizer_free_hook(UserPtr); |
Kostya Kortchinsky | 0ce4999 | 2017-06-29 16:45:20 +0000 | [diff] [blame] | 480 | if (UNLIKELY(!UserPtr)) |
Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 481 | return; |
Kostya Kortchinsky | 006805d | 2017-04-20 15:11:00 +0000 | [diff] [blame] | 482 | uptr UserBeg = reinterpret_cast<uptr>(UserPtr); |
| 483 | if (UNLIKELY(!IsAligned(UserBeg, MinAlignment))) { |
Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 484 | dieWithMessage("ERROR: attempted to deallocate a chunk not properly " |
| 485 | "aligned at address %p\n", UserPtr); |
| 486 | } |
Kostya Kortchinsky | 006805d | 2017-04-20 15:11:00 +0000 | [diff] [blame] | 487 | ScudoChunk *Chunk = getScudoChunk(UserBeg); |
Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 488 | UnpackedHeader OldHeader; |
| 489 | Chunk->loadHeader(&OldHeader); |
Kostya Kortchinsky | 006805d | 2017-04-20 15:11:00 +0000 | [diff] [blame] | 490 | if (UNLIKELY(OldHeader.State != ChunkAllocated)) { |
Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 491 | dieWithMessage("ERROR: invalid chunk state when deallocating address " |
Kostya Kortchinsky | 1148dc5 | 2016-11-30 17:32:20 +0000 | [diff] [blame] | 492 | "%p\n", UserPtr); |
Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 493 | } |
Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 494 | if (DeallocationTypeMismatch) { |
| 495 | // The deallocation type has to match the allocation one. |
Kostya Kortchinsky | 006805d | 2017-04-20 15:11:00 +0000 | [diff] [blame] | 496 | if (OldHeader.AllocType != Type) { |
Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 497 | // 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] | 498 | if (OldHeader.AllocType != FromMemalign || Type != FromMalloc) { |
Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 499 | dieWithMessage("ERROR: allocation type mismatch on address %p\n", |
Kostya Kortchinsky | 006805d | 2017-04-20 15:11:00 +0000 | [diff] [blame] | 500 | UserPtr); |
Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 501 | } |
| 502 | } |
| 503 | } |
Kostya Kortchinsky | fff8e06 | 2017-04-20 18:07:17 +0000 | [diff] [blame] | 504 | uptr Size = OldHeader.FromPrimary ? OldHeader.SizeOrUnusedBytes : |
| 505 | Chunk->getUsableSize(&OldHeader) - OldHeader.SizeOrUnusedBytes; |
Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 506 | if (DeleteSizeMismatch) { |
| 507 | if (DeleteSize && DeleteSize != Size) { |
| 508 | dieWithMessage("ERROR: invalid sized delete on chunk at address %p\n", |
Kostya Kortchinsky | 006805d | 2017-04-20 15:11:00 +0000 | [diff] [blame] | 509 | UserPtr); |
Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 510 | } |
| 511 | } |
Kostya Kortchinsky | 1148dc5 | 2016-11-30 17:32:20 +0000 | [diff] [blame] | 512 | |
Kostya Kortchinsky | fff8e06 | 2017-04-20 18:07:17 +0000 | [diff] [blame] | 513 | // If a small memory amount was allocated with a larger alignment, we want |
| 514 | // to take that into account. Otherwise the Quarantine would be filled with |
Kostya Kortchinsky | f1a54fd | 2017-04-21 18:10:53 +0000 | [diff] [blame] | 515 | // 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] | 516 | // usable size, that allows us to not call GetActuallyAllocatedSize. |
| 517 | uptr LiableSize = Size + (OldHeader.Offset << MinAlignment); |
Kostya Kortchinsky | f1a54fd | 2017-04-21 18:10:53 +0000 | [diff] [blame] | 518 | quarantineOrDeallocateChunk(Chunk, &OldHeader, LiableSize); |
Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 519 | } |
| 520 | |
Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 521 | // Reallocates a chunk. We can save on a new allocation if the new requested |
| 522 | // size still fits in the chunk. |
| 523 | void *reallocate(void *OldPtr, uptr NewSize) { |
Kostya Kortchinsky | 36b3434 | 2017-04-27 20:21:16 +0000 | [diff] [blame] | 524 | initThreadMaybe(); |
Kostya Kortchinsky | 006805d | 2017-04-20 15:11:00 +0000 | [diff] [blame] | 525 | uptr UserBeg = reinterpret_cast<uptr>(OldPtr); |
| 526 | if (UNLIKELY(!IsAligned(UserBeg, MinAlignment))) { |
| 527 | dieWithMessage("ERROR: attempted to reallocate a chunk not properly " |
| 528 | "aligned at address %p\n", OldPtr); |
| 529 | } |
| 530 | ScudoChunk *Chunk = getScudoChunk(UserBeg); |
Kostya Kortchinsky | 1148dc5 | 2016-11-30 17:32:20 +0000 | [diff] [blame] | 531 | UnpackedHeader OldHeader; |
| 532 | Chunk->loadHeader(&OldHeader); |
Kostya Kortchinsky | 006805d | 2017-04-20 15:11:00 +0000 | [diff] [blame] | 533 | if (UNLIKELY(OldHeader.State != ChunkAllocated)) { |
Kostya Kortchinsky | 1148dc5 | 2016-11-30 17:32:20 +0000 | [diff] [blame] | 534 | dieWithMessage("ERROR: invalid chunk state when reallocating address " |
| 535 | "%p\n", OldPtr); |
| 536 | } |
Kostya Kortchinsky | 006805d | 2017-04-20 15:11:00 +0000 | [diff] [blame] | 537 | if (UNLIKELY(OldHeader.AllocType != FromMalloc)) { |
Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 538 | dieWithMessage("ERROR: invalid chunk type when reallocating address %p\n", |
Kostya Kortchinsky | 006805d | 2017-04-20 15:11:00 +0000 | [diff] [blame] | 539 | OldPtr); |
Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 540 | } |
Kostya Kortchinsky | 006805d | 2017-04-20 15:11:00 +0000 | [diff] [blame] | 541 | uptr UsableSize = Chunk->getUsableSize(&OldHeader); |
Kostya Kortchinsky | fff8e06 | 2017-04-20 18:07:17 +0000 | [diff] [blame] | 542 | // The new size still fits in the current chunk, and the size difference |
| 543 | // is reasonable. |
| 544 | if (NewSize <= UsableSize && |
| 545 | (UsableSize - NewSize) < (SizeClassMap::kMaxSize / 2)) { |
Kostya Kortchinsky | f1a54fd | 2017-04-21 18:10:53 +0000 | [diff] [blame] | 546 | UnpackedHeader NewHeader = OldHeader; |
Kostya Kortchinsky | fff8e06 | 2017-04-20 18:07:17 +0000 | [diff] [blame] | 547 | NewHeader.SizeOrUnusedBytes = |
| 548 | OldHeader.FromPrimary ? NewSize : UsableSize - NewSize; |
Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 549 | Chunk->compareExchangeHeader(&NewHeader, &OldHeader); |
| 550 | return OldPtr; |
| 551 | } |
| 552 | // Otherwise, we have to allocate a new chunk and copy the contents of the |
| 553 | // old one. |
| 554 | void *NewPtr = allocate(NewSize, MinAlignment, FromMalloc); |
| 555 | if (NewPtr) { |
Kostya Kortchinsky | fff8e06 | 2017-04-20 18:07:17 +0000 | [diff] [blame] | 556 | uptr OldSize = OldHeader.FromPrimary ? OldHeader.SizeOrUnusedBytes : |
| 557 | UsableSize - OldHeader.SizeOrUnusedBytes; |
Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 558 | memcpy(NewPtr, OldPtr, Min(NewSize, OldSize)); |
Kostya Kortchinsky | f1a54fd | 2017-04-21 18:10:53 +0000 | [diff] [blame] | 559 | quarantineOrDeallocateChunk(Chunk, &OldHeader, UsableSize); |
Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 560 | } |
| 561 | return NewPtr; |
| 562 | } |
| 563 | |
Kostya Kortchinsky | 1148dc5 | 2016-11-30 17:32:20 +0000 | [diff] [blame] | 564 | // Helper function that returns the actual usable size of a chunk. |
| 565 | uptr getUsableSize(const void *Ptr) { |
Kostya Kortchinsky | 36b3434 | 2017-04-27 20:21:16 +0000 | [diff] [blame] | 566 | initThreadMaybe(); |
Kostya Kortchinsky | 0ce4999 | 2017-06-29 16:45:20 +0000 | [diff] [blame] | 567 | if (UNLIKELY(!Ptr)) |
Kostya Kortchinsky | 1148dc5 | 2016-11-30 17:32:20 +0000 | [diff] [blame] | 568 | return 0; |
Kostya Kortchinsky | 006805d | 2017-04-20 15:11:00 +0000 | [diff] [blame] | 569 | uptr UserBeg = reinterpret_cast<uptr>(Ptr); |
| 570 | ScudoChunk *Chunk = getScudoChunk(UserBeg); |
Kostya Kortchinsky | 1148dc5 | 2016-11-30 17:32:20 +0000 | [diff] [blame] | 571 | UnpackedHeader Header; |
| 572 | Chunk->loadHeader(&Header); |
| 573 | // 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] | 574 | if (UNLIKELY(Header.State != ChunkAllocated)) { |
Kostya Kortchinsky | 1148dc5 | 2016-11-30 17:32:20 +0000 | [diff] [blame] | 575 | dieWithMessage("ERROR: invalid chunk state when sizing address %p\n", |
| 576 | Ptr); |
| 577 | } |
| 578 | return Chunk->getUsableSize(&Header); |
| 579 | } |
| 580 | |
Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 581 | void *calloc(uptr NMemB, uptr Size) { |
Kostya Kortchinsky | 36b3434 | 2017-04-27 20:21:16 +0000 | [diff] [blame] | 582 | initThreadMaybe(); |
Alex Shlyapnikov | 346988b | 2017-06-29 21:54:38 +0000 | [diff] [blame] | 583 | if (CheckForCallocOverflow(NMemB, Size)) |
Alex Shlyapnikov | ccab11b | 2017-06-20 21:23:02 +0000 | [diff] [blame] | 584 | return FailureHandler::OnBadRequest(); |
Kostya Kortchinsky | 0ce4999 | 2017-06-29 16:45:20 +0000 | [diff] [blame] | 585 | return allocate(NMemB * Size, MinAlignment, FromMalloc, true); |
Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 586 | } |
| 587 | |
Kostya Kortchinsky | 36b3434 | 2017-04-27 20:21:16 +0000 | [diff] [blame] | 588 | void commitBack(ScudoThreadContext *ThreadContext) { |
| 589 | AllocatorCache *Cache = getAllocatorCache(ThreadContext); |
| 590 | AllocatorQuarantine.Drain(getQuarantineCache(ThreadContext), |
| 591 | QuarantineCallback(Cache)); |
| 592 | BackendAllocator.DestroyCache(Cache); |
Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 593 | } |
Kostya Kortchinsky | 8d6257b | 2017-02-03 20:49:42 +0000 | [diff] [blame] | 594 | |
| 595 | uptr getStats(AllocatorStat StatType) { |
Kostya Kortchinsky | 36b3434 | 2017-04-27 20:21:16 +0000 | [diff] [blame] | 596 | initThreadMaybe(); |
Kostya Kortchinsky | 8d6257b | 2017-02-03 20:49:42 +0000 | [diff] [blame] | 597 | uptr stats[AllocatorStatCount]; |
| 598 | BackendAllocator.GetStats(stats); |
| 599 | return stats[StatType]; |
| 600 | } |
Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 601 | }; |
| 602 | |
Kostya Kortchinsky | 006805d | 2017-04-20 15:11:00 +0000 | [diff] [blame] | 603 | static ScudoAllocator Instance(LINKER_INITIALIZED); |
Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 604 | |
Kostya Kortchinsky | 006805d | 2017-04-20 15:11:00 +0000 | [diff] [blame] | 605 | static ScudoBackendAllocator &getBackendAllocator() { |
Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 606 | return Instance.BackendAllocator; |
| 607 | } |
| 608 | |
Kostya Kortchinsky | 36b3434 | 2017-04-27 20:21:16 +0000 | [diff] [blame] | 609 | static void initScudoInternal(const AllocatorOptions &Options) { |
Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 610 | Instance.init(Options); |
| 611 | } |
| 612 | |
Kostya Kortchinsky | 36b3434 | 2017-04-27 20:21:16 +0000 | [diff] [blame] | 613 | void ScudoThreadContext::init() { |
| 614 | getBackendAllocator().InitCache(&Cache); |
Kostya Kortchinsky | 0058256 | 2017-07-12 15:29:08 +0000 | [diff] [blame] | 615 | Prng.init(); |
Kostya Kortchinsky | 36b3434 | 2017-04-27 20:21:16 +0000 | [diff] [blame] | 616 | memset(QuarantineCachePlaceHolder, 0, sizeof(QuarantineCachePlaceHolder)); |
| 617 | } |
| 618 | |
| 619 | void ScudoThreadContext::commitBack() { |
| 620 | Instance.commitBack(this); |
Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 621 | } |
| 622 | |
| 623 | void *scudoMalloc(uptr Size, AllocType Type) { |
Kostya Kortchinsky | 71dcc33 | 2016-10-26 16:16:58 +0000 | [diff] [blame] | 624 | return Instance.allocate(Size, MinAlignment, Type); |
Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 625 | } |
| 626 | |
| 627 | void scudoFree(void *Ptr, AllocType Type) { |
| 628 | Instance.deallocate(Ptr, 0, Type); |
| 629 | } |
| 630 | |
| 631 | void scudoSizedFree(void *Ptr, uptr Size, AllocType Type) { |
| 632 | Instance.deallocate(Ptr, Size, Type); |
| 633 | } |
| 634 | |
| 635 | void *scudoRealloc(void *Ptr, uptr Size) { |
| 636 | if (!Ptr) |
Kostya Kortchinsky | 71dcc33 | 2016-10-26 16:16:58 +0000 | [diff] [blame] | 637 | return Instance.allocate(Size, MinAlignment, FromMalloc); |
Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 638 | if (Size == 0) { |
| 639 | Instance.deallocate(Ptr, 0, FromMalloc); |
| 640 | return nullptr; |
| 641 | } |
| 642 | return Instance.reallocate(Ptr, Size); |
| 643 | } |
| 644 | |
| 645 | void *scudoCalloc(uptr NMemB, uptr Size) { |
| 646 | return Instance.calloc(NMemB, Size); |
| 647 | } |
| 648 | |
| 649 | void *scudoValloc(uptr Size) { |
| 650 | return Instance.allocate(Size, GetPageSizeCached(), FromMemalign); |
| 651 | } |
| 652 | |
Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 653 | void *scudoPvalloc(uptr Size) { |
| 654 | uptr PageSize = GetPageSizeCached(); |
| 655 | Size = RoundUpTo(Size, PageSize); |
| 656 | if (Size == 0) { |
| 657 | // pvalloc(0) should allocate one page. |
| 658 | Size = PageSize; |
| 659 | } |
| 660 | return Instance.allocate(Size, PageSize, FromMemalign); |
| 661 | } |
| 662 | |
Kostya Kortchinsky | 0ce4999 | 2017-06-29 16:45:20 +0000 | [diff] [blame] | 663 | void *scudoMemalign(uptr Alignment, uptr Size) { |
| 664 | if (UNLIKELY(!IsPowerOfTwo(Alignment))) |
| 665 | return ScudoAllocator::FailureHandler::OnBadRequest(); |
| 666 | return Instance.allocate(Size, Alignment, FromMemalign); |
| 667 | } |
| 668 | |
Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 669 | int scudoPosixMemalign(void **MemPtr, uptr Alignment, uptr Size) { |
Kostya Kortchinsky | 0ce4999 | 2017-06-29 16:45:20 +0000 | [diff] [blame] | 670 | if (UNLIKELY(!IsPowerOfTwo(Alignment) || (Alignment % sizeof(void *)) != 0)) { |
| 671 | *MemPtr = ScudoAllocator::FailureHandler::OnBadRequest(); |
| 672 | return EINVAL; |
| 673 | } |
Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 674 | *MemPtr = Instance.allocate(Size, Alignment, FromMemalign); |
Kostya Kortchinsky | 0ce4999 | 2017-06-29 16:45:20 +0000 | [diff] [blame] | 675 | if (!*MemPtr) |
| 676 | return ENOMEM; |
Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 677 | return 0; |
| 678 | } |
| 679 | |
| 680 | void *scudoAlignedAlloc(uptr Alignment, uptr Size) { |
Kostya Kortchinsky | 0ce4999 | 2017-06-29 16:45:20 +0000 | [diff] [blame] | 681 | // Alignment must be a power of 2, Size must be a multiple of Alignment. |
| 682 | if (UNLIKELY(!IsPowerOfTwo(Alignment) || (Size & (Alignment - 1)) != 0)) |
| 683 | return ScudoAllocator::FailureHandler::OnBadRequest(); |
Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 684 | return Instance.allocate(Size, Alignment, FromMalloc); |
| 685 | } |
| 686 | |
| 687 | uptr scudoMallocUsableSize(void *Ptr) { |
| 688 | return Instance.getUsableSize(Ptr); |
| 689 | } |
| 690 | |
Kostya Kortchinsky | 1148dc5 | 2016-11-30 17:32:20 +0000 | [diff] [blame] | 691 | } // namespace __scudo |
Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 692 | |
| 693 | using namespace __scudo; |
| 694 | |
| 695 | // MallocExtension helper functions |
| 696 | |
| 697 | uptr __sanitizer_get_current_allocated_bytes() { |
Kostya Kortchinsky | 8d6257b | 2017-02-03 20:49:42 +0000 | [diff] [blame] | 698 | return Instance.getStats(AllocatorStatAllocated); |
Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 699 | } |
| 700 | |
| 701 | uptr __sanitizer_get_heap_size() { |
Kostya Kortchinsky | 8d6257b | 2017-02-03 20:49:42 +0000 | [diff] [blame] | 702 | return Instance.getStats(AllocatorStatMapped); |
Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 703 | } |
| 704 | |
| 705 | uptr __sanitizer_get_free_bytes() { |
| 706 | return 1; |
| 707 | } |
| 708 | |
| 709 | uptr __sanitizer_get_unmapped_bytes() { |
| 710 | return 1; |
| 711 | } |
| 712 | |
| 713 | uptr __sanitizer_get_estimated_allocated_size(uptr size) { |
| 714 | return size; |
| 715 | } |
| 716 | |
Kostya Kortchinsky | 1148dc5 | 2016-11-30 17:32:20 +0000 | [diff] [blame] | 717 | int __sanitizer_get_ownership(const void *Ptr) { |
| 718 | return Instance.isValidPointer(Ptr); |
Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 719 | } |
| 720 | |
Kostya Kortchinsky | 1148dc5 | 2016-11-30 17:32:20 +0000 | [diff] [blame] | 721 | uptr __sanitizer_get_allocated_size(const void *Ptr) { |
| 722 | return Instance.getUsableSize(Ptr); |
Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 723 | } |