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
3 // found in the LICENSE file.
5 #include "gin/public/isolate_holder.h"
10 #include "base/files/memory_mapped_file.h"
11 #include "base/logging.h"
12 #include "base/message_loop/message_loop.h"
13 #include "base/metrics/field_trial.h"
14 #include "base/rand_util.h"
15 #include "base/strings/sys_string_conversions.h"
16 #include "base/sys_info.h"
17 #include "crypto/sha2.h"
18 #include "gin/array_buffer.h"
19 #include "gin/debug_impl.h"
20 #include "gin/function_template.h"
21 #include "gin/per_isolate_data.h"
22 #include "gin/public/v8_platform.h"
23 #include "gin/run_microtasks_observer.h"
25 #if defined(V8_USE_EXTERNAL_STARTUP_DATA)
26 #if defined(OS_MACOSX)
27 #include "base/mac/foundation_util.h"
29 #include "base/path_service.h"
30 #endif // V8_USE_EXTERNAL_STARTUP_DATA
36 v8::ArrayBuffer::Allocator
* g_array_buffer_allocator
= NULL
;
38 bool GenerateEntropy(unsigned char* buffer
, size_t amount
) {
39 base::RandBytes(buffer
, amount
);
43 base::MemoryMappedFile
* g_mapped_natives
= NULL
;
44 base::MemoryMappedFile
* g_mapped_snapshot
= NULL
;
46 #if defined(V8_USE_EXTERNAL_STARTUP_DATA)
47 bool MapV8Files(base::FilePath
* natives_path
,
48 base::FilePath
* snapshot_path
,
51 base::MemoryMappedFile::Region natives_region
=
52 base::MemoryMappedFile::Region::kWholeFile
,
53 base::MemoryMappedFile::Region snapshot_region
=
54 base::MemoryMappedFile::Region::kWholeFile
) {
55 int flags
= base::File::FLAG_OPEN
| base::File::FLAG_READ
;
57 g_mapped_natives
= new base::MemoryMappedFile
;
58 if (!g_mapped_natives
->IsValid()) {
61 ? !g_mapped_natives
->Initialize(base::File(*natives_path
, flags
),
63 : !g_mapped_natives
->Initialize(base::File(natives_fd
),
66 if (!g_mapped_natives
->Initialize(base::File(*natives_path
, flags
),
69 delete g_mapped_natives
;
70 g_mapped_natives
= NULL
;
71 LOG(FATAL
) << "Couldn't mmap v8 natives data file";
76 g_mapped_snapshot
= new base::MemoryMappedFile
;
77 if (!g_mapped_snapshot
->IsValid()) {
80 ? !g_mapped_snapshot
->Initialize(base::File(*snapshot_path
, flags
),
82 : !g_mapped_snapshot
->Initialize(base::File(snapshot_fd
),
85 if (!g_mapped_snapshot
->Initialize(base::File(*snapshot_path
, flags
),
88 delete g_mapped_snapshot
;
89 g_mapped_snapshot
= NULL
;
90 LOG(ERROR
) << "Couldn't mmap v8 snapshot data file";
98 #if defined(V8_VERIFY_EXTERNAL_STARTUP_DATA)
99 bool VerifyV8SnapshotFile(base::MemoryMappedFile
* snapshot_file
,
100 const unsigned char* fingerprint
) {
101 unsigned char output
[crypto::kSHA256Length
];
102 crypto::SHA256HashString(
103 base::StringPiece(reinterpret_cast<const char*>(snapshot_file
->data()),
104 snapshot_file
->length()),
105 output
, sizeof(output
));
106 return !memcmp(fingerprint
, output
, sizeof(output
));
108 #endif // V8_VERIFY_EXTERNAL_STARTUP_DATA
110 #if !defined(OS_MACOSX)
111 const int v8_snapshot_dir
=
112 #if defined(OS_ANDROID)
113 base::DIR_ANDROID_APP_DATA
;
114 #elif defined(OS_POSIX)
116 #elif defined(OS_WIN)
121 #endif // V8_USE_EXTERNAL_STARTUP_DATA
125 #if defined(V8_USE_EXTERNAL_STARTUP_DATA)
127 #if defined(V8_VERIFY_EXTERNAL_STARTUP_DATA)
128 // Defined in gen/gin/v8_snapshot_fingerprint.cc
129 extern const unsigned char g_natives_fingerprint
[];
130 extern const unsigned char g_snapshot_fingerprint
[];
131 #endif // V8_VERIFY_EXTERNAL_STARTUP_DATA
133 const char IsolateHolder::kNativesFileName
[] = "natives_blob.bin";
134 const char IsolateHolder::kSnapshotFileName
[] = "snapshot_blob.bin";
137 bool IsolateHolder::LoadV8Snapshot() {
138 if (g_mapped_natives
&& g_mapped_snapshot
)
141 #if !defined(OS_MACOSX)
142 base::FilePath data_path
;
143 PathService::Get(v8_snapshot_dir
, &data_path
);
144 DCHECK(!data_path
.empty());
146 base::FilePath natives_path
= data_path
.AppendASCII(kNativesFileName
);
147 base::FilePath snapshot_path
= data_path
.AppendASCII(kSnapshotFileName
);
148 #else // !defined(OS_MACOSX)
149 base::ScopedCFTypeRef
<CFStringRef
> natives_file_name(
150 base::SysUTF8ToCFStringRef(kNativesFileName
));
151 base::FilePath natives_path
= base::mac::PathForFrameworkBundleResource(
153 base::ScopedCFTypeRef
<CFStringRef
> snapshot_file_name(
154 base::SysUTF8ToCFStringRef(kSnapshotFileName
));
155 base::FilePath snapshot_path
= base::mac::PathForFrameworkBundleResource(
157 DCHECK(!natives_path
.empty());
158 DCHECK(!snapshot_path
.empty());
159 #endif // !defined(OS_MACOSX)
161 if (!MapV8Files(&natives_path
, &snapshot_path
))
164 #if defined(V8_VERIFY_EXTERNAL_STARTUP_DATA)
165 return VerifyV8SnapshotFile(g_mapped_natives
, g_natives_fingerprint
) &&
166 VerifyV8SnapshotFile(g_mapped_snapshot
, g_snapshot_fingerprint
);
169 #endif // V8_VERIFY_EXTERNAL_STARTUP_DATA
173 bool IsolateHolder::LoadV8SnapshotFd(int natives_fd
,
174 int64 natives_offset
,
177 int64 snapshot_offset
,
178 int64 snapshot_size
) {
179 if (g_mapped_natives
&& g_mapped_snapshot
)
182 base::MemoryMappedFile::Region natives_region
=
183 base::MemoryMappedFile::Region::kWholeFile
;
184 if (natives_size
!= 0 || natives_offset
!= 0) {
186 base::MemoryMappedFile::Region(natives_offset
, natives_size
);
189 base::MemoryMappedFile::Region snapshot_region
=
190 base::MemoryMappedFile::Region::kWholeFile
;
191 if (natives_size
!= 0 || natives_offset
!= 0) {
193 base::MemoryMappedFile::Region(snapshot_offset
, snapshot_size
);
197 NULL
, NULL
, natives_fd
, snapshot_fd
, natives_region
, snapshot_region
);
199 #endif // V8_USE_EXTERNAL_STARTUP_DATA
202 void IsolateHolder::GetV8ExternalSnapshotData(const char** natives_data_out
,
203 int* natives_size_out
,
204 const char** snapshot_data_out
,
205 int* snapshot_size_out
) {
206 if (!g_mapped_natives
|| !g_mapped_snapshot
) {
207 *natives_data_out
= *snapshot_data_out
= NULL
;
208 *natives_size_out
= *snapshot_size_out
= 0;
211 *natives_data_out
= reinterpret_cast<const char*>(g_mapped_natives
->data());
212 *snapshot_data_out
= reinterpret_cast<const char*>(g_mapped_snapshot
->data());
213 *natives_size_out
= static_cast<int>(g_mapped_natives
->length());
214 *snapshot_size_out
= static_cast<int>(g_mapped_snapshot
->length());
217 IsolateHolder::IsolateHolder() {
218 CHECK(g_array_buffer_allocator
)
219 << "You need to invoke gin::IsolateHolder::Initialize first";
220 v8::Isolate::CreateParams params
;
221 params
.entry_hook
= DebugImpl::GetFunctionEntryHook();
222 params
.code_event_handler
= DebugImpl::GetJitCodeEventHandler();
223 params
.constraints
.ConfigureDefaults(base::SysInfo::AmountOfPhysicalMemory(),
224 base::SysInfo::AmountOfVirtualMemory(),
225 base::SysInfo::NumberOfProcessors());
226 isolate_
= v8::Isolate::New(params
);
227 isolate_data_
.reset(new PerIsolateData(isolate_
, g_array_buffer_allocator
));
232 isolate_
->GetCodeRange(&code_range
, &size
);
233 Debug::CodeRangeCreatedCallback callback
=
234 DebugImpl::GetCodeRangeCreatedCallback();
235 if (code_range
&& size
&& callback
)
236 callback(code_range
, size
);
241 IsolateHolder::~IsolateHolder() {
242 if (task_observer_
.get())
243 base::MessageLoop::current()->RemoveTaskObserver(task_observer_
.get());
248 isolate_
->GetCodeRange(&code_range
, &size
);
249 Debug::CodeRangeDeletedCallback callback
=
250 DebugImpl::GetCodeRangeDeletedCallback();
251 if (code_range
&& callback
)
252 callback(code_range
);
255 isolate_data_
.reset();
260 void IsolateHolder::Initialize(ScriptMode mode
,
261 v8::ArrayBuffer::Allocator
* allocator
) {
263 static bool v8_is_initialized
= false;
264 if (v8_is_initialized
)
266 v8::V8::InitializePlatform(V8Platform::Get());
267 v8::V8::SetArrayBufferAllocator(allocator
);
268 g_array_buffer_allocator
= allocator
;
269 if (mode
== gin::IsolateHolder::kStrictMode
) {
270 static const char use_strict
[] = "--use_strict";
271 v8::V8::SetFlagsFromString(use_strict
, sizeof(use_strict
) - 1);
273 if (base::FieldTrialList::FindFullName("V8VerifyHeap") == "Enabled") {
274 static const char verify_heap
[] = "--verify_heap";
275 v8::V8::SetFlagsFromString(verify_heap
, sizeof(verify_heap
) - 1);
277 v8::V8::SetEntropySource(&GenerateEntropy
);
278 #if defined(V8_USE_EXTERNAL_STARTUP_DATA)
279 v8::StartupData natives
;
280 natives
.data
= reinterpret_cast<const char*>(g_mapped_natives
->data());
281 natives
.raw_size
= static_cast<int>(g_mapped_natives
->length());
282 v8::V8::SetNativesDataBlob(&natives
);
284 v8::StartupData snapshot
;
285 snapshot
.data
= reinterpret_cast<const char*>(g_mapped_snapshot
->data());
286 snapshot
.raw_size
= static_cast<int>(g_mapped_snapshot
->length());
287 v8::V8::SetSnapshotDataBlob(&snapshot
);
288 #endif // V8_USE_EXTERNAL_STARTUP_DATA
289 v8::V8::Initialize();
290 v8_is_initialized
= true;
293 void IsolateHolder::AddRunMicrotasksObserver() {
294 DCHECK(!task_observer_
.get());
295 task_observer_
.reset(new RunMicrotasksObserver(isolate_
));;
296 base::MessageLoop::current()->AddTaskObserver(task_observer_
.get());
299 void IsolateHolder::RemoveRunMicrotasksObserver() {
300 DCHECK(task_observer_
.get());
301 base::MessageLoop::current()->RemoveTaskObserver(task_observer_
.get());
302 task_observer_
.reset();