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