xref: /freebsd/contrib/llvm-project/compiler-rt/lib/scudo/standalone/chunk.h (revision d5b0e70f7e04d971691517ce1304d86a1e367e2e)
1 //===-- chunk.h -------------------------------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef SCUDO_CHUNK_H_
10 #define SCUDO_CHUNK_H_
11 
12 #include "platform.h"
13 
14 #include "atomic_helpers.h"
15 #include "checksum.h"
16 #include "common.h"
17 #include "report.h"
18 
19 namespace scudo {
20 
21 extern Checksum HashAlgorithm;
22 
23 inline u16 computeChecksum(u32 Seed, uptr Value, uptr *Array, uptr ArraySize) {
24   // If the hardware CRC32 feature is defined here, it was enabled everywhere,
25   // as opposed to only for crc32_hw.cpp. This means that other hardware
26   // specific instructions were likely emitted at other places, and as a result
27   // there is no reason to not use it here.
28 #if defined(__CRC32__) || defined(__SSE4_2__) || defined(__ARM_FEATURE_CRC32)
29   u32 Crc = static_cast<u32>(CRC32_INTRINSIC(Seed, Value));
30   for (uptr I = 0; I < ArraySize; I++)
31     Crc = static_cast<u32>(CRC32_INTRINSIC(Crc, Array[I]));
32   return static_cast<u16>(Crc ^ (Crc >> 16));
33 #else
34   if (HashAlgorithm == Checksum::HardwareCRC32) {
35     u32 Crc = computeHardwareCRC32(Seed, Value);
36     for (uptr I = 0; I < ArraySize; I++)
37       Crc = computeHardwareCRC32(Crc, Array[I]);
38     return static_cast<u16>(Crc ^ (Crc >> 16));
39   } else {
40     u16 Checksum = computeBSDChecksum(static_cast<u16>(Seed), Value);
41     for (uptr I = 0; I < ArraySize; I++)
42       Checksum = computeBSDChecksum(Checksum, Array[I]);
43     return Checksum;
44   }
45 #endif // defined(__CRC32__) || defined(__SSE4_2__) || defined(__ARM_FEATURE_CRC32)
46 }
47 
48 namespace Chunk {
49 
50 // Note that in an ideal world, `State` and `Origin` should be `enum class`, and
51 // the associated `UnpackedHeader` fields of their respective enum class type
52 // but https://gcc.gnu.org/bugzilla/show_bug.cgi?id=61414 prevents it from
53 // happening, as it will error, complaining the number of bits is not enough.
54 enum Origin : u8 {
55   Malloc = 0,
56   New = 1,
57   NewArray = 2,
58   Memalign = 3,
59 };
60 
61 enum State : u8 { Available = 0, Allocated = 1, Quarantined = 2 };
62 
63 typedef u64 PackedHeader;
64 // Update the 'Mask' constants to reflect changes in this structure.
65 struct UnpackedHeader {
66   uptr ClassId : 8;
67   u8 State : 2;
68   // Origin if State == Allocated, or WasZeroed otherwise.
69   u8 OriginOrWasZeroed : 2;
70   uptr SizeOrUnusedBytes : 20;
71   uptr Offset : 16;
72   uptr Checksum : 16;
73 };
74 typedef atomic_u64 AtomicPackedHeader;
75 static_assert(sizeof(UnpackedHeader) == sizeof(PackedHeader), "");
76 
77 // Those constants are required to silence some -Werror=conversion errors when
78 // assigning values to the related bitfield variables.
79 constexpr uptr ClassIdMask = (1UL << 8) - 1;
80 constexpr u8 StateMask = (1U << 2) - 1;
81 constexpr u8 OriginMask = (1U << 2) - 1;
82 constexpr uptr SizeOrUnusedBytesMask = (1UL << 20) - 1;
83 constexpr uptr OffsetMask = (1UL << 16) - 1;
84 constexpr uptr ChecksumMask = (1UL << 16) - 1;
85 
86 constexpr uptr getHeaderSize() {
87   return roundUpTo(sizeof(PackedHeader), 1U << SCUDO_MIN_ALIGNMENT_LOG);
88 }
89 
90 inline AtomicPackedHeader *getAtomicHeader(void *Ptr) {
91   return reinterpret_cast<AtomicPackedHeader *>(reinterpret_cast<uptr>(Ptr) -
92                                                 getHeaderSize());
93 }
94 
95 inline const AtomicPackedHeader *getConstAtomicHeader(const void *Ptr) {
96   return reinterpret_cast<const AtomicPackedHeader *>(
97       reinterpret_cast<uptr>(Ptr) - getHeaderSize());
98 }
99 
100 // We do not need a cryptographically strong hash for the checksum, but a CRC
101 // type function that can alert us in the event a header is invalid or
102 // corrupted. Ideally slightly better than a simple xor of all fields.
103 static inline u16 computeHeaderChecksum(u32 Cookie, const void *Ptr,
104                                         UnpackedHeader *Header) {
105   UnpackedHeader ZeroChecksumHeader = *Header;
106   ZeroChecksumHeader.Checksum = 0;
107   uptr HeaderHolder[sizeof(UnpackedHeader) / sizeof(uptr)];
108   memcpy(&HeaderHolder, &ZeroChecksumHeader, sizeof(HeaderHolder));
109   return computeChecksum(Cookie, reinterpret_cast<uptr>(Ptr), HeaderHolder,
110                          ARRAY_SIZE(HeaderHolder));
111 }
112 
113 inline void storeHeader(u32 Cookie, void *Ptr,
114                         UnpackedHeader *NewUnpackedHeader) {
115   NewUnpackedHeader->Checksum =
116       computeHeaderChecksum(Cookie, Ptr, NewUnpackedHeader);
117   PackedHeader NewPackedHeader = bit_cast<PackedHeader>(*NewUnpackedHeader);
118   atomic_store_relaxed(getAtomicHeader(Ptr), NewPackedHeader);
119 }
120 
121 inline void loadHeader(u32 Cookie, const void *Ptr,
122                        UnpackedHeader *NewUnpackedHeader) {
123   PackedHeader NewPackedHeader = atomic_load_relaxed(getConstAtomicHeader(Ptr));
124   *NewUnpackedHeader = bit_cast<UnpackedHeader>(NewPackedHeader);
125   if (UNLIKELY(NewUnpackedHeader->Checksum !=
126                computeHeaderChecksum(Cookie, Ptr, NewUnpackedHeader)))
127     reportHeaderCorruption(const_cast<void *>(Ptr));
128 }
129 
130 inline void compareExchangeHeader(u32 Cookie, void *Ptr,
131                                   UnpackedHeader *NewUnpackedHeader,
132                                   UnpackedHeader *OldUnpackedHeader) {
133   NewUnpackedHeader->Checksum =
134       computeHeaderChecksum(Cookie, Ptr, NewUnpackedHeader);
135   PackedHeader NewPackedHeader = bit_cast<PackedHeader>(*NewUnpackedHeader);
136   PackedHeader OldPackedHeader = bit_cast<PackedHeader>(*OldUnpackedHeader);
137   if (UNLIKELY(!atomic_compare_exchange_strong(
138           getAtomicHeader(Ptr), &OldPackedHeader, NewPackedHeader,
139           memory_order_relaxed)))
140     reportHeaderRace(Ptr);
141 }
142 
143 inline bool isValid(u32 Cookie, const void *Ptr,
144                     UnpackedHeader *NewUnpackedHeader) {
145   PackedHeader NewPackedHeader = atomic_load_relaxed(getConstAtomicHeader(Ptr));
146   *NewUnpackedHeader = bit_cast<UnpackedHeader>(NewPackedHeader);
147   return NewUnpackedHeader->Checksum ==
148          computeHeaderChecksum(Cookie, Ptr, NewUnpackedHeader);
149 }
150 
151 } // namespace Chunk
152 
153 } // namespace scudo
154 
155 #endif // SCUDO_CHUNK_H_
156