]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/compiler-rt/lib/scudo/scudo_utils.h
Update our copy of DTS from the ones from Linux 4.14
[FreeBSD/FreeBSD.git] / contrib / compiler-rt / lib / scudo / scudo_utils.h
1 //===-- scudo_utils.h -------------------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 ///
10 /// Header for scudo_utils.cpp.
11 ///
12 //===----------------------------------------------------------------------===//
13
14 #ifndef SCUDO_UTILS_H_
15 #define SCUDO_UTILS_H_
16
17 #include <string.h>
18
19 #include "sanitizer_common/sanitizer_common.h"
20
21 namespace __scudo {
22
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!");
26   Dest dest;
27   memcpy(&dest, &source, sizeof(dest));
28   return dest;
29 }
30
31 void NORETURN dieWithMessage(const char *Format, ...);
32
33 enum CPUFeature {
34   CRC32CPUFeature = 0,
35   MaxCPUFeature,
36 };
37 bool testCPUFeature(CPUFeature feature);
38
39 INLINE u64 rotl(const u64 X, int K) {
40   return (X << K) | (X >> (64 - K));
41 }
42
43 // XoRoShiRo128+ PRNG (http://xoroshiro.di.unimi.it/).
44 struct XoRoShiRo128Plus {
45  public:
46   void init() {
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();
53       State[1] = 0;
54     }
55     fillCache();
56   }
57   u8 getU8() {
58     if (UNLIKELY(isCacheEmpty()))
59       fillCache();
60     const u8 Result = static_cast<u8>(CachedBytes & 0xff);
61     CachedBytes >>= 8;
62     CachedBytesAvailable--;
63     return Result;
64   }
65   u64 getU64() { return next(); }
66
67  private:
68   u8 CachedBytesAvailable;
69   u64 CachedBytes;
70   u64 State[2];
71   u64 next() {
72     const u64 S0 = State[0];
73     u64 S1 = State[1];
74     const u64 Result = S0 + S1;
75     S1 ^= S0;
76     State[0] = rotl(S0, 55) ^ S1 ^ (S1 << 14);
77     State[1] = rotl(S1, 36);
78     return Result;
79   }
80   bool isCacheEmpty() {
81     return CachedBytesAvailable == 0;
82   }
83   void fillCache() {
84     CachedBytes = next();
85     CachedBytesAvailable = sizeof(CachedBytes);
86   }
87 };
88
89 typedef XoRoShiRo128Plus ScudoPrng;
90
91 }  // namespace __scudo
92
93 #endif  // SCUDO_UTILS_H_