Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(9)

Side by Side Diff: src/utils/random-number-generator.cc

Issue 24315007: Avoid fallback to weak entropy for the PRNGs on Windows. (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Fix typo. Created 7 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | tools/gyp/v8.gyp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2013 the V8 project authors. All rights reserved. 1 // Copyright 2013 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without 2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are 3 // modification, are permitted provided that the following conditions are
4 // met: 4 // met:
5 // 5 //
6 // * Redistributions of source code must retain the above copyright 6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer. 7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above 8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following 9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided 10 // disclaimer in the documentation and/or other materials provided
(...skipping 10 matching lines...) Expand all
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 27
28 #include "utils/random-number-generator.h" 28 #include "utils/random-number-generator.h"
29 29
30 #include <cstdio> 30 #include <cstdio>
31 #include <cstdlib>
31 32
32 #include "flags.h" 33 #include "flags.h"
33 #include "platform/mutex.h" 34 #include "platform/mutex.h"
34 #include "platform/time.h" 35 #include "platform/time.h"
35 #include "utils.h" 36 #include "utils.h"
36 37
37 namespace v8 { 38 namespace v8 {
38 namespace internal { 39 namespace internal {
39 40
40 static LazyMutex entropy_mutex = LAZY_MUTEX_INITIALIZER; 41 static LazyMutex entropy_mutex = LAZY_MUTEX_INITIALIZER;
(...skipping 19 matching lines...) Expand all
60 if (entropy_source != NULL) { 61 if (entropy_source != NULL) {
61 int64_t seed; 62 int64_t seed;
62 if (entropy_source(reinterpret_cast<unsigned char*>(&seed), 63 if (entropy_source(reinterpret_cast<unsigned char*>(&seed),
63 sizeof(seed))) { 64 sizeof(seed))) {
64 SetSeed(seed); 65 SetSeed(seed);
65 return; 66 return;
66 } 67 }
67 } 68 }
68 } 69 }
69 70
71 #if V8_OS_CYGWIN || V8_OS_WIN
72 // Use rand_s() to gather entropy on Windows. See:
73 // https://code.google.com/p/v8/issues/detail?id=2905
74 unsigned first_half, second_half;
75 errno_t result = rand_s(&first_half);
76 ASSERT_EQ(0, result);
77 result = rand_s(&second_half);
78 ASSERT_EQ(0, result);
79 SetSeed((static_cast<int64_t>(first_half) << 32) + second_half);
80 #else
70 // Gather entropy from /dev/urandom if available. 81 // Gather entropy from /dev/urandom if available.
71 FILE* fp = fopen("/dev/urandom", "rb"); 82 FILE* fp = fopen("/dev/urandom", "rb");
72 if (fp != NULL) { 83 if (fp != NULL) {
73 int64_t seed; 84 int64_t seed;
74 size_t n = fread(&seed, sizeof(seed), 1, fp); 85 size_t n = fread(&seed, sizeof(seed), 1, fp);
75 fclose(fp); 86 fclose(fp);
76 if (n == 1) { 87 if (n == 1) {
77 SetSeed(seed); 88 SetSeed(seed);
78 return; 89 return;
79 } 90 }
80 } 91 }
81 92
82 // We cannot assume that random() or rand() were seeded 93 // We cannot assume that random() or rand() were seeded
83 // properly, so instead of relying on random() or rand(), 94 // properly, so instead of relying on random() or rand(),
84 // we just seed our PRNG using timing data as fallback. 95 // we just seed our PRNG using timing data as fallback.
85 // This is weak entropy, but it's sufficient, because 96 // This is weak entropy, but it's sufficient, because
86 // it is the responsibility of the embedder to install 97 // it is the responsibility of the embedder to install
87 // an entropy source using v8::V8::SetEntropySource(), 98 // an entropy source using v8::V8::SetEntropySource(),
88 // which provides reasonable entropy, see: 99 // which provides reasonable entropy, see:
89 // https://code.google.com/p/v8/issues/detail?id=2905 100 // https://code.google.com/p/v8/issues/detail?id=2905
90 int64_t seed = Time::NowFromSystemTime().ToInternalValue() << 24; 101 int64_t seed = Time::NowFromSystemTime().ToInternalValue() << 24;
91 seed ^= TimeTicks::HighResNow().ToInternalValue() << 16; 102 seed ^= TimeTicks::HighResNow().ToInternalValue() << 16;
92 seed ^= TimeTicks::Now().ToInternalValue() << 8; 103 seed ^= TimeTicks::Now().ToInternalValue() << 8;
93 SetSeed(seed); 104 SetSeed(seed);
105 #endif // V8_OS_CYGWIN || V8_OS_WIN
94 } 106 }
95 107
96 108
97 int RandomNumberGenerator::NextInt(int max) { 109 int RandomNumberGenerator::NextInt(int max) {
98 ASSERT_LE(0, max); 110 ASSERT_LE(0, max);
99 111
100 // Fast path if max is a power of 2. 112 // Fast path if max is a power of 2.
101 if (IsPowerOf2(max)) { 113 if (IsPowerOf2(max)) {
102 return static_cast<int>((max * static_cast<int64_t>(Next(31))) >> 31); 114 return static_cast<int>((max * static_cast<int64_t>(Next(31))) >> 31);
103 } 115 }
(...skipping 28 matching lines...) Expand all
132 seed_ = seed; 144 seed_ = seed;
133 return static_cast<int>(seed >> (48 - bits)); 145 return static_cast<int>(seed >> (48 - bits));
134 } 146 }
135 147
136 148
137 void RandomNumberGenerator::SetSeed(int64_t seed) { 149 void RandomNumberGenerator::SetSeed(int64_t seed) {
138 seed_ = (seed ^ kMultiplier) & kMask; 150 seed_ = (seed ^ kMultiplier) & kMask;
139 } 151 }
140 152
141 } } // namespace v8::internal 153 } } // namespace v8::internal
OLDNEW
« no previous file with comments | « no previous file | tools/gyp/v8.gyp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698