OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "gin/public/isolate_holder.h" | 5 #include "gin/v8_initializer.h" |
6 | 6 |
7 #include <stdlib.h> | 7 #include "base/basictypes.h" |
8 #include <string.h> | 8 #include "base/files/file.h" |
9 | 9 #include "base/files/file_path.h" |
10 #include "base/files/memory_mapped_file.h" | 10 #include "base/files/memory_mapped_file.h" |
11 #include "base/logging.h" | 11 #include "base/logging.h" |
12 #include "base/message_loop/message_loop.h" | |
13 #include "base/rand_util.h" | 12 #include "base/rand_util.h" |
14 #include "base/strings/sys_string_conversions.h" | 13 #include "base/strings/sys_string_conversions.h" |
15 #include "base/sys_info.h" | |
16 #include "crypto/sha2.h" | 14 #include "crypto/sha2.h" |
17 #include "gin/array_buffer.h" | |
18 #include "gin/debug_impl.h" | |
19 #include "gin/function_template.h" | |
20 #include "gin/per_isolate_data.h" | |
21 #include "gin/public/v8_platform.h" | |
22 #include "gin/run_microtasks_observer.h" | |
23 | 15 |
24 #if defined(V8_USE_EXTERNAL_STARTUP_DATA) | 16 #if defined(V8_USE_EXTERNAL_STARTUP_DATA) |
25 #if defined(OS_MACOSX) | 17 #if defined(OS_MACOSX) |
26 #include "base/mac/foundation_util.h" | 18 #include "base/mac/foundation_util.h" |
27 #endif // OS_MACOSX | 19 #endif // OS_MACOSX |
28 #include "base/path_service.h" | 20 #include "base/path_service.h" |
29 #endif // V8_USE_EXTERNAL_STARTUP_DATA | 21 #endif // V8_USE_EXTERNAL_STARTUP_DATA |
30 | 22 |
31 namespace gin { | 23 namespace gin { |
32 | 24 |
33 namespace { | 25 namespace { |
34 | 26 |
35 v8::ArrayBuffer::Allocator* g_array_buffer_allocator = NULL; | 27 v8::ArrayBuffer::Allocator* g_array_buffer_allocator = nullptr; |
36 | 28 |
37 bool GenerateEntropy(unsigned char* buffer, size_t amount) { | 29 bool GenerateEntropy(unsigned char* buffer, size_t amount) { |
38 base::RandBytes(buffer, amount); | 30 base::RandBytes(buffer, amount); |
39 return true; | 31 return true; |
40 } | 32 } |
41 | 33 |
42 base::MemoryMappedFile* g_mapped_natives = NULL; | 34 base::MemoryMappedFile* g_mapped_natives = NULL; |
43 base::MemoryMappedFile* g_mapped_snapshot = NULL; | 35 base::MemoryMappedFile* g_mapped_snapshot = NULL; |
rmcilroy
2015/03/30 10:29:22
nit - could you update these to nullptr's too sinc
oth
2015/03/30 15:55:19
Done.
| |
44 | 36 |
45 #if defined(V8_USE_EXTERNAL_STARTUP_DATA) | 37 #if defined(V8_USE_EXTERNAL_STARTUP_DATA) |
46 bool MapV8Files(base::FilePath* natives_path, | 38 bool MapV8Files(base::File natives_file, |
47 base::FilePath* snapshot_path, | 39 base::File snapshot_file, |
48 int natives_fd = -1, | |
49 int snapshot_fd = -1, | |
50 base::MemoryMappedFile::Region natives_region = | 40 base::MemoryMappedFile::Region natives_region = |
51 base::MemoryMappedFile::Region::kWholeFile, | 41 base::MemoryMappedFile::Region::kWholeFile, |
52 base::MemoryMappedFile::Region snapshot_region = | 42 base::MemoryMappedFile::Region snapshot_region = |
53 base::MemoryMappedFile::Region::kWholeFile) { | 43 base::MemoryMappedFile::Region::kWholeFile) { |
54 int flags = base::File::FLAG_OPEN | base::File::FLAG_READ; | |
55 | |
56 g_mapped_natives = new base::MemoryMappedFile; | 44 g_mapped_natives = new base::MemoryMappedFile; |
57 if (!g_mapped_natives->IsValid()) { | 45 if (!g_mapped_natives->IsValid()) { |
58 #if !defined(OS_WIN) | 46 if (!g_mapped_natives->Initialize(natives_file.Pass(), natives_region)) { |
59 if (natives_fd == -1 | |
60 ? !g_mapped_natives->Initialize(base::File(*natives_path, flags), | |
61 natives_region) | |
62 : !g_mapped_natives->Initialize(base::File(natives_fd), | |
63 natives_region)) { | |
64 #else | |
65 if (!g_mapped_natives->Initialize(base::File(*natives_path, flags), | |
66 natives_region)) { | |
67 #endif // !OS_WIN | |
68 delete g_mapped_natives; | 47 delete g_mapped_natives; |
69 g_mapped_natives = NULL; | 48 g_mapped_natives = NULL; |
70 LOG(FATAL) << "Couldn't mmap v8 natives data file"; | 49 LOG(FATAL) << "Couldn't mmap v8 natives data file"; |
71 return false; | 50 return false; |
72 } | 51 } |
73 } | 52 } |
74 | 53 |
75 g_mapped_snapshot = new base::MemoryMappedFile; | 54 g_mapped_snapshot = new base::MemoryMappedFile; |
76 if (!g_mapped_snapshot->IsValid()) { | 55 if (!g_mapped_snapshot->IsValid()) { |
77 #if !defined(OS_WIN) | 56 if (!g_mapped_snapshot->Initialize(snapshot_file.Pass(), snapshot_region)) { |
78 if (snapshot_fd == -1 | |
79 ? !g_mapped_snapshot->Initialize(base::File(*snapshot_path, flags), | |
80 snapshot_region) | |
81 : !g_mapped_snapshot->Initialize(base::File(snapshot_fd), | |
82 snapshot_region)) { | |
83 #else | |
84 if (!g_mapped_snapshot->Initialize(base::File(*snapshot_path, flags), | |
85 snapshot_region)) { | |
86 #endif // !OS_WIN | |
87 delete g_mapped_snapshot; | 57 delete g_mapped_snapshot; |
88 g_mapped_snapshot = NULL; | 58 g_mapped_snapshot = NULL; |
89 LOG(ERROR) << "Couldn't mmap v8 snapshot data file"; | 59 LOG(ERROR) << "Couldn't mmap v8 snapshot data file"; |
90 return false; | 60 return false; |
91 } | 61 } |
92 } | 62 } |
93 | 63 |
94 return true; | 64 return true; |
95 } | 65 } |
96 | 66 |
(...skipping 14 matching lines...) Expand all Loading... | |
111 | 81 |
112 #if defined(V8_USE_EXTERNAL_STARTUP_DATA) | 82 #if defined(V8_USE_EXTERNAL_STARTUP_DATA) |
113 | 83 |
114 #if defined(V8_VERIFY_EXTERNAL_STARTUP_DATA) | 84 #if defined(V8_VERIFY_EXTERNAL_STARTUP_DATA) |
115 // Defined in gen/gin/v8_snapshot_fingerprint.cc | 85 // Defined in gen/gin/v8_snapshot_fingerprint.cc |
116 extern const unsigned char g_natives_fingerprint[]; | 86 extern const unsigned char g_natives_fingerprint[]; |
117 extern const unsigned char g_snapshot_fingerprint[]; | 87 extern const unsigned char g_snapshot_fingerprint[]; |
118 #endif // V8_VERIFY_EXTERNAL_STARTUP_DATA | 88 #endif // V8_VERIFY_EXTERNAL_STARTUP_DATA |
119 | 89 |
120 #if !defined(OS_MACOSX) | 90 #if !defined(OS_MACOSX) |
121 const int IsolateHolder::kV8SnapshotBasePathKey = | 91 const int V8Initializer::kV8SnapshotBasePathKey = |
122 #if defined(OS_ANDROID) | 92 #if defined(OS_ANDROID) |
123 base::DIR_ANDROID_APP_DATA; | 93 base::DIR_ANDROID_APP_DATA; |
124 #elif defined(OS_POSIX) | 94 #elif defined(OS_POSIX) |
125 base::DIR_EXE; | 95 base::DIR_EXE; |
126 #elif defined(OS_WIN) | 96 #elif defined(OS_WIN) |
127 base::DIR_MODULE; | 97 base::DIR_MODULE; |
128 #endif // OS_ANDROID | 98 #endif // OS_ANDROID |
129 #endif // !OS_MACOSX | 99 #endif // !OS_MACOSX |
130 | 100 |
131 const char IsolateHolder::kNativesFileName[] = "natives_blob.bin"; | 101 const char V8Initializer::kNativesFileName[] = "natives_blob.bin"; |
132 const char IsolateHolder::kSnapshotFileName[] = "snapshot_blob.bin"; | 102 const char V8Initializer::kSnapshotFileName[] = "snapshot_blob.bin"; |
133 | 103 |
134 // static | 104 // static |
135 bool IsolateHolder::LoadV8Snapshot() { | 105 void V8Initializer::Initialize(ScriptMode mode, |
106 v8::ArrayBuffer::Allocator* allocator) { | |
107 CHECK(allocator); | |
108 static bool v8_is_initialized = false; | |
109 if (v8_is_initialized) | |
110 return; | |
111 v8::V8::InitializePlatform(V8Platform::Get()); | |
112 v8::V8::SetArrayBufferAllocator(allocator); | |
113 g_array_buffer_allocator = allocator; | |
114 if (mode == gin::V8Initializer::kStrictMode) { | |
115 static const char use_strict[] = "--use_strict"; | |
116 v8::V8::SetFlagsFromString(use_strict, sizeof(use_strict) - 1); | |
117 } | |
118 | |
119 v8::StartupData natives; | |
120 natives.data = reinterpret_cast<const char*>(g_mapped_natives->data()); | |
121 natives.raw_size = static_cast<int>(g_mapped_natives->length()); | |
122 v8::V8::SetNativesDataBlob(&natives); | |
123 | |
124 v8::StartupData snapshot; | |
125 snapshot.data = reinterpret_cast<const char*>(g_mapped_snapshot->data()); | |
126 snapshot.raw_size = static_cast<int>(g_mapped_snapshot->length()); | |
127 v8::V8::SetSnapshotDataBlob(&snapshot); | |
128 | |
129 v8::V8::SetEntropySource(&GenerateEntropy); | |
130 v8::V8::Initialize(); | |
131 | |
132 v8_is_initialized = true; | |
133 } | |
134 | |
135 // static | |
136 v8::ArrayBuffer::Allocator* V8Initializer::GetArrayBufferAllocator() { | |
137 return g_array_buffer_allocator; | |
rmcilroy
2015/03/30 10:29:22
let's keep the g_array_buffer_allocator in Isolate
| |
138 } | |
139 | |
140 // static | |
141 void V8Initializer::GetV8ExternalSnapshotData(const char** natives_data_out, | |
142 int* natives_size_out, | |
143 const char** snapshot_data_out, | |
144 int* snapshot_size_out) { | |
145 if (!g_mapped_natives || !g_mapped_snapshot) { | |
146 *natives_data_out = *snapshot_data_out = NULL; | |
147 *natives_size_out = *snapshot_size_out = 0; | |
148 return; | |
149 } | |
150 *natives_data_out = reinterpret_cast<const char*>(g_mapped_natives->data()); | |
151 *snapshot_data_out = reinterpret_cast<const char*>(g_mapped_snapshot->data()); | |
152 *natives_size_out = static_cast<int>(g_mapped_natives->length()); | |
153 *snapshot_size_out = static_cast<int>(g_mapped_snapshot->length()); | |
154 } | |
155 | |
156 // static | |
157 bool V8Initializer::LoadV8Snapshot() { | |
136 if (g_mapped_natives && g_mapped_snapshot) | 158 if (g_mapped_natives && g_mapped_snapshot) |
137 return true; | 159 return true; |
138 | 160 |
139 #if !defined(OS_MACOSX) | 161 #if !defined(OS_MACOSX) |
140 base::FilePath data_path; | 162 base::FilePath data_path; |
141 PathService::Get(kV8SnapshotBasePathKey, &data_path); | 163 PathService::Get(kV8SnapshotBasePathKey, &data_path); |
142 DCHECK(!data_path.empty()); | 164 DCHECK(!data_path.empty()); |
143 | 165 |
144 base::FilePath natives_path = data_path.AppendASCII(kNativesFileName); | 166 base::FilePath natives_path = data_path.AppendASCII(kNativesFileName); |
145 base::FilePath snapshot_path = data_path.AppendASCII(kSnapshotFileName); | 167 base::FilePath snapshot_path = data_path.AppendASCII(kSnapshotFileName); |
146 #else // !defined(OS_MACOSX) | 168 #else // !defined(OS_MACOSX) |
147 base::ScopedCFTypeRef<CFStringRef> natives_file_name( | 169 base::ScopedCFTypeRef<CFStringRef> natives_file_name( |
148 base::SysUTF8ToCFStringRef(kNativesFileName)); | 170 base::SysUTF8ToCFStringRef(kNativesFileName)); |
149 base::FilePath natives_path = base::mac::PathForFrameworkBundleResource( | 171 base::FilePath natives_path = |
150 natives_file_name); | 172 base::mac::PathForFrameworkBundleResource(natives_file_name); |
151 base::ScopedCFTypeRef<CFStringRef> snapshot_file_name( | 173 base::ScopedCFTypeRef<CFStringRef> snapshot_file_name( |
152 base::SysUTF8ToCFStringRef(kSnapshotFileName)); | 174 base::SysUTF8ToCFStringRef(kSnapshotFileName)); |
153 base::FilePath snapshot_path = base::mac::PathForFrameworkBundleResource( | 175 base::FilePath snapshot_path = |
154 snapshot_file_name); | 176 base::mac::PathForFrameworkBundleResource(snapshot_file_name); |
155 DCHECK(!natives_path.empty()); | 177 DCHECK(!natives_path.empty()); |
156 DCHECK(!snapshot_path.empty()); | 178 DCHECK(!snapshot_path.empty()); |
157 #endif // !defined(OS_MACOSX) | 179 #endif // !defined(OS_MACOSX) |
158 | 180 |
159 if (!MapV8Files(&natives_path, &snapshot_path)) | 181 int flags = base::File::FLAG_OPEN | base::File::FLAG_READ; |
182 if (!MapV8Files(base::File(natives_path, flags), | |
183 base::File(snapshot_path, flags))) | |
160 return false; | 184 return false; |
161 | 185 |
162 #if defined(V8_VERIFY_EXTERNAL_STARTUP_DATA) | 186 #if defined(V8_VERIFY_EXTERNAL_STARTUP_DATA) |
163 return VerifyV8SnapshotFile(g_mapped_natives, g_natives_fingerprint) && | 187 return VerifyV8SnapshotFile(g_mapped_natives, g_natives_fingerprint) && |
164 VerifyV8SnapshotFile(g_mapped_snapshot, g_snapshot_fingerprint); | 188 VerifyV8SnapshotFile(g_mapped_snapshot, g_snapshot_fingerprint); |
165 #else | 189 #else |
166 return true; | 190 return true; |
167 #endif // V8_VERIFY_EXTERNAL_STARTUP_DATA | 191 #endif // V8_VERIFY_EXTERNAL_STARTUP_DATA |
168 } | 192 } |
169 | 193 |
170 //static | 194 // static |
171 bool IsolateHolder::LoadV8SnapshotFd(int natives_fd, | 195 bool V8Initializer::LoadV8SnapshotFromFile(base::PlatformFile natives_pf, |
172 int64 natives_offset, | 196 int64 natives_offset, |
173 int64 natives_size, | 197 int64 natives_size, |
174 int snapshot_fd, | 198 base::PlatformFile snapshot_pf, |
175 int64 snapshot_offset, | 199 int64 snapshot_offset, |
176 int64 snapshot_size) { | 200 int64 snapshot_size) { |
177 if (g_mapped_natives && g_mapped_snapshot) | 201 if (g_mapped_natives && g_mapped_snapshot) |
178 return true; | 202 return true; |
179 | 203 |
180 base::MemoryMappedFile::Region natives_region = | 204 base::MemoryMappedFile::Region natives_region = |
181 base::MemoryMappedFile::Region::kWholeFile; | 205 base::MemoryMappedFile::Region::kWholeFile; |
182 if (natives_size != 0 || natives_offset != 0) { | 206 if (natives_size != 0 || natives_offset != 0) { |
183 natives_region = | 207 natives_region = |
184 base::MemoryMappedFile::Region(natives_offset, natives_size); | 208 base::MemoryMappedFile::Region(natives_offset, natives_size); |
185 } | 209 } |
186 | 210 |
187 base::MemoryMappedFile::Region snapshot_region = | 211 base::MemoryMappedFile::Region snapshot_region = |
188 base::MemoryMappedFile::Region::kWholeFile; | 212 base::MemoryMappedFile::Region::kWholeFile; |
189 if (natives_size != 0 || natives_offset != 0) { | 213 if (natives_size != 0 || natives_offset != 0) { |
190 snapshot_region = | 214 snapshot_region = |
191 base::MemoryMappedFile::Region(snapshot_offset, snapshot_size); | 215 base::MemoryMappedFile::Region(snapshot_offset, snapshot_size); |
192 } | 216 } |
193 | 217 |
194 return MapV8Files( | 218 return MapV8Files(base::File(natives_pf), base::File(snapshot_pf), |
195 NULL, NULL, natives_fd, snapshot_fd, natives_region, snapshot_region); | 219 natives_region, snapshot_region); |
196 } | 220 } |
221 | |
197 #endif // V8_USE_EXTERNAL_STARTUP_DATA | 222 #endif // V8_USE_EXTERNAL_STARTUP_DATA |
198 | 223 |
199 //static | |
200 void IsolateHolder::GetV8ExternalSnapshotData(const char** natives_data_out, | |
201 int* natives_size_out, | |
202 const char** snapshot_data_out, | |
203 int* snapshot_size_out) { | |
204 if (!g_mapped_natives || !g_mapped_snapshot) { | |
205 *natives_data_out = *snapshot_data_out = NULL; | |
206 *natives_size_out = *snapshot_size_out = 0; | |
207 return; | |
208 } | |
209 *natives_data_out = reinterpret_cast<const char*>(g_mapped_natives->data()); | |
210 *snapshot_data_out = reinterpret_cast<const char*>(g_mapped_snapshot->data()); | |
211 *natives_size_out = static_cast<int>(g_mapped_natives->length()); | |
212 *snapshot_size_out = static_cast<int>(g_mapped_snapshot->length()); | |
213 } | |
214 | |
215 IsolateHolder::IsolateHolder() { | |
216 CHECK(g_array_buffer_allocator) | |
217 << "You need to invoke gin::IsolateHolder::Initialize first"; | |
218 v8::Isolate::CreateParams params; | |
219 params.entry_hook = DebugImpl::GetFunctionEntryHook(); | |
220 params.code_event_handler = DebugImpl::GetJitCodeEventHandler(); | |
221 params.constraints.ConfigureDefaults(base::SysInfo::AmountOfPhysicalMemory(), | |
222 base::SysInfo::AmountOfVirtualMemory(), | |
223 base::SysInfo::NumberOfProcessors()); | |
224 isolate_ = v8::Isolate::New(params); | |
225 isolate_data_.reset(new PerIsolateData(isolate_, g_array_buffer_allocator)); | |
226 #if defined(OS_WIN) | |
227 { | |
228 void* code_range; | |
229 size_t size; | |
230 isolate_->GetCodeRange(&code_range, &size); | |
231 Debug::CodeRangeCreatedCallback callback = | |
232 DebugImpl::GetCodeRangeCreatedCallback(); | |
233 if (code_range && size && callback) | |
234 callback(code_range, size); | |
235 } | |
236 #endif | |
237 } | |
238 | |
239 IsolateHolder::~IsolateHolder() { | |
240 if (task_observer_.get()) | |
241 base::MessageLoop::current()->RemoveTaskObserver(task_observer_.get()); | |
242 #if defined(OS_WIN) | |
243 { | |
244 void* code_range; | |
245 size_t size; | |
246 isolate_->GetCodeRange(&code_range, &size); | |
247 Debug::CodeRangeDeletedCallback callback = | |
248 DebugImpl::GetCodeRangeDeletedCallback(); | |
249 if (code_range && callback) | |
250 callback(code_range); | |
251 } | |
252 #endif | |
253 isolate_data_.reset(); | |
254 isolate_->Dispose(); | |
255 isolate_ = NULL; | |
256 } | |
257 | |
258 // static | |
259 void IsolateHolder::Initialize(ScriptMode mode, | |
260 v8::ArrayBuffer::Allocator* allocator) { | |
261 CHECK(allocator); | |
262 static bool v8_is_initialized = false; | |
263 if (v8_is_initialized) | |
264 return; | |
265 v8::V8::InitializePlatform(V8Platform::Get()); | |
266 v8::V8::SetArrayBufferAllocator(allocator); | |
267 g_array_buffer_allocator = allocator; | |
268 if (mode == gin::IsolateHolder::kStrictMode) { | |
269 static const char use_strict[] = "--use_strict"; | |
270 v8::V8::SetFlagsFromString(use_strict, sizeof(use_strict) - 1); | |
271 } | |
272 v8::V8::SetEntropySource(&GenerateEntropy); | |
273 #if defined(V8_USE_EXTERNAL_STARTUP_DATA) | |
274 v8::StartupData natives; | |
275 natives.data = reinterpret_cast<const char*>(g_mapped_natives->data()); | |
276 natives.raw_size = static_cast<int>(g_mapped_natives->length()); | |
277 v8::V8::SetNativesDataBlob(&natives); | |
278 | |
279 v8::StartupData snapshot; | |
280 snapshot.data = reinterpret_cast<const char*>(g_mapped_snapshot->data()); | |
281 snapshot.raw_size = static_cast<int>(g_mapped_snapshot->length()); | |
282 v8::V8::SetSnapshotDataBlob(&snapshot); | |
283 #endif // V8_USE_EXTERNAL_STARTUP_DATA | |
284 v8::V8::Initialize(); | |
285 v8_is_initialized = true; | |
286 } | |
287 | |
288 void IsolateHolder::AddRunMicrotasksObserver() { | |
289 DCHECK(!task_observer_.get()); | |
290 task_observer_.reset(new RunMicrotasksObserver(isolate_));; | |
291 base::MessageLoop::current()->AddTaskObserver(task_observer_.get()); | |
292 } | |
293 | |
294 void IsolateHolder::RemoveRunMicrotasksObserver() { | |
295 DCHECK(task_observer_.get()); | |
296 base::MessageLoop::current()->RemoveTaskObserver(task_observer_.get()); | |
297 task_observer_.reset(); | |
298 } | |
299 | |
300 } // namespace gin | 224 } // namespace gin |
OLD | NEW |