xref: /freebsd/contrib/llvm-project/compiler-rt/lib/scudo/standalone/chunk.h (revision 162ae9c834f6d9f9cb443bd62cceb23e0b5fef48)
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.cc. This means that other hardware specific
26   // instructions were likely emitted at other places, and as a result there is
27   // no reason to not use it here.
28 #if 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 & 0xffff) ^ (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 & 0xffff) ^ (Crc >> 16));
39   } else {
40     u16 Checksum = computeBSDChecksum(static_cast<u16>(Seed & 0xffff), Value);
41     for (uptr I = 0; I < ArraySize; I++)
42       Checksum = computeBSDChecksum(Checksum, Array[I]);
43     return Checksum;
44   }
45 #endif // 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   u64 Checksum : 16;
67   u64 ClassId : 8;
68   u64 SizeOrUnusedBytes : 20;
69   u8 State : 2;
70   u8 Origin : 2;
71   u64 Offset : 16;
72 };
73 typedef atomic_u64 AtomicPackedHeader;
74 COMPILER_CHECK(sizeof(UnpackedHeader) == sizeof(PackedHeader));
75 
76 // Those constants are required to silence some -Werror=conversion errors when
77 // assigning values to the related bitfield variables.
78 constexpr uptr ChecksumMask = (1UL << 16) - 1;
79 constexpr uptr ClassIdMask = (1UL << 8) - 1;
80 constexpr uptr SizeOrUnusedBytesMask = (1UL << 20) - 1;
81 constexpr uptr StateMask = (1UL << 2) - 1;
82 constexpr uptr OriginMask = (1UL << 2) - 1;
83 constexpr uptr OffsetMask = (1UL << 16) - 1;
84 
85 constexpr uptr getHeaderSize() {
86   return roundUpTo(sizeof(PackedHeader), 1U << SCUDO_MIN_ALIGNMENT_LOG);
87 }
88 
89 INLINE AtomicPackedHeader *getAtomicHeader(void *Ptr) {
90   return reinterpret_cast<AtomicPackedHeader *>(reinterpret_cast<uptr>(Ptr) -
91                                                 getHeaderSize());
92 }
93 
94 INLINE
95 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
122 void loadHeader(u32 Cookie, const void *Ptr,
123                 UnpackedHeader *NewUnpackedHeader) {
124   PackedHeader NewPackedHeader = atomic_load_relaxed(getConstAtomicHeader(Ptr));
125   *NewUnpackedHeader = bit_cast<UnpackedHeader>(NewPackedHeader);
126   if (UNLIKELY(NewUnpackedHeader->Checksum !=
127                computeHeaderChecksum(Cookie, Ptr, NewUnpackedHeader)))
128     reportHeaderCorruption(const_cast<void *>(Ptr));
129 }
130 
131 INLINE void compareExchangeHeader(u32 Cookie, void *Ptr,
132                                   UnpackedHeader *NewUnpackedHeader,
133                                   UnpackedHeader *OldUnpackedHeader) {
134   NewUnpackedHeader->Checksum =
135       computeHeaderChecksum(Cookie, Ptr, NewUnpackedHeader);
136   PackedHeader NewPackedHeader = bit_cast<PackedHeader>(*NewUnpackedHeader);
137   PackedHeader OldPackedHeader = bit_cast<PackedHeader>(*OldUnpackedHeader);
138   if (UNLIKELY(!atomic_compare_exchange_strong(
139           getAtomicHeader(Ptr), &OldPackedHeader, NewPackedHeader,
140           memory_order_relaxed)))
141     reportHeaderRace(Ptr);
142 }
143 
144 INLINE
145 bool isValid(u32 Cookie, const void *Ptr, UnpackedHeader *NewUnpackedHeader) {
146   PackedHeader NewPackedHeader = atomic_load_relaxed(getConstAtomicHeader(Ptr));
147   *NewUnpackedHeader = bit_cast<UnpackedHeader>(NewPackedHeader);
148   return NewUnpackedHeader->Checksum ==
149          computeHeaderChecksum(Cookie, Ptr, NewUnpackedHeader);
150 }
151 
152 } // namespace Chunk
153 
154 } // namespace scudo
155 
156 #endif // SCUDO_CHUNK_H_
157