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