From be0f740d990c3d8cad413fd864f235ceebc8db31 Mon Sep 17 00:00:00 2001 From: "ager@chromium.org" Date: Thu, 25 Nov 2010 07:39:17 +0000 Subject: [PATCH] Landing for Justin Schuh. Seed the random number generator in Windows with rand_s This is a quick fix for m9. It works on Windows Chrome because the random device is already initialized before permissions are dropped for the Chrome sandbox. The same trick isn't possible on Linux or Mac. I think the long-term solution is to provide an interface for supplying v8 with a true random number generator. Then Chrome can just hook up the generator from base/rand_util.h BUG=http://code.google.com/p/v8/issues/detail?id=936 TEST=None. git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@5889 ce2b1a6d-e550-0410-aec6-3dcde31c8c00 --- src/platform-win32.cc | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/platform-win32.cc b/src/platform-win32.cc index a0ba5e865c..2a9dd67f7a 100644 --- a/src/platform-win32.cc +++ b/src/platform-win32.cc @@ -54,6 +54,11 @@ #define _WIN32_WINNT 0x501 #endif +// Required before stdlib.h inclusion for cryptographically strong rand_s. +#ifndef _CRT_RAND_S +#define _CRT_RAND_S +#endif // _CRT_RAND_S + #include #include // For LocalOffset() implementation. @@ -584,11 +589,8 @@ char* Time::LocalTimezone() { void OS::Setup() { // Seed the random number generator. - // Convert the current time to a 64-bit integer first, before converting it - // to an unsigned. Going directly can cause an overflow and the seed to be - // set to all ones. The seed will be identical for different instances that - // call this setup code within the same millisecond. - uint64_t seed = static_cast(TimeCurrentMillis()); + unsigned int seed; + CHECK_EQ(rand_s(&seed), 0); srand(static_cast(seed)); }