Commit 78f173c4 authored by ager@chromium.org's avatar ager@chromium.org

Introduce a random entropy source which can optionally be provided at initialization.

BUG=89462

Review URL: http://codereview.chromium.org/7395012
Patch from Chris Neckar <cdn@chromium.org>.

git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@8666 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
parent 6c71920a
......@@ -2801,6 +2801,13 @@ class V8EXPORT StartupDataDecompressor { // NOLINT
char** raw_data;
};
/**
* EntropySource is used as a callback function when v8 needs a source
* of entropy.
*/
typedef bool (*EntropySource)(unsigned char* buffer, size_t length);
/**
* Container class for static utility functions.
*/
......@@ -3025,6 +3032,12 @@ class V8EXPORT V8 {
*/
static bool Initialize();
/**
* Allows the host application to provide a callback which can be used
* as a source of entropy for random number generators.
*/
static void SetEntropySource(EntropySource source);
/**
* Adjusts the amount of registered external memory. Used to give
* V8 an indication of the amount of externally allocated memory
......
......@@ -3933,6 +3933,11 @@ bool v8::V8::Initialize() {
}
void v8::V8::SetEntropySource(EntropySource source) {
i::V8::SetEntropySource(source);
}
bool v8::V8::Dispose() {
i::Isolate* isolate = i::Isolate::Current();
if (!ApiCheck(isolate != NULL && isolate->IsDefaultIsolate(),
......
......@@ -50,6 +50,9 @@ bool V8::has_been_disposed_ = false;
bool V8::has_fatal_error_ = false;
bool V8::use_crankshaft_ = true;
static Mutex* entropy_mutex = OS::CreateMutex();
static EntropySource entropy_source;
bool V8::Initialize(Deserializer* des) {
InitializeOncePerProcess();
......@@ -102,8 +105,14 @@ void V8::TearDown() {
static void seed_random(uint32_t* state) {
for (int i = 0; i < 2; ++i) {
state[i] = FLAG_random_seed;
while (state[i] == 0) {
if (FLAG_random_seed != 0) {
state[i] = FLAG_random_seed;
} else if (entropy_source != NULL) {
uint32_t val;
ScopedLock lock(entropy_mutex);
entropy_source(reinterpret_cast<unsigned char*>(&val), sizeof(uint32_t));
state[i] = val;
} else {
state[i] = random();
}
}
......@@ -124,6 +133,11 @@ static uint32_t random_base(uint32_t* state) {
}
void V8::SetEntropySource(EntropySource source) {
entropy_source = source;
}
// Used by JavaScript APIs
uint32_t V8::Random(Isolate* isolate) {
ASSERT(isolate == Isolate::Current());
......
......@@ -91,6 +91,9 @@ class V8 : public AllStatic {
static void FatalProcessOutOfMemory(const char* location,
bool take_snapshot = false);
// Allows an entropy source to be provided for use in random number
// generation.
static void SetEntropySource(EntropySource source);
// Random number generation support. Not cryptographically safe.
static uint32_t Random(Isolate* isolate);
// We use random numbers internally in memory allocation and in the
......
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