Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 1 | //===-- scudo_allocator.h ---------------------------------------*- 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 | /// Header for scudo_allocator.cpp. |
| 11 | /// |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #ifndef SCUDO_ALLOCATOR_H_ |
| 15 | #define SCUDO_ALLOCATOR_H_ |
| 16 | |
Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 17 | #include "scudo_flags.h" |
| 18 | |
| 19 | #include "sanitizer_common/sanitizer_allocator.h" |
| 20 | |
Kostya Kortchinsky | b39dff4 | 2017-01-18 17:11:17 +0000 | [diff] [blame] | 21 | #if !SANITIZER_LINUX |
| 22 | # error "The Scudo hardened allocator is currently only supported on Linux." |
| 23 | #endif |
| 24 | |
Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 25 | namespace __scudo { |
| 26 | |
| 27 | enum AllocType : u8 { |
| 28 | FromMalloc = 0, // Memory block came from malloc, realloc, calloc, etc. |
| 29 | FromNew = 1, // Memory block came from operator new. |
| 30 | FromNewArray = 2, // Memory block came from operator new []. |
| 31 | FromMemalign = 3, // Memory block came from memalign, posix_memalign, etc. |
| 32 | }; |
| 33 | |
Kostya Kortchinsky | 71dcc33 | 2016-10-26 16:16:58 +0000 | [diff] [blame] | 34 | enum ChunkState : u8 { |
| 35 | ChunkAvailable = 0, |
| 36 | ChunkAllocated = 1, |
| 37 | ChunkQuarantine = 2 |
| 38 | }; |
| 39 | |
Kostya Kortchinsky | 1148dc5 | 2016-11-30 17:32:20 +0000 | [diff] [blame] | 40 | // Our header requires 64 bits of storage. Having the offset saves us from |
Kostya Kortchinsky | 71dcc33 | 2016-10-26 16:16:58 +0000 | [diff] [blame] | 41 | // using functions such as GetBlockBegin, that is fairly costly. Our first |
| 42 | // implementation used the MetaData as well, which offers the advantage of |
| 43 | // being stored away from the chunk itself, but accessing it was costly as |
Kostya Kortchinsky | 006805d | 2017-04-20 15:11:00 +0000 | [diff] [blame] | 44 | // well. The header will be atomically loaded and stored. |
Kostya Kortchinsky | 71dcc33 | 2016-10-26 16:16:58 +0000 | [diff] [blame] | 45 | typedef u64 PackedHeader; |
| 46 | struct UnpackedHeader { |
Kostya Kortchinsky | fff8e06 | 2017-04-20 18:07:17 +0000 | [diff] [blame^] | 47 | u64 Checksum : 16; |
| 48 | u64 SizeOrUnusedBytes : 19; // Size for Primary backed allocations, amount of |
| 49 | // unused bytes in the chunk for Secondary ones. |
| 50 | u64 FromPrimary : 1; |
| 51 | u64 State : 2; // available, allocated, or quarantined |
| 52 | u64 AllocType : 2; // malloc, new, new[], or memalign |
| 53 | u64 Offset : 16; // Offset from the beginning of the backend |
| 54 | // allocation to the beginning of the chunk |
| 55 | // itself, in multiples of MinAlignment. See |
| 56 | /// comment about its maximum value and in init(). |
| 57 | u64 Salt : 8; |
Kostya Kortchinsky | 71dcc33 | 2016-10-26 16:16:58 +0000 | [diff] [blame] | 58 | }; |
Kostya Kortchinsky | 71dcc33 | 2016-10-26 16:16:58 +0000 | [diff] [blame] | 59 | |
Kostya Kortchinsky | a00b922 | 2017-01-20 18:32:18 +0000 | [diff] [blame] | 60 | typedef atomic_uint64_t AtomicPackedHeader; |
Kostya Kortchinsky | 71dcc33 | 2016-10-26 16:16:58 +0000 | [diff] [blame] | 61 | COMPILER_CHECK(sizeof(UnpackedHeader) == sizeof(PackedHeader)); |
| 62 | |
Kostya Kortchinsky | 71dcc33 | 2016-10-26 16:16:58 +0000 | [diff] [blame] | 63 | // Minimum alignment of 8 bytes for 32-bit, 16 for 64-bit |
| 64 | const uptr MinAlignmentLog = FIRST_32_SECOND_64(3, 4); |
| 65 | const uptr MaxAlignmentLog = 24; // 16 MB |
| 66 | const uptr MinAlignment = 1 << MinAlignmentLog; |
| 67 | const uptr MaxAlignment = 1 << MaxAlignmentLog; |
| 68 | |
Kostya Kortchinsky | 1148dc5 | 2016-11-30 17:32:20 +0000 | [diff] [blame] | 69 | const uptr ChunkHeaderSize = sizeof(PackedHeader); |
| 70 | const uptr AlignedChunkHeaderSize = |
| 71 | (ChunkHeaderSize + MinAlignment - 1) & ~(MinAlignment - 1); |
| 72 | |
Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 73 | struct AllocatorOptions { |
| 74 | u32 QuarantineSizeMb; |
| 75 | u32 ThreadLocalQuarantineSizeKb; |
| 76 | bool MayReturnNull; |
Evgeniy Stepanov | d3305af | 2016-11-29 00:22:50 +0000 | [diff] [blame] | 77 | s32 ReleaseToOSIntervalMs; |
Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 78 | bool DeallocationTypeMismatch; |
| 79 | bool DeleteSizeMismatch; |
| 80 | bool ZeroContents; |
| 81 | |
| 82 | void setFrom(const Flags *f, const CommonFlags *cf); |
| 83 | void copyTo(Flags *f, CommonFlags *cf) const; |
| 84 | }; |
| 85 | |
| 86 | void initAllocator(const AllocatorOptions &options); |
| 87 | void drainQuarantine(); |
| 88 | |
| 89 | void *scudoMalloc(uptr Size, AllocType Type); |
| 90 | void scudoFree(void *Ptr, AllocType Type); |
| 91 | void scudoSizedFree(void *Ptr, uptr Size, AllocType Type); |
| 92 | void *scudoRealloc(void *Ptr, uptr Size); |
| 93 | void *scudoCalloc(uptr NMemB, uptr Size); |
| 94 | void *scudoMemalign(uptr Alignment, uptr Size); |
| 95 | void *scudoValloc(uptr Size); |
| 96 | void *scudoPvalloc(uptr Size); |
| 97 | int scudoPosixMemalign(void **MemPtr, uptr Alignment, uptr Size); |
| 98 | void *scudoAlignedAlloc(uptr Alignment, uptr Size); |
| 99 | uptr scudoMallocUsableSize(void *Ptr); |
| 100 | |
Kostya Kortchinsky | 71dcc33 | 2016-10-26 16:16:58 +0000 | [diff] [blame] | 101 | #include "scudo_allocator_secondary.h" |
| 102 | |
Kostya Kortchinsky | 1148dc5 | 2016-11-30 17:32:20 +0000 | [diff] [blame] | 103 | } // namespace __scudo |
Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 104 | |
| 105 | #endif // SCUDO_ALLOCATOR_H_ |