Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 43 | 43 |
| 44 static Mutex* init_once_mutex = OS::CreateMutex(); | 44 static Mutex* init_once_mutex = OS::CreateMutex(); |
| 45 static bool init_once_called = false; | 45 static bool init_once_called = false; |
| 46 | 46 |
| 47 bool V8::is_running_ = false; | 47 bool V8::is_running_ = false; |
| 48 bool V8::has_been_setup_ = false; | 48 bool V8::has_been_setup_ = false; |
| 49 bool V8::has_been_disposed_ = false; | 49 bool V8::has_been_disposed_ = false; |
| 50 bool V8::has_fatal_error_ = false; | 50 bool V8::has_fatal_error_ = false; |
| 51 bool V8::use_crankshaft_ = true; | 51 bool V8::use_crankshaft_ = true; |
| 52 | 52 |
| 53 static Mutex* entropy_mutex = OS::CreateMutex(); | |
| 54 static EntropySource entropy_source; | |
| 55 | |
| 53 | 56 |
| 54 bool V8::Initialize(Deserializer* des) { | 57 bool V8::Initialize(Deserializer* des) { |
| 55 InitializeOncePerProcess(); | 58 InitializeOncePerProcess(); |
| 56 | 59 |
| 57 // 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. |
| 58 // Note the Isolate::Current() may be non-null because for various | 61 // Note the Isolate::Current() may be non-null because for various |
| 59 // initialization purposes an initializing thread may be assigned an isolate | 62 // initialization purposes an initializing thread may be assigned an isolate |
| 60 // but not actually enter it. | 63 // but not actually enter it. |
| 61 if (i::Isolate::CurrentPerIsolateThreadData() == NULL) { | 64 if (i::Isolate::CurrentPerIsolateThreadData() == NULL) { |
| 62 i::Isolate::EnterDefaultIsolate(); | 65 i::Isolate::EnterDefaultIsolate(); |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 95 if (!has_been_setup_ || has_been_disposed_) return; | 98 if (!has_been_setup_ || has_been_disposed_) return; |
| 96 isolate->TearDown(); | 99 isolate->TearDown(); |
| 97 | 100 |
| 98 is_running_ = false; | 101 is_running_ = false; |
| 99 has_been_disposed_ = true; | 102 has_been_disposed_ = true; |
| 100 } | 103 } |
| 101 | 104 |
| 102 | 105 |
| 103 static void seed_random(uint32_t* state) { | 106 static void seed_random(uint32_t* state) { |
| 104 for (int i = 0; i < 2; ++i) { | 107 for (int i = 0; i < 2; ++i) { |
| 105 state[i] = FLAG_random_seed; | 108 if (FLAG_random_seed != NULL) |
|
Mads Ager (chromium)
2011/07/17 09:11:39
Please consistently use braces around the body. Al
| |
| 106 while (state[i] == 0) { | 109 state[i] = FLAG_random_seed; |
| 110 else if (entropy_source != NULL) { | |
| 111 uint32_t val; | |
| 112 ScopedLock lock(entropy_mutex); | |
| 113 entropy_source(reinterpret_cast<unsigned char*>(&val), sizeof(uint32_t)); | |
| 114 state[i] = val; | |
| 115 } else { | |
| 107 state[i] = random(); | 116 state[i] = random(); |
| 108 } | 117 } |
| 109 } | 118 } |
| 110 } | 119 } |
| 111 | 120 |
| 112 | 121 |
| 113 // Random number generator using George Marsaglia's MWC algorithm. | 122 // Random number generator using George Marsaglia's MWC algorithm. |
| 114 static uint32_t random_base(uint32_t* state) { | 123 static uint32_t random_base(uint32_t* state) { |
| 115 // Initialize seed using the system random(). | 124 // Initialize seed using the system random(). |
| 116 // No non-zero seed will ever become zero again. | 125 // No non-zero seed will ever become zero again. |
| 117 if (state[0] == 0) seed_random(state); | 126 if (state[0] == 0) seed_random(state); |
| 118 | 127 |
| 119 // Mix the bits. Never replaces state[i] with 0 if it is nonzero. | 128 // Mix the bits. Never replaces state[i] with 0 if it is nonzero. |
| 120 state[0] = 18273 * (state[0] & 0xFFFF) + (state[0] >> 16); | 129 state[0] = 18273 * (state[0] & 0xFFFF) + (state[0] >> 16); |
| 121 state[1] = 36969 * (state[1] & 0xFFFF) + (state[1] >> 16); | 130 state[1] = 36969 * (state[1] & 0xFFFF) + (state[1] >> 16); |
| 122 | 131 |
| 123 return (state[0] << 14) + (state[1] & 0x3FFFF); | 132 return (state[0] << 14) + (state[1] & 0x3FFFF); |
| 124 } | 133 } |
| 125 | 134 |
| 126 | 135 |
| 136 void V8::SetEntropySource(EntropySource source) { | |
| 137 entropy_source = source; | |
| 138 } | |
| 139 | |
| 140 | |
| 127 // Used by JavaScript APIs | 141 // Used by JavaScript APIs |
| 128 uint32_t V8::Random(Isolate* isolate) { | 142 uint32_t V8::Random(Isolate* isolate) { |
| 129 ASSERT(isolate == Isolate::Current()); | 143 ASSERT(isolate == Isolate::Current()); |
| 130 return random_base(isolate->random_seed()); | 144 return random_base(isolate->random_seed()); |
| 131 } | 145 } |
| 132 | 146 |
| 133 | 147 |
| 134 // Used internally by the JIT and memory allocator for security | 148 // Used internally by the JIT and memory allocator for security |
| 135 // purposes. So, we keep a different state to prevent informations | 149 // purposes. So, we keep a different state to prevent informations |
| 136 // leaks that could be used in an exploit. | 150 // leaks that could be used in an exploit. |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 194 use_crankshaft_ = false; | 208 use_crankshaft_ = false; |
| 195 } | 209 } |
| 196 | 210 |
| 197 RuntimeProfiler::GlobalSetup(); | 211 RuntimeProfiler::GlobalSetup(); |
| 198 | 212 |
| 199 // Peephole optimization might interfere with deoptimization. | 213 // Peephole optimization might interfere with deoptimization. |
| 200 FLAG_peephole_optimization = !use_crankshaft_; | 214 FLAG_peephole_optimization = !use_crankshaft_; |
| 201 } | 215 } |
| 202 | 216 |
| 203 } } // namespace v8::internal | 217 } } // namespace v8::internal |
| OLD | NEW |