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