blob: f93f26ef122aada0f36e6ecd7901b0b18429d4cf [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
39// Tiny PRNG based on https://en.wikipedia.org/wiki/Xorshift#xorshift.2B
40// The state (128 bits) will be stored in thread local storage.
41struct Xorshift128Plus {
42 public:
43 Xorshift128Plus();
44 u64 Next() {
Kostya Kortchinsky1148dc52016-11-30 17:32:20 +000045 u64 x = State[0];
46 const u64 y = State[1];
47 State[0] = y;
Kostya Serebryany712fc982016-06-07 01:20:26 +000048 x ^= x << 23;
Kostya Kortchinsky1148dc52016-11-30 17:32:20 +000049 State[1] = x ^ y ^ (x >> 17) ^ (y >> 26);
50 return State[1] + y;
Kostya Serebryany712fc982016-06-07 01:20:26 +000051 }
52 private:
Kostya Kortchinsky1148dc52016-11-30 17:32:20 +000053 u64 State[2];
Kostya Serebryany712fc982016-06-07 01:20:26 +000054};
55
Kostya Kortchinsky1148dc52016-11-30 17:32:20 +000056// Software CRC32 functions, to be used when SSE 4.2 support is not detected.
57u32 computeCRC32(u32 Crc, uptr Data);
58
59} // namespace __scudo
Kostya Serebryany712fc982016-06-07 01:20:26 +000060
61#endif // SCUDO_UTILS_H_