summaryrefslogtreecommitdiff
path: root/lib/scudo/scudo_utils.h
blob: ef2a609671ac41f51100182f07d4dd7fbe23a4c4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
//===-- scudo_utils.h -------------------------------------------*- C++ -*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
///
/// Header for scudo_utils.cpp.
///
//===----------------------------------------------------------------------===//

#ifndef SCUDO_UTILS_H_
#define SCUDO_UTILS_H_

#include <string.h>

#include "sanitizer_common/sanitizer_common.h"

namespace __scudo {

template <class Dest, class Source>
inline Dest bit_cast(const Source& source) {
  static_assert(sizeof(Dest) == sizeof(Source), "Sizes are not equal!");
  Dest dest;
  memcpy(&dest, &source, sizeof(dest));
  return dest;
}

void NORETURN dieWithMessage(const char *Format, ...);

enum CPUFeature {
  CRC32CPUFeature = 0,
  MaxCPUFeature,
};
bool testCPUFeature(CPUFeature feature);

// Tiny PRNG based on https://en.wikipedia.org/wiki/Xorshift#xorshift.2B
// The state (128 bits) will be stored in thread local storage.
struct Xorshift128Plus {
 public:
  Xorshift128Plus();
  u64 Next() {
    u64 x = State[0];
    const u64 y = State[1];
    State[0] = y;
    x ^= x << 23;
    State[1] = x ^ y ^ (x >> 17) ^ (y >> 26);
    return State[1] + y;
  }
 private:
  u64 State[2];
};

// Software CRC32 functions, to be used when hardware support is not detected.
u32 computeSoftwareCRC32(u32 Crc, uptr Data);

}  // namespace __scudo

#endif  // SCUDO_UTILS_H_