blob: 33798194d618e1562becfe5fd1563b32773aacdd [file] [log] [blame]
Kostya Serebryany712fc982016-06-07 01:20:26 +00001//===-- 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
Kostya Serebryany712fc982016-06-07 01:20:26 +000017#include "sanitizer_common/sanitizer_common.h"
18
Kostya Kortchinsky4a0ebbf2017-11-03 23:48:25 +000019#include <string.h>
20
Kostya Serebryany712fc982016-06-07 01:20:26 +000021namespace __scudo {
22
23template <class Dest, class Source>
Kostya Kortchinsky0207b6f2017-11-22 18:30:44 +000024INLINE Dest bit_cast(const Source& source) {
Kostya Serebryany712fc982016-06-07 01:20:26 +000025 static_assert(sizeof(Dest) == sizeof(Source), "Sizes are not equal!");
26 Dest dest;
27 memcpy(&dest, &source, sizeof(dest));
28 return dest;
29}
30
Kostya Serebryany8b4904f2016-08-02 23:23:13 +000031void NORETURN dieWithMessage(const char *Format, ...);
Kostya Serebryany712fc982016-06-07 01:20:26 +000032
Kostya Kortchinsky0207b6f2017-11-22 18:30:44 +000033bool hasHardwareCRC32();
Kostya Serebryany712fc982016-06-07 01:20:26 +000034
Kostya Kortchinsky00582562017-07-12 15:29:08 +000035INLINE u64 rotl(const u64 X, int K) {
36 return (X << K) | (X >> (64 - K));
37}
38
39// XoRoShiRo128+ PRNG (http://xoroshiro.di.unimi.it/).
40struct XoRoShiRo128Plus {
Kostya Serebryany712fc982016-06-07 01:20:26 +000041 public:
Kostya Kortchinsky00582562017-07-12 15:29:08 +000042 void init() {
Kostya Kortchinskye1dde072017-08-14 14:53:47 +000043 if (UNLIKELY(!GetRandom(reinterpret_cast<void *>(State), sizeof(State),
44 /*blocking=*/false))) {
45 // On some platforms, early processes like `init` do not have an
46 // initialized random pool (getrandom blocks and /dev/urandom doesn't
47 // exist yet), but we still have to provide them with some degree of
48 // entropy. Not having a secure seed is not as problematic for them, as
49 // they are less likely to be the target of heap based vulnerabilities
50 // exploitation attempts.
Kostya Kortchinsky00582562017-07-12 15:29:08 +000051 State[0] = NanoTime();
52 State[1] = 0;
53 }
54 fillCache();
Kostya Serebryany712fc982016-06-07 01:20:26 +000055 }
Kostya Kortchinsky00582562017-07-12 15:29:08 +000056 u8 getU8() {
57 if (UNLIKELY(isCacheEmpty()))
58 fillCache();
59 const u8 Result = static_cast<u8>(CachedBytes & 0xff);
60 CachedBytes >>= 8;
61 CachedBytesAvailable--;
62 return Result;
63 }
64 u64 getU64() { return next(); }
65
Kostya Serebryany712fc982016-06-07 01:20:26 +000066 private:
Kostya Kortchinsky00582562017-07-12 15:29:08 +000067 u8 CachedBytesAvailable;
68 u64 CachedBytes;
Kostya Kortchinsky1148dc52016-11-30 17:32:20 +000069 u64 State[2];
Kostya Kortchinsky00582562017-07-12 15:29:08 +000070 u64 next() {
71 const u64 S0 = State[0];
72 u64 S1 = State[1];
73 const u64 Result = S0 + S1;
74 S1 ^= S0;
75 State[0] = rotl(S0, 55) ^ S1 ^ (S1 << 14);
76 State[1] = rotl(S1, 36);
77 return Result;
78 }
79 bool isCacheEmpty() {
80 return CachedBytesAvailable == 0;
81 }
82 void fillCache() {
83 CachedBytes = next();
84 CachedBytesAvailable = sizeof(CachedBytes);
85 }
Kostya Serebryany712fc982016-06-07 01:20:26 +000086};
87
Kostya Kortchinsky00582562017-07-12 15:29:08 +000088typedef XoRoShiRo128Plus ScudoPrng;
89
Kostya Kortchinsky1148dc52016-11-30 17:32:20 +000090} // namespace __scudo
Kostya Serebryany712fc982016-06-07 01:20:26 +000091
92#endif // SCUDO_UTILS_H_