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