Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1 | // Copyright 2012 the V8 project authors. All rights reserved. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +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 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 30 | #include "isolate.h" |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 31 | #include "elements.h" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 32 | #include "bootstrapper.h" |
| 33 | #include "debug.h" |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 34 | #include "deoptimizer.h" |
| 35 | #include "heap-profiler.h" |
| 36 | #include "hydrogen.h" |
| 37 | #include "lithium-allocator.h" |
| 38 | #include "log.h" |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 39 | #include "once.h" |
| 40 | #include "platform.h" |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 41 | #include "runtime-profiler.h" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 42 | #include "serialize.h" |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 43 | #include "store-buffer.h" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 44 | |
| 45 | namespace v8 { |
| 46 | namespace internal { |
| 47 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 48 | V8_DECLARE_ONCE(init_once); |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 49 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 50 | bool V8::is_running_ = false; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 51 | bool V8::has_been_set_up_ = false; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 52 | bool V8::has_been_disposed_ = false; |
| 53 | bool V8::has_fatal_error_ = false; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 54 | bool V8::use_crankshaft_ = true; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 55 | List<CallCompletedCallback>* V8::call_completed_callbacks_ = NULL; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 56 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 57 | static LazyMutex entropy_mutex = LAZY_MUTEX_INITIALIZER; |
| 58 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 59 | static EntropySource entropy_source; |
| 60 | |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 61 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 62 | bool V8::Initialize(Deserializer* des) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 63 | FlagList::EnforceFlagImplications(); |
| 64 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 65 | InitializeOncePerProcess(); |
| 66 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 67 | // The current thread may not yet had entered an isolate to run. |
| 68 | // Note the Isolate::Current() may be non-null because for various |
| 69 | // initialization purposes an initializing thread may be assigned an isolate |
| 70 | // but not actually enter it. |
| 71 | if (i::Isolate::CurrentPerIsolateThreadData() == NULL) { |
| 72 | i::Isolate::EnterDefaultIsolate(); |
| 73 | } |
| 74 | |
| 75 | ASSERT(i::Isolate::CurrentPerIsolateThreadData() != NULL); |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 76 | ASSERT(i::Isolate::CurrentPerIsolateThreadData()->thread_id().Equals( |
| 77 | i::ThreadId::Current())); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 78 | ASSERT(i::Isolate::CurrentPerIsolateThreadData()->isolate() == |
| 79 | i::Isolate::Current()); |
| 80 | |
| 81 | if (IsDead()) return false; |
| 82 | |
| 83 | Isolate* isolate = Isolate::Current(); |
| 84 | if (isolate->IsInitialized()) return true; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 85 | |
| 86 | is_running_ = true; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 87 | has_been_set_up_ = true; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 88 | has_fatal_error_ = false; |
| 89 | has_been_disposed_ = false; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 90 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 91 | return isolate->Init(des); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | |
| 95 | void V8::SetFatalError() { |
| 96 | is_running_ = false; |
| 97 | has_fatal_error_ = true; |
| 98 | } |
| 99 | |
| 100 | |
| 101 | void V8::TearDown() { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 102 | Isolate* isolate = Isolate::Current(); |
| 103 | ASSERT(isolate->IsDefaultIsolate()); |
| 104 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 105 | if (!has_been_set_up_ || has_been_disposed_) return; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 106 | isolate->TearDown(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 107 | |
| 108 | is_running_ = false; |
| 109 | has_been_disposed_ = true; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 110 | |
| 111 | delete call_completed_callbacks_; |
| 112 | call_completed_callbacks_ = NULL; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 116 | static void seed_random(uint32_t* state) { |
| 117 | for (int i = 0; i < 2; ++i) { |
| 118 | if (FLAG_random_seed != 0) { |
| 119 | state[i] = FLAG_random_seed; |
| 120 | } else if (entropy_source != NULL) { |
| 121 | uint32_t val; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 122 | ScopedLock lock(entropy_mutex.Pointer()); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 123 | entropy_source(reinterpret_cast<unsigned char*>(&val), sizeof(uint32_t)); |
| 124 | state[i] = val; |
| 125 | } else { |
| 126 | state[i] = random(); |
| 127 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 128 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 132 | // Random number generator using George Marsaglia's MWC algorithm. |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 133 | static uint32_t random_base(uint32_t* state) { |
| 134 | // Initialize seed using the system random(). |
| 135 | // No non-zero seed will ever become zero again. |
| 136 | if (state[0] == 0) seed_random(state); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 137 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 138 | // Mix the bits. Never replaces state[i] with 0 if it is nonzero. |
| 139 | state[0] = 18273 * (state[0] & 0xFFFF) + (state[0] >> 16); |
| 140 | state[1] = 36969 * (state[1] & 0xFFFF) + (state[1] >> 16); |
| 141 | |
| 142 | return (state[0] << 14) + (state[1] & 0x3FFFF); |
| 143 | } |
| 144 | |
| 145 | |
| 146 | void V8::SetEntropySource(EntropySource source) { |
| 147 | entropy_source = source; |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 151 | void V8::SetReturnAddressLocationResolver( |
| 152 | ReturnAddressLocationResolver resolver) { |
| 153 | StackFrame::SetReturnAddressLocationResolver(resolver); |
| 154 | } |
| 155 | |
| 156 | |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 157 | // Used by JavaScript APIs |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 158 | uint32_t V8::Random(Context* context) { |
| 159 | ASSERT(context->IsGlobalContext()); |
| 160 | ByteArray* seed = context->random_seed(); |
| 161 | return random_base(reinterpret_cast<uint32_t*>(seed->GetDataStartAddress())); |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | |
| 165 | // Used internally by the JIT and memory allocator for security |
| 166 | // purposes. So, we keep a different state to prevent informations |
| 167 | // leaks that could be used in an exploit. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 168 | uint32_t V8::RandomPrivate(Isolate* isolate) { |
| 169 | ASSERT(isolate == Isolate::Current()); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 170 | return random_base(isolate->private_random_seed()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 174 | bool V8::IdleNotification(int hint) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 175 | // Returning true tells the caller that there is no need to call |
| 176 | // IdleNotification again. |
| 177 | if (!FLAG_use_idle_notification) return true; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 178 | |
| 179 | // Tell the heap that it may want to adjust. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 180 | return HEAP->IdleNotification(hint); |
| 181 | } |
| 182 | |
| 183 | |
| 184 | void V8::AddCallCompletedCallback(CallCompletedCallback callback) { |
| 185 | if (call_completed_callbacks_ == NULL) { // Lazy init. |
| 186 | call_completed_callbacks_ = new List<CallCompletedCallback>(); |
| 187 | } |
| 188 | for (int i = 0; i < call_completed_callbacks_->length(); i++) { |
| 189 | if (callback == call_completed_callbacks_->at(i)) return; |
| 190 | } |
| 191 | call_completed_callbacks_->Add(callback); |
| 192 | } |
| 193 | |
| 194 | |
| 195 | void V8::RemoveCallCompletedCallback(CallCompletedCallback callback) { |
| 196 | if (call_completed_callbacks_ == NULL) return; |
| 197 | for (int i = 0; i < call_completed_callbacks_->length(); i++) { |
| 198 | if (callback == call_completed_callbacks_->at(i)) { |
| 199 | call_completed_callbacks_->Remove(i); |
| 200 | } |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | |
| 205 | void V8::FireCallCompletedCallback(Isolate* isolate) { |
| 206 | if (call_completed_callbacks_ == NULL) return; |
| 207 | HandleScopeImplementer* handle_scope_implementer = |
| 208 | isolate->handle_scope_implementer(); |
| 209 | if (!handle_scope_implementer->CallDepthIsZero()) return; |
| 210 | // Fire callbacks. Increase call depth to prevent recursive callbacks. |
| 211 | handle_scope_implementer->IncrementCallDepth(); |
| 212 | for (int i = 0; i < call_completed_callbacks_->length(); i++) { |
| 213 | call_completed_callbacks_->at(i)(); |
| 214 | } |
| 215 | handle_scope_implementer->DecrementCallDepth(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 219 | // Use a union type to avoid type-aliasing optimizations in GCC. |
| 220 | typedef union { |
| 221 | double double_value; |
| 222 | uint64_t uint64_t_value; |
| 223 | } double_int_union; |
| 224 | |
| 225 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 226 | Object* V8::FillHeapNumberWithRandom(Object* heap_number, |
| 227 | Context* context) { |
| 228 | double_int_union r; |
| 229 | uint64_t random_bits = Random(context); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 230 | // Convert 32 random bits to 0.(32 random bits) in a double |
| 231 | // by computing: |
| 232 | // ( 1.(20 0s)(32 random bits) x 2^20 ) - (1.0 x 2^20)). |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 233 | static const double binary_million = 1048576.0; |
| 234 | r.double_value = binary_million; |
| 235 | r.uint64_t_value |= random_bits; |
| 236 | r.double_value -= binary_million; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 237 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 238 | HeapNumber::cast(heap_number)->set_value(r.double_value); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 239 | return heap_number; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 240 | } |
| 241 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 242 | void V8::InitializeOncePerProcessImpl() { |
| 243 | // Set up the platform OS support. |
| 244 | OS::SetUp(); |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 245 | |
| 246 | use_crankshaft_ = FLAG_crankshaft; |
| 247 | |
| 248 | if (Serializer::enabled()) { |
| 249 | use_crankshaft_ = false; |
| 250 | } |
| 251 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 252 | CPU::SetUp(); |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 253 | if (!CPU::SupportsCrankshaft()) { |
| 254 | use_crankshaft_ = false; |
| 255 | } |
| 256 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 257 | OS::PostSetUp(); |
| 258 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 259 | RuntimeProfiler::GlobalSetup(); |
| 260 | |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 261 | ElementsAccessor::InitializeOncePerProcess(); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 262 | |
| 263 | if (FLAG_stress_compaction) { |
| 264 | FLAG_force_marking_deque_overflows = true; |
| 265 | FLAG_gc_global = true; |
| 266 | FLAG_max_new_space_size = (1 << (kPageSizeBits - 10)) * 2; |
| 267 | } |
| 268 | |
| 269 | LOperand::SetUpCaches(); |
| 270 | } |
| 271 | |
| 272 | void V8::InitializeOncePerProcess() { |
| 273 | CallOnce(&init_once, &InitializeOncePerProcessImpl); |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 274 | } |
| 275 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 276 | } } // namespace v8::internal |