blob: cb7300db91868835ee582f3583d94583d099b04f [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>
24inline 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
Kostya Serebryany8b4904f2016-08-02 23:23:13 +000031void NORETURN dieWithMessage(const char *Format, ...);
Kostya Serebryany712fc982016-06-07 01:20:26 +000032
Kostya Kortchinsky1148dc52016-11-30 17:32:20 +000033enum CPUFeature {
34 CRC32CPUFeature = 0,
35 MaxCPUFeature,
Kostya Serebryany712fc982016-06-07 01:20:26 +000036};
Kostya Kortchinsky4a0ebbf2017-11-03 23:48:25 +000037bool testCPUFeature(CPUFeature Feature);
Kostya Serebryany712fc982016-06-07 01:20:26 +000038
Kostya Kortchinsky00582562017-07-12 15:29:08 +000039INLINE u64 rotl(const u64 X, int K) {
40 return (X << K) | (X >> (64 - K));
41}
42
43// XoRoShiRo128+ PRNG (http://xoroshiro.di.unimi.it/).
44struct XoRoShiRo128Plus {
Kostya Serebryany712fc982016-06-07 01:20:26 +000045 public:
Kostya Kortchinsky00582562017-07-12 15:29:08 +000046 void init() {
Kostya Kortchinskye1dde072017-08-14 14:53:47 +000047 if (UNLIKELY(!GetRandom(reinterpret_cast<void *>(State), sizeof(State),
48 /*blocking=*/false))) {
49 // On some platforms, early processes like `init` do not have an
50 // initialized random pool (getrandom blocks and /dev/urandom doesn't
51 // exist yet), but we still have to provide them with some degree of
52 // entropy. Not having a secure seed is not as problematic for them, as
53 // they are less likely to be the target of heap based vulnerabilities
54 // exploitation attempts.
Kostya Kortchinsky00582562017-07-12 15:29:08 +000055 State[0] = NanoTime();
56 State[1] = 0;
57 }
58 fillCache();
Kostya Serebryany712fc982016-06-07 01:20:26 +000059 }
Kostya Kortchinsky00582562017-07-12 15:29:08 +000060 u8 getU8() {
61 if (UNLIKELY(isCacheEmpty()))
62 fillCache();
63 const u8 Result = static_cast<u8>(CachedBytes & 0xff);
64 CachedBytes >>= 8;
65 CachedBytesAvailable--;
66 return Result;
67 }
68 u64 getU64() { return next(); }
69
Kostya Serebryany712fc982016-06-07 01:20:26 +000070 private:
Kostya Kortchinsky00582562017-07-12 15:29:08 +000071 u8 CachedBytesAvailable;
72 u64 CachedBytes;
Kostya Kortchinsky1148dc52016-11-30 17:32:20 +000073 u64 State[2];
Kostya Kortchinsky00582562017-07-12 15:29:08 +000074 u64 next() {
75 const u64 S0 = State[0];
76 u64 S1 = State[1];
77 const u64 Result = S0 + S1;
78 S1 ^= S0;
79 State[0] = rotl(S0, 55) ^ S1 ^ (S1 << 14);
80 State[1] = rotl(S1, 36);
81 return Result;
82 }
83 bool isCacheEmpty() {
84 return CachedBytesAvailable == 0;
85 }
86 void fillCache() {
87 CachedBytes = next();
88 CachedBytesAvailable = sizeof(CachedBytes);
89 }
Kostya Serebryany712fc982016-06-07 01:20:26 +000090};
91
Kostya Kortchinsky00582562017-07-12 15:29:08 +000092typedef XoRoShiRo128Plus ScudoPrng;
93
Kostya Kortchinsky1148dc52016-11-30 17:32:20 +000094} // namespace __scudo
Kostya Serebryany712fc982016-06-07 01:20:26 +000095
96#endif // SCUDO_UTILS_H_