blob: 1753650f25765dbe5c0a5d73a759368929c00824 [file] [log] [blame]
svenpanne@chromium.orgb1df11d2012-02-08 10:26:21 +00001// Copyright 2012 the V8 project authors. All rights reserved.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002// 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
fschneider@chromium.org7d10be52012-04-10 12:30:14 +000030#include "assembler.h"
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000031#include "isolate.h"
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +000032#include "elements.h"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000033#include "bootstrapper.h"
34#include "debug.h"
kasperl@chromium.orga5551262010-12-07 12:49:48 +000035#include "deoptimizer.h"
fschneider@chromium.org7d10be52012-04-10 12:30:14 +000036#include "frames.h"
kasperl@chromium.orga5551262010-12-07 12:49:48 +000037#include "heap-profiler.h"
38#include "hydrogen.h"
39#include "lithium-allocator.h"
40#include "log.h"
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +000041#include "objects.h"
jkummerow@chromium.org1456e702012-03-30 08:38:13 +000042#include "once.h"
43#include "platform.h"
kasperl@chromium.orga5551262010-12-07 12:49:48 +000044#include "runtime-profiler.h"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000045#include "serialize.h"
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +000046#include "store-buffer.h"
ager@chromium.orgeadaf222009-06-16 09:43:10 +000047
kasperl@chromium.org71affb52009-05-26 05:44:31 +000048namespace v8 {
49namespace internal {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000050
jkummerow@chromium.org1456e702012-03-30 08:38:13 +000051V8_DECLARE_ONCE(init_once);
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +000052
kasperl@chromium.org71affb52009-05-26 05:44:31 +000053bool V8::is_running_ = false;
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +000054bool V8::has_been_set_up_ = false;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000055bool V8::has_been_disposed_ = false;
kasperl@chromium.org71affb52009-05-26 05:44:31 +000056bool V8::has_fatal_error_ = false;
kasperl@chromium.orga5551262010-12-07 12:49:48 +000057bool V8::use_crankshaft_ = true;
rossberg@chromium.orgfab14982012-01-05 15:02:15 +000058List<CallCompletedCallback>* V8::call_completed_callbacks_ = NULL;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000059
jkummerow@chromium.org1456e702012-03-30 08:38:13 +000060static LazyMutex entropy_mutex = LAZY_MUTEX_INITIALIZER;
61
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +000062static EntropySource entropy_source;
63
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +000064
whesse@chromium.orgcec079d2010-03-22 14:44:04 +000065bool V8::Initialize(Deserializer* des) {
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +000066 InitializeOncePerProcess();
67
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000068 // The current thread may not yet had entered an isolate to run.
69 // Note the Isolate::Current() may be non-null because for various
70 // initialization purposes an initializing thread may be assigned an isolate
71 // but not actually enter it.
72 if (i::Isolate::CurrentPerIsolateThreadData() == NULL) {
73 i::Isolate::EnterDefaultIsolate();
74 }
75
76 ASSERT(i::Isolate::CurrentPerIsolateThreadData() != NULL);
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +000077 ASSERT(i::Isolate::CurrentPerIsolateThreadData()->thread_id().Equals(
78 i::ThreadId::Current()));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000079 ASSERT(i::Isolate::CurrentPerIsolateThreadData()->isolate() ==
80 i::Isolate::Current());
81
82 if (IsDead()) return false;
83
84 Isolate* isolate = Isolate::Current();
85 if (isolate->IsInitialized()) return true;
kasperl@chromium.org71affb52009-05-26 05:44:31 +000086
87 is_running_ = true;
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +000088 has_been_set_up_ = true;
kasperl@chromium.org71affb52009-05-26 05:44:31 +000089 has_fatal_error_ = false;
90 has_been_disposed_ = false;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000091
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000092 return isolate->Init(des);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000093}
94
95
kasperl@chromium.org71affb52009-05-26 05:44:31 +000096void V8::SetFatalError() {
97 is_running_ = false;
98 has_fatal_error_ = true;
99}
100
101
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000102void V8::TearDown() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000103 Isolate* isolate = Isolate::Current();
104 ASSERT(isolate->IsDefaultIsolate());
105
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000106 if (!has_been_set_up_ || has_been_disposed_) return;
jkummerow@chromium.org1456e702012-03-30 08:38:13 +0000107
yangguo@chromium.org304cc332012-07-24 07:59:48 +0000108 // The isolate has to be torn down before clearing the LOperand
109 // caches so that the optimizing compiler thread (if running)
110 // doesn't see an inconsistent view of the lithium instructions.
111 isolate->TearDown();
112 delete isolate;
113
jkummerow@chromium.org1456e702012-03-30 08:38:13 +0000114 ElementsAccessor::TearDown();
115 LOperand::TearDownCaches();
danno@chromium.org1f34ad32012-11-26 14:53:56 +0000116 ExternalReference::TearDownMathExpData();
jkummerow@chromium.org1456e702012-03-30 08:38:13 +0000117 RegisteredExtension::UnregisterAll();
hpayer@chromium.org7c3372b2013-02-13 17:26:04 +0000118 Isolate::GlobalTearDown();
jkummerow@chromium.org1456e702012-03-30 08:38:13 +0000119
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000120 is_running_ = false;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000121 has_been_disposed_ = true;
rossberg@chromium.orgfab14982012-01-05 15:02:15 +0000122
123 delete call_completed_callbacks_;
124 call_completed_callbacks_ = NULL;
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000125
126 OS::TearDown();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000127}
128
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000129
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000130static void seed_random(uint32_t* state) {
131 for (int i = 0; i < 2; ++i) {
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +0000132 if (FLAG_random_seed != 0) {
133 state[i] = FLAG_random_seed;
134 } else if (entropy_source != NULL) {
135 uint32_t val;
jkummerow@chromium.org1456e702012-03-30 08:38:13 +0000136 ScopedLock lock(entropy_mutex.Pointer());
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +0000137 entropy_source(reinterpret_cast<unsigned char*>(&val), sizeof(uint32_t));
138 state[i] = val;
139 } else {
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000140 state[i] = random();
141 }
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +0000142 }
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +0000143}
144
145
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +0000146// Random number generator using George Marsaglia's MWC algorithm.
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000147static uint32_t random_base(uint32_t* state) {
148 // Initialize seed using the system random().
149 // No non-zero seed will ever become zero again.
150 if (state[0] == 0) seed_random(state);
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000151
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000152 // Mix the bits. Never replaces state[i] with 0 if it is nonzero.
153 state[0] = 18273 * (state[0] & 0xFFFF) + (state[0] >> 16);
154 state[1] = 36969 * (state[1] & 0xFFFF) + (state[1] >> 16);
155
156 return (state[0] << 14) + (state[1] & 0x3FFFF);
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +0000157}
158
159
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +0000160void V8::SetEntropySource(EntropySource source) {
161 entropy_source = source;
162}
163
164
ulan@chromium.org967e2702012-02-28 09:49:15 +0000165void V8::SetReturnAddressLocationResolver(
166 ReturnAddressLocationResolver resolver) {
167 StackFrame::SetReturnAddressLocationResolver(resolver);
168}
169
170
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +0000171// Used by JavaScript APIs
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000172uint32_t V8::Random(Context* context) {
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000173 ASSERT(context->IsNativeContext());
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000174 ByteArray* seed = context->random_seed();
175 return random_base(reinterpret_cast<uint32_t*>(seed->GetDataStartAddress()));
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +0000176}
177
178
179// Used internally by the JIT and memory allocator for security
180// purposes. So, we keep a different state to prevent informations
181// leaks that could be used in an exploit.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000182uint32_t V8::RandomPrivate(Isolate* isolate) {
183 ASSERT(isolate == Isolate::Current());
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000184 return random_base(isolate->private_random_seed());
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000185}
186
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +0000187
svenpanne@chromium.orgecb9dd62011-12-01 08:22:35 +0000188bool V8::IdleNotification(int hint) {
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000189 // Returning true tells the caller that there is no need to call
190 // IdleNotification again.
191 if (!FLAG_use_idle_notification) return true;
ager@chromium.orgadd848f2009-08-13 12:44:13 +0000192
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000193 // Tell the heap that it may want to adjust.
svenpanne@chromium.orgecb9dd62011-12-01 08:22:35 +0000194 return HEAP->IdleNotification(hint);
ager@chromium.orgadd848f2009-08-13 12:44:13 +0000195}
196
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000197
rossberg@chromium.orgfab14982012-01-05 15:02:15 +0000198void V8::AddCallCompletedCallback(CallCompletedCallback callback) {
199 if (call_completed_callbacks_ == NULL) { // Lazy init.
200 call_completed_callbacks_ = new List<CallCompletedCallback>();
201 }
202 for (int i = 0; i < call_completed_callbacks_->length(); i++) {
203 if (callback == call_completed_callbacks_->at(i)) return;
204 }
205 call_completed_callbacks_->Add(callback);
206}
207
208
209void V8::RemoveCallCompletedCallback(CallCompletedCallback callback) {
210 if (call_completed_callbacks_ == NULL) return;
211 for (int i = 0; i < call_completed_callbacks_->length(); i++) {
212 if (callback == call_completed_callbacks_->at(i)) {
213 call_completed_callbacks_->Remove(i);
214 }
215 }
216}
217
218
219void V8::FireCallCompletedCallback(Isolate* isolate) {
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +0000220 bool has_call_completed_callbacks = call_completed_callbacks_ != NULL;
221 bool observer_delivery_pending =
222 FLAG_harmony_observation && isolate->observer_delivery_pending();
223 if (!has_call_completed_callbacks && !observer_delivery_pending) return;
rossberg@chromium.orgfab14982012-01-05 15:02:15 +0000224 HandleScopeImplementer* handle_scope_implementer =
225 isolate->handle_scope_implementer();
226 if (!handle_scope_implementer->CallDepthIsZero()) return;
227 // Fire callbacks. Increase call depth to prevent recursive callbacks.
228 handle_scope_implementer->IncrementCallDepth();
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +0000229 if (observer_delivery_pending) {
230 JSObject::DeliverChangeRecords(isolate);
231 }
232 if (has_call_completed_callbacks) {
233 for (int i = 0; i < call_completed_callbacks_->length(); i++) {
234 call_completed_callbacks_->at(i)();
235 }
rossberg@chromium.orgfab14982012-01-05 15:02:15 +0000236 }
237 handle_scope_implementer->DecrementCallDepth();
238}
239
240
ager@chromium.org357bf652010-04-12 11:30:10 +0000241// Use a union type to avoid type-aliasing optimizations in GCC.
242typedef union {
243 double double_value;
244 uint64_t uint64_t_value;
245} double_int_union;
246
247
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000248Object* V8::FillHeapNumberWithRandom(Object* heap_number,
249 Context* context) {
erik.corry@gmail.combbceb572012-03-09 10:52:05 +0000250 double_int_union r;
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000251 uint64_t random_bits = Random(context);
ager@chromium.org357bf652010-04-12 11:30:10 +0000252 // Convert 32 random bits to 0.(32 random bits) in a double
253 // by computing:
254 // ( 1.(20 0s)(32 random bits) x 2^20 ) - (1.0 x 2^20)).
erik.corry@gmail.combbceb572012-03-09 10:52:05 +0000255 static const double binary_million = 1048576.0;
256 r.double_value = binary_million;
257 r.uint64_t_value |= random_bits;
258 r.double_value -= binary_million;
ager@chromium.org357bf652010-04-12 11:30:10 +0000259
erik.corry@gmail.combbceb572012-03-09 10:52:05 +0000260 HeapNumber::cast(heap_number)->set_value(r.double_value);
ager@chromium.org357bf652010-04-12 11:30:10 +0000261 return heap_number;
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000262}
263
jkummerow@chromium.org1456e702012-03-30 08:38:13 +0000264void V8::InitializeOncePerProcessImpl() {
yangguo@chromium.org28381b42013-01-21 14:39:38 +0000265 FlagList::EnforceFlagImplications();
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +0000266 if (FLAG_stress_compaction) {
267 FLAG_force_marking_deque_overflows = true;
268 FLAG_gc_global = true;
269 FLAG_max_new_space_size = (1 << (kPageSizeBits - 10)) * 2;
270 }
yangguo@chromium.org28381b42013-01-21 14:39:38 +0000271 OS::SetUp();
272 CPU::SetUp();
273 use_crankshaft_ = FLAG_crankshaft
274 && !Serializer::enabled()
275 && CPU::SupportsCrankshaft();
276 OS::PostSetUp();
277 RuntimeProfiler::GlobalSetUp();
278 ElementsAccessor::InitializeOncePerProcess();
jkummerow@chromium.org1456e702012-03-30 08:38:13 +0000279 LOperand::SetUpCaches();
fschneider@chromium.org7d10be52012-04-10 12:30:14 +0000280 SetUpJSCallerSavedCodeData();
281 SamplerRegistry::SetUp();
282 ExternalReference::SetUp();
jkummerow@chromium.org1456e702012-03-30 08:38:13 +0000283}
284
285void V8::InitializeOncePerProcess() {
286 CallOnce(&init_once, &InitializeOncePerProcessImpl);
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000287}
288
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000289} } // namespace v8::internal