| 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 |
| 11 // with the distribution. | 11 // with the distribution. |
| 12 // * Neither the name of Google Inc. nor the names of its | 12 // * Neither the name of Google Inc. nor the names of its |
| 13 // contributors may be used to endorse or promote products derived | 13 // contributors may be used to endorse or promote products derived |
| 14 // from this software without specific prior written permission. | 14 // from this software without specific prior written permission. |
| 15 // | 15 // |
| 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | 17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | 18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | 19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | 20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | 27 |
| 28 #include "v8.h" | 28 #include "v8.h" |
| 29 | 29 |
| 30 #include "isolate.h" |
| 30 #include "bootstrapper.h" | 31 #include "bootstrapper.h" |
| 31 #include "debug.h" | 32 #include "debug.h" |
| 32 #include "deoptimizer.h" | 33 #include "deoptimizer.h" |
| 33 #include "heap-profiler.h" | 34 #include "heap-profiler.h" |
| 34 #include "hydrogen.h" | 35 #include "hydrogen.h" |
| 35 #include "lithium-allocator.h" | 36 #include "lithium-allocator.h" |
| 36 #include "log.h" | 37 #include "log.h" |
| 37 #include "runtime-profiler.h" | 38 #include "runtime-profiler.h" |
| 38 #include "serialize.h" | 39 #include "serialize.h" |
| 39 #include "simulator.h" | |
| 40 #include "stub-cache.h" | |
| 41 #include "store-buffer.h" | 40 #include "store-buffer.h" |
| 42 | 41 |
| 43 namespace v8 { | 42 namespace v8 { |
| 44 namespace internal { | 43 namespace internal { |
| 45 | 44 |
| 46 bool V8::is_running_ = false; | 45 bool V8::is_running_ = false; |
| 47 bool V8::has_been_setup_ = false; | 46 bool V8::has_been_setup_ = false; |
| 48 bool V8::has_been_disposed_ = false; | 47 bool V8::has_been_disposed_ = false; |
| 49 bool V8::has_fatal_error_ = false; | 48 bool V8::has_fatal_error_ = false; |
| 50 bool V8::use_crankshaft_ = true; | 49 bool V8::use_crankshaft_ = true; |
| 51 | 50 |
| 52 | 51 |
| 53 bool V8::Initialize(Deserializer* des) { | 52 bool V8::Initialize(Deserializer* des) { |
| 54 bool create_heap_objects = des == NULL; | 53 // The current thread may not yet had entered an isolate to run. |
| 55 if (has_been_disposed_ || has_fatal_error_) return false; | 54 // Note the Isolate::Current() may be non-null because for various |
| 56 if (IsRunning()) return true; | 55 // initialization purposes an initializing thread may be assigned an isolate |
| 56 // but not actually enter it. |
| 57 if (i::Isolate::CurrentPerIsolateThreadData() == NULL) { |
| 58 i::Isolate::EnterDefaultIsolate(); |
| 59 } |
| 60 |
| 61 ASSERT(i::Isolate::CurrentPerIsolateThreadData() != NULL); |
| 62 ASSERT(i::Isolate::CurrentPerIsolateThreadData()->thread_id() == |
| 63 i::Thread::GetThreadLocalInt(i::Isolate::thread_id_key())); |
| 64 ASSERT(i::Isolate::CurrentPerIsolateThreadData()->isolate() == |
| 65 i::Isolate::Current()); |
| 66 |
| 67 if (IsDead()) return false; |
| 68 |
| 69 Isolate* isolate = Isolate::Current(); |
| 70 if (isolate->IsInitialized()) return true; |
| 57 | 71 |
| 58 #if defined(V8_TARGET_ARCH_ARM) && !defined(USE_ARM_EABI) | 72 #if defined(V8_TARGET_ARCH_ARM) && !defined(USE_ARM_EABI) |
| 59 use_crankshaft_ = false; | 73 use_crankshaft_ = false; |
| 60 #else | 74 #else |
| 61 use_crankshaft_ = FLAG_crankshaft; | 75 use_crankshaft_ = FLAG_crankshaft; |
| 62 #endif | 76 #endif |
| 63 | 77 |
| 64 // Peephole optimization might interfere with deoptimization. | 78 // Peephole optimization might interfere with deoptimization. |
| 65 FLAG_peephole_optimization = !use_crankshaft_; | 79 FLAG_peephole_optimization = !use_crankshaft_; |
| 80 |
| 66 is_running_ = true; | 81 is_running_ = true; |
| 67 has_been_setup_ = true; | 82 has_been_setup_ = true; |
| 68 has_fatal_error_ = false; | 83 has_fatal_error_ = false; |
| 69 has_been_disposed_ = false; | 84 has_been_disposed_ = false; |
| 70 #ifdef DEBUG | |
| 71 // The initialization process does not handle memory exhaustion. | |
| 72 DisallowAllocationFailure disallow_allocation_failure; | |
| 73 #endif | |
| 74 | 85 |
| 75 // Enable logging before setting up the heap | 86 return isolate->Init(des); |
| 76 Logger::Setup(); | |
| 77 | |
| 78 CpuProfiler::Setup(); | |
| 79 HeapProfiler::Setup(); | |
| 80 | |
| 81 // Setup the platform OS support. | |
| 82 OS::Setup(); | |
| 83 | |
| 84 // Initialize other runtime facilities | |
| 85 #if defined(USE_SIMULATOR) | |
| 86 #if defined(V8_TARGET_ARCH_ARM) | |
| 87 Simulator::Initialize(); | |
| 88 #elif defined(V8_TARGET_ARCH_MIPS) | |
| 89 ::assembler::mips::Simulator::Initialize(); | |
| 90 #endif | |
| 91 #endif | |
| 92 | |
| 93 { // NOLINT | |
| 94 // Ensure that the thread has a valid stack guard. The v8::Locker object | |
| 95 // will ensure this too, but we don't have to use lockers if we are only | |
| 96 // using one thread. | |
| 97 ExecutionAccess lock; | |
| 98 StackGuard::InitThread(lock); | |
| 99 } | |
| 100 | |
| 101 StoreBuffer::Setup(); | |
| 102 | |
| 103 // Setup the object heap | |
| 104 ASSERT(!Heap::HasBeenSetup()); | |
| 105 if (!Heap::Setup(create_heap_objects)) { | |
| 106 SetFatalError(); | |
| 107 return false; | |
| 108 } | |
| 109 | |
| 110 Bootstrapper::Initialize(create_heap_objects); | |
| 111 Builtins::Setup(create_heap_objects); | |
| 112 Top::Initialize(); | |
| 113 | |
| 114 if (FLAG_preemption) { | |
| 115 v8::Locker locker; | |
| 116 v8::Locker::StartPreemption(100); | |
| 117 } | |
| 118 | |
| 119 #ifdef ENABLE_DEBUGGER_SUPPORT | |
| 120 Debug::Setup(create_heap_objects); | |
| 121 #endif | |
| 122 StubCache::Initialize(create_heap_objects); | |
| 123 | |
| 124 // If we are deserializing, read the state into the now-empty heap. | |
| 125 if (des != NULL) { | |
| 126 des->Deserialize(); | |
| 127 StubCache::Clear(); | |
| 128 } | |
| 129 | |
| 130 // Deserializing may put strange things in the root array's copy of the | |
| 131 // stack guard. | |
| 132 Heap::SetStackLimits(); | |
| 133 | |
| 134 // Setup the CPU support. Must be done after heap setup and after | |
| 135 // any deserialization because we have to have the initial heap | |
| 136 // objects in place for creating the code object used for probing. | |
| 137 CPU::Setup(); | |
| 138 | |
| 139 Deoptimizer::Setup(); | |
| 140 LAllocator::Setup(); | |
| 141 RuntimeProfiler::Setup(); | |
| 142 | |
| 143 // If we are deserializing, log non-function code objects and compiled | |
| 144 // functions found in the snapshot. | |
| 145 if (des != NULL && FLAG_log_code) { | |
| 146 HandleScope scope; | |
| 147 LOG(LogCodeObjects()); | |
| 148 LOG(LogCompiledFunctions()); | |
| 149 } | |
| 150 | |
| 151 return true; | |
| 152 } | 87 } |
| 153 | 88 |
| 154 | 89 |
| 155 void V8::SetFatalError() { | 90 void V8::SetFatalError() { |
| 156 is_running_ = false; | 91 is_running_ = false; |
| 157 has_fatal_error_ = true; | 92 has_fatal_error_ = true; |
| 158 } | 93 } |
| 159 | 94 |
| 160 | 95 |
| 161 void V8::TearDown() { | 96 void V8::TearDown() { |
| 97 Isolate* isolate = Isolate::Current(); |
| 98 ASSERT(isolate->IsDefaultIsolate()); |
| 99 |
| 162 if (!has_been_setup_ || has_been_disposed_) return; | 100 if (!has_been_setup_ || has_been_disposed_) return; |
| 163 | 101 isolate->TearDown(); |
| 164 if (FLAG_hydrogen_stats) HStatistics::Instance()->Print(); | |
| 165 | |
| 166 // We must stop the logger before we tear down other components. | |
| 167 Logger::EnsureTickerStopped(); | |
| 168 | |
| 169 Deoptimizer::TearDown(); | |
| 170 | |
| 171 if (FLAG_preemption) { | |
| 172 v8::Locker locker; | |
| 173 v8::Locker::StopPreemption(); | |
| 174 } | |
| 175 | |
| 176 Builtins::TearDown(); | |
| 177 Bootstrapper::TearDown(); | |
| 178 | |
| 179 Top::TearDown(); | |
| 180 | |
| 181 HeapProfiler::TearDown(); | |
| 182 CpuProfiler::TearDown(); | |
| 183 RuntimeProfiler::TearDown(); | |
| 184 | |
| 185 Logger::TearDown(); | |
| 186 Heap::TearDown(); | |
| 187 | |
| 188 StoreBuffer::TearDown(); | |
| 189 | 102 |
| 190 is_running_ = false; | 103 is_running_ = false; |
| 191 has_been_disposed_ = true; | 104 has_been_disposed_ = true; |
| 192 } | 105 } |
| 193 | 106 |
| 194 | 107 |
| 195 static uint32_t random_seed() { | 108 static uint32_t random_seed() { |
| 196 if (FLAG_random_seed == 0) { | 109 if (FLAG_random_seed == 0) { |
| 197 return random(); | 110 return random(); |
| 198 } | 111 } |
| (...skipping 17 matching lines...) Expand all Loading... |
| 216 if (state->lo == 0) state->lo = random_seed(); | 129 if (state->lo == 0) state->lo = random_seed(); |
| 217 | 130 |
| 218 // Mix the bits. | 131 // Mix the bits. |
| 219 state->hi = 36969 * (state->hi & 0xFFFF) + (state->hi >> 16); | 132 state->hi = 36969 * (state->hi & 0xFFFF) + (state->hi >> 16); |
| 220 state->lo = 18273 * (state->lo & 0xFFFF) + (state->lo >> 16); | 133 state->lo = 18273 * (state->lo & 0xFFFF) + (state->lo >> 16); |
| 221 return (state->hi << 16) + (state->lo & 0xFFFF); | 134 return (state->hi << 16) + (state->lo & 0xFFFF); |
| 222 } | 135 } |
| 223 | 136 |
| 224 | 137 |
| 225 // Used by JavaScript APIs | 138 // Used by JavaScript APIs |
| 226 uint32_t V8::Random() { | 139 uint32_t V8::Random(Isolate* isolate) { |
| 140 ASSERT(isolate == Isolate::Current()); |
| 141 // TODO(isolates): move lo and hi to isolate |
| 227 static random_state state = {0, 0}; | 142 static random_state state = {0, 0}; |
| 228 return random_base(&state); | 143 return random_base(&state); |
| 229 } | 144 } |
| 230 | 145 |
| 231 | 146 |
| 232 // Used internally by the JIT and memory allocator for security | 147 // Used internally by the JIT and memory allocator for security |
| 233 // purposes. So, we keep a different state to prevent informations | 148 // purposes. So, we keep a different state to prevent informations |
| 234 // leaks that could be used in an exploit. | 149 // leaks that could be used in an exploit. |
| 235 uint32_t V8::RandomPrivate() { | 150 uint32_t V8::RandomPrivate(Isolate* isolate) { |
| 151 ASSERT(isolate == Isolate::Current()); |
| 152 // TODO(isolates): move lo and hi to isolate |
| 236 static random_state state = {0, 0}; | 153 static random_state state = {0, 0}; |
| 237 return random_base(&state); | 154 return random_base(&state); |
| 238 } | 155 } |
| 239 | 156 |
| 240 | 157 |
| 241 bool V8::IdleNotification() { | 158 bool V8::IdleNotification() { |
| 242 // Returning true tells the caller that there is no need to call | 159 // Returning true tells the caller that there is no need to call |
| 243 // IdleNotification again. | 160 // IdleNotification again. |
| 244 if (!FLAG_use_idle_notification) return true; | 161 if (!FLAG_use_idle_notification) return true; |
| 245 | 162 |
| 246 // Tell the heap that it may want to adjust. | 163 // Tell the heap that it may want to adjust. |
| 247 return Heap::IdleNotification(); | 164 return HEAP->IdleNotification(); |
| 248 } | 165 } |
| 249 | 166 |
| 250 | 167 |
| 251 // Use a union type to avoid type-aliasing optimizations in GCC. | 168 // Use a union type to avoid type-aliasing optimizations in GCC. |
| 252 typedef union { | 169 typedef union { |
| 253 double double_value; | 170 double double_value; |
| 254 uint64_t uint64_t_value; | 171 uint64_t uint64_t_value; |
| 255 } double_int_union; | 172 } double_int_union; |
| 256 | 173 |
| 257 | 174 |
| 258 Object* V8::FillHeapNumberWithRandom(Object* heap_number) { | 175 Object* V8::FillHeapNumberWithRandom(Object* heap_number) { |
| 259 uint64_t random_bits = Random(); | 176 uint64_t random_bits = Random(Isolate::Current()); |
| 260 // Make a double* from address (heap_number + sizeof(double)). | 177 // Make a double* from address (heap_number + sizeof(double)). |
| 261 double_int_union* r = reinterpret_cast<double_int_union*>( | 178 double_int_union* r = reinterpret_cast<double_int_union*>( |
| 262 reinterpret_cast<char*>(heap_number) + | 179 reinterpret_cast<char*>(heap_number) + |
| 263 HeapNumber::kValueOffset - kHeapObjectTag); | 180 HeapNumber::kValueOffset - kHeapObjectTag); |
| 264 // Convert 32 random bits to 0.(32 random bits) in a double | 181 // Convert 32 random bits to 0.(32 random bits) in a double |
| 265 // by computing: | 182 // by computing: |
| 266 // ( 1.(20 0s)(32 random bits) x 2^20 ) - (1.0 x 2^20)). | 183 // ( 1.(20 0s)(32 random bits) x 2^20 ) - (1.0 x 2^20)). |
| 267 const double binary_million = 1048576.0; | 184 const double binary_million = 1048576.0; |
| 268 r->double_value = binary_million; | 185 r->double_value = binary_million; |
| 269 r->uint64_t_value |= random_bits; | 186 r->uint64_t_value |= random_bits; |
| 270 r->double_value -= binary_million; | 187 r->double_value -= binary_million; |
| 271 | 188 |
| 272 return heap_number; | 189 return heap_number; |
| 273 } | 190 } |
| 274 | 191 |
| 275 } } // namespace v8::internal | 192 } } // namespace v8::internal |
| OLD | NEW |