Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(36)

Side by Side Diff: gin/isolate_holder.cc

Issue 832393003: [gin] Fingerprint the V8 snapshot files on Windows and verify before loading the snapshot. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix 'all' target when v8_use_external_startup_data==0 Created 5 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 // defined(OS_MACOSX)
26 #include "base/path_service.h" 27 #include "base/path_service.h"
27 #endif // V8_USE_EXTERNAL_STARTUP_DATA 28 #endif // defined(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
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(OS_WIN)
grt (UTC plus 2) 2015/01/08 17:46:35 i can imagine other platforms eventually wanting t
rmcilroy 2015/01/08 18:54:14 Done.
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 // defined(OS_WIN)
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 // defined(OS_ANDROID)
82 #endif // !defined(OS_MACOSX) 95 #endif // !defined(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(OS_WIN)
104 // Declared in gen/gin/v8_snapshot_fingerprint.cc
grt (UTC plus 2) 2015/01/08 17:46:35 ping: Declared -> Defined
rmcilroy 2015/01/08 18:54:14 Oops, sorry missed this. Done.
105 extern const unsigned char g_natives_fingerprint[];
106 extern const unsigned char g_snapshot_fingerprint[];
107 #endif // defined(OS_WIN)
108
90 // static 109 // static
91 bool IsolateHolder::LoadV8Snapshot() { 110 bool IsolateHolder::LoadV8SnapshotCommon() {
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 return MapV8Files(&natives_path, &snapshot_path);
112 } 131 }
113 132
114 //static 133 //static
115 bool IsolateHolder::LoadV8SnapshotFD(int natives_fd, int snapshot_fd) { 134 bool IsolateHolder::LoadV8SnapshotFD(int natives_fd, int snapshot_fd) {
116 if (g_mapped_natives && g_mapped_snapshot) 135 if (g_mapped_natives && g_mapped_snapshot)
117 return true; 136 return true;
118 137
119 return MapV8Files(NULL, NULL, natives_fd, snapshot_fd); 138 return MapV8Files(NULL, NULL, natives_fd, snapshot_fd);
120 } 139 }
121 #endif // V8_USE_EXTERNAL_STARTUP_DATA 140
141 #if defined(OS_WIN)
142 // static
143 bool IsolateHolder::LoadAndVerifyV8Snapshot() {
144 if (!LoadV8SnapshotCommon())
145 return false;
146 return VerifyV8SnapshotFile(g_mapped_natives, g_natives_fingerprint) &&
147 VerifyV8SnapshotFile(g_mapped_snapshot, g_snapshot_fingerprint);
148 }
149 #else
150 // static
151 bool IsolateHolder::LoadV8Snapshot() {
152 return LoadV8SnapshotCommon();
153 }
154 #endif // defined(OS_WIN)
155
156 #endif // defined(V8_USE_EXTERNAL_STARTUP_DATA)
122 157
123 //static 158 //static
124 void IsolateHolder::GetV8ExternalSnapshotData(const char** natives_data_out, 159 void IsolateHolder::GetV8ExternalSnapshotData(const char** natives_data_out,
125 int* natives_size_out, 160 int* natives_size_out,
126 const char** snapshot_data_out, 161 const char** snapshot_data_out,
127 int* snapshot_size_out) { 162 int* snapshot_size_out) {
128 if (!g_mapped_natives || !g_mapped_snapshot) { 163 if (!g_mapped_natives || !g_mapped_snapshot) {
129 *natives_data_out = *snapshot_data_out = NULL; 164 *natives_data_out = *snapshot_data_out = NULL;
130 *natives_size_out = *snapshot_size_out = 0; 165 *natives_size_out = *snapshot_size_out = 0;
131 return; 166 return;
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
186 if (v8_is_initialized) 221 if (v8_is_initialized)
187 return; 222 return;
188 v8::V8::InitializePlatform(V8Platform::Get()); 223 v8::V8::InitializePlatform(V8Platform::Get());
189 v8::V8::SetArrayBufferAllocator(allocator); 224 v8::V8::SetArrayBufferAllocator(allocator);
190 g_array_buffer_allocator = allocator; 225 g_array_buffer_allocator = allocator;
191 if (mode == gin::IsolateHolder::kStrictMode) { 226 if (mode == gin::IsolateHolder::kStrictMode) {
192 static const char v8_flags[] = "--use_strict"; 227 static const char v8_flags[] = "--use_strict";
193 v8::V8::SetFlagsFromString(v8_flags, sizeof(v8_flags) - 1); 228 v8::V8::SetFlagsFromString(v8_flags, sizeof(v8_flags) - 1);
194 } 229 }
195 v8::V8::SetEntropySource(&GenerateEntropy); 230 v8::V8::SetEntropySource(&GenerateEntropy);
196 #ifdef V8_USE_EXTERNAL_STARTUP_DATA 231 #if defined(V8_USE_EXTERNAL_STARTUP_DATA)
197 v8::StartupData natives; 232 v8::StartupData natives;
198 natives.data = reinterpret_cast<const char*>(g_mapped_natives->data()); 233 natives.data = reinterpret_cast<const char*>(g_mapped_natives->data());
199 natives.raw_size = static_cast<int>(g_mapped_natives->length()); 234 natives.raw_size = static_cast<int>(g_mapped_natives->length());
200 v8::V8::SetNativesDataBlob(&natives); 235 v8::V8::SetNativesDataBlob(&natives);
201 236
202 v8::StartupData snapshot; 237 v8::StartupData snapshot;
203 snapshot.data = reinterpret_cast<const char*>(g_mapped_snapshot->data()); 238 snapshot.data = reinterpret_cast<const char*>(g_mapped_snapshot->data());
204 snapshot.raw_size = static_cast<int>(g_mapped_snapshot->length()); 239 snapshot.raw_size = static_cast<int>(g_mapped_snapshot->length());
205 v8::V8::SetSnapshotDataBlob(&snapshot); 240 v8::V8::SetSnapshotDataBlob(&snapshot);
206 #endif // V8_USE_EXTERNAL_STARTUP_DATA 241 #endif // defined(V8_USE_EXTERNAL_STARTUP_DATA)
207 v8::V8::Initialize(); 242 v8::V8::Initialize();
208 v8_is_initialized = true; 243 v8_is_initialized = true;
209 } 244 }
210 245
211 void IsolateHolder::AddRunMicrotasksObserver() { 246 void IsolateHolder::AddRunMicrotasksObserver() {
212 DCHECK(!task_observer_.get()); 247 DCHECK(!task_observer_.get());
213 task_observer_.reset(new RunMicrotasksObserver(isolate_));; 248 task_observer_.reset(new RunMicrotasksObserver(isolate_));;
214 base::MessageLoop::current()->AddTaskObserver(task_observer_.get()); 249 base::MessageLoop::current()->AddTaskObserver(task_observer_.get());
215 } 250 }
216 251
217 void IsolateHolder::RemoveRunMicrotasksObserver() { 252 void IsolateHolder::RemoveRunMicrotasksObserver() {
218 DCHECK(task_observer_.get()); 253 DCHECK(task_observer_.get());
219 base::MessageLoop::current()->RemoveTaskObserver(task_observer_.get()); 254 base::MessageLoop::current()->RemoveTaskObserver(task_observer_.get());
220 task_observer_.reset(); 255 task_observer_.reset();
221 } 256 }
222 257
223 } // namespace gin 258 } // namespace gin
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698