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

Side by Side Diff: src/v8.cc

Issue 23548024: Introduce a RandonNumberGenerator class. Refactor the random/private_random uses in Isolate/Context. (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: REBASE Created 7 years, 3 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/v8.h ('k') | test/cctest/cctest.gyp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2012 the V8 project authors. All rights reserved. 1 // Copyright 2012 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 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
46 #include "store-buffer.h" 46 #include "store-buffer.h"
47 47
48 namespace v8 { 48 namespace v8 {
49 namespace internal { 49 namespace internal {
50 50
51 V8_DECLARE_ONCE(init_once); 51 V8_DECLARE_ONCE(init_once);
52 52
53 List<CallCompletedCallback>* V8::call_completed_callbacks_ = NULL; 53 List<CallCompletedCallback>* V8::call_completed_callbacks_ = NULL;
54 v8::ArrayBuffer::Allocator* V8::array_buffer_allocator_ = NULL; 54 v8::ArrayBuffer::Allocator* V8::array_buffer_allocator_ = NULL;
55 55
56 static LazyMutex entropy_mutex = LAZY_MUTEX_INITIALIZER;
57
58 static EntropySource entropy_source;
59
60 56
61 bool V8::Initialize(Deserializer* des) { 57 bool V8::Initialize(Deserializer* des) {
62 InitializeOncePerProcess(); 58 InitializeOncePerProcess();
63 59
64 // The current thread may not yet had entered an isolate to run. 60 // The current thread may not yet had entered an isolate to run.
65 // Note the Isolate::Current() may be non-null because for various 61 // Note the Isolate::Current() may be non-null because for various
66 // initialization purposes an initializing thread may be assigned an isolate 62 // initialization purposes an initializing thread may be assigned an isolate
67 // but not actually enter it. 63 // but not actually enter it.
68 if (i::Isolate::CurrentPerIsolateThreadData() == NULL) { 64 if (i::Isolate::CurrentPerIsolateThreadData() == NULL) {
69 i::Isolate::EnterDefaultIsolate(); 65 i::Isolate::EnterDefaultIsolate();
(...skipping 30 matching lines...) Expand all
100 RegisteredExtension::UnregisterAll(); 96 RegisteredExtension::UnregisterAll();
101 Isolate::GlobalTearDown(); 97 Isolate::GlobalTearDown();
102 98
103 delete call_completed_callbacks_; 99 delete call_completed_callbacks_;
104 call_completed_callbacks_ = NULL; 100 call_completed_callbacks_ = NULL;
105 101
106 Sampler::TearDown(); 102 Sampler::TearDown();
107 } 103 }
108 104
109 105
110 static void seed_random(uint32_t* state) {
111 for (int i = 0; i < 2; ++i) {
112 if (FLAG_random_seed != 0) {
113 state[i] = FLAG_random_seed;
114 } else if (entropy_source != NULL) {
115 uint32_t val;
116 LockGuard<Mutex> lock_guard(entropy_mutex.Pointer());
117 entropy_source(reinterpret_cast<unsigned char*>(&val), sizeof(uint32_t));
118 state[i] = val;
119 } else {
120 state[i] = random();
121 }
122 }
123 }
124
125
126 // Random number generator using George Marsaglia's MWC algorithm.
127 static uint32_t random_base(uint32_t* state) {
128 // Initialize seed using the system random().
129 // No non-zero seed will ever become zero again.
130 if (state[0] == 0) seed_random(state);
131
132 // Mix the bits. Never replaces state[i] with 0 if it is nonzero.
133 state[0] = 18273 * (state[0] & 0xFFFF) + (state[0] >> 16);
134 state[1] = 36969 * (state[1] & 0xFFFF) + (state[1] >> 16);
135
136 return (state[0] << 14) + (state[1] & 0x3FFFF);
137 }
138
139
140 void V8::SetEntropySource(EntropySource source) {
141 entropy_source = source;
142 }
143
144
145 void V8::SetReturnAddressLocationResolver( 106 void V8::SetReturnAddressLocationResolver(
146 ReturnAddressLocationResolver resolver) { 107 ReturnAddressLocationResolver resolver) {
147 StackFrame::SetReturnAddressLocationResolver(resolver); 108 StackFrame::SetReturnAddressLocationResolver(resolver);
148 } 109 }
149 110
150 111
151 // Used by JavaScript APIs 112 // Used by JavaScript APIs
152 uint32_t V8::Random(Context* context) { 113 uint32_t V8::Random(Context* context) {
153 ASSERT(context->IsNativeContext()); 114 ASSERT(context->IsNativeContext());
154 ByteArray* seed = context->random_seed(); 115 ByteArray* seed = context->random_seed();
155 return random_base(reinterpret_cast<uint32_t*>(seed->GetDataStartAddress())); 116 uint32_t* state = reinterpret_cast<uint32_t*>(seed->GetDataStartAddress());
117
118 // When we get here, the RNG must have been initialized,
119 // see the Genesis constructor in file bootstrapper.cc.
120 ASSERT_NE(0, state[0]);
121 ASSERT_NE(0, state[1]);
122
123 // Mix the bits. Never replaces state[i] with 0 if it is nonzero.
124 state[0] = 18273 * (state[0] & 0xFFFF) + (state[0] >> 16);
125 state[1] = 36969 * (state[1] & 0xFFFF) + (state[1] >> 16);
126
127 return (state[0] << 14) + (state[1] & 0x3FFFF);
156 } 128 }
157 129
158 130
159 // Used internally by the JIT and memory allocator for security
160 // purposes. So, we keep a different state to prevent informations
161 // leaks that could be used in an exploit.
162 uint32_t V8::RandomPrivate(Isolate* isolate) {
163 return random_base(isolate->private_random_seed());
164 }
165
166
167 void V8::AddCallCompletedCallback(CallCompletedCallback callback) { 131 void V8::AddCallCompletedCallback(CallCompletedCallback callback) {
168 if (call_completed_callbacks_ == NULL) { // Lazy init. 132 if (call_completed_callbacks_ == NULL) { // Lazy init.
169 call_completed_callbacks_ = new List<CallCompletedCallback>(); 133 call_completed_callbacks_ = new List<CallCompletedCallback>();
170 } 134 }
171 for (int i = 0; i < call_completed_callbacks_->length(); i++) { 135 for (int i = 0; i < call_completed_callbacks_->length(); i++) {
172 if (callback == call_completed_callbacks_->at(i)) return; 136 if (callback == call_completed_callbacks_->at(i)) return;
173 } 137 }
174 call_completed_callbacks_->Add(callback); 138 call_completed_callbacks_->Add(callback);
175 } 139 }
176 140
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after
275 } else { 239 } else {
276 FLAG_marking_threads = 0; 240 FLAG_marking_threads = 0;
277 } 241 }
278 242
279 if (FLAG_concurrent_recompilation && 243 if (FLAG_concurrent_recompilation &&
280 SystemThreadManager::NumberOfParallelSystemThreads( 244 SystemThreadManager::NumberOfParallelSystemThreads(
281 SystemThreadManager::PARALLEL_RECOMPILATION) == 0) { 245 SystemThreadManager::PARALLEL_RECOMPILATION) == 0) {
282 FLAG_concurrent_recompilation = false; 246 FLAG_concurrent_recompilation = false;
283 } 247 }
284 248
285 OS::SetUp();
286 Sampler::SetUp(); 249 Sampler::SetUp();
287 CPU::SetUp(); 250 CPU::SetUp();
288 OS::PostSetUp(); 251 OS::PostSetUp();
289 ElementsAccessor::InitializeOncePerProcess(); 252 ElementsAccessor::InitializeOncePerProcess();
290 LOperand::SetUpCaches(); 253 LOperand::SetUpCaches();
291 SetUpJSCallerSavedCodeData(); 254 SetUpJSCallerSavedCodeData();
292 ExternalReference::SetUp(); 255 ExternalReference::SetUp();
293 Bootstrapper::InitializeOncePerProcess(); 256 Bootstrapper::InitializeOncePerProcess();
294 } 257 }
295 258
296 259
297 void V8::InitializeOncePerProcess() { 260 void V8::InitializeOncePerProcess() {
298 CallOnce(&init_once, &InitializeOncePerProcessImpl); 261 CallOnce(&init_once, &InitializeOncePerProcessImpl);
299 } 262 }
300 263
301 } } // namespace v8::internal 264 } } // namespace v8::internal
OLDNEW
« no previous file with comments | « src/v8.h ('k') | test/cctest/cctest.gyp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698