blob: 1157c9d3884d8ab329f63aac02b827c4f23afc48 [file] [log] [blame]
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001// Copyright 2012 the V8 project authors. All rights reserved.
Steve Blocka7e24c12009-10-30 11:49:00 +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
Steve Block44f0eee2011-05-26 01:26:41 +010030#include "isolate.h"
Ben Murdoch69a99ed2011-11-30 16:03:39 +000031#include "elements.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000032#include "bootstrapper.h"
33#include "debug.h"
Ben Murdochb0fe1622011-05-05 13:52:32 +010034#include "deoptimizer.h"
35#include "heap-profiler.h"
36#include "hydrogen.h"
37#include "lithium-allocator.h"
38#include "log.h"
Ben Murdoch3ef787d2012-04-12 10:51:47 +010039#include "once.h"
40#include "platform.h"
Ben Murdochb0fe1622011-05-05 13:52:32 +010041#include "runtime-profiler.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000042#include "serialize.h"
Ben Murdoch3ef787d2012-04-12 10:51:47 +010043#include "store-buffer.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000044
45namespace v8 {
46namespace internal {
47
Ben Murdoch3ef787d2012-04-12 10:51:47 +010048V8_DECLARE_ONCE(init_once);
Ben Murdoch8b112d22011-06-08 16:22:53 +010049
Steve Blocka7e24c12009-10-30 11:49:00 +000050bool V8::is_running_ = false;
Ben Murdoch3ef787d2012-04-12 10:51:47 +010051bool V8::has_been_set_up_ = false;
Steve Blocka7e24c12009-10-30 11:49:00 +000052bool V8::has_been_disposed_ = false;
53bool V8::has_fatal_error_ = false;
Ben Murdochb0fe1622011-05-05 13:52:32 +010054bool V8::use_crankshaft_ = true;
Ben Murdoch3ef787d2012-04-12 10:51:47 +010055List<CallCompletedCallback>* V8::call_completed_callbacks_ = NULL;
Steve Blocka7e24c12009-10-30 11:49:00 +000056
Ben Murdoch3ef787d2012-04-12 10:51:47 +010057static LazyMutex entropy_mutex = LAZY_MUTEX_INITIALIZER;
58
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000059static EntropySource entropy_source;
60
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -080061
Steve Block6ded16b2010-05-10 14:33:55 +010062bool V8::Initialize(Deserializer* des) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +010063 FlagList::EnforceFlagImplications();
64
Ben Murdoch8b112d22011-06-08 16:22:53 +010065 InitializeOncePerProcess();
66
Steve Block44f0eee2011-05-26 01:26:41 +010067 // 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 Murdoch8b112d22011-06-08 16:22:53 +010076 ASSERT(i::Isolate::CurrentPerIsolateThreadData()->thread_id().Equals(
77 i::ThreadId::Current()));
Steve Block44f0eee2011-05-26 01:26:41 +010078 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 Blocka7e24c12009-10-30 11:49:00 +000085
86 is_running_ = true;
Ben Murdoch3ef787d2012-04-12 10:51:47 +010087 has_been_set_up_ = true;
Steve Blocka7e24c12009-10-30 11:49:00 +000088 has_fatal_error_ = false;
89 has_been_disposed_ = false;
Steve Blocka7e24c12009-10-30 11:49:00 +000090
Steve Block44f0eee2011-05-26 01:26:41 +010091 return isolate->Init(des);
Steve Blocka7e24c12009-10-30 11:49:00 +000092}
93
94
95void V8::SetFatalError() {
96 is_running_ = false;
97 has_fatal_error_ = true;
98}
99
100
101void V8::TearDown() {
Steve Block44f0eee2011-05-26 01:26:41 +0100102 Isolate* isolate = Isolate::Current();
103 ASSERT(isolate->IsDefaultIsolate());
104
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100105 if (!has_been_set_up_ || has_been_disposed_) return;
Steve Block44f0eee2011-05-26 01:26:41 +0100106 isolate->TearDown();
Steve Blocka7e24c12009-10-30 11:49:00 +0000107
108 is_running_ = false;
109 has_been_disposed_ = true;
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100110
111 delete call_completed_callbacks_;
112 call_completed_callbacks_ = NULL;
Steve Blocka7e24c12009-10-30 11:49:00 +0000113}
114
115
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000116static 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 Murdoch3ef787d2012-04-12 10:51:47 +0100122 ScopedLock lock(entropy_mutex.Pointer());
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000123 entropy_source(reinterpret_cast<unsigned char*>(&val), sizeof(uint32_t));
124 state[i] = val;
125 } else {
126 state[i] = random();
127 }
Steve Block6ded16b2010-05-10 14:33:55 +0100128 }
Steve Block6ded16b2010-05-10 14:33:55 +0100129}
130
131
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800132// Random number generator using George Marsaglia's MWC algorithm.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000133static 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 Blocka7e24c12009-10-30 11:49:00 +0000137
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000138 // 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
146void V8::SetEntropySource(EntropySource source) {
147 entropy_source = source;
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800148}
149
150
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100151void V8::SetReturnAddressLocationResolver(
152 ReturnAddressLocationResolver resolver) {
153 StackFrame::SetReturnAddressLocationResolver(resolver);
154}
155
156
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800157// Used by JavaScript APIs
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100158uint32_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) Wang8a31eba2010-12-06 19:01:33 -0800162}
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 Block44f0eee2011-05-26 01:26:41 +0100168uint32_t V8::RandomPrivate(Isolate* isolate) {
169 ASSERT(isolate == Isolate::Current());
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000170 return random_base(isolate->private_random_seed());
Steve Blocka7e24c12009-10-30 11:49:00 +0000171}
172
173
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100174bool V8::IdleNotification(int hint) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000175 // Returning true tells the caller that there is no need to call
176 // IdleNotification again.
177 if (!FLAG_use_idle_notification) return true;
Steve Blocka7e24c12009-10-30 11:49:00 +0000178
179 // Tell the heap that it may want to adjust.
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100180 return HEAP->IdleNotification(hint);
181}
182
183
184void 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
195void 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
205void 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 Blocka7e24c12009-10-30 11:49:00 +0000216}
217
218
Steve Block6ded16b2010-05-10 14:33:55 +0100219// Use a union type to avoid type-aliasing optimizations in GCC.
220typedef union {
221 double double_value;
222 uint64_t uint64_t_value;
223} double_int_union;
224
225
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100226Object* V8::FillHeapNumberWithRandom(Object* heap_number,
227 Context* context) {
228 double_int_union r;
229 uint64_t random_bits = Random(context);
Steve Block6ded16b2010-05-10 14:33:55 +0100230 // 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 Murdoch3ef787d2012-04-12 10:51:47 +0100233 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 Block6ded16b2010-05-10 14:33:55 +0100237
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100238 HeapNumber::cast(heap_number)->set_value(r.double_value);
Steve Block6ded16b2010-05-10 14:33:55 +0100239 return heap_number;
Steve Blocka7e24c12009-10-30 11:49:00 +0000240}
241
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100242void V8::InitializeOncePerProcessImpl() {
243 // Set up the platform OS support.
244 OS::SetUp();
Ben Murdoch8b112d22011-06-08 16:22:53 +0100245
246 use_crankshaft_ = FLAG_crankshaft;
247
248 if (Serializer::enabled()) {
249 use_crankshaft_ = false;
250 }
251
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100252 CPU::SetUp();
Ben Murdoch8b112d22011-06-08 16:22:53 +0100253 if (!CPU::SupportsCrankshaft()) {
254 use_crankshaft_ = false;
255 }
256
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100257 OS::PostSetUp();
258
Ben Murdoch8b112d22011-06-08 16:22:53 +0100259 RuntimeProfiler::GlobalSetup();
260
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000261 ElementsAccessor::InitializeOncePerProcess();
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100262
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
272void V8::InitializeOncePerProcess() {
273 CallOnce(&init_once, &InitializeOncePerProcessImpl);
Ben Murdoch8b112d22011-06-08 16:22:53 +0100274}
275
Steve Blocka7e24c12009-10-30 11:49:00 +0000276} } // namespace v8::internal