Commit 03df9dd5 authored by whesse@chromium.org's avatar whesse@chromium.org

Improve pseudorandom number generation and move the PNG state to Isolate.

Review URL: http://codereview.chromium.org/7248060

git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@8490 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
parent dd7e3cbd
...@@ -332,6 +332,8 @@ class HashMap; ...@@ -332,6 +332,8 @@ class HashMap;
V(int, bad_char_shift_table, kUC16AlphabetSize) \ V(int, bad_char_shift_table, kUC16AlphabetSize) \
V(int, good_suffix_shift_table, (kBMMaxShift + 1)) \ V(int, good_suffix_shift_table, (kBMMaxShift + 1)) \
V(int, suffix_table, (kBMMaxShift + 1)) \ V(int, suffix_table, (kBMMaxShift + 1)) \
V(uint32_t, random_seed, 4) \
V(uint32_t, private_random_seed, 4) \
ISOLATE_INIT_DEBUG_ARRAY_LIST(V) ISOLATE_INIT_DEBUG_ARRAY_LIST(V)
typedef List<HeapObject*, PreallocatedStorage> DebugObjectCache; typedef List<HeapObject*, PreallocatedStorage> DebugObjectCache;
......
...@@ -100,42 +100,36 @@ void V8::TearDown() { ...@@ -100,42 +100,36 @@ void V8::TearDown() {
} }
static uint32_t random_seed() { static void seed_random(uint32_t* state) {
if (FLAG_random_seed == 0) { for (int i = 0; i < 4; ++i) {
return random(); state[i] = FLAG_random_seed;
while (state[i] == 0) {
state[i] = random();
}
} }
return FLAG_random_seed;
} }
typedef struct {
uint32_t hi;
uint32_t lo;
} random_state;
// Random number generator using George Marsaglia's MWC algorithm. // Random number generator using George Marsaglia's MWC algorithm.
static uint32_t random_base(random_state *state) { static uint32_t random_base(uint32_t* state) {
// Initialize seed using the system random(). If one of the seeds // Initialize seed using the system random().
// should ever become zero again, or if random() returns zero, we // No non-zero seed will ever become zero again.
// avoid getting stuck with zero bits in hi or lo by re-initializing if (state[0] == 0) seed_random(state);
// them on demand.
if (state->hi == 0) state->hi = random_seed(); // Mix the bits. Never replaces state[i] with 0 if it is nonzero.
if (state->lo == 0) state->lo = random_seed(); state[0] = 18273 * (state[0] & 0xFFFF) + (state[0] >> 16);
state[1] = 36969 * (state[1] & 0xFFFF) + (state[1] >> 16);
// Mix the bits. state[2] = 23208 * (state[2] & 0xFFFF) + (state[2] >> 16);
state->hi = 36969 * (state->hi & 0xFFFF) + (state->hi >> 16); state[3] = 27753 * (state[3] & 0xFFFF) + (state[3] >> 16);
state->lo = 18273 * (state->lo & 0xFFFF) + (state->lo >> 16);
return (state->hi << 16) + (state->lo & 0xFFFF); return ((state[2] ^ state[3]) << 16) + ((state[0] ^ state[1]) & 0xFFFF);
} }
// Used by JavaScript APIs // Used by JavaScript APIs
uint32_t V8::Random(Isolate* isolate) { uint32_t V8::Random(Isolate* isolate) {
ASSERT(isolate == Isolate::Current()); ASSERT(isolate == Isolate::Current());
// TODO(isolates): move lo and hi to isolate return random_base(isolate->random_seed());
static random_state state = {0, 0};
return random_base(&state);
} }
...@@ -144,9 +138,7 @@ uint32_t V8::Random(Isolate* isolate) { ...@@ -144,9 +138,7 @@ uint32_t V8::Random(Isolate* isolate) {
// leaks that could be used in an exploit. // leaks that could be used in an exploit.
uint32_t V8::RandomPrivate(Isolate* isolate) { uint32_t V8::RandomPrivate(Isolate* isolate) {
ASSERT(isolate == Isolate::Current()); ASSERT(isolate == Isolate::Current());
// TODO(isolates): move lo and hi to isolate return random_base(isolate->private_random_seed());
static random_state state = {0, 0};
return random_base(&state);
} }
......
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