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