blob: c8d719b1447e2b1a7275870726c644b423d6d782 [file] [log] [blame]
Steve Blocka7e24c12009-10-30 11:49:00 +00001// Copyright 2006-2009 the V8 project authors. All rights reserved.
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
30#include "bootstrapper.h"
31#include "debug.h"
32#include "serialize.h"
Steve Blockd0582a62009-12-15 09:54:21 +000033#include "simulator.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000034#include "stub-cache.h"
Kristian Monsen9dcf7e22010-06-28 14:14:28 +010035#include "heap-profiler.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000036#include "oprofile-agent.h"
Steve Blockd0582a62009-12-15 09:54:21 +000037#include "log.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000038
39namespace v8 {
40namespace internal {
41
42bool V8::is_running_ = false;
43bool V8::has_been_setup_ = false;
44bool V8::has_been_disposed_ = false;
45bool V8::has_fatal_error_ = false;
46
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -080047
Steve Block6ded16b2010-05-10 14:33:55 +010048bool V8::Initialize(Deserializer* des) {
Steve Blocka7e24c12009-10-30 11:49:00 +000049 bool create_heap_objects = des == NULL;
50 if (has_been_disposed_ || has_fatal_error_) return false;
51 if (IsRunning()) return true;
52
53 is_running_ = true;
54 has_been_setup_ = true;
55 has_fatal_error_ = false;
56 has_been_disposed_ = false;
57#ifdef DEBUG
58 // The initialization process does not handle memory exhaustion.
59 DisallowAllocationFailure disallow_allocation_failure;
60#endif
61
62 // Enable logging before setting up the heap
63 Logger::Setup();
Steve Blocka7e24c12009-10-30 11:49:00 +000064
Steve Block6ded16b2010-05-10 14:33:55 +010065 CpuProfiler::Setup();
Kristian Monsen9dcf7e22010-06-28 14:14:28 +010066 HeapProfiler::Setup();
Steve Block6ded16b2010-05-10 14:33:55 +010067
Steve Blocka7e24c12009-10-30 11:49:00 +000068 // Setup the platform OS support.
69 OS::Setup();
70
71 // Initialize other runtime facilities
John Reck59135872010-11-02 12:39:01 -070072#if defined(USE_SIMULATOR)
73#if defined(V8_TARGET_ARCH_ARM)
Steve Blocka7e24c12009-10-30 11:49:00 +000074 ::assembler::arm::Simulator::Initialize();
John Reck59135872010-11-02 12:39:01 -070075#elif defined(V8_TARGET_ARCH_MIPS)
76 ::assembler::mips::Simulator::Initialize();
77#endif
Steve Blocka7e24c12009-10-30 11:49:00 +000078#endif
79
80 { // NOLINT
81 // Ensure that the thread has a valid stack guard. The v8::Locker object
82 // will ensure this too, but we don't have to use lockers if we are only
83 // using one thread.
84 ExecutionAccess lock;
85 StackGuard::InitThread(lock);
86 }
87
88 // Setup the object heap
89 ASSERT(!Heap::HasBeenSetup());
90 if (!Heap::Setup(create_heap_objects)) {
91 SetFatalError();
92 return false;
93 }
94
95 Bootstrapper::Initialize(create_heap_objects);
96 Builtins::Setup(create_heap_objects);
97 Top::Initialize();
98
99 if (FLAG_preemption) {
100 v8::Locker locker;
101 v8::Locker::StartPreemption(100);
102 }
103
104#ifdef ENABLE_DEBUGGER_SUPPORT
105 Debug::Setup(create_heap_objects);
106#endif
107 StubCache::Initialize(create_heap_objects);
108
109 // If we are deserializing, read the state into the now-empty heap.
110 if (des != NULL) {
111 des->Deserialize();
112 StubCache::Clear();
113 }
114
115 // Deserializing may put strange things in the root array's copy of the
116 // stack guard.
Steve Blockd0582a62009-12-15 09:54:21 +0000117 Heap::SetStackLimits();
Steve Blocka7e24c12009-10-30 11:49:00 +0000118
119 // Setup the CPU support. Must be done after heap setup and after
120 // any deserialization because we have to have the initial heap
121 // objects in place for creating the code object used for probing.
122 CPU::Setup();
123
124 OProfileAgent::Initialize();
125
Andrei Popescu31002712010-02-23 13:46:05 +0000126 // If we are deserializing, log non-function code objects and compiled
127 // functions found in the snapshot.
128 if (des != NULL && FLAG_log_code) {
Steve Blockd0582a62009-12-15 09:54:21 +0000129 HandleScope scope;
Andrei Popescu31002712010-02-23 13:46:05 +0000130 LOG(LogCodeObjects());
Steve Blockd0582a62009-12-15 09:54:21 +0000131 LOG(LogCompiledFunctions());
132 }
133
Steve Blocka7e24c12009-10-30 11:49:00 +0000134 return true;
135}
136
137
138void V8::SetFatalError() {
139 is_running_ = false;
140 has_fatal_error_ = true;
141}
142
143
144void V8::TearDown() {
145 if (!has_been_setup_ || has_been_disposed_) return;
146
147 OProfileAgent::TearDown();
148
149 if (FLAG_preemption) {
150 v8::Locker locker;
151 v8::Locker::StopPreemption();
152 }
153
154 Builtins::TearDown();
155 Bootstrapper::TearDown();
156
157 Top::TearDown();
158
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100159 HeapProfiler::TearDown();
160
Steve Block6ded16b2010-05-10 14:33:55 +0100161 CpuProfiler::TearDown();
162
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100163 Heap::TearDown();
164
Steve Blocka7e24c12009-10-30 11:49:00 +0000165 Logger::TearDown();
166
167 is_running_ = false;
168 has_been_disposed_ = true;
169}
170
171
Steve Block6ded16b2010-05-10 14:33:55 +0100172static uint32_t random_seed() {
173 if (FLAG_random_seed == 0) {
174 return random();
175 }
176 return FLAG_random_seed;
177}
178
179
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800180typedef struct {
181 uint32_t hi;
182 uint32_t lo;
183} random_state;
Steve Blocka7e24c12009-10-30 11:49:00 +0000184
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800185
186// Random number generator using George Marsaglia's MWC algorithm.
187static uint32_t random_base(random_state *state) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000188 // Initialize seed using the system random(). If one of the seeds
189 // should ever become zero again, or if random() returns zero, we
190 // avoid getting stuck with zero bits in hi or lo by re-initializing
191 // them on demand.
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800192 if (state->hi == 0) state->hi = random_seed();
193 if (state->lo == 0) state->lo = random_seed();
Steve Blocka7e24c12009-10-30 11:49:00 +0000194
195 // Mix the bits.
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800196 state->hi = 36969 * (state->hi & 0xFFFF) + (state->hi >> 16);
197 state->lo = 18273 * (state->lo & 0xFFFF) + (state->lo >> 16);
198 return (state->hi << 16) + (state->lo & 0xFFFF);
199}
200
201
202// Used by JavaScript APIs
203uint32_t V8::Random() {
204 static random_state state = {0, 0};
205 return random_base(&state);
206}
207
208
209// Used internally by the JIT and memory allocator for security
210// purposes. So, we keep a different state to prevent informations
211// leaks that could be used in an exploit.
212uint32_t V8::RandomPrivate() {
213 static random_state state = {0, 0};
214 return random_base(&state);
Steve Blocka7e24c12009-10-30 11:49:00 +0000215}
216
217
Steve Block3ce2e202009-11-05 08:53:23 +0000218bool V8::IdleNotification() {
Steve Blocka7e24c12009-10-30 11:49:00 +0000219 // Returning true tells the caller that there is no need to call
220 // IdleNotification again.
221 if (!FLAG_use_idle_notification) return true;
Steve Blocka7e24c12009-10-30 11:49:00 +0000222
223 // Tell the heap that it may want to adjust.
224 return Heap::IdleNotification();
225}
226
227
Steve Block6ded16b2010-05-10 14:33:55 +0100228// Use a union type to avoid type-aliasing optimizations in GCC.
229typedef union {
230 double double_value;
231 uint64_t uint64_t_value;
232} double_int_union;
233
234
235Object* V8::FillHeapNumberWithRandom(Object* heap_number) {
236 uint64_t random_bits = Random();
237 // Make a double* from address (heap_number + sizeof(double)).
238 double_int_union* r = reinterpret_cast<double_int_union*>(
239 reinterpret_cast<char*>(heap_number) +
240 HeapNumber::kValueOffset - kHeapObjectTag);
241 // Convert 32 random bits to 0.(32 random bits) in a double
242 // by computing:
243 // ( 1.(20 0s)(32 random bits) x 2^20 ) - (1.0 x 2^20)).
244 const double binary_million = 1048576.0;
245 r->double_value = binary_million;
246 r->uint64_t_value |= random_bits;
247 r->double_value -= binary_million;
248
249 return heap_number;
Steve Blocka7e24c12009-10-30 11:49:00 +0000250}
251
252} } // namespace v8::internal