blob: 1696e1389c77bcc0f453874c61654831fb81af7a [file] [log] [blame]
Kostya Serebryany712fc982016-06-07 01:20:26 +00001//===-- 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 Serebryany712fc982016-06-07 01:20:26 +000017#include "scudo_flags.h"
18
19#include "sanitizer_common/sanitizer_allocator.h"
20
Kostya Kortchinskyb39dff42017-01-18 17:11:17 +000021#if !SANITIZER_LINUX
22# error "The Scudo hardened allocator is currently only supported on Linux."
23#endif
24
Kostya Serebryany712fc982016-06-07 01:20:26 +000025namespace __scudo {
26
27enum 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 Kortchinsky71dcc332016-10-26 16:16:58 +000034enum ChunkState : u8 {
35 ChunkAvailable = 0,
36 ChunkAllocated = 1,
37 ChunkQuarantine = 2
38};
39
Kostya Kortchinsky1148dc52016-11-30 17:32:20 +000040// Our header requires 64 bits of storage. Having the offset saves us from
Kostya Kortchinsky71dcc332016-10-26 16:16:58 +000041// 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 Kortchinsky006805d2017-04-20 15:11:00 +000044// well. The header will be atomically loaded and stored.
Kostya Kortchinsky71dcc332016-10-26 16:16:58 +000045typedef u64 PackedHeader;
46struct UnpackedHeader {
Kostya Kortchinsky1148dc52016-11-30 17:32:20 +000047 u64 Checksum : 16;
Kostya Kortchinsky47be0ed2016-12-15 18:06:55 +000048 u64 UnusedBytes : 20; // Needed for reallocation purposes.
Kostya Kortchinsky1148dc52016-11-30 17:32:20 +000049 u64 State : 2; // available, allocated, or quarantined
50 u64 AllocType : 2; // malloc, new, new[], or memalign
Kostya Kortchinsky47be0ed2016-12-15 18:06:55 +000051 u64 Offset : 16; // Offset from the beginning of the backend
Kostya Kortchinsky1148dc52016-11-30 17:32:20 +000052 // allocation to the beginning of the chunk itself,
53 // in multiples of MinAlignment. See comment about
54 // its maximum value and test in init().
55 u64 Salt : 8;
Kostya Kortchinsky71dcc332016-10-26 16:16:58 +000056};
Kostya Kortchinsky71dcc332016-10-26 16:16:58 +000057
Kostya Kortchinskya00b9222017-01-20 18:32:18 +000058typedef atomic_uint64_t AtomicPackedHeader;
Kostya Kortchinsky71dcc332016-10-26 16:16:58 +000059COMPILER_CHECK(sizeof(UnpackedHeader) == sizeof(PackedHeader));
60
Kostya Kortchinsky71dcc332016-10-26 16:16:58 +000061// Minimum alignment of 8 bytes for 32-bit, 16 for 64-bit
62const uptr MinAlignmentLog = FIRST_32_SECOND_64(3, 4);
63const uptr MaxAlignmentLog = 24; // 16 MB
64const uptr MinAlignment = 1 << MinAlignmentLog;
65const uptr MaxAlignment = 1 << MaxAlignmentLog;
66
Kostya Kortchinsky1148dc52016-11-30 17:32:20 +000067const uptr ChunkHeaderSize = sizeof(PackedHeader);
68const uptr AlignedChunkHeaderSize =
69 (ChunkHeaderSize + MinAlignment - 1) & ~(MinAlignment - 1);
70
Kostya Serebryany712fc982016-06-07 01:20:26 +000071struct AllocatorOptions {
72 u32 QuarantineSizeMb;
73 u32 ThreadLocalQuarantineSizeKb;
74 bool MayReturnNull;
Evgeniy Stepanovd3305af2016-11-29 00:22:50 +000075 s32 ReleaseToOSIntervalMs;
Kostya Serebryany712fc982016-06-07 01:20:26 +000076 bool DeallocationTypeMismatch;
77 bool DeleteSizeMismatch;
78 bool ZeroContents;
79
80 void setFrom(const Flags *f, const CommonFlags *cf);
81 void copyTo(Flags *f, CommonFlags *cf) const;
82};
83
84void initAllocator(const AllocatorOptions &options);
85void drainQuarantine();
86
87void *scudoMalloc(uptr Size, AllocType Type);
88void scudoFree(void *Ptr, AllocType Type);
89void scudoSizedFree(void *Ptr, uptr Size, AllocType Type);
90void *scudoRealloc(void *Ptr, uptr Size);
91void *scudoCalloc(uptr NMemB, uptr Size);
92void *scudoMemalign(uptr Alignment, uptr Size);
93void *scudoValloc(uptr Size);
94void *scudoPvalloc(uptr Size);
95int scudoPosixMemalign(void **MemPtr, uptr Alignment, uptr Size);
96void *scudoAlignedAlloc(uptr Alignment, uptr Size);
97uptr scudoMallocUsableSize(void *Ptr);
98
Kostya Kortchinsky71dcc332016-10-26 16:16:58 +000099#include "scudo_allocator_secondary.h"
100
Kostya Kortchinsky1148dc52016-11-30 17:32:20 +0000101} // namespace __scudo
Kostya Serebryany712fc982016-06-07 01:20:26 +0000102
103#endif // SCUDO_ALLOCATOR_H_