| 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/strings/sys_string_conversions.h" |
| 14 #include "base/sys_info.h" | 15 #include "base/sys_info.h" |
| 16 #include "crypto/sha2.h" |
| 15 #include "gin/array_buffer.h" | 17 #include "gin/array_buffer.h" |
| 16 #include "gin/debug_impl.h" | 18 #include "gin/debug_impl.h" |
| 17 #include "gin/function_template.h" | 19 #include "gin/function_template.h" |
| 18 #include "gin/per_isolate_data.h" | 20 #include "gin/per_isolate_data.h" |
| 19 #include "gin/public/v8_platform.h" | 21 #include "gin/public/v8_platform.h" |
| 20 #include "gin/run_microtasks_observer.h" | 22 #include "gin/run_microtasks_observer.h" |
| 21 | 23 |
| 22 #ifdef V8_USE_EXTERNAL_STARTUP_DATA | 24 #if defined(V8_USE_EXTERNAL_STARTUP_DATA) |
| 23 #ifdef OS_MACOSX | 25 #if defined(OS_MACOSX) |
| 24 #include "base/mac/foundation_util.h" | 26 #include "base/mac/foundation_util.h" |
| 25 #endif // OS_MACOSX | 27 #endif // OS_MACOSX |
| 26 #include "base/path_service.h" | 28 #include "base/path_service.h" |
| 27 #endif // V8_USE_EXTERNAL_STARTUP_DATA | 29 #endif // V8_USE_EXTERNAL_STARTUP_DATA |
| 28 | 30 |
| 29 namespace gin { | 31 namespace gin { |
| 30 | 32 |
| 31 namespace { | 33 namespace { |
| 32 | 34 |
| 33 v8::ArrayBuffer::Allocator* g_array_buffer_allocator = NULL; | 35 v8::ArrayBuffer::Allocator* g_array_buffer_allocator = NULL; |
| 34 | 36 |
| 35 bool GenerateEntropy(unsigned char* buffer, size_t amount) { | 37 bool GenerateEntropy(unsigned char* buffer, size_t amount) { |
| 36 base::RandBytes(buffer, amount); | 38 base::RandBytes(buffer, amount); |
| 37 return true; | 39 return true; |
| 38 } | 40 } |
| 39 | 41 |
| 40 base::MemoryMappedFile* g_mapped_natives = NULL; | 42 base::MemoryMappedFile* g_mapped_natives = NULL; |
| 41 base::MemoryMappedFile* g_mapped_snapshot = NULL; | 43 base::MemoryMappedFile* g_mapped_snapshot = NULL; |
| 42 | 44 |
| 43 #ifdef V8_USE_EXTERNAL_STARTUP_DATA | 45 #if defined(V8_USE_EXTERNAL_STARTUP_DATA) |
| 44 bool MapV8Files(base::FilePath* natives_path, base::FilePath* snapshot_path, | 46 bool MapV8Files(base::FilePath* natives_path, |
| 45 int natives_fd = -1, int snapshot_fd = -1) { | 47 base::FilePath* snapshot_path, |
| 48 int natives_fd = -1, |
| 49 int snapshot_fd = -1, |
| 50 base::MemoryMappedFile::Region natives_region = |
| 51 base::MemoryMappedFile::Region::kWholeFile, |
| 52 base::MemoryMappedFile::Region snapshot_region = |
| 53 base::MemoryMappedFile::Region::kWholeFile) { |
| 46 int flags = base::File::FLAG_OPEN | base::File::FLAG_READ; | 54 int flags = base::File::FLAG_OPEN | base::File::FLAG_READ; |
| 47 | 55 |
| 48 g_mapped_natives = new base::MemoryMappedFile; | 56 g_mapped_natives = new base::MemoryMappedFile; |
| 49 if (!g_mapped_natives->IsValid()) { | 57 if (!g_mapped_natives->IsValid()) { |
| 58 #if !defined(OS_WIN) |
| 50 if (natives_fd == -1 | 59 if (natives_fd == -1 |
| 51 ? !g_mapped_natives->Initialize(base::File(*natives_path, flags)) | 60 ? !g_mapped_natives->Initialize(base::File(*natives_path, flags), |
| 52 : !g_mapped_natives->Initialize(base::File(natives_fd))) { | 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 |
| 53 delete g_mapped_natives; | 68 delete g_mapped_natives; |
| 54 g_mapped_natives = NULL; | 69 g_mapped_natives = NULL; |
| 55 LOG(FATAL) << "Couldn't mmap v8 natives data file"; | 70 LOG(FATAL) << "Couldn't mmap v8 natives data file"; |
| 56 return false; | 71 return false; |
| 57 } | 72 } |
| 58 } | 73 } |
| 59 | 74 |
| 60 g_mapped_snapshot = new base::MemoryMappedFile; | 75 g_mapped_snapshot = new base::MemoryMappedFile; |
| 61 if (!g_mapped_snapshot->IsValid()) { | 76 if (!g_mapped_snapshot->IsValid()) { |
| 77 #if !defined(OS_WIN) |
| 62 if (snapshot_fd == -1 | 78 if (snapshot_fd == -1 |
| 63 ? !g_mapped_snapshot->Initialize(base::File(*snapshot_path, flags)) | 79 ? !g_mapped_snapshot->Initialize(base::File(*snapshot_path, flags), |
| 64 : !g_mapped_snapshot->Initialize(base::File(snapshot_fd))) { | 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 |
| 65 delete g_mapped_snapshot; | 87 delete g_mapped_snapshot; |
| 66 g_mapped_snapshot = NULL; | 88 g_mapped_snapshot = NULL; |
| 67 LOG(ERROR) << "Couldn't mmap v8 snapshot data file"; | 89 LOG(ERROR) << "Couldn't mmap v8 snapshot data file"; |
| 68 return false; | 90 return false; |
| 69 } | 91 } |
| 70 } | 92 } |
| 71 | 93 |
| 72 return true; | 94 return true; |
| 73 } | 95 } |
| 74 | 96 |
| 97 #if defined(V8_VERIFY_EXTERNAL_STARTUP_DATA) |
| 98 bool VerifyV8SnapshotFile(base::MemoryMappedFile* snapshot_file, |
| 99 const unsigned char* fingerprint) { |
| 100 unsigned char output[crypto::kSHA256Length]; |
| 101 crypto::SHA256HashString( |
| 102 base::StringPiece(reinterpret_cast<const char*>(snapshot_file->data()), |
| 103 snapshot_file->length()), |
| 104 output, sizeof(output)); |
| 105 return !memcmp(fingerprint, output, sizeof(output)); |
| 106 } |
| 107 #endif // V8_VERIFY_EXTERNAL_STARTUP_DATA |
| 108 |
| 75 #if !defined(OS_MACOSX) | 109 #if !defined(OS_MACOSX) |
| 76 const int v8_snapshot_dir = | 110 const int v8_snapshot_dir = |
| 77 #if defined(OS_ANDROID) | 111 #if defined(OS_ANDROID) |
| 78 base::DIR_ANDROID_APP_DATA; | 112 base::DIR_ANDROID_APP_DATA; |
| 79 #elif defined(OS_POSIX) | 113 #elif defined(OS_POSIX) |
| 80 base::DIR_EXE; | 114 base::DIR_EXE; |
| 81 #endif // defined(OS_ANDROID) | 115 #elif defined(OS_WIN) |
| 82 #endif // !defined(OS_MACOSX) | 116 base::DIR_MODULE; |
| 117 #endif // OS_ANDROID |
| 118 #endif // !OS_MACOSX |
| 83 | 119 |
| 84 #endif // V8_USE_EXTERNAL_STARTUP_DATA | 120 #endif // V8_USE_EXTERNAL_STARTUP_DATA |
| 85 | 121 |
| 86 } // namespace | 122 } // namespace |
| 87 | 123 |
| 124 #if defined(V8_USE_EXTERNAL_STARTUP_DATA) |
| 88 | 125 |
| 89 #ifdef V8_USE_EXTERNAL_STARTUP_DATA | 126 #if defined(V8_VERIFY_EXTERNAL_STARTUP_DATA) |
| 127 // Defined in gen/gin/v8_snapshot_fingerprint.cc |
| 128 extern const unsigned char g_natives_fingerprint[]; |
| 129 extern const unsigned char g_snapshot_fingerprint[]; |
| 130 #endif // V8_VERIFY_EXTERNAL_STARTUP_DATA |
| 131 |
| 132 const char IsolateHolder::kNativesFileName[] = "natives_blob.bin"; |
| 133 const char IsolateHolder::kSnapshotFileName[] = "snapshot_blob.bin"; |
| 134 |
| 90 // static | 135 // static |
| 91 bool IsolateHolder::LoadV8Snapshot() { | 136 bool IsolateHolder::LoadV8Snapshot() { |
| 92 if (g_mapped_natives && g_mapped_snapshot) | 137 if (g_mapped_natives && g_mapped_snapshot) |
| 93 return true; | 138 return true; |
| 94 | 139 |
| 95 #if !defined(OS_MACOSX) | 140 #if !defined(OS_MACOSX) |
| 96 base::FilePath data_path; | 141 base::FilePath data_path; |
| 97 PathService::Get(v8_snapshot_dir, &data_path); | 142 PathService::Get(v8_snapshot_dir, &data_path); |
| 98 DCHECK(!data_path.empty()); | 143 DCHECK(!data_path.empty()); |
| 99 | 144 |
| 100 base::FilePath natives_path = data_path.AppendASCII("natives_blob.bin"); | 145 base::FilePath natives_path = data_path.AppendASCII(kNativesFileName); |
| 101 base::FilePath snapshot_path = data_path.AppendASCII("snapshot_blob.bin"); | 146 base::FilePath snapshot_path = data_path.AppendASCII(kSnapshotFileName); |
| 102 #else // !defined(OS_MACOSX) | 147 #else // !defined(OS_MACOSX) |
| 148 base::ScopedCFTypeRef<CFStringRef> natives_file_name( |
| 149 base::SysUTF8ToCFStringRef(kNativesFileName)); |
| 103 base::FilePath natives_path = base::mac::PathForFrameworkBundleResource( | 150 base::FilePath natives_path = base::mac::PathForFrameworkBundleResource( |
| 104 CFSTR("natives_blob.bin")); | 151 natives_file_name); |
| 152 base::ScopedCFTypeRef<CFStringRef> snapshot_file_name( |
| 153 base::SysUTF8ToCFStringRef(kSnapshotFileName)); |
| 105 base::FilePath snapshot_path = base::mac::PathForFrameworkBundleResource( | 154 base::FilePath snapshot_path = base::mac::PathForFrameworkBundleResource( |
| 106 CFSTR("snapshot_blob.bin")); | 155 snapshot_file_name); |
| 107 DCHECK(!natives_path.empty()); | 156 DCHECK(!natives_path.empty()); |
| 108 DCHECK(!snapshot_path.empty()); | 157 DCHECK(!snapshot_path.empty()); |
| 109 #endif // !defined(OS_MACOSX) | 158 #endif // !defined(OS_MACOSX) |
| 110 | 159 |
| 111 return MapV8Files(&natives_path, &snapshot_path); | 160 if (!MapV8Files(&natives_path, &snapshot_path)) |
| 161 return false; |
| 162 |
| 163 #if defined(V8_VERIFY_EXTERNAL_STARTUP_DATA) |
| 164 return VerifyV8SnapshotFile(g_mapped_natives, g_natives_fingerprint) && |
| 165 VerifyV8SnapshotFile(g_mapped_snapshot, g_snapshot_fingerprint); |
| 166 #else |
| 167 return true; |
| 168 #endif // V8_VERIFY_EXTERNAL_STARTUP_DATA |
| 112 } | 169 } |
| 113 | 170 |
| 114 //static | 171 //static |
| 115 bool IsolateHolder::LoadV8SnapshotFD(int natives_fd, int snapshot_fd) { | 172 bool IsolateHolder::LoadV8SnapshotFd(int natives_fd, |
| 173 int64 natives_offset, |
| 174 int64 natives_size, |
| 175 int snapshot_fd, |
| 176 int64 snapshot_offset, |
| 177 int64 snapshot_size) { |
| 116 if (g_mapped_natives && g_mapped_snapshot) | 178 if (g_mapped_natives && g_mapped_snapshot) |
| 117 return true; | 179 return true; |
| 118 | 180 |
| 119 return MapV8Files(NULL, NULL, natives_fd, snapshot_fd); | 181 base::MemoryMappedFile::Region natives_region = |
| 182 base::MemoryMappedFile::Region::kWholeFile; |
| 183 if (natives_size != 0 || natives_offset != 0) { |
| 184 natives_region = |
| 185 base::MemoryMappedFile::Region(natives_offset, natives_size); |
| 186 } |
| 187 |
| 188 base::MemoryMappedFile::Region snapshot_region = |
| 189 base::MemoryMappedFile::Region::kWholeFile; |
| 190 if (natives_size != 0 || natives_offset != 0) { |
| 191 snapshot_region = |
| 192 base::MemoryMappedFile::Region(snapshot_offset, snapshot_size); |
| 193 } |
| 194 |
| 195 return MapV8Files( |
| 196 NULL, NULL, natives_fd, snapshot_fd, natives_region, snapshot_region); |
| 120 } | 197 } |
| 121 #endif // V8_USE_EXTERNAL_STARTUP_DATA | 198 #endif // V8_USE_EXTERNAL_STARTUP_DATA |
| 122 | 199 |
| 123 //static | 200 //static |
| 124 void IsolateHolder::GetV8ExternalSnapshotData(const char** natives_data_out, | 201 void IsolateHolder::GetV8ExternalSnapshotData(const char** natives_data_out, |
| 125 int* natives_size_out, | 202 int* natives_size_out, |
| 126 const char** snapshot_data_out, | 203 const char** snapshot_data_out, |
| 127 int* snapshot_size_out) { | 204 int* snapshot_size_out) { |
| 128 if (!g_mapped_natives || !g_mapped_snapshot) { | 205 if (!g_mapped_natives || !g_mapped_snapshot) { |
| 129 *natives_data_out = *snapshot_data_out = NULL; | 206 *natives_data_out = *snapshot_data_out = NULL; |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 186 if (v8_is_initialized) | 263 if (v8_is_initialized) |
| 187 return; | 264 return; |
| 188 v8::V8::InitializePlatform(V8Platform::Get()); | 265 v8::V8::InitializePlatform(V8Platform::Get()); |
| 189 v8::V8::SetArrayBufferAllocator(allocator); | 266 v8::V8::SetArrayBufferAllocator(allocator); |
| 190 g_array_buffer_allocator = allocator; | 267 g_array_buffer_allocator = allocator; |
| 191 if (mode == gin::IsolateHolder::kStrictMode) { | 268 if (mode == gin::IsolateHolder::kStrictMode) { |
| 192 static const char v8_flags[] = "--use_strict"; | 269 static const char v8_flags[] = "--use_strict"; |
| 193 v8::V8::SetFlagsFromString(v8_flags, sizeof(v8_flags) - 1); | 270 v8::V8::SetFlagsFromString(v8_flags, sizeof(v8_flags) - 1); |
| 194 } | 271 } |
| 195 v8::V8::SetEntropySource(&GenerateEntropy); | 272 v8::V8::SetEntropySource(&GenerateEntropy); |
| 196 #ifdef V8_USE_EXTERNAL_STARTUP_DATA | 273 #if defined(V8_USE_EXTERNAL_STARTUP_DATA) |
| 197 v8::StartupData natives; | 274 v8::StartupData natives; |
| 198 natives.data = reinterpret_cast<const char*>(g_mapped_natives->data()); | 275 natives.data = reinterpret_cast<const char*>(g_mapped_natives->data()); |
| 199 natives.raw_size = static_cast<int>(g_mapped_natives->length()); | 276 natives.raw_size = static_cast<int>(g_mapped_natives->length()); |
| 200 v8::V8::SetNativesDataBlob(&natives); | 277 v8::V8::SetNativesDataBlob(&natives); |
| 201 | 278 |
| 202 v8::StartupData snapshot; | 279 v8::StartupData snapshot; |
| 203 snapshot.data = reinterpret_cast<const char*>(g_mapped_snapshot->data()); | 280 snapshot.data = reinterpret_cast<const char*>(g_mapped_snapshot->data()); |
| 204 snapshot.raw_size = static_cast<int>(g_mapped_snapshot->length()); | 281 snapshot.raw_size = static_cast<int>(g_mapped_snapshot->length()); |
| 205 v8::V8::SetSnapshotDataBlob(&snapshot); | 282 v8::V8::SetSnapshotDataBlob(&snapshot); |
| 206 #endif // V8_USE_EXTERNAL_STARTUP_DATA | 283 #endif // V8_USE_EXTERNAL_STARTUP_DATA |
| 207 v8::V8::Initialize(); | 284 v8::V8::Initialize(); |
| 208 v8_is_initialized = true; | 285 v8_is_initialized = true; |
| 209 } | 286 } |
| 210 | 287 |
| 211 void IsolateHolder::AddRunMicrotasksObserver() { | 288 void IsolateHolder::AddRunMicrotasksObserver() { |
| 212 DCHECK(!task_observer_.get()); | 289 DCHECK(!task_observer_.get()); |
| 213 task_observer_.reset(new RunMicrotasksObserver(isolate_));; | 290 task_observer_.reset(new RunMicrotasksObserver(isolate_));; |
| 214 base::MessageLoop::current()->AddTaskObserver(task_observer_.get()); | 291 base::MessageLoop::current()->AddTaskObserver(task_observer_.get()); |
| 215 } | 292 } |
| 216 | 293 |
| 217 void IsolateHolder::RemoveRunMicrotasksObserver() { | 294 void IsolateHolder::RemoveRunMicrotasksObserver() { |
| 218 DCHECK(task_observer_.get()); | 295 DCHECK(task_observer_.get()); |
| 219 base::MessageLoop::current()->RemoveTaskObserver(task_observer_.get()); | 296 base::MessageLoop::current()->RemoveTaskObserver(task_observer_.get()); |
| 220 task_observer_.reset(); | 297 task_observer_.reset(); |
| 221 } | 298 } |
| 222 | 299 |
| 223 } // namespace gin | 300 } // namespace gin |
| OLD | NEW |