1//===-- random.cpp ----------------------------------------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "gwp_asan/random.h"
10#include "gwp_asan/common.h"
11
12#include <time.h>
13
14// Initialised to a magic constant so that an uninitialised GWP-ASan won't
15// regenerate its sample counter for as long as possible. The xorshift32()
16// algorithm used below results in getRandomUnsigned32(0xff82eb50) ==
17// 0xfffffea4.
18GWP_ASAN_TLS_INITIAL_EXEC uint32_t RandomState = 0xff82eb50;
19
20namespace gwp_asan {
21void initPRNG() {
22  RandomState = time(nullptr) + getThreadID();
23}
24
25uint32_t getRandomUnsigned32() {
26  RandomState ^= RandomState << 13;
27  RandomState ^= RandomState >> 17;
28  RandomState ^= RandomState << 5;
29  return RandomState;
30}
31} // namespace gwp_asan
32