| 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/public/isolate_holder.h" |
| 6 | 6 |
| 7 #include <stdlib.h> | 7 #include <stdlib.h> |
| 8 #include <string.h> | 8 #include <string.h> |
| 9 | 9 |
| 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" | 12 #include "base/message_loop/message_loop.h" |
| 13 #include "base/rand_util.h" | 13 #include "base/rand_util.h" |
| 14 #include "base/sys_info.h" | 14 #include "base/sys_info.h" |
| 15 #include "crypto/sha2.h" |
| 15 #include "gin/array_buffer.h" | 16 #include "gin/array_buffer.h" |
| 16 #include "gin/debug_impl.h" | 17 #include "gin/debug_impl.h" |
| 17 #include "gin/function_template.h" | 18 #include "gin/function_template.h" |
| 18 #include "gin/per_isolate_data.h" | 19 #include "gin/per_isolate_data.h" |
| 19 #include "gin/public/v8_platform.h" | 20 #include "gin/public/v8_platform.h" |
| 20 #include "gin/run_microtasks_observer.h" | 21 #include "gin/run_microtasks_observer.h" |
| 21 | 22 |
| 22 #ifdef V8_USE_EXTERNAL_STARTUP_DATA | 23 #if defined(V8_USE_EXTERNAL_STARTUP_DATA) |
| 23 #ifdef OS_MACOSX | 24 #if defined(OS_MACOSX) |
| 24 #include "base/mac/foundation_util.h" | 25 #include "base/mac/foundation_util.h" |
| 25 #endif // OS_MACOSX | 26 #endif // OS_MACOSX |
| 26 #include "base/path_service.h" | 27 #include "base/path_service.h" |
| 27 #endif // V8_USE_EXTERNAL_STARTUP_DATA | 28 #endif // V8_USE_EXTERNAL_STARTUP_DATA |
| 28 | 29 |
| 29 namespace gin { | 30 namespace gin { |
| 30 | 31 |
| 31 namespace { | 32 namespace { |
| 32 | 33 |
| 33 v8::ArrayBuffer::Allocator* g_array_buffer_allocator = NULL; | 34 v8::ArrayBuffer::Allocator* g_array_buffer_allocator = NULL; |
| 34 | 35 |
| 35 bool GenerateEntropy(unsigned char* buffer, size_t amount) { | 36 bool GenerateEntropy(unsigned char* buffer, size_t amount) { |
| 36 base::RandBytes(buffer, amount); | 37 base::RandBytes(buffer, amount); |
| 37 return true; | 38 return true; |
| 38 } | 39 } |
| 39 | 40 |
| 40 base::MemoryMappedFile* g_mapped_natives = NULL; | 41 base::MemoryMappedFile* g_mapped_natives = NULL; |
| 41 base::MemoryMappedFile* g_mapped_snapshot = NULL; | 42 base::MemoryMappedFile* g_mapped_snapshot = NULL; |
| 42 | 43 |
| 43 #ifdef V8_USE_EXTERNAL_STARTUP_DATA | 44 #if defined(V8_USE_EXTERNAL_STARTUP_DATA) |
| 44 bool MapV8Files(base::FilePath* natives_path, base::FilePath* snapshot_path, | 45 bool MapV8Files(base::FilePath* natives_path, base::FilePath* snapshot_path, |
| 45 int natives_fd = -1, int snapshot_fd = -1) { | 46 int natives_fd = -1, int snapshot_fd = -1) { |
| 46 int flags = base::File::FLAG_OPEN | base::File::FLAG_READ; | 47 int flags = base::File::FLAG_OPEN | base::File::FLAG_READ; |
| 47 | 48 |
| 48 g_mapped_natives = new base::MemoryMappedFile; | 49 g_mapped_natives = new base::MemoryMappedFile; |
| 49 if (!g_mapped_natives->IsValid()) { | 50 if (!g_mapped_natives->IsValid()) { |
| 50 if (natives_fd == -1 | 51 if (natives_fd == -1 |
| 51 ? !g_mapped_natives->Initialize(base::File(*natives_path, flags)) | 52 ? !g_mapped_natives->Initialize(base::File(*natives_path, flags)) |
| 52 : !g_mapped_natives->Initialize(base::File(natives_fd))) { | 53 : !g_mapped_natives->Initialize(base::File(natives_fd))) { |
| 53 delete g_mapped_natives; | 54 delete g_mapped_natives; |
| (...skipping 11 matching lines...) Expand all Loading... |
| 65 delete g_mapped_snapshot; | 66 delete g_mapped_snapshot; |
| 66 g_mapped_snapshot = NULL; | 67 g_mapped_snapshot = NULL; |
| 67 LOG(ERROR) << "Couldn't mmap v8 snapshot data file"; | 68 LOG(ERROR) << "Couldn't mmap v8 snapshot data file"; |
| 68 return false; | 69 return false; |
| 69 } | 70 } |
| 70 } | 71 } |
| 71 | 72 |
| 72 return true; | 73 return true; |
| 73 } | 74 } |
| 74 | 75 |
| 76 #if defined(V8_VERIFY_EXTERNAL_STARTUP_DATA) |
| 77 bool VerifyV8SnapshotFile(base::MemoryMappedFile* snapshot_file, |
| 78 const unsigned char* fingerprint) { |
| 79 unsigned char output[crypto::kSHA256Length]; |
| 80 crypto::SHA256HashString( |
| 81 base::StringPiece(reinterpret_cast<const char*>(snapshot_file->data()), |
| 82 snapshot_file->length()), |
| 83 output, sizeof(output)); |
| 84 return !memcmp(fingerprint, output, sizeof(output)); |
| 85 } |
| 86 #endif // V8_VERIFY_EXTERNAL_STARTUP_DATA |
| 87 |
| 75 #if !defined(OS_MACOSX) | 88 #if !defined(OS_MACOSX) |
| 76 const int v8_snapshot_dir = | 89 const int v8_snapshot_dir = |
| 77 #if defined(OS_ANDROID) | 90 #if defined(OS_ANDROID) |
| 78 base::DIR_ANDROID_APP_DATA; | 91 base::DIR_ANDROID_APP_DATA; |
| 79 #elif defined(OS_POSIX) | 92 #elif defined(OS_POSIX) |
| 80 base::DIR_EXE; | 93 base::DIR_EXE; |
| 81 #endif // defined(OS_ANDROID) | 94 #endif // OS_ANDROID |
| 82 #endif // !defined(OS_MACOSX) | 95 #endif // !OS_MACOSX |
| 83 | 96 |
| 84 #endif // V8_USE_EXTERNAL_STARTUP_DATA | 97 #endif // V8_USE_EXTERNAL_STARTUP_DATA |
| 85 | 98 |
| 86 } // namespace | 99 } // namespace |
| 87 | 100 |
| 101 #if defined(V8_USE_EXTERNAL_STARTUP_DATA) |
| 88 | 102 |
| 89 #ifdef V8_USE_EXTERNAL_STARTUP_DATA | 103 #if defined(V8_VERIFY_EXTERNAL_STARTUP_DATA) |
| 104 // Defined in gen/gin/v8_snapshot_fingerprint.cc |
| 105 extern const unsigned char g_natives_fingerprint[]; |
| 106 extern const unsigned char g_snapshot_fingerprint[]; |
| 107 #endif // V8_VERIFY_EXTERNAL_STARTUP_DATA |
| 108 |
| 90 // static | 109 // static |
| 91 bool IsolateHolder::LoadV8Snapshot() { | 110 bool IsolateHolder::LoadV8Snapshot() { |
| 92 if (g_mapped_natives && g_mapped_snapshot) | 111 if (g_mapped_natives && g_mapped_snapshot) |
| 93 return true; | 112 return true; |
| 94 | 113 |
| 95 #if !defined(OS_MACOSX) | 114 #if !defined(OS_MACOSX) |
| 96 base::FilePath data_path; | 115 base::FilePath data_path; |
| 97 PathService::Get(v8_snapshot_dir, &data_path); | 116 PathService::Get(v8_snapshot_dir, &data_path); |
| 98 DCHECK(!data_path.empty()); | 117 DCHECK(!data_path.empty()); |
| 99 | 118 |
| 100 base::FilePath natives_path = data_path.AppendASCII("natives_blob.bin"); | 119 base::FilePath natives_path = data_path.AppendASCII("natives_blob.bin"); |
| 101 base::FilePath snapshot_path = data_path.AppendASCII("snapshot_blob.bin"); | 120 base::FilePath snapshot_path = data_path.AppendASCII("snapshot_blob.bin"); |
| 102 #else // !defined(OS_MACOSX) | 121 #else // !defined(OS_MACOSX) |
| 103 base::FilePath natives_path = base::mac::PathForFrameworkBundleResource( | 122 base::FilePath natives_path = base::mac::PathForFrameworkBundleResource( |
| 104 CFSTR("natives_blob.bin")); | 123 CFSTR("natives_blob.bin")); |
| 105 base::FilePath snapshot_path = base::mac::PathForFrameworkBundleResource( | 124 base::FilePath snapshot_path = base::mac::PathForFrameworkBundleResource( |
| 106 CFSTR("snapshot_blob.bin")); | 125 CFSTR("snapshot_blob.bin")); |
| 107 DCHECK(!natives_path.empty()); | 126 DCHECK(!natives_path.empty()); |
| 108 DCHECK(!snapshot_path.empty()); | 127 DCHECK(!snapshot_path.empty()); |
| 109 #endif // !defined(OS_MACOSX) | 128 #endif // !defined(OS_MACOSX) |
| 110 | 129 |
| 111 return MapV8Files(&natives_path, &snapshot_path); | 130 if (!MapV8Files(&natives_path, &snapshot_path)) |
| 131 return false; |
| 132 |
| 133 #if defined(V8_VERIFY_EXTERNAL_STARTUP_DATA) |
| 134 return VerifyV8SnapshotFile(g_mapped_natives, g_natives_fingerprint) && |
| 135 VerifyV8SnapshotFile(g_mapped_snapshot, g_snapshot_fingerprint); |
| 136 #else |
| 137 return true; |
| 138 #endif // V8_VERIFY_EXTERNAL_STARTUP_DATA |
| 112 } | 139 } |
| 113 | 140 |
| 114 //static | 141 //static |
| 115 bool IsolateHolder::LoadV8SnapshotFD(int natives_fd, int snapshot_fd) { | 142 bool IsolateHolder::LoadV8SnapshotFD(int natives_fd, int snapshot_fd) { |
| 116 if (g_mapped_natives && g_mapped_snapshot) | 143 if (g_mapped_natives && g_mapped_snapshot) |
| 117 return true; | 144 return true; |
| 118 | 145 |
| 119 return MapV8Files(NULL, NULL, natives_fd, snapshot_fd); | 146 return MapV8Files(NULL, NULL, natives_fd, snapshot_fd); |
| 120 } | 147 } |
| 121 #endif // V8_USE_EXTERNAL_STARTUP_DATA | 148 #endif // V8_USE_EXTERNAL_STARTUP_DATA |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 186 if (v8_is_initialized) | 213 if (v8_is_initialized) |
| 187 return; | 214 return; |
| 188 v8::V8::InitializePlatform(V8Platform::Get()); | 215 v8::V8::InitializePlatform(V8Platform::Get()); |
| 189 v8::V8::SetArrayBufferAllocator(allocator); | 216 v8::V8::SetArrayBufferAllocator(allocator); |
| 190 g_array_buffer_allocator = allocator; | 217 g_array_buffer_allocator = allocator; |
| 191 if (mode == gin::IsolateHolder::kStrictMode) { | 218 if (mode == gin::IsolateHolder::kStrictMode) { |
| 192 static const char v8_flags[] = "--use_strict"; | 219 static const char v8_flags[] = "--use_strict"; |
| 193 v8::V8::SetFlagsFromString(v8_flags, sizeof(v8_flags) - 1); | 220 v8::V8::SetFlagsFromString(v8_flags, sizeof(v8_flags) - 1); |
| 194 } | 221 } |
| 195 v8::V8::SetEntropySource(&GenerateEntropy); | 222 v8::V8::SetEntropySource(&GenerateEntropy); |
| 196 #ifdef V8_USE_EXTERNAL_STARTUP_DATA | 223 #if defined(V8_USE_EXTERNAL_STARTUP_DATA) |
| 197 v8::StartupData natives; | 224 v8::StartupData natives; |
| 198 natives.data = reinterpret_cast<const char*>(g_mapped_natives->data()); | 225 natives.data = reinterpret_cast<const char*>(g_mapped_natives->data()); |
| 199 natives.raw_size = static_cast<int>(g_mapped_natives->length()); | 226 natives.raw_size = static_cast<int>(g_mapped_natives->length()); |
| 200 v8::V8::SetNativesDataBlob(&natives); | 227 v8::V8::SetNativesDataBlob(&natives); |
| 201 | 228 |
| 202 v8::StartupData snapshot; | 229 v8::StartupData snapshot; |
| 203 snapshot.data = reinterpret_cast<const char*>(g_mapped_snapshot->data()); | 230 snapshot.data = reinterpret_cast<const char*>(g_mapped_snapshot->data()); |
| 204 snapshot.raw_size = static_cast<int>(g_mapped_snapshot->length()); | 231 snapshot.raw_size = static_cast<int>(g_mapped_snapshot->length()); |
| 205 v8::V8::SetSnapshotDataBlob(&snapshot); | 232 v8::V8::SetSnapshotDataBlob(&snapshot); |
| 206 #endif // V8_USE_EXTERNAL_STARTUP_DATA | 233 #endif // V8_USE_EXTERNAL_STARTUP_DATA |
| 207 v8::V8::Initialize(); | 234 v8::V8::Initialize(); |
| 208 v8_is_initialized = true; | 235 v8_is_initialized = true; |
| 209 } | 236 } |
| 210 | 237 |
| 211 void IsolateHolder::AddRunMicrotasksObserver() { | 238 void IsolateHolder::AddRunMicrotasksObserver() { |
| 212 DCHECK(!task_observer_.get()); | 239 DCHECK(!task_observer_.get()); |
| 213 task_observer_.reset(new RunMicrotasksObserver(isolate_));; | 240 task_observer_.reset(new RunMicrotasksObserver(isolate_));; |
| 214 base::MessageLoop::current()->AddTaskObserver(task_observer_.get()); | 241 base::MessageLoop::current()->AddTaskObserver(task_observer_.get()); |
| 215 } | 242 } |
| 216 | 243 |
| 217 void IsolateHolder::RemoveRunMicrotasksObserver() { | 244 void IsolateHolder::RemoveRunMicrotasksObserver() { |
| 218 DCHECK(task_observer_.get()); | 245 DCHECK(task_observer_.get()); |
| 219 base::MessageLoop::current()->RemoveTaskObserver(task_observer_.get()); | 246 base::MessageLoop::current()->RemoveTaskObserver(task_observer_.get()); |
| 220 task_observer_.reset(); | 247 task_observer_.reset(); |
| 221 } | 248 } |
| 222 | 249 |
| 223 } // namespace gin | 250 } // namespace gin |
| OLD | NEW |