OLD | NEW |
1 // Copyright 2006-2009 the V8 project authors. All rights reserved. | 1 // Copyright 2006-2009 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
11 // with the distribution. | 11 // with the distribution. |
12 // * Neither the name of Google Inc. nor the names of its | 12 // * Neither the name of Google Inc. nor the names of its |
13 // contributors may be used to endorse or promote products derived | 13 // contributors may be used to endorse or promote products derived |
14 // from this software without specific prior written permission. | 14 // from this software without specific prior written permission. |
15 // | 15 // |
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | 17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | 18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | 19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | 20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 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. | 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | 27 |
28 #include "v8.h" | 28 #include "v8.h" |
29 | 29 |
| 30 #include "isolate.h" |
30 #include "bootstrapper.h" | 31 #include "bootstrapper.h" |
31 #include "debug.h" | 32 #include "debug.h" |
32 #include "deoptimizer.h" | 33 #include "deoptimizer.h" |
33 #include "heap-profiler.h" | 34 #include "heap-profiler.h" |
34 #include "hydrogen.h" | 35 #include "hydrogen.h" |
35 #include "lithium-allocator.h" | 36 #include "lithium-allocator.h" |
36 #include "log.h" | 37 #include "log.h" |
37 #include "runtime-profiler.h" | 38 #include "runtime-profiler.h" |
38 #include "serialize.h" | 39 #include "serialize.h" |
39 #include "simulator.h" | |
40 #include "stub-cache.h" | |
41 | 40 |
42 namespace v8 { | 41 namespace v8 { |
43 namespace internal { | 42 namespace internal { |
44 | 43 |
45 bool V8::is_running_ = false; | 44 bool V8::is_running_ = false; |
46 bool V8::has_been_setup_ = false; | 45 bool V8::has_been_setup_ = false; |
47 bool V8::has_been_disposed_ = false; | 46 bool V8::has_been_disposed_ = false; |
48 bool V8::has_fatal_error_ = false; | 47 bool V8::has_fatal_error_ = false; |
49 bool V8::use_crankshaft_ = true; | 48 bool V8::use_crankshaft_ = true; |
50 | 49 |
51 | 50 |
52 bool V8::Initialize(Deserializer* des) { | 51 bool V8::Initialize(Deserializer* des) { |
53 bool create_heap_objects = des == NULL; | 52 // The current thread may not yet had entered an isolate to run. |
54 if (has_been_disposed_ || has_fatal_error_) return false; | 53 // Note the Isolate::Current() may be non-null because for various |
55 if (IsRunning()) return true; | 54 // initialization purposes an initializing thread may be assigned an isolate |
| 55 // but not actually enter it. |
| 56 if (i::Isolate::CurrentPerIsolateThreadData() == NULL) { |
| 57 i::Isolate::EnterDefaultIsolate(); |
| 58 } |
| 59 |
| 60 ASSERT(i::Isolate::CurrentPerIsolateThreadData() != NULL); |
| 61 ASSERT(i::Isolate::CurrentPerIsolateThreadData()->thread_id() == |
| 62 i::Thread::GetThreadLocalInt(i::Isolate::thread_id_key())); |
| 63 ASSERT(i::Isolate::CurrentPerIsolateThreadData()->isolate() == |
| 64 i::Isolate::Current()); |
| 65 |
| 66 if (IsDead()) return false; |
| 67 |
| 68 Isolate* isolate = Isolate::Current(); |
| 69 if (isolate->IsInitialized()) return true; |
56 | 70 |
57 #if defined(V8_TARGET_ARCH_ARM) && !defined(USE_ARM_EABI) | 71 #if defined(V8_TARGET_ARCH_ARM) && !defined(USE_ARM_EABI) |
58 use_crankshaft_ = false; | 72 use_crankshaft_ = false; |
59 #else | 73 #else |
60 use_crankshaft_ = FLAG_crankshaft; | 74 use_crankshaft_ = FLAG_crankshaft; |
61 #endif | 75 #endif |
62 | 76 |
63 // Peephole optimization might interfere with deoptimization. | 77 // Peephole optimization might interfere with deoptimization. |
64 FLAG_peephole_optimization = !use_crankshaft_; | 78 FLAG_peephole_optimization = !use_crankshaft_; |
| 79 |
65 is_running_ = true; | 80 is_running_ = true; |
66 has_been_setup_ = true; | 81 has_been_setup_ = true; |
67 has_fatal_error_ = false; | 82 has_fatal_error_ = false; |
68 has_been_disposed_ = false; | 83 has_been_disposed_ = false; |
69 #ifdef DEBUG | |
70 // The initialization process does not handle memory exhaustion. | |
71 DisallowAllocationFailure disallow_allocation_failure; | |
72 #endif | |
73 | 84 |
74 // Enable logging before setting up the heap | 85 return isolate->Init(des); |
75 Logger::Setup(); | |
76 | |
77 CpuProfiler::Setup(); | |
78 HeapProfiler::Setup(); | |
79 | |
80 // Setup the platform OS support. | |
81 OS::Setup(); | |
82 | |
83 // Initialize other runtime facilities | |
84 #if defined(USE_SIMULATOR) | |
85 #if defined(V8_TARGET_ARCH_ARM) | |
86 Simulator::Initialize(); | |
87 #elif defined(V8_TARGET_ARCH_MIPS) | |
88 ::assembler::mips::Simulator::Initialize(); | |
89 #endif | |
90 #endif | |
91 | |
92 { // NOLINT | |
93 // Ensure that the thread has a valid stack guard. The v8::Locker object | |
94 // will ensure this too, but we don't have to use lockers if we are only | |
95 // using one thread. | |
96 ExecutionAccess lock; | |
97 StackGuard::InitThread(lock); | |
98 } | |
99 | |
100 // Setup the object heap | |
101 ASSERT(!Heap::HasBeenSetup()); | |
102 if (!Heap::Setup(create_heap_objects)) { | |
103 SetFatalError(); | |
104 return false; | |
105 } | |
106 | |
107 Bootstrapper::Initialize(create_heap_objects); | |
108 Builtins::Setup(create_heap_objects); | |
109 Top::Initialize(); | |
110 | |
111 if (FLAG_preemption) { | |
112 v8::Locker locker; | |
113 v8::Locker::StartPreemption(100); | |
114 } | |
115 | |
116 #ifdef ENABLE_DEBUGGER_SUPPORT | |
117 Debug::Setup(create_heap_objects); | |
118 #endif | |
119 StubCache::Initialize(create_heap_objects); | |
120 | |
121 // If we are deserializing, read the state into the now-empty heap. | |
122 if (des != NULL) { | |
123 des->Deserialize(); | |
124 StubCache::Clear(); | |
125 } | |
126 | |
127 // Deserializing may put strange things in the root array's copy of the | |
128 // stack guard. | |
129 Heap::SetStackLimits(); | |
130 | |
131 // Setup the CPU support. Must be done after heap setup and after | |
132 // any deserialization because we have to have the initial heap | |
133 // objects in place for creating the code object used for probing. | |
134 CPU::Setup(); | |
135 | |
136 Deoptimizer::Setup(); | |
137 LAllocator::Setup(); | |
138 RuntimeProfiler::Setup(); | |
139 | |
140 // If we are deserializing, log non-function code objects and compiled | |
141 // functions found in the snapshot. | |
142 if (des != NULL && FLAG_log_code) { | |
143 HandleScope scope; | |
144 LOG(LogCodeObjects()); | |
145 LOG(LogCompiledFunctions()); | |
146 } | |
147 | |
148 return true; | |
149 } | 86 } |
150 | 87 |
151 | 88 |
152 void V8::SetFatalError() { | 89 void V8::SetFatalError() { |
153 is_running_ = false; | 90 is_running_ = false; |
154 has_fatal_error_ = true; | 91 has_fatal_error_ = true; |
155 } | 92 } |
156 | 93 |
157 | 94 |
158 void V8::TearDown() { | 95 void V8::TearDown() { |
| 96 Isolate* isolate = Isolate::Current(); |
| 97 ASSERT(isolate->IsDefaultIsolate()); |
| 98 |
159 if (!has_been_setup_ || has_been_disposed_) return; | 99 if (!has_been_setup_ || has_been_disposed_) return; |
160 | 100 isolate->TearDown(); |
161 if (FLAG_hydrogen_stats) HStatistics::Instance()->Print(); | |
162 | |
163 // We must stop the logger before we tear down other components. | |
164 Logger::EnsureTickerStopped(); | |
165 | |
166 Deoptimizer::TearDown(); | |
167 | |
168 if (FLAG_preemption) { | |
169 v8::Locker locker; | |
170 v8::Locker::StopPreemption(); | |
171 } | |
172 | |
173 Builtins::TearDown(); | |
174 Bootstrapper::TearDown(); | |
175 | |
176 Top::TearDown(); | |
177 | |
178 HeapProfiler::TearDown(); | |
179 CpuProfiler::TearDown(); | |
180 RuntimeProfiler::TearDown(); | |
181 | |
182 Logger::TearDown(); | |
183 Heap::TearDown(); | |
184 | 101 |
185 is_running_ = false; | 102 is_running_ = false; |
186 has_been_disposed_ = true; | 103 has_been_disposed_ = true; |
187 } | 104 } |
188 | 105 |
189 | 106 |
190 static uint32_t random_seed() { | 107 static uint32_t random_seed() { |
191 if (FLAG_random_seed == 0) { | 108 if (FLAG_random_seed == 0) { |
192 return random(); | 109 return random(); |
193 } | 110 } |
(...skipping 17 matching lines...) Expand all Loading... |
211 if (state->lo == 0) state->lo = random_seed(); | 128 if (state->lo == 0) state->lo = random_seed(); |
212 | 129 |
213 // Mix the bits. | 130 // Mix the bits. |
214 state->hi = 36969 * (state->hi & 0xFFFF) + (state->hi >> 16); | 131 state->hi = 36969 * (state->hi & 0xFFFF) + (state->hi >> 16); |
215 state->lo = 18273 * (state->lo & 0xFFFF) + (state->lo >> 16); | 132 state->lo = 18273 * (state->lo & 0xFFFF) + (state->lo >> 16); |
216 return (state->hi << 16) + (state->lo & 0xFFFF); | 133 return (state->hi << 16) + (state->lo & 0xFFFF); |
217 } | 134 } |
218 | 135 |
219 | 136 |
220 // Used by JavaScript APIs | 137 // Used by JavaScript APIs |
221 uint32_t V8::Random() { | 138 uint32_t V8::Random(Isolate* isolate) { |
| 139 ASSERT(isolate == Isolate::Current()); |
| 140 // TODO(isolates): move lo and hi to isolate |
222 static random_state state = {0, 0}; | 141 static random_state state = {0, 0}; |
223 return random_base(&state); | 142 return random_base(&state); |
224 } | 143 } |
225 | 144 |
226 | 145 |
227 // Used internally by the JIT and memory allocator for security | 146 // Used internally by the JIT and memory allocator for security |
228 // purposes. So, we keep a different state to prevent informations | 147 // purposes. So, we keep a different state to prevent informations |
229 // leaks that could be used in an exploit. | 148 // leaks that could be used in an exploit. |
230 uint32_t V8::RandomPrivate() { | 149 uint32_t V8::RandomPrivate(Isolate* isolate) { |
| 150 ASSERT(isolate == Isolate::Current()); |
| 151 // TODO(isolates): move lo and hi to isolate |
231 static random_state state = {0, 0}; | 152 static random_state state = {0, 0}; |
232 return random_base(&state); | 153 return random_base(&state); |
233 } | 154 } |
234 | 155 |
235 | 156 |
236 bool V8::IdleNotification() { | 157 bool V8::IdleNotification() { |
237 // Returning true tells the caller that there is no need to call | 158 // Returning true tells the caller that there is no need to call |
238 // IdleNotification again. | 159 // IdleNotification again. |
239 if (!FLAG_use_idle_notification) return true; | 160 if (!FLAG_use_idle_notification) return true; |
240 | 161 |
241 // Tell the heap that it may want to adjust. | 162 // Tell the heap that it may want to adjust. |
242 return Heap::IdleNotification(); | 163 return HEAP->IdleNotification(); |
243 } | 164 } |
244 | 165 |
245 | 166 |
246 // Use a union type to avoid type-aliasing optimizations in GCC. | 167 // Use a union type to avoid type-aliasing optimizations in GCC. |
247 typedef union { | 168 typedef union { |
248 double double_value; | 169 double double_value; |
249 uint64_t uint64_t_value; | 170 uint64_t uint64_t_value; |
250 } double_int_union; | 171 } double_int_union; |
251 | 172 |
252 | 173 |
253 Object* V8::FillHeapNumberWithRandom(Object* heap_number) { | 174 Object* V8::FillHeapNumberWithRandom(Object* heap_number) { |
254 uint64_t random_bits = Random(); | 175 uint64_t random_bits = Random(Isolate::Current()); |
255 // Make a double* from address (heap_number + sizeof(double)). | 176 // Make a double* from address (heap_number + sizeof(double)). |
256 double_int_union* r = reinterpret_cast<double_int_union*>( | 177 double_int_union* r = reinterpret_cast<double_int_union*>( |
257 reinterpret_cast<char*>(heap_number) + | 178 reinterpret_cast<char*>(heap_number) + |
258 HeapNumber::kValueOffset - kHeapObjectTag); | 179 HeapNumber::kValueOffset - kHeapObjectTag); |
259 // Convert 32 random bits to 0.(32 random bits) in a double | 180 // Convert 32 random bits to 0.(32 random bits) in a double |
260 // by computing: | 181 // by computing: |
261 // ( 1.(20 0s)(32 random bits) x 2^20 ) - (1.0 x 2^20)). | 182 // ( 1.(20 0s)(32 random bits) x 2^20 ) - (1.0 x 2^20)). |
262 const double binary_million = 1048576.0; | 183 const double binary_million = 1048576.0; |
263 r->double_value = binary_million; | 184 r->double_value = binary_million; |
264 r->uint64_t_value |= random_bits; | 185 r->uint64_t_value |= random_bits; |
265 r->double_value -= binary_million; | 186 r->double_value -= binary_million; |
266 | 187 |
267 return heap_number; | 188 return heap_number; |
268 } | 189 } |
269 | 190 |
270 } } // namespace v8::internal | 191 } } // namespace v8::internal |
OLD | NEW |