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