reloc_packer: Allow aarch64 DSO linked with gold
[chromium-blink-merge.git] / gin / isolate_holder.cc
blobdaa04f177c9bd053b61b08cb2015d3b089d3a145
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"
7 #include <stdlib.h>
8 #include <string.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"
28 #endif // OS_MACOSX
29 #include "base/path_service.h"
30 #endif // V8_USE_EXTERNAL_STARTUP_DATA
32 namespace gin {
34 namespace {
36 v8::ArrayBuffer::Allocator* g_array_buffer_allocator = NULL;
38 bool GenerateEntropy(unsigned char* buffer, size_t amount) {
39 base::RandBytes(buffer, amount);
40 return true;
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,
49 int natives_fd = -1,
50 int snapshot_fd = -1,
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()) {
59 #if !defined(OS_WIN)
60 if (natives_fd == -1
61 ? !g_mapped_natives->Initialize(base::File(*natives_path, flags),
62 natives_region)
63 : !g_mapped_natives->Initialize(base::File(natives_fd),
64 natives_region)) {
65 #else
66 if (!g_mapped_natives->Initialize(base::File(*natives_path, flags),
67 natives_region)) {
68 #endif // !OS_WIN
69 delete g_mapped_natives;
70 g_mapped_natives = NULL;
71 LOG(FATAL) << "Couldn't mmap v8 natives data file";
72 return false;
76 g_mapped_snapshot = new base::MemoryMappedFile;
77 if (!g_mapped_snapshot->IsValid()) {
78 #if !defined(OS_WIN)
79 if (snapshot_fd == -1
80 ? !g_mapped_snapshot->Initialize(base::File(*snapshot_path, flags),
81 snapshot_region)
82 : !g_mapped_snapshot->Initialize(base::File(snapshot_fd),
83 snapshot_region)) {
84 #else
85 if (!g_mapped_snapshot->Initialize(base::File(*snapshot_path, flags),
86 snapshot_region)) {
87 #endif // !OS_WIN
88 delete g_mapped_snapshot;
89 g_mapped_snapshot = NULL;
90 LOG(ERROR) << "Couldn't mmap v8 snapshot data file";
91 return false;
95 return true;
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)
115 base::DIR_EXE;
116 #elif defined(OS_WIN)
117 base::DIR_MODULE;
118 #endif // OS_ANDROID
119 #endif // !OS_MACOSX
121 #endif // V8_USE_EXTERNAL_STARTUP_DATA
123 } // namespace
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";
136 // static
137 bool IsolateHolder::LoadV8Snapshot() {
138 if (g_mapped_natives && g_mapped_snapshot)
139 return true;
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(
152 natives_file_name);
153 base::ScopedCFTypeRef<CFStringRef> snapshot_file_name(
154 base::SysUTF8ToCFStringRef(kSnapshotFileName));
155 base::FilePath snapshot_path = base::mac::PathForFrameworkBundleResource(
156 snapshot_file_name);
157 DCHECK(!natives_path.empty());
158 DCHECK(!snapshot_path.empty());
159 #endif // !defined(OS_MACOSX)
161 if (!MapV8Files(&natives_path, &snapshot_path))
162 return false;
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);
167 #else
168 return true;
169 #endif // V8_VERIFY_EXTERNAL_STARTUP_DATA
172 //static
173 bool IsolateHolder::LoadV8SnapshotFd(int natives_fd,
174 int64 natives_offset,
175 int64 natives_size,
176 int snapshot_fd,
177 int64 snapshot_offset,
178 int64 snapshot_size) {
179 if (g_mapped_natives && g_mapped_snapshot)
180 return true;
182 base::MemoryMappedFile::Region natives_region =
183 base::MemoryMappedFile::Region::kWholeFile;
184 if (natives_size != 0 || natives_offset != 0) {
185 natives_region =
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) {
192 snapshot_region =
193 base::MemoryMappedFile::Region(snapshot_offset, snapshot_size);
196 return MapV8Files(
197 NULL, NULL, natives_fd, snapshot_fd, natives_region, snapshot_region);
199 #endif // V8_USE_EXTERNAL_STARTUP_DATA
201 //static
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;
209 return;
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));
228 #if defined(OS_WIN)
230 void* code_range;
231 size_t size;
232 isolate_->GetCodeRange(&code_range, &size);
233 Debug::CodeRangeCreatedCallback callback =
234 DebugImpl::GetCodeRangeCreatedCallback();
235 if (code_range && size && callback)
236 callback(code_range, size);
238 #endif
241 IsolateHolder::~IsolateHolder() {
242 if (task_observer_.get())
243 base::MessageLoop::current()->RemoveTaskObserver(task_observer_.get());
244 #if defined(OS_WIN)
246 void* code_range;
247 size_t size;
248 isolate_->GetCodeRange(&code_range, &size);
249 Debug::CodeRangeDeletedCallback callback =
250 DebugImpl::GetCodeRangeDeletedCallback();
251 if (code_range && callback)
252 callback(code_range);
254 #endif
255 isolate_data_.reset();
256 isolate_->Dispose();
257 isolate_ = NULL;
260 // static
261 void IsolateHolder::Initialize(ScriptMode mode,
262 v8::ArrayBuffer::Allocator* allocator) {
263 CHECK(allocator);
264 static bool v8_is_initialized = false;
265 if (v8_is_initialized)
266 return;
267 v8::V8::InitializePlatform(V8Platform::Get());
268 v8::V8::SetArrayBufferAllocator(allocator);
269 g_array_buffer_allocator = allocator;
270 if (mode == gin::IsolateHolder::kStrictMode) {
271 static const char use_strict[] = "--use_strict";
272 v8::V8::SetFlagsFromString(use_strict, sizeof(use_strict) - 1);
274 if (base::FieldTrialList::FindFullName("V8VerifyHeap") == "Enabled") {
275 static const char verify_heap[] = "--verify_heap";
276 v8::V8::SetFlagsFromString(verify_heap, sizeof(verify_heap) - 1);
278 v8::V8::SetEntropySource(&GenerateEntropy);
279 #if defined(V8_USE_EXTERNAL_STARTUP_DATA)
280 v8::StartupData natives;
281 natives.data = reinterpret_cast<const char*>(g_mapped_natives->data());
282 natives.raw_size = static_cast<int>(g_mapped_natives->length());
283 v8::V8::SetNativesDataBlob(&natives);
285 v8::StartupData snapshot;
286 snapshot.data = reinterpret_cast<const char*>(g_mapped_snapshot->data());
287 snapshot.raw_size = static_cast<int>(g_mapped_snapshot->length());
288 v8::V8::SetSnapshotDataBlob(&snapshot);
289 #endif // V8_USE_EXTERNAL_STARTUP_DATA
290 v8::V8::Initialize();
291 v8_is_initialized = true;
294 void IsolateHolder::AddRunMicrotasksObserver() {
295 DCHECK(!task_observer_.get());
296 task_observer_.reset(new RunMicrotasksObserver(isolate_));;
297 base::MessageLoop::current()->AddTaskObserver(task_observer_.get());
300 void IsolateHolder::RemoveRunMicrotasksObserver() {
301 DCHECK(task_observer_.get());
302 base::MessageLoop::current()->RemoveTaskObserver(task_observer_.get());
303 task_observer_.reset();
306 } // namespace gin