1 //===-- scudo_utils.h -------------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 /// Header for scudo_utils.cpp.
12 //===----------------------------------------------------------------------===//
14 #ifndef SCUDO_UTILS_H_
15 #define SCUDO_UTILS_H_
19 #include "sanitizer_common/sanitizer_common.h"
23 template <class Dest, class Source>
24 inline Dest bit_cast(const Source& source) {
25 static_assert(sizeof(Dest) == sizeof(Source), "Sizes are not equal!");
27 memcpy(&dest, &source, sizeof(dest));
31 void NORETURN dieWithMessage(const char *Format, ...);
37 bool testCPUFeature(CPUFeature feature);
39 INLINE u64 rotl(const u64 X, int K) {
40 return (X << K) | (X >> (64 - K));
43 // XoRoShiRo128+ PRNG (http://xoroshiro.di.unimi.it/).
44 struct XoRoShiRo128Plus {
47 if (UNLIKELY(!GetRandom(reinterpret_cast<void *>(State), sizeof(State)))) {
48 // Early processes (eg: init) do not have /dev/urandom yet, but we still
49 // have to provide them with some degree of entropy. Not having a secure
50 // seed is not as problematic for them, as they are less likely to be
51 // the target of heap based vulnerabilities exploitation attempts.
52 State[0] = NanoTime();
58 if (UNLIKELY(isCacheEmpty()))
60 const u8 Result = static_cast<u8>(CachedBytes & 0xff);
62 CachedBytesAvailable--;
65 u64 getU64() { return next(); }
68 u8 CachedBytesAvailable;
72 const u64 S0 = State[0];
74 const u64 Result = S0 + S1;
76 State[0] = rotl(S0, 55) ^ S1 ^ (S1 << 14);
77 State[1] = rotl(S1, 36);
81 return CachedBytesAvailable == 0;
85 CachedBytesAvailable = sizeof(CachedBytes);
89 typedef XoRoShiRo128Plus ScudoPrng;
91 } // namespace __scudo
93 #endif // SCUDO_UTILS_H_