svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 1 | // Copyright 2012 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 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 30 | #include "isolate.h" |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 31 | #include "elements.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 32 | #include "bootstrapper.h" |
| 33 | #include "debug.h" |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 34 | #include "deoptimizer.h" |
| 35 | #include "heap-profiler.h" |
| 36 | #include "hydrogen.h" |
| 37 | #include "lithium-allocator.h" |
| 38 | #include "log.h" |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 39 | #include "runtime-profiler.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 40 | #include "serialize.h" |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 41 | #include "store-buffer.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 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 46 | static Mutex* init_once_mutex = OS::CreateMutex(); |
| 47 | static bool init_once_called = false; |
| 48 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 49 | bool V8::is_running_ = false; |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 50 | bool V8::has_been_set_up_ = false; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 51 | bool V8::has_been_disposed_ = false; |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 52 | bool V8::has_fatal_error_ = false; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 53 | bool V8::use_crankshaft_ = true; |
rossberg@chromium.org | fab1498 | 2012-01-05 15:02:15 +0000 | [diff] [blame] | 54 | List<CallCompletedCallback>* V8::call_completed_callbacks_ = NULL; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 55 | |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 56 | static Mutex* entropy_mutex = OS::CreateMutex(); |
| 57 | static EntropySource entropy_source; |
| 58 | |
erik.corry@gmail.com | 4a6c327 | 2010-11-18 12:04:40 +0000 | [diff] [blame] | 59 | |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 60 | bool V8::Initialize(Deserializer* des) { |
ricow@chromium.org | 7ad6522 | 2011-12-19 12:13:11 +0000 | [diff] [blame] | 61 | FlagList::EnforceFlagImplications(); |
rossberg@chromium.org | b4b2aa6 | 2011-10-13 09:49:59 +0000 | [diff] [blame] | 62 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 63 | InitializeOncePerProcess(); |
| 64 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [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); |
ager@chromium.org | a9aa5fa | 2011-04-13 08:46:07 +0000 | [diff] [blame] | 74 | ASSERT(i::Isolate::CurrentPerIsolateThreadData()->thread_id().Equals( |
| 75 | i::ThreadId::Current())); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [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; |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 83 | |
| 84 | is_running_ = true; |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 85 | has_been_set_up_ = true; |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 86 | has_fatal_error_ = false; |
| 87 | has_been_disposed_ = false; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 88 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 89 | return isolate->Init(des); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 93 | void V8::SetFatalError() { |
| 94 | is_running_ = false; |
| 95 | has_fatal_error_ = true; |
| 96 | } |
| 97 | |
| 98 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 99 | void V8::TearDown() { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 100 | Isolate* isolate = Isolate::Current(); |
| 101 | ASSERT(isolate->IsDefaultIsolate()); |
| 102 | |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 103 | if (!has_been_set_up_ || has_been_disposed_) return; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 104 | isolate->TearDown(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 105 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 106 | is_running_ = false; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 107 | has_been_disposed_ = true; |
rossberg@chromium.org | fab1498 | 2012-01-05 15:02:15 +0000 | [diff] [blame] | 108 | |
| 109 | delete call_completed_callbacks_; |
| 110 | call_completed_callbacks_ = NULL; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 111 | } |
| 112 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 113 | |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 114 | static void seed_random(uint32_t* state) { |
| 115 | for (int i = 0; i < 2; ++i) { |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 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 { |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 124 | state[i] = random(); |
| 125 | } |
sgjesse@chromium.org | df7a284 | 2010-03-25 14:34:15 +0000 | [diff] [blame] | 126 | } |
sgjesse@chromium.org | df7a284 | 2010-03-25 14:34:15 +0000 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | |
erik.corry@gmail.com | 4a6c327 | 2010-11-18 12:04:40 +0000 | [diff] [blame] | 130 | // Random number generator using George Marsaglia's MWC algorithm. |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +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); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 135 | |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +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); |
erik.corry@gmail.com | 4a6c327 | 2010-11-18 12:04:40 +0000 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 144 | void V8::SetEntropySource(EntropySource source) { |
| 145 | entropy_source = source; |
| 146 | } |
| 147 | |
| 148 | |
ulan@chromium.org | 967e270 | 2012-02-28 09:49:15 +0000 | [diff] [blame] | 149 | void V8::SetReturnAddressLocationResolver( |
| 150 | ReturnAddressLocationResolver resolver) { |
| 151 | StackFrame::SetReturnAddressLocationResolver(resolver); |
| 152 | } |
| 153 | |
| 154 | |
erik.corry@gmail.com | 4a6c327 | 2010-11-18 12:04:40 +0000 | [diff] [blame] | 155 | // Used by JavaScript APIs |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 156 | uint32_t V8::Random(Context* context) { |
| 157 | ASSERT(context->IsGlobalContext()); |
| 158 | ByteArray* seed = context->random_seed(); |
| 159 | return random_base(reinterpret_cast<uint32_t*>(seed->GetDataStartAddress())); |
erik.corry@gmail.com | 4a6c327 | 2010-11-18 12:04:40 +0000 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | |
| 163 | // Used internally by the JIT and memory allocator for security |
| 164 | // purposes. So, we keep a different state to prevent informations |
| 165 | // leaks that could be used in an exploit. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 166 | uint32_t V8::RandomPrivate(Isolate* isolate) { |
| 167 | ASSERT(isolate == Isolate::Current()); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 168 | return random_base(isolate->private_random_seed()); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 169 | } |
| 170 | |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 171 | |
svenpanne@chromium.org | ecb9dd6 | 2011-12-01 08:22:35 +0000 | [diff] [blame] | 172 | bool V8::IdleNotification(int hint) { |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 173 | // Returning true tells the caller that there is no need to call |
| 174 | // IdleNotification again. |
| 175 | if (!FLAG_use_idle_notification) return true; |
ager@chromium.org | add848f | 2009-08-13 12:44:13 +0000 | [diff] [blame] | 176 | |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 177 | // Tell the heap that it may want to adjust. |
svenpanne@chromium.org | ecb9dd6 | 2011-12-01 08:22:35 +0000 | [diff] [blame] | 178 | return HEAP->IdleNotification(hint); |
ager@chromium.org | add848f | 2009-08-13 12:44:13 +0000 | [diff] [blame] | 179 | } |
| 180 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 181 | |
rossberg@chromium.org | fab1498 | 2012-01-05 15:02:15 +0000 | [diff] [blame] | 182 | void V8::AddCallCompletedCallback(CallCompletedCallback callback) { |
| 183 | if (call_completed_callbacks_ == NULL) { // Lazy init. |
| 184 | call_completed_callbacks_ = new List<CallCompletedCallback>(); |
| 185 | } |
| 186 | for (int i = 0; i < call_completed_callbacks_->length(); i++) { |
| 187 | if (callback == call_completed_callbacks_->at(i)) return; |
| 188 | } |
| 189 | call_completed_callbacks_->Add(callback); |
| 190 | } |
| 191 | |
| 192 | |
| 193 | void V8::RemoveCallCompletedCallback(CallCompletedCallback callback) { |
| 194 | if (call_completed_callbacks_ == NULL) return; |
| 195 | for (int i = 0; i < call_completed_callbacks_->length(); i++) { |
| 196 | if (callback == call_completed_callbacks_->at(i)) { |
| 197 | call_completed_callbacks_->Remove(i); |
| 198 | } |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | |
| 203 | void V8::FireCallCompletedCallback(Isolate* isolate) { |
| 204 | if (call_completed_callbacks_ == NULL) return; |
| 205 | HandleScopeImplementer* handle_scope_implementer = |
| 206 | isolate->handle_scope_implementer(); |
| 207 | if (!handle_scope_implementer->CallDepthIsZero()) return; |
| 208 | // Fire callbacks. Increase call depth to prevent recursive callbacks. |
| 209 | handle_scope_implementer->IncrementCallDepth(); |
| 210 | for (int i = 0; i < call_completed_callbacks_->length(); i++) { |
| 211 | call_completed_callbacks_->at(i)(); |
| 212 | } |
| 213 | handle_scope_implementer->DecrementCallDepth(); |
| 214 | } |
| 215 | |
| 216 | |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 217 | // Use a union type to avoid type-aliasing optimizations in GCC. |
| 218 | typedef union { |
| 219 | double double_value; |
| 220 | uint64_t uint64_t_value; |
| 221 | } double_int_union; |
| 222 | |
| 223 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 224 | Object* V8::FillHeapNumberWithRandom(Object* heap_number, |
| 225 | Context* context) { |
erik.corry@gmail.com | bbceb57 | 2012-03-09 10:52:05 +0000 | [diff] [blame^] | 226 | double_int_union r; |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 227 | uint64_t random_bits = Random(context); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 228 | // Convert 32 random bits to 0.(32 random bits) in a double |
| 229 | // by computing: |
| 230 | // ( 1.(20 0s)(32 random bits) x 2^20 ) - (1.0 x 2^20)). |
erik.corry@gmail.com | bbceb57 | 2012-03-09 10:52:05 +0000 | [diff] [blame^] | 231 | static const double binary_million = 1048576.0; |
| 232 | r.double_value = binary_million; |
| 233 | r.uint64_t_value |= random_bits; |
| 234 | r.double_value -= binary_million; |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 235 | |
erik.corry@gmail.com | bbceb57 | 2012-03-09 10:52:05 +0000 | [diff] [blame^] | 236 | HeapNumber::cast(heap_number)->set_value(r.double_value); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 237 | return heap_number; |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 238 | } |
| 239 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 240 | |
| 241 | void V8::InitializeOncePerProcess() { |
| 242 | ScopedLock lock(init_once_mutex); |
| 243 | if (init_once_called) return; |
| 244 | init_once_called = true; |
| 245 | |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 246 | // Set up the platform OS support. |
| 247 | OS::SetUp(); |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 248 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 249 | use_crankshaft_ = FLAG_crankshaft; |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 250 | |
| 251 | if (Serializer::enabled()) { |
| 252 | use_crankshaft_ = false; |
| 253 | } |
| 254 | |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 255 | CPU::SetUp(); |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 256 | if (!CPU::SupportsCrankshaft()) { |
| 257 | use_crankshaft_ = false; |
| 258 | } |
| 259 | |
ager@chromium.org | a9aa5fa | 2011-04-13 08:46:07 +0000 | [diff] [blame] | 260 | RuntimeProfiler::GlobalSetup(); |
| 261 | |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 262 | ElementsAccessor::InitializeOncePerProcess(); |
rossberg@chromium.org | b4b2aa6 | 2011-10-13 09:49:59 +0000 | [diff] [blame] | 263 | |
| 264 | if (FLAG_stress_compaction) { |
| 265 | FLAG_force_marking_deque_overflows = true; |
| 266 | FLAG_gc_global = true; |
| 267 | FLAG_max_new_space_size = (1 << (kPageSizeBits - 10)) * 2; |
| 268 | } |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 269 | } |
| 270 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 271 | } } // namespace v8::internal |