1 //===-- common.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_COMMON_H_
10 #define SCUDO_COMMON_H_
11
12 #include "internal_defs.h"
13
14 #include "fuchsia.h"
15 #include "linux.h"
16
17 #include <stddef.h>
18 #include <string.h>
19
20 namespace scudo {
21
bit_cast(const Source & S)22 template <class Dest, class Source> inline Dest bit_cast(const Source &S) {
23 static_assert(sizeof(Dest) == sizeof(Source), "");
24 Dest D;
25 memcpy(&D, &S, sizeof(D));
26 return D;
27 }
28
roundUpTo(uptr X,uptr Boundary)29 inline constexpr uptr roundUpTo(uptr X, uptr Boundary) {
30 return (X + Boundary - 1) & ~(Boundary - 1);
31 }
32
roundDownTo(uptr X,uptr Boundary)33 inline constexpr uptr roundDownTo(uptr X, uptr Boundary) {
34 return X & ~(Boundary - 1);
35 }
36
isAligned(uptr X,uptr Alignment)37 inline constexpr bool isAligned(uptr X, uptr Alignment) {
38 return (X & (Alignment - 1)) == 0;
39 }
40
Min(T A,T B)41 template <class T> constexpr T Min(T A, T B) { return A < B ? A : B; }
42
Max(T A,T B)43 template <class T> constexpr T Max(T A, T B) { return A > B ? A : B; }
44
Swap(T & A,T & B)45 template <class T> void Swap(T &A, T &B) {
46 T Tmp = A;
47 A = B;
48 B = Tmp;
49 }
50
isPowerOfTwo(uptr X)51 inline bool isPowerOfTwo(uptr X) { return (X & (X - 1)) == 0; }
52
getMostSignificantSetBitIndex(uptr X)53 inline uptr getMostSignificantSetBitIndex(uptr X) {
54 DCHECK_NE(X, 0U);
55 return SCUDO_WORDSIZE - 1U - static_cast<uptr>(__builtin_clzl(X));
56 }
57
roundUpToPowerOfTwo(uptr Size)58 inline uptr roundUpToPowerOfTwo(uptr Size) {
59 DCHECK(Size);
60 if (isPowerOfTwo(Size))
61 return Size;
62 const uptr Up = getMostSignificantSetBitIndex(Size);
63 DCHECK_LT(Size, (1UL << (Up + 1)));
64 DCHECK_GT(Size, (1UL << Up));
65 return 1UL << (Up + 1);
66 }
67
getLeastSignificantSetBitIndex(uptr X)68 inline uptr getLeastSignificantSetBitIndex(uptr X) {
69 DCHECK_NE(X, 0U);
70 return static_cast<uptr>(__builtin_ctzl(X));
71 }
72
getLog2(uptr X)73 inline uptr getLog2(uptr X) {
74 DCHECK(isPowerOfTwo(X));
75 return getLeastSignificantSetBitIndex(X);
76 }
77
getRandomU32(u32 * State)78 inline u32 getRandomU32(u32 *State) {
79 // ANSI C linear congruential PRNG (16-bit output).
80 // return (*State = *State * 1103515245 + 12345) >> 16;
81 // XorShift (32-bit output).
82 *State ^= *State << 13;
83 *State ^= *State >> 17;
84 *State ^= *State << 5;
85 return *State;
86 }
87
getRandomModN(u32 * State,u32 N)88 inline u32 getRandomModN(u32 *State, u32 N) {
89 return getRandomU32(State) % N; // [0, N)
90 }
91
shuffle(T * A,u32 N,u32 * RandState)92 template <typename T> inline void shuffle(T *A, u32 N, u32 *RandState) {
93 if (N <= 1)
94 return;
95 u32 State = *RandState;
96 for (u32 I = N - 1; I > 0; I--)
97 Swap(A[I], A[getRandomModN(&State, I + 1)]);
98 *RandState = State;
99 }
100
101 // Hardware specific inlinable functions.
102
yieldProcessor(u8 Count)103 inline void yieldProcessor(u8 Count) {
104 #if defined(__i386__) || defined(__x86_64__)
105 __asm__ __volatile__("" ::: "memory");
106 for (u8 I = 0; I < Count; I++)
107 __asm__ __volatile__("pause");
108 #elif defined(__aarch64__) || defined(__arm__)
109 __asm__ __volatile__("" ::: "memory");
110 for (u8 I = 0; I < Count; I++)
111 __asm__ __volatile__("yield");
112 #endif
113 __asm__ __volatile__("" ::: "memory");
114 }
115
116 // Platform specific functions.
117
118 extern uptr PageSizeCached;
119 uptr getPageSizeSlow();
getPageSizeCached()120 inline uptr getPageSizeCached() {
121 // Bionic uses a hardcoded value.
122 if (SCUDO_ANDROID)
123 return 4096U;
124 if (LIKELY(PageSizeCached))
125 return PageSizeCached;
126 return getPageSizeSlow();
127 }
128
129 // Returns 0 if the number of CPUs could not be determined.
130 u32 getNumberOfCPUs();
131
132 const char *getEnv(const char *Name);
133
134 u64 getMonotonicTime();
135
136 // Our randomness gathering function is limited to 256 bytes to ensure we get
137 // as many bytes as requested, and avoid interruptions (on Linux).
138 constexpr uptr MaxRandomLength = 256U;
139 bool getRandom(void *Buffer, uptr Length, bool Blocking = false);
140
141 // Platform memory mapping functions.
142
143 #define MAP_ALLOWNOMEM (1U << 0)
144 #define MAP_NOACCESS (1U << 1)
145 #define MAP_RESIZABLE (1U << 2)
146 #define MAP_MEMTAG (1U << 3)
147
148 // Our platform memory mapping use is restricted to 3 scenarios:
149 // - reserve memory at a random address (MAP_NOACCESS);
150 // - commit memory in a previously reserved space;
151 // - commit memory at a random address.
152 // As such, only a subset of parameters combinations is valid, which is checked
153 // by the function implementation. The Data parameter allows to pass opaque
154 // platform specific data to the function.
155 // Returns nullptr on error or dies if MAP_ALLOWNOMEM is not specified.
156 void *map(void *Addr, uptr Size, const char *Name, uptr Flags = 0,
157 MapPlatformData *Data = nullptr);
158
159 // Indicates that we are getting rid of the whole mapping, which might have
160 // further consequences on Data, depending on the platform.
161 #define UNMAP_ALL (1U << 0)
162
163 void unmap(void *Addr, uptr Size, uptr Flags = 0,
164 MapPlatformData *Data = nullptr);
165
166 void releasePagesToOS(uptr BaseAddress, uptr Offset, uptr Size,
167 MapPlatformData *Data = nullptr);
168
169 // Internal map & unmap fatal error. This must not call map().
170 void NORETURN dieOnMapUnmapError(bool OutOfMemory = false);
171
172 // Logging related functions.
173
174 void setAbortMessage(const char *Message);
175
176 enum class Option : u8 {
177 ReleaseInterval, // Release to OS interval in milliseconds.
178 MemtagTuning, // Whether to tune tagging for UAF or overflow.
179 MaxCacheEntriesCount, // Maximum number of blocks that can be cached.
180 MaxCacheEntrySize, // Maximum size of a block that can be cached.
181 MaxTSDsCount, // Number of usable TSDs for the shared registry.
182 };
183
184 } // namespace scudo
185
186 #endif // SCUDO_COMMON_H_
187