ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 1 | // Copyright 2006-2009 the V8 project authors. All rights reserved. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2 | // Redistribution and use in source and binary forms, with or without |
| 3 | // modification, are permitted provided that the following conditions are |
| 4 | // met: |
| 5 | // |
| 6 | // * Redistributions of source code must retain the above copyright |
| 7 | // notice, this list of conditions and the following disclaimer. |
| 8 | // * Redistributions in binary form must reproduce the above |
| 9 | // copyright notice, this list of conditions and the following |
| 10 | // disclaimer in the documentation and/or other materials provided |
| 11 | // with the distribution. |
| 12 | // * Neither the name of Google Inc. nor the names of its |
| 13 | // contributors may be used to endorse or promote products derived |
| 14 | // from this software without specific prior written permission. |
| 15 | // |
| 16 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 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. |
| 27 | |
| 28 | #include "v8.h" |
| 29 | |
| 30 | #include "bootstrapper.h" |
| 31 | #include "debug.h" |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 32 | #include "deoptimizer.h" |
| 33 | #include "heap-profiler.h" |
| 34 | #include "hydrogen.h" |
| 35 | #include "lithium-allocator.h" |
| 36 | #include "log.h" |
| 37 | #include "oprofile-agent.h" |
| 38 | #include "runtime-profiler.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 39 | #include "serialize.h" |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 40 | #include "simulator.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 41 | #include "stub-cache.h" |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 42 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 43 | namespace v8 { |
| 44 | namespace internal { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 45 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 46 | bool V8::is_running_ = false; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 47 | bool V8::has_been_setup_ = false; |
| 48 | bool V8::has_been_disposed_ = false; |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 49 | bool V8::has_fatal_error_ = false; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 50 | bool V8::use_crankshaft_ = true; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 51 | |
erik.corry@gmail.com | 4a6c327 | 2010-11-18 12:04:40 +0000 | [diff] [blame] | 52 | |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 53 | bool V8::Initialize(Deserializer* des) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 54 | bool create_heap_objects = des == NULL; |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 55 | if (has_been_disposed_ || has_fatal_error_) return false; |
| 56 | if (IsRunning()) return true; |
| 57 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 58 | use_crankshaft_ = FLAG_crankshaft; |
| 59 | // Peephole optimization might interfere with deoptimization. |
| 60 | FLAG_peephole_optimization = !use_crankshaft_; |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 61 | is_running_ = true; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 62 | has_been_setup_ = true; |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 63 | has_fatal_error_ = false; |
| 64 | has_been_disposed_ = false; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 65 | #ifdef DEBUG |
| 66 | // The initialization process does not handle memory exhaustion. |
| 67 | DisallowAllocationFailure disallow_allocation_failure; |
| 68 | #endif |
| 69 | |
| 70 | // Enable logging before setting up the heap |
| 71 | Logger::Setup(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 72 | |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 73 | CpuProfiler::Setup(); |
whesse@chromium.org | 2c186ca | 2010-06-16 11:32:39 +0000 | [diff] [blame] | 74 | HeapProfiler::Setup(); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 75 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 76 | // Setup the platform OS support. |
| 77 | OS::Setup(); |
| 78 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 79 | // Initialize other runtime facilities |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 80 | #if defined(USE_SIMULATOR) |
| 81 | #if defined(V8_TARGET_ARCH_ARM) |
ager@chromium.org | 378b34e | 2011-01-28 08:04:38 +0000 | [diff] [blame] | 82 | Simulator::Initialize(); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 83 | #elif defined(V8_TARGET_ARCH_MIPS) |
| 84 | ::assembler::mips::Simulator::Initialize(); |
| 85 | #endif |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 86 | #endif |
| 87 | |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 88 | { // NOLINT |
| 89 | // Ensure that the thread has a valid stack guard. The v8::Locker object |
| 90 | // will ensure this too, but we don't have to use lockers if we are only |
| 91 | // using one thread. |
| 92 | ExecutionAccess lock; |
| 93 | StackGuard::InitThread(lock); |
| 94 | } |
| 95 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 96 | // Setup the object heap |
| 97 | ASSERT(!Heap::HasBeenSetup()); |
| 98 | if (!Heap::Setup(create_heap_objects)) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 99 | SetFatalError(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 100 | return false; |
| 101 | } |
| 102 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 103 | Bootstrapper::Initialize(create_heap_objects); |
| 104 | Builtins::Setup(create_heap_objects); |
| 105 | Top::Initialize(); |
| 106 | |
| 107 | if (FLAG_preemption) { |
| 108 | v8::Locker locker; |
| 109 | v8::Locker::StartPreemption(100); |
| 110 | } |
| 111 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 112 | #ifdef ENABLE_DEBUGGER_SUPPORT |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 113 | Debug::Setup(create_heap_objects); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 114 | #endif |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 115 | StubCache::Initialize(create_heap_objects); |
| 116 | |
| 117 | // If we are deserializing, read the state into the now-empty heap. |
| 118 | if (des != NULL) { |
| 119 | des->Deserialize(); |
| 120 | StubCache::Clear(); |
| 121 | } |
| 122 | |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 123 | // Deserializing may put strange things in the root array's copy of the |
| 124 | // stack guard. |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 125 | Heap::SetStackLimits(); |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 126 | |
kasperl@chromium.org | 061ef74 | 2009-02-27 12:16:20 +0000 | [diff] [blame] | 127 | // Setup the CPU support. Must be done after heap setup and after |
| 128 | // any deserialization because we have to have the initial heap |
| 129 | // objects in place for creating the code object used for probing. |
| 130 | CPU::Setup(); |
| 131 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 132 | OProfileAgent::Initialize(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 133 | Deoptimizer::Setup(); |
| 134 | LAllocator::Setup(); |
| 135 | RuntimeProfiler::Setup(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 136 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 137 | // If we are deserializing, log non-function code objects and compiled |
| 138 | // functions found in the snapshot. |
| 139 | if (des != NULL && FLAG_log_code) { |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 140 | HandleScope scope; |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 141 | LOG(LogCodeObjects()); |
ager@chromium.org | 01beca7 | 2009-11-24 14:29:16 +0000 | [diff] [blame] | 142 | LOG(LogCompiledFunctions()); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 143 | } |
| 144 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 145 | return true; |
| 146 | } |
| 147 | |
| 148 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 149 | void V8::SetFatalError() { |
| 150 | is_running_ = false; |
| 151 | has_fatal_error_ = true; |
| 152 | } |
| 153 | |
| 154 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 155 | void V8::TearDown() { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 156 | if (!has_been_setup_ || has_been_disposed_) return; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 157 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 158 | if (FLAG_time_hydrogen) HStatistics::Instance()->Print(); |
| 159 | |
| 160 | // We must stop the logger before we tear down other components. |
| 161 | Logger::EnsureTickerStopped(); |
| 162 | |
| 163 | Deoptimizer::TearDown(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 164 | OProfileAgent::TearDown(); |
| 165 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 166 | if (FLAG_preemption) { |
| 167 | v8::Locker locker; |
| 168 | v8::Locker::StopPreemption(); |
| 169 | } |
| 170 | |
| 171 | Builtins::TearDown(); |
| 172 | Bootstrapper::TearDown(); |
| 173 | |
kasper.lund | af4734f | 2008-07-28 12:50:18 +0000 | [diff] [blame] | 174 | Top::TearDown(); |
| 175 | |
whesse@chromium.org | 2c186ca | 2010-06-16 11:32:39 +0000 | [diff] [blame] | 176 | HeapProfiler::TearDown(); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 177 | CpuProfiler::TearDown(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 178 | RuntimeProfiler::TearDown(); |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 179 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 180 | Logger::TearDown(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 181 | Heap::TearDown(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 182 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 183 | is_running_ = false; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 184 | has_been_disposed_ = true; |
| 185 | } |
| 186 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 187 | |
sgjesse@chromium.org | df7a284 | 2010-03-25 14:34:15 +0000 | [diff] [blame] | 188 | static uint32_t random_seed() { |
| 189 | if (FLAG_random_seed == 0) { |
| 190 | return random(); |
| 191 | } |
| 192 | return FLAG_random_seed; |
| 193 | } |
| 194 | |
| 195 | |
erik.corry@gmail.com | 4a6c327 | 2010-11-18 12:04:40 +0000 | [diff] [blame] | 196 | typedef struct { |
| 197 | uint32_t hi; |
| 198 | uint32_t lo; |
| 199 | } random_state; |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 200 | |
erik.corry@gmail.com | 4a6c327 | 2010-11-18 12:04:40 +0000 | [diff] [blame] | 201 | |
| 202 | // Random number generator using George Marsaglia's MWC algorithm. |
| 203 | static uint32_t random_base(random_state *state) { |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 204 | // Initialize seed using the system random(). If one of the seeds |
| 205 | // should ever become zero again, or if random() returns zero, we |
| 206 | // avoid getting stuck with zero bits in hi or lo by re-initializing |
| 207 | // them on demand. |
erik.corry@gmail.com | 4a6c327 | 2010-11-18 12:04:40 +0000 | [diff] [blame] | 208 | if (state->hi == 0) state->hi = random_seed(); |
| 209 | if (state->lo == 0) state->lo = random_seed(); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 210 | |
| 211 | // Mix the bits. |
erik.corry@gmail.com | 4a6c327 | 2010-11-18 12:04:40 +0000 | [diff] [blame] | 212 | state->hi = 36969 * (state->hi & 0xFFFF) + (state->hi >> 16); |
| 213 | state->lo = 18273 * (state->lo & 0xFFFF) + (state->lo >> 16); |
| 214 | return (state->hi << 16) + (state->lo & 0xFFFF); |
| 215 | } |
| 216 | |
| 217 | |
| 218 | // Used by JavaScript APIs |
| 219 | uint32_t V8::Random() { |
| 220 | static random_state state = {0, 0}; |
| 221 | return random_base(&state); |
| 222 | } |
| 223 | |
| 224 | |
| 225 | // Used internally by the JIT and memory allocator for security |
| 226 | // purposes. So, we keep a different state to prevent informations |
| 227 | // leaks that could be used in an exploit. |
| 228 | uint32_t V8::RandomPrivate() { |
| 229 | static random_state state = {0, 0}; |
| 230 | return random_base(&state); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 231 | } |
| 232 | |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 233 | |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 234 | bool V8::IdleNotification() { |
| 235 | // Returning true tells the caller that there is no need to call |
| 236 | // IdleNotification again. |
| 237 | if (!FLAG_use_idle_notification) return true; |
ager@chromium.org | add848f | 2009-08-13 12:44:13 +0000 | [diff] [blame] | 238 | |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 239 | // Tell the heap that it may want to adjust. |
| 240 | return Heap::IdleNotification(); |
ager@chromium.org | add848f | 2009-08-13 12:44:13 +0000 | [diff] [blame] | 241 | } |
| 242 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 243 | |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 244 | // Use a union type to avoid type-aliasing optimizations in GCC. |
| 245 | typedef union { |
| 246 | double double_value; |
| 247 | uint64_t uint64_t_value; |
| 248 | } double_int_union; |
| 249 | |
| 250 | |
| 251 | Object* V8::FillHeapNumberWithRandom(Object* heap_number) { |
| 252 | uint64_t random_bits = Random(); |
| 253 | // Make a double* from address (heap_number + sizeof(double)). |
| 254 | double_int_union* r = reinterpret_cast<double_int_union*>( |
| 255 | reinterpret_cast<char*>(heap_number) + |
| 256 | HeapNumber::kValueOffset - kHeapObjectTag); |
| 257 | // Convert 32 random bits to 0.(32 random bits) in a double |
| 258 | // by computing: |
| 259 | // ( 1.(20 0s)(32 random bits) x 2^20 ) - (1.0 x 2^20)). |
| 260 | const double binary_million = 1048576.0; |
| 261 | r->double_value = binary_million; |
| 262 | r->uint64_t_value |= random_bits; |
| 263 | r->double_value -= binary_million; |
| 264 | |
| 265 | return heap_number; |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 266 | } |
| 267 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 268 | } } // namespace v8::internal |