Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1 | // Copyright 2006-2009 the V8 project authors. All rights reserved. |
| 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" |
| 32 | #include "serialize.h" |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 33 | #include "simulator.h" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 34 | #include "stub-cache.h" |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 35 | #include "heap-profiler.h" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 36 | #include "oprofile-agent.h" |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 37 | #include "log.h" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 38 | |
| 39 | namespace v8 { |
| 40 | namespace internal { |
| 41 | |
| 42 | bool V8::is_running_ = false; |
| 43 | bool V8::has_been_setup_ = false; |
| 44 | bool V8::has_been_disposed_ = false; |
| 45 | bool V8::has_fatal_error_ = false; |
| 46 | |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame^] | 47 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 48 | bool V8::Initialize(Deserializer* des) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 49 | bool create_heap_objects = des == NULL; |
| 50 | if (has_been_disposed_ || has_fatal_error_) return false; |
| 51 | if (IsRunning()) return true; |
| 52 | |
| 53 | is_running_ = true; |
| 54 | has_been_setup_ = true; |
| 55 | has_fatal_error_ = false; |
| 56 | has_been_disposed_ = false; |
| 57 | #ifdef DEBUG |
| 58 | // The initialization process does not handle memory exhaustion. |
| 59 | DisallowAllocationFailure disallow_allocation_failure; |
| 60 | #endif |
| 61 | |
| 62 | // Enable logging before setting up the heap |
| 63 | Logger::Setup(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 64 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 65 | CpuProfiler::Setup(); |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 66 | HeapProfiler::Setup(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 67 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 68 | // Setup the platform OS support. |
| 69 | OS::Setup(); |
| 70 | |
| 71 | // Initialize other runtime facilities |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 72 | #if defined(USE_SIMULATOR) |
| 73 | #if defined(V8_TARGET_ARCH_ARM) |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 74 | ::assembler::arm::Simulator::Initialize(); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 75 | #elif defined(V8_TARGET_ARCH_MIPS) |
| 76 | ::assembler::mips::Simulator::Initialize(); |
| 77 | #endif |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 78 | #endif |
| 79 | |
| 80 | { // NOLINT |
| 81 | // Ensure that the thread has a valid stack guard. The v8::Locker object |
| 82 | // will ensure this too, but we don't have to use lockers if we are only |
| 83 | // using one thread. |
| 84 | ExecutionAccess lock; |
| 85 | StackGuard::InitThread(lock); |
| 86 | } |
| 87 | |
| 88 | // Setup the object heap |
| 89 | ASSERT(!Heap::HasBeenSetup()); |
| 90 | if (!Heap::Setup(create_heap_objects)) { |
| 91 | SetFatalError(); |
| 92 | return false; |
| 93 | } |
| 94 | |
| 95 | Bootstrapper::Initialize(create_heap_objects); |
| 96 | Builtins::Setup(create_heap_objects); |
| 97 | Top::Initialize(); |
| 98 | |
| 99 | if (FLAG_preemption) { |
| 100 | v8::Locker locker; |
| 101 | v8::Locker::StartPreemption(100); |
| 102 | } |
| 103 | |
| 104 | #ifdef ENABLE_DEBUGGER_SUPPORT |
| 105 | Debug::Setup(create_heap_objects); |
| 106 | #endif |
| 107 | StubCache::Initialize(create_heap_objects); |
| 108 | |
| 109 | // If we are deserializing, read the state into the now-empty heap. |
| 110 | if (des != NULL) { |
| 111 | des->Deserialize(); |
| 112 | StubCache::Clear(); |
| 113 | } |
| 114 | |
| 115 | // Deserializing may put strange things in the root array's copy of the |
| 116 | // stack guard. |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 117 | Heap::SetStackLimits(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 118 | |
| 119 | // Setup the CPU support. Must be done after heap setup and after |
| 120 | // any deserialization because we have to have the initial heap |
| 121 | // objects in place for creating the code object used for probing. |
| 122 | CPU::Setup(); |
| 123 | |
| 124 | OProfileAgent::Initialize(); |
| 125 | |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 126 | // If we are deserializing, log non-function code objects and compiled |
| 127 | // functions found in the snapshot. |
| 128 | if (des != NULL && FLAG_log_code) { |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 129 | HandleScope scope; |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 130 | LOG(LogCodeObjects()); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 131 | LOG(LogCompiledFunctions()); |
| 132 | } |
| 133 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 134 | return true; |
| 135 | } |
| 136 | |
| 137 | |
| 138 | void V8::SetFatalError() { |
| 139 | is_running_ = false; |
| 140 | has_fatal_error_ = true; |
| 141 | } |
| 142 | |
| 143 | |
| 144 | void V8::TearDown() { |
| 145 | if (!has_been_setup_ || has_been_disposed_) return; |
| 146 | |
| 147 | OProfileAgent::TearDown(); |
| 148 | |
| 149 | if (FLAG_preemption) { |
| 150 | v8::Locker locker; |
| 151 | v8::Locker::StopPreemption(); |
| 152 | } |
| 153 | |
| 154 | Builtins::TearDown(); |
| 155 | Bootstrapper::TearDown(); |
| 156 | |
| 157 | Top::TearDown(); |
| 158 | |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 159 | HeapProfiler::TearDown(); |
| 160 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 161 | CpuProfiler::TearDown(); |
| 162 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 163 | Heap::TearDown(); |
| 164 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 165 | Logger::TearDown(); |
| 166 | |
| 167 | is_running_ = false; |
| 168 | has_been_disposed_ = true; |
| 169 | } |
| 170 | |
| 171 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 172 | static uint32_t random_seed() { |
| 173 | if (FLAG_random_seed == 0) { |
| 174 | return random(); |
| 175 | } |
| 176 | return FLAG_random_seed; |
| 177 | } |
| 178 | |
| 179 | |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame^] | 180 | typedef struct { |
| 181 | uint32_t hi; |
| 182 | uint32_t lo; |
| 183 | } random_state; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 184 | |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame^] | 185 | |
| 186 | // Random number generator using George Marsaglia's MWC algorithm. |
| 187 | static uint32_t random_base(random_state *state) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 188 | // Initialize seed using the system random(). If one of the seeds |
| 189 | // should ever become zero again, or if random() returns zero, we |
| 190 | // avoid getting stuck with zero bits in hi or lo by re-initializing |
| 191 | // them on demand. |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame^] | 192 | if (state->hi == 0) state->hi = random_seed(); |
| 193 | if (state->lo == 0) state->lo = random_seed(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 194 | |
| 195 | // Mix the bits. |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame^] | 196 | state->hi = 36969 * (state->hi & 0xFFFF) + (state->hi >> 16); |
| 197 | state->lo = 18273 * (state->lo & 0xFFFF) + (state->lo >> 16); |
| 198 | return (state->hi << 16) + (state->lo & 0xFFFF); |
| 199 | } |
| 200 | |
| 201 | |
| 202 | // Used by JavaScript APIs |
| 203 | uint32_t V8::Random() { |
| 204 | static random_state state = {0, 0}; |
| 205 | return random_base(&state); |
| 206 | } |
| 207 | |
| 208 | |
| 209 | // Used internally by the JIT and memory allocator for security |
| 210 | // purposes. So, we keep a different state to prevent informations |
| 211 | // leaks that could be used in an exploit. |
| 212 | uint32_t V8::RandomPrivate() { |
| 213 | static random_state state = {0, 0}; |
| 214 | return random_base(&state); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 218 | bool V8::IdleNotification() { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 219 | // Returning true tells the caller that there is no need to call |
| 220 | // IdleNotification again. |
| 221 | if (!FLAG_use_idle_notification) return true; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 222 | |
| 223 | // Tell the heap that it may want to adjust. |
| 224 | return Heap::IdleNotification(); |
| 225 | } |
| 226 | |
| 227 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 228 | // Use a union type to avoid type-aliasing optimizations in GCC. |
| 229 | typedef union { |
| 230 | double double_value; |
| 231 | uint64_t uint64_t_value; |
| 232 | } double_int_union; |
| 233 | |
| 234 | |
| 235 | Object* V8::FillHeapNumberWithRandom(Object* heap_number) { |
| 236 | uint64_t random_bits = Random(); |
| 237 | // Make a double* from address (heap_number + sizeof(double)). |
| 238 | double_int_union* r = reinterpret_cast<double_int_union*>( |
| 239 | reinterpret_cast<char*>(heap_number) + |
| 240 | HeapNumber::kValueOffset - kHeapObjectTag); |
| 241 | // Convert 32 random bits to 0.(32 random bits) in a double |
| 242 | // by computing: |
| 243 | // ( 1.(20 0s)(32 random bits) x 2^20 ) - (1.0 x 2^20)). |
| 244 | const double binary_million = 1048576.0; |
| 245 | r->double_value = binary_million; |
| 246 | r->uint64_t_value |= random_bits; |
| 247 | r->double_value -= binary_million; |
| 248 | |
| 249 | return heap_number; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | } } // namespace v8::internal |