OLD | NEW |
1 // Copyright 2006-2009 the V8 project authors. All rights reserved. | 1 // Copyright 2006-2009 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 158 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
169 } | 169 } |
170 | 170 |
171 | 171 |
172 static uint32_t random_seed() { | 172 static uint32_t random_seed() { |
173 if (FLAG_random_seed == 0) { | 173 if (FLAG_random_seed == 0) { |
174 return random(); | 174 return random(); |
175 } | 175 } |
176 return FLAG_random_seed; | 176 return FLAG_random_seed; |
177 } | 177 } |
178 | 178 |
| 179 typedef struct { |
| 180 uint32_t hi; |
| 181 uint32_t lo; |
| 182 } random_state; |
179 | 183 |
180 uint32_t V8::Random() { | 184 // Random number generator using George Marsaglia's MWC algorithm. |
181 // Random number generator using George Marsaglia's MWC algorithm. | 185 static uint32_t random_base(random_state *state) { |
182 static uint32_t hi = 0; | |
183 static uint32_t lo = 0; | |
184 | |
185 // Initialize seed using the system random(). If one of the seeds | 186 // Initialize seed using the system random(). If one of the seeds |
186 // should ever become zero again, or if random() returns zero, we | 187 // should ever become zero again, or if random() returns zero, we |
187 // avoid getting stuck with zero bits in hi or lo by re-initializing | 188 // avoid getting stuck with zero bits in hi or lo by re-initializing |
188 // them on demand. | 189 // them on demand. |
189 if (hi == 0) hi = random_seed(); | 190 if (state->hi == 0) state->hi = random_seed(); |
190 if (lo == 0) lo = random_seed(); | 191 if (state->lo == 0) state->lo = random_seed(); |
191 | 192 |
192 // Mix the bits. | 193 // Mix the bits. |
193 hi = 36969 * (hi & 0xFFFF) + (hi >> 16); | 194 state->hi = 36969 * (state->hi & 0xFFFF) + (state->hi >> 16); |
194 lo = 18273 * (lo & 0xFFFF) + (lo >> 16); | 195 state->lo = 18273 * (state->lo & 0xFFFF) + (state->lo >> 16); |
195 return (hi << 16) + (lo & 0xFFFF); | 196 return (state->hi << 16) + (state->lo & 0xFFFF); |
| 197 } |
| 198 |
| 199 // Used by JavaScript APIs |
| 200 uint32_t V8::Random() { |
| 201 static random_state state = {0, 0}; |
| 202 return random_base(&state); |
| 203 } |
| 204 |
| 205 // Used internally by the JIT for security purposes. So, we keep a different |
| 206 // state to prevent informations leaks that could be used in an exploit. |
| 207 uint32_t V8::RandomPrivate() { |
| 208 static random_state state = {0, 0}; |
| 209 return random_base(&state); |
196 } | 210 } |
197 | 211 |
198 | 212 |
199 bool V8::IdleNotification() { | 213 bool V8::IdleNotification() { |
200 // Returning true tells the caller that there is no need to call | 214 // Returning true tells the caller that there is no need to call |
201 // IdleNotification again. | 215 // IdleNotification again. |
202 if (!FLAG_use_idle_notification) return true; | 216 if (!FLAG_use_idle_notification) return true; |
203 | 217 |
204 // Tell the heap that it may want to adjust. | 218 // Tell the heap that it may want to adjust. |
205 return Heap::IdleNotification(); | 219 return Heap::IdleNotification(); |
(...skipping 18 matching lines...) Expand all Loading... |
224 // ( 1.(20 0s)(32 random bits) x 2^20 ) - (1.0 x 2^20)). | 238 // ( 1.(20 0s)(32 random bits) x 2^20 ) - (1.0 x 2^20)). |
225 const double binary_million = 1048576.0; | 239 const double binary_million = 1048576.0; |
226 r->double_value = binary_million; | 240 r->double_value = binary_million; |
227 r->uint64_t_value |= random_bits; | 241 r->uint64_t_value |= random_bits; |
228 r->double_value -= binary_million; | 242 r->double_value -= binary_million; |
229 | 243 |
230 return heap_number; | 244 return heap_number; |
231 } | 245 } |
232 | 246 |
233 } } // namespace v8::internal | 247 } } // namespace v8::internal |
OLD | NEW |