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

Side by Side Diff: src/v8.cc

Issue 7248060: Improve pseudorandom number generation and move the PNG state to Isolate. (Closed) Base URL: http://v8.googlecode.com/svn/branches/bleeding_edge/
Patch Set: Created 9 years, 5 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 | « src/isolate.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2011 the V8 project authors. All rights reserved. 1 // Copyright 2011 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 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
93 ASSERT(isolate->IsDefaultIsolate()); 93 ASSERT(isolate->IsDefaultIsolate());
94 94
95 if (!has_been_setup_ || has_been_disposed_) return; 95 if (!has_been_setup_ || has_been_disposed_) return;
96 isolate->TearDown(); 96 isolate->TearDown();
97 97
98 is_running_ = false; 98 is_running_ = false;
99 has_been_disposed_ = true; 99 has_been_disposed_ = true;
100 } 100 }
101 101
102 102
103 static uint32_t random_seed() { 103 static void seed_random(uint32_t* state) {
104 if (FLAG_random_seed == 0) { 104 for (int i = 0; i < 4; ++i) {
105 return random(); 105 state[i] = FLAG_random_seed;
106 while (state[i] == 0) {
107 state[i] = random();
108 }
106 } 109 }
107 return FLAG_random_seed;
108 } 110 }
109 111
110 112
111 typedef struct { 113 // Random number generator using George Marsaglia's MWC algorithm.
112 uint32_t hi; 114 static uint32_t random_base(uint32_t* state) {
113 uint32_t lo; 115 // Initialize seed using the system random().
114 } random_state; 116 // No non-zero seed will ever become zero again.
117 if (state[0] == 0) seed_random(state);
115 118
119 // Mix the bits. Never replaces state[i] with 0 if it is nonzero.
120 state[0] = 18273 * (state[0] & 0xFFFF) + (state[0] >> 16);
121 state[1] = 36969 * (state[1] & 0xFFFF) + (state[1] >> 16);
122 state[2] = 23208 * (state[2] & 0xFFFF) + (state[2] >> 16);
123 state[3] = 27753 * (state[3] & 0xFFFF) + (state[3] >> 16);
116 124
117 // Random number generator using George Marsaglia's MWC algorithm. 125 return ((state[2] ^ state[3]) << 16) + ((state[0] ^ state[1]) & 0xFFFF);
118 static uint32_t random_base(random_state *state) {
119 // Initialize seed using the system random(). If one of the seeds
120 // should ever become zero again, or if random() returns zero, we
121 // avoid getting stuck with zero bits in hi or lo by re-initializing
122 // them on demand.
123 if (state->hi == 0) state->hi = random_seed();
124 if (state->lo == 0) state->lo = random_seed();
125
126 // Mix the bits.
127 state->hi = 36969 * (state->hi & 0xFFFF) + (state->hi >> 16);
128 state->lo = 18273 * (state->lo & 0xFFFF) + (state->lo >> 16);
129 return (state->hi << 16) + (state->lo & 0xFFFF);
130 } 126 }
131 127
132 128
133 // Used by JavaScript APIs 129 // Used by JavaScript APIs
134 uint32_t V8::Random(Isolate* isolate) { 130 uint32_t V8::Random(Isolate* isolate) {
135 ASSERT(isolate == Isolate::Current()); 131 ASSERT(isolate == Isolate::Current());
136 // TODO(isolates): move lo and hi to isolate 132 return random_base(isolate->random_seed());
137 static random_state state = {0, 0};
138 return random_base(&state);
139 } 133 }
140 134
141 135
142 // Used internally by the JIT and memory allocator for security 136 // Used internally by the JIT and memory allocator for security
143 // purposes. So, we keep a different state to prevent informations 137 // purposes. So, we keep a different state to prevent informations
144 // leaks that could be used in an exploit. 138 // leaks that could be used in an exploit.
145 uint32_t V8::RandomPrivate(Isolate* isolate) { 139 uint32_t V8::RandomPrivate(Isolate* isolate) {
146 ASSERT(isolate == Isolate::Current()); 140 ASSERT(isolate == Isolate::Current());
147 // TODO(isolates): move lo and hi to isolate 141 return random_base(isolate->private_random_seed());
148 static random_state state = {0, 0};
149 return random_base(&state);
150 } 142 }
151 143
152 144
153 bool V8::IdleNotification() { 145 bool V8::IdleNotification() {
154 // Returning true tells the caller that there is no need to call 146 // Returning true tells the caller that there is no need to call
155 // IdleNotification again. 147 // IdleNotification again.
156 if (!FLAG_use_idle_notification) return true; 148 if (!FLAG_use_idle_notification) return true;
157 149
158 // Tell the heap that it may want to adjust. 150 // Tell the heap that it may want to adjust.
159 return HEAP->IdleNotification(); 151 return HEAP->IdleNotification();
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
204 use_crankshaft_ = false; 196 use_crankshaft_ = false;
205 } 197 }
206 198
207 RuntimeProfiler::GlobalSetup(); 199 RuntimeProfiler::GlobalSetup();
208 200
209 // Peephole optimization might interfere with deoptimization. 201 // Peephole optimization might interfere with deoptimization.
210 FLAG_peephole_optimization = !use_crankshaft_; 202 FLAG_peephole_optimization = !use_crankshaft_;
211 } 203 }
212 204
213 } } // namespace v8::internal 205 } } // namespace v8::internal
OLDNEW
« no previous file with comments | « src/isolate.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698