blob: 66c65e7b00a36ef63f6fe54eccb09c8431db6866 [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;
50bool V8::has_been_setup_ = false;
51bool V8::has_been_disposed_ = false;
52bool V8::has_fatal_error_ = false;
Ben Murdochb0fe1622011-05-05 13:52:32 +010053bool V8::use_crankshaft_ = true;
Steve Blocka7e24c12009-10-30 11:49:00 +000054
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000055static Mutex* entropy_mutex = OS::CreateMutex();
56static EntropySource entropy_source;
57
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -080058
Steve Block6ded16b2010-05-10 14:33:55 +010059bool V8::Initialize(Deserializer* des) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +000060 // Setting --harmony implies all other harmony flags.
61 // TODO(rossberg): Is there a better place to put this?
62 if (FLAG_harmony) {
63 FLAG_harmony_typeof = true;
64 FLAG_harmony_scoping = true;
65 FLAG_harmony_proxies = true;
66 FLAG_harmony_collections = true;
67 }
68
Ben Murdoch8b112d22011-06-08 16:22:53 +010069 InitializeOncePerProcess();
70
Steve Block44f0eee2011-05-26 01:26:41 +010071 // The current thread may not yet had entered an isolate to run.
72 // Note the Isolate::Current() may be non-null because for various
73 // initialization purposes an initializing thread may be assigned an isolate
74 // but not actually enter it.
75 if (i::Isolate::CurrentPerIsolateThreadData() == NULL) {
76 i::Isolate::EnterDefaultIsolate();
77 }
78
79 ASSERT(i::Isolate::CurrentPerIsolateThreadData() != NULL);
Ben Murdoch8b112d22011-06-08 16:22:53 +010080 ASSERT(i::Isolate::CurrentPerIsolateThreadData()->thread_id().Equals(
81 i::ThreadId::Current()));
Steve Block44f0eee2011-05-26 01:26:41 +010082 ASSERT(i::Isolate::CurrentPerIsolateThreadData()->isolate() ==
83 i::Isolate::Current());
84
85 if (IsDead()) return false;
86
87 Isolate* isolate = Isolate::Current();
88 if (isolate->IsInitialized()) return true;
Steve Blocka7e24c12009-10-30 11:49:00 +000089
90 is_running_ = true;
91 has_been_setup_ = true;
92 has_fatal_error_ = false;
93 has_been_disposed_ = false;
Steve Blocka7e24c12009-10-30 11:49:00 +000094
Steve Block44f0eee2011-05-26 01:26:41 +010095 return isolate->Init(des);
Steve Blocka7e24c12009-10-30 11:49:00 +000096}
97
98
99void V8::SetFatalError() {
100 is_running_ = false;
101 has_fatal_error_ = true;
102}
103
104
105void V8::TearDown() {
Steve Block44f0eee2011-05-26 01:26:41 +0100106 Isolate* isolate = Isolate::Current();
107 ASSERT(isolate->IsDefaultIsolate());
108
Steve Blocka7e24c12009-10-30 11:49:00 +0000109 if (!has_been_setup_ || has_been_disposed_) return;
Steve Block44f0eee2011-05-26 01:26:41 +0100110 isolate->TearDown();
Steve Blocka7e24c12009-10-30 11:49:00 +0000111
112 is_running_ = false;
113 has_been_disposed_ = true;
114}
115
116
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000117static void seed_random(uint32_t* state) {
118 for (int i = 0; i < 2; ++i) {
119 if (FLAG_random_seed != 0) {
120 state[i] = FLAG_random_seed;
121 } else if (entropy_source != NULL) {
122 uint32_t val;
123 ScopedLock lock(entropy_mutex);
124 entropy_source(reinterpret_cast<unsigned char*>(&val), sizeof(uint32_t));
125 state[i] = val;
126 } else {
127 state[i] = random();
128 }
Steve Block6ded16b2010-05-10 14:33:55 +0100129 }
Steve Block6ded16b2010-05-10 14:33:55 +0100130}
131
132
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800133// Random number generator using George Marsaglia's MWC algorithm.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000134static uint32_t random_base(uint32_t* state) {
135 // Initialize seed using the system random().
136 // No non-zero seed will ever become zero again.
137 if (state[0] == 0) seed_random(state);
Steve Blocka7e24c12009-10-30 11:49:00 +0000138
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000139 // Mix the bits. Never replaces state[i] with 0 if it is nonzero.
140 state[0] = 18273 * (state[0] & 0xFFFF) + (state[0] >> 16);
141 state[1] = 36969 * (state[1] & 0xFFFF) + (state[1] >> 16);
142
143 return (state[0] << 14) + (state[1] & 0x3FFFF);
144}
145
146
147void V8::SetEntropySource(EntropySource source) {
148 entropy_source = source;
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800149}
150
151
152// Used by JavaScript APIs
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000153uint32_t V8::Random(Context* context) {
154 ASSERT(context->IsGlobalContext());
155 ByteArray* seed = context->random_seed();
156 return random_base(reinterpret_cast<uint32_t*>(seed->GetDataStartAddress()));
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800157}
158
159
160// Used internally by the JIT and memory allocator for security
161// purposes. So, we keep a different state to prevent informations
162// leaks that could be used in an exploit.
Steve Block44f0eee2011-05-26 01:26:41 +0100163uint32_t V8::RandomPrivate(Isolate* isolate) {
164 ASSERT(isolate == Isolate::Current());
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000165 return random_base(isolate->private_random_seed());
Steve Blocka7e24c12009-10-30 11:49:00 +0000166}
167
168
Steve Block3ce2e202009-11-05 08:53:23 +0000169bool V8::IdleNotification() {
Steve Blocka7e24c12009-10-30 11:49:00 +0000170 // Returning true tells the caller that there is no need to call
171 // IdleNotification again.
172 if (!FLAG_use_idle_notification) return true;
Steve Blocka7e24c12009-10-30 11:49:00 +0000173
174 // Tell the heap that it may want to adjust.
Steve Block44f0eee2011-05-26 01:26:41 +0100175 return HEAP->IdleNotification();
Steve Blocka7e24c12009-10-30 11:49:00 +0000176}
177
178
Steve Block6ded16b2010-05-10 14:33:55 +0100179// Use a union type to avoid type-aliasing optimizations in GCC.
180typedef union {
181 double double_value;
182 uint64_t uint64_t_value;
183} double_int_union;
184
185
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000186Object* V8::FillHeapNumberWithRandom(Object* heap_number,
187 Context* context) {
188 uint64_t random_bits = Random(context);
Steve Block6ded16b2010-05-10 14:33:55 +0100189 // Make a double* from address (heap_number + sizeof(double)).
190 double_int_union* r = reinterpret_cast<double_int_union*>(
191 reinterpret_cast<char*>(heap_number) +
192 HeapNumber::kValueOffset - kHeapObjectTag);
193 // Convert 32 random bits to 0.(32 random bits) in a double
194 // by computing:
195 // ( 1.(20 0s)(32 random bits) x 2^20 ) - (1.0 x 2^20)).
196 const double binary_million = 1048576.0;
197 r->double_value = binary_million;
198 r->uint64_t_value |= random_bits;
199 r->double_value -= binary_million;
200
201 return heap_number;
Steve Blocka7e24c12009-10-30 11:49:00 +0000202}
203
Ben Murdoch8b112d22011-06-08 16:22:53 +0100204
205void V8::InitializeOncePerProcess() {
206 ScopedLock lock(init_once_mutex);
207 if (init_once_called) return;
208 init_once_called = true;
209
210 // Setup the platform OS support.
211 OS::Setup();
212
213 use_crankshaft_ = FLAG_crankshaft;
214
215 if (Serializer::enabled()) {
216 use_crankshaft_ = false;
217 }
218
219 CPU::Setup();
220 if (!CPU::SupportsCrankshaft()) {
221 use_crankshaft_ = false;
222 }
223
224 RuntimeProfiler::GlobalSetup();
225
226 // Peephole optimization might interfere with deoptimization.
227 FLAG_peephole_optimization = !use_crankshaft_;
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000228
229 ElementsAccessor::InitializeOncePerProcess();
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000230
231 if (FLAG_stress_compaction) {
232 FLAG_force_marking_deque_overflows = true;
233 FLAG_gc_global = true;
234 FLAG_max_new_space_size = (1 << (kPageSizeBits - 10)) * 2;
235 }
Ben Murdoch8b112d22011-06-08 16:22:53 +0100236}
237
Steve Blocka7e24c12009-10-30 11:49:00 +0000238} } // namespace v8::internal