blob: 36ee221948dd3a639a609907765fb18e5d5187be [file] [log] [blame]
Ben Murdoch8b112d22011-06-08 16:22:53 +01001// Copyright 2011 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 Murdochb0fe1622011-05-05 13:52:32 +010039#include "runtime-profiler.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000040#include "serialize.h"
Ben Murdoch592a9fc2012-03-05 11:04:45 +000041#include "store-buffer.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000042
43namespace v8 {
44namespace internal {
45
Ben Murdoch8b112d22011-06-08 16:22:53 +010046static Mutex* init_once_mutex = OS::CreateMutex();
47static bool init_once_called = false;
48
Steve Blocka7e24c12009-10-30 11:49:00 +000049bool V8::is_running_ = false;
Ben Murdochc7cc0282012-03-05 14:35:55 +000050bool V8::has_been_set_up_ = false;
Steve Blocka7e24c12009-10-30 11:49:00 +000051bool V8::has_been_disposed_ = false;
52bool V8::has_fatal_error_ = false;
Ben Murdochb0fe1622011-05-05 13:52:32 +010053bool V8::use_crankshaft_ = true;
Ben Murdochc7cc0282012-03-05 14:35:55 +000054List<CallCompletedCallback>* V8::call_completed_callbacks_ = NULL;
Steve Blocka7e24c12009-10-30 11:49:00 +000055
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000056static Mutex* entropy_mutex = OS::CreateMutex();
57static EntropySource entropy_source;
58
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -080059
Steve Block6ded16b2010-05-10 14:33:55 +010060bool V8::Initialize(Deserializer* des) {
Ben Murdochc7cc0282012-03-05 14:35:55 +000061 FlagList::EnforceFlagImplications();
Ben Murdoch592a9fc2012-03-05 11:04:45 +000062
Ben Murdoch8b112d22011-06-08 16:22:53 +010063 InitializeOncePerProcess();
64
Steve Block44f0eee2011-05-26 01:26:41 +010065 // 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 Murdoch8b112d22011-06-08 16:22:53 +010074 ASSERT(i::Isolate::CurrentPerIsolateThreadData()->thread_id().Equals(
75 i::ThreadId::Current()));
Steve Block44f0eee2011-05-26 01:26:41 +010076 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 Blocka7e24c12009-10-30 11:49:00 +000083
84 is_running_ = true;
Ben Murdochc7cc0282012-03-05 14:35:55 +000085 has_been_set_up_ = true;
Steve Blocka7e24c12009-10-30 11:49:00 +000086 has_fatal_error_ = false;
87 has_been_disposed_ = false;
Steve Blocka7e24c12009-10-30 11:49:00 +000088
Steve Block44f0eee2011-05-26 01:26:41 +010089 return isolate->Init(des);
Steve Blocka7e24c12009-10-30 11:49:00 +000090}
91
92
93void V8::SetFatalError() {
94 is_running_ = false;
95 has_fatal_error_ = true;
96}
97
98
99void V8::TearDown() {
Steve Block44f0eee2011-05-26 01:26:41 +0100100 Isolate* isolate = Isolate::Current();
101 ASSERT(isolate->IsDefaultIsolate());
102
Ben Murdochc7cc0282012-03-05 14:35:55 +0000103 if (!has_been_set_up_ || has_been_disposed_) return;
Steve Block44f0eee2011-05-26 01:26:41 +0100104 isolate->TearDown();
Steve Blocka7e24c12009-10-30 11:49:00 +0000105
106 is_running_ = false;
107 has_been_disposed_ = true;
Ben Murdochc7cc0282012-03-05 14:35:55 +0000108
109 delete call_completed_callbacks_;
110 call_completed_callbacks_ = NULL;
Steve Blocka7e24c12009-10-30 11:49:00 +0000111}
112
113
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000114static 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 Block6ded16b2010-05-10 14:33:55 +0100126 }
Steve Block6ded16b2010-05-10 14:33:55 +0100127}
128
129
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800130// Random number generator using George Marsaglia's MWC algorithm.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000131static 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 Blocka7e24c12009-10-30 11:49:00 +0000135
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000136 // 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
144void V8::SetEntropySource(EntropySource source) {
145 entropy_source = source;
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800146}
147
148
149// Used by JavaScript APIs
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000150uint32_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) Wang8a31eba2010-12-06 19:01:33 -0800154}
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 Block44f0eee2011-05-26 01:26:41 +0100160uint32_t V8::RandomPrivate(Isolate* isolate) {
161 ASSERT(isolate == Isolate::Current());
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000162 return random_base(isolate->private_random_seed());
Steve Blocka7e24c12009-10-30 11:49:00 +0000163}
164
165
Ben Murdochc7cc0282012-03-05 14:35:55 +0000166bool V8::IdleNotification(int hint) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000167 // Returning true tells the caller that there is no need to call
168 // IdleNotification again.
169 if (!FLAG_use_idle_notification) return true;
Steve Blocka7e24c12009-10-30 11:49:00 +0000170
171 // Tell the heap that it may want to adjust.
Ben Murdochc7cc0282012-03-05 14:35:55 +0000172 return HEAP->IdleNotification(hint);
173}
174
175
176void 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
187void 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
197void 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 Blocka7e24c12009-10-30 11:49:00 +0000208}
209
210
Steve Block6ded16b2010-05-10 14:33:55 +0100211// Use a union type to avoid type-aliasing optimizations in GCC.
212typedef union {
213 double double_value;
214 uint64_t uint64_t_value;
215} double_int_union;
216
217
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000218Object* V8::FillHeapNumberWithRandom(Object* heap_number,
219 Context* context) {
220 uint64_t random_bits = Random(context);
Steve Block6ded16b2010-05-10 14:33:55 +0100221 // 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 Blocka7e24c12009-10-30 11:49:00 +0000234}
235
Ben Murdoch8b112d22011-06-08 16:22:53 +0100236
237void V8::InitializeOncePerProcess() {
238 ScopedLock lock(init_once_mutex);
239 if (init_once_called) return;
240 init_once_called = true;
241
Ben Murdochc7cc0282012-03-05 14:35:55 +0000242 // Set up the platform OS support.
243 OS::SetUp();
Ben Murdoch8b112d22011-06-08 16:22:53 +0100244
245 use_crankshaft_ = FLAG_crankshaft;
246
247 if (Serializer::enabled()) {
248 use_crankshaft_ = false;
249 }
250
Ben Murdochc7cc0282012-03-05 14:35:55 +0000251 CPU::SetUp();
Ben Murdoch8b112d22011-06-08 16:22:53 +0100252 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 Murdoch69a99ed2011-11-30 16:03:39 +0000260
261 ElementsAccessor::InitializeOncePerProcess();
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000262
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 Murdoch8b112d22011-06-08 16:22:53 +0100268}
269
Steve Blocka7e24c12009-10-30 11:49:00 +0000270} } // namespace v8::internal