V8 Project
random-number-generator.cc
Go to the documentation of this file.
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.
4 
6 
7 #include <stdio.h>
8 #include <stdlib.h>
9 
10 #include <new>
11 
12 #include "src/base/macros.h"
14 #include "src/base/platform/time.h"
15 
16 namespace v8 {
17 namespace base {
18 
21 
22 
23 // static
24 void RandomNumberGenerator::SetEntropySource(EntropySource source) {
25  LockGuard<Mutex> lock_guard(entropy_mutex.Pointer());
26  entropy_source = source;
27 }
28 
29 
30 RandomNumberGenerator::RandomNumberGenerator() {
31  // Check if embedder supplied an entropy source.
32  { LockGuard<Mutex> lock_guard(entropy_mutex.Pointer());
33  if (entropy_source != NULL) {
34  int64_t seed;
35  if (entropy_source(reinterpret_cast<unsigned char*>(&seed),
36  sizeof(seed))) {
37  SetSeed(seed);
38  return;
39  }
40  }
41  }
42 
43 #if V8_OS_CYGWIN || V8_OS_WIN
44  // Use rand_s() to gather entropy on Windows. See:
45  // https://code.google.com/p/v8/issues/detail?id=2905
46  unsigned first_half, second_half;
47  errno_t result = rand_s(&first_half);
48  DCHECK_EQ(0, result);
49  result = rand_s(&second_half);
50  DCHECK_EQ(0, result);
51  SetSeed((static_cast<int64_t>(first_half) << 32) + second_half);
52 #else
53  // Gather entropy from /dev/urandom if available.
54  FILE* fp = fopen("/dev/urandom", "rb");
55  if (fp != NULL) {
56  int64_t seed;
57  size_t n = fread(&seed, sizeof(seed), 1, fp);
58  fclose(fp);
59  if (n == 1) {
60  SetSeed(seed);
61  return;
62  }
63  }
64 
65  // We cannot assume that random() or rand() were seeded
66  // properly, so instead of relying on random() or rand(),
67  // we just seed our PRNG using timing data as fallback.
68  // This is weak entropy, but it's sufficient, because
69  // it is the responsibility of the embedder to install
70  // an entropy source using v8::V8::SetEntropySource(),
71  // which provides reasonable entropy, see:
72  // https://code.google.com/p/v8/issues/detail?id=2905
73  int64_t seed = Time::NowFromSystemTime().ToInternalValue() << 24;
74  seed ^= TimeTicks::HighResolutionNow().ToInternalValue() << 16;
75  seed ^= TimeTicks::Now().ToInternalValue() << 8;
76  SetSeed(seed);
77 #endif // V8_OS_CYGWIN || V8_OS_WIN
78 }
79 
80 
81 int RandomNumberGenerator::NextInt(int max) {
82  DCHECK_LT(0, max);
83 
84  // Fast path if max is a power of 2.
85  if (IS_POWER_OF_TWO(max)) {
86  return static_cast<int>((max * static_cast<int64_t>(Next(31))) >> 31);
87  }
88 
89  while (true) {
90  int rnd = Next(31);
91  int val = rnd % max;
92  if (rnd - val + (max - 1) >= 0) {
93  return val;
94  }
95  }
96 }
97 
98 
99 double RandomNumberGenerator::NextDouble() {
100  return ((static_cast<int64_t>(Next(26)) << 27) + Next(27)) /
101  static_cast<double>(static_cast<int64_t>(1) << 53);
102 }
103 
104 
105 void RandomNumberGenerator::NextBytes(void* buffer, size_t buflen) {
106  for (size_t n = 0; n < buflen; ++n) {
107  static_cast<uint8_t*>(buffer)[n] = static_cast<uint8_t>(Next(8));
108  }
109 }
110 
111 
112 int RandomNumberGenerator::Next(int bits) {
113  DCHECK_LT(0, bits);
114  DCHECK_GE(32, bits);
115  // Do unsigned multiplication, which has the intended modulo semantics, while
116  // signed multiplication would expose undefined behavior.
117  uint64_t product = static_cast<uint64_t>(seed_) * kMultiplier;
118  // Assigning a uint64_t to an int64_t is implementation defined, but this
119  // should be OK. Use a static_cast to explicitly state that we know what we're
120  // doing. (Famous last words...)
121  int64_t seed = static_cast<int64_t>((product + kAddend) & kMask);
122  seed_ = seed;
123  return static_cast<int>(seed >> (48 - bits));
124 }
125 
126 
127 void RandomNumberGenerator::SetSeed(int64_t seed) {
128  initial_seed_ = seed;
129  seed_ = (seed ^ kMultiplier) & kMask;
130 }
131 
132 } } // namespace v8::base
enable harmony numeric enable harmony object literal extensions Optimize object Array DOM strings and string trace pretenuring decisions of HAllocate instructions Enables optimizations which favor memory size over execution speed maximum source size in bytes considered for a single inlining maximum cumulative number of AST nodes considered for inlining trace the tracking of allocation sites deoptimize every n garbage collections perform array bounds checks elimination analyze liveness of environment slots and zap dead values flushes the cache of optimized code for closures on every GC allow uint32 values on optimize frames if they are used only in safe operations track concurrent recompilation artificial compilation delay in ms do not emit check maps for constant values that have a leaf deoptimize the optimized code if the layout of the maps changes enable context specialization in TurboFan execution budget before interrupt is triggered max percentage of megamorphic generic ICs to allow optimization enable use of SAHF instruction if enable use of VFP3 instructions if available enable use of NEON instructions if enable use of SDIV and UDIV instructions if enable use of MLS instructions if enable loading bit constant by means of movw movt instruction enable unaligned accesses for enable use of d16 d31 registers on ARM this requires VFP3 force all emitted branches to be in long enable alignment of csp to bytes on platforms which prefer the register to always be NULL
#define DCHECK_GE(v1, v2)
Definition: logging.h:208
#define DCHECK_LT(v1, v2)
Definition: logging.h:209
#define DCHECK_EQ(v1, v2)
Definition: logging.h:206
#define IS_POWER_OF_TWO(x)
Definition: macros.h:325
#define LAZY_MUTEX_INITIALIZER
Definition: mutex.h:107
static LazyMutex entropy_mutex
static RandomNumberGenerator::EntropySource entropy_source
const Register fp
Debugger support for the V8 JavaScript engine.
Definition: accessors.cc:20
bool(* EntropySource)(unsigned char *buffer, size_t length)
EntropySource is used as a callback function when v8 needs a source of entropy.
Definition: v8.h:4920