Commit 227bd354 authored by bmeurer@chromium.org's avatar bmeurer@chromium.org

Avoid fallback to weak entropy for the PRNGs on Windows.

Add fallback to rand_s() to gather entropy on Windows for
seeding the random number generator. This is compatible
with what Blink does.

BUG=v8:2905
R=machenbach@chromium.org

Review URL: https://codereview.chromium.org/24315007

git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@16920 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
parent e7130a1e
......@@ -28,6 +28,7 @@
#include "utils/random-number-generator.h"
#include <cstdio>
#include <cstdlib>
#include "flags.h"
#include "platform/mutex.h"
......@@ -67,6 +68,16 @@ RandomNumberGenerator::RandomNumberGenerator() {
}
}
#if V8_OS_CYGWIN || V8_OS_WIN
// Use rand_s() to gather entropy on Windows. See:
// https://code.google.com/p/v8/issues/detail?id=2905
unsigned first_half, second_half;
errno_t result = rand_s(&first_half);
ASSERT_EQ(0, result);
result = rand_s(&second_half);
ASSERT_EQ(0, result);
SetSeed((static_cast<int64_t>(first_half) << 32) + second_half);
#else
// Gather entropy from /dev/urandom if available.
FILE* fp = fopen("/dev/urandom", "rb");
if (fp != NULL) {
......@@ -91,6 +102,7 @@ RandomNumberGenerator::RandomNumberGenerator() {
seed ^= TimeTicks::HighResNow().ToInternalValue() << 16;
seed ^= TimeTicks::Now().ToInternalValue() << 8;
SetSeed(seed);
#endif // V8_OS_CYGWIN || V8_OS_WIN
}
......
......@@ -805,6 +805,9 @@
]},
],
['OS=="win"', {
'defines': [
'_CRT_RAND_S' # for rand_s()
],
'variables': {
'gyp_generators': '<!(echo $GYP_GENERATORS)',
},
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment