1 // Copyright 2013 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "utils/random-number-generator.h"
11 #include "platform/mutex.h"
12 #include "platform/time.h"
18 static LazyMutex entropy_mutex = LAZY_MUTEX_INITIALIZER;
19 static RandomNumberGenerator::EntropySource entropy_source = NULL;
23 void RandomNumberGenerator::SetEntropySource(EntropySource source) {
24 LockGuard<Mutex> lock_guard(entropy_mutex.Pointer());
25 entropy_source = source;
29 RandomNumberGenerator::RandomNumberGenerator() {
30 // Check --random-seed flag first.
31 if (FLAG_random_seed != 0) {
32 SetSeed(FLAG_random_seed);
36 // Check if embedder supplied an entropy source.
37 { LockGuard<Mutex> lock_guard(entropy_mutex.Pointer());
38 if (entropy_source != NULL) {
40 if (entropy_source(reinterpret_cast<unsigned char*>(&seed),
48 #if V8_OS_CYGWIN || V8_OS_WIN
49 // Use rand_s() to gather entropy on Windows. See:
50 // https://code.google.com/p/v8/issues/detail?id=2905
51 unsigned first_half, second_half;
52 errno_t result = rand_s(&first_half);
54 result = rand_s(&second_half);
56 SetSeed((static_cast<int64_t>(first_half) << 32) + second_half);
58 // Gather entropy from /dev/urandom if available.
59 FILE* fp = fopen("/dev/urandom", "rb");
62 size_t n = fread(&seed, sizeof(seed), 1, fp);
70 // We cannot assume that random() or rand() were seeded
71 // properly, so instead of relying on random() or rand(),
72 // we just seed our PRNG using timing data as fallback.
73 // This is weak entropy, but it's sufficient, because
74 // it is the responsibility of the embedder to install
75 // an entropy source using v8::V8::SetEntropySource(),
76 // which provides reasonable entropy, see:
77 // https://code.google.com/p/v8/issues/detail?id=2905
78 int64_t seed = Time::NowFromSystemTime().ToInternalValue() << 24;
79 seed ^= TimeTicks::HighResolutionNow().ToInternalValue() << 16;
80 seed ^= TimeTicks::Now().ToInternalValue() << 8;
82 #endif // V8_OS_CYGWIN || V8_OS_WIN
86 int RandomNumberGenerator::NextInt(int max) {
89 // Fast path if max is a power of 2.
90 if (IsPowerOf2(max)) {
91 return static_cast<int>((max * static_cast<int64_t>(Next(31))) >> 31);
97 if (rnd - val + (max - 1) >= 0) {
104 double RandomNumberGenerator::NextDouble() {
105 return ((static_cast<int64_t>(Next(26)) << 27) + Next(27)) /
106 static_cast<double>(static_cast<int64_t>(1) << 53);
110 void RandomNumberGenerator::NextBytes(void* buffer, size_t buflen) {
111 for (size_t n = 0; n < buflen; ++n) {
112 static_cast<uint8_t*>(buffer)[n] = static_cast<uint8_t>(Next(8));
117 int RandomNumberGenerator::Next(int bits) {
120 int64_t seed = (seed_ * kMultiplier + kAddend) & kMask;
122 return static_cast<int>(seed >> (48 - bits));
126 void RandomNumberGenerator::SetSeed(int64_t seed) {
127 seed_ = (seed ^ kMultiplier) & kMask;
130 } } // namespace v8::internal