blob: 6c6c9d893404515669a629dca19c4c36f5ece1c9 [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
17#include <string.h>
18
19#include "sanitizer_common/sanitizer_common.h"
20
21namespace __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};
37bool testCPUFeature(CPUFeature feature);
38
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() {
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();
Kostya Serebryany712fc982016-06-07 01:20:26 +000056 }
Kostya Kortchinsky00582562017-07-12 15:29:08 +000057 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
Kostya Serebryany712fc982016-06-07 01:20:26 +000067 private:
Kostya Kortchinsky00582562017-07-12 15:29:08 +000068 u8 CachedBytesAvailable;
69 u64 CachedBytes;
Kostya Kortchinsky1148dc52016-11-30 17:32:20 +000070 u64 State[2];
Kostya Kortchinsky00582562017-07-12 15:29:08 +000071 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 }
Kostya Serebryany712fc982016-06-07 01:20:26 +000087};
88
Kostya Kortchinsky00582562017-07-12 15:29:08 +000089typedef XoRoShiRo128Plus ScudoPrng;
90
Kostya Kortchinsky1148dc52016-11-30 17:32:20 +000091} // namespace __scudo
Kostya Serebryany712fc982016-06-07 01:20:26 +000092
93#endif // SCUDO_UTILS_H_