diff options
author | oth <oth@chromium.org> | 2015-04-05 07:30:57 -0700 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2015-04-05 14:31:16 +0000 |
commit | 05c26fde422600a25247a22f9670463216d0519a (patch) | |
tree | a2443f512888c10b64f892b4f78f200d18c4575d | |
parent | f13b7592e312f69e70062fbcf72b746eefaf13fc (diff) | |
download | chromium_src-05c26fde422600a25247a22f9670463216d0519a.zip chromium_src-05c26fde422600a25247a22f9670463216d0519a.tar.gz chromium_src-05c26fde422600a25247a22f9670463216d0519a.tar.bz2 |
Move V8 snapshot loading code from isolate_holder to gin/v8_startup_data.{h,cc}.
isolate_holder.h is public and used by blink and therefore cannot refer to types from base. As part of porting this to other platforms we needed to load snapshots from BaseFile descriptors.
BUG=421063
Review URL: https://codereview.chromium.org/1011133006
Cr-Commit-Position: refs/heads/master@{#323886}
26 files changed, 350 insertions, 277 deletions
diff --git a/chrome/browser/DEPS b/chrome/browser/DEPS index 7c78fa9..0368828 100644 --- a/chrome/browser/DEPS +++ b/chrome/browser/DEPS @@ -106,7 +106,7 @@ include_rules = [ "+extensions/components/javascript_dialog_extensions_client", "+extensions/grit", "+extensions/test", - "+gin/public/isolate_holder.h", + "+gin", "+google/cacheinvalidation", # Sync invalidation API protobuf files. "+google_apis", "+google_update", diff --git a/chrome/browser/chrome_content_browser_client.cc b/chrome/browser/chrome_content_browser_client.cc index c644b41..51ab5ea 100644 --- a/chrome/browser/chrome_content_browser_client.cc +++ b/chrome/browser/chrome_content_browser_client.cc @@ -117,7 +117,7 @@ #include "content/public/common/content_descriptors.h" #include "content/public/common/url_utils.h" #include "content/public/common/web_preferences.h" -#include "gin/public/isolate_holder.h" +#include "gin/v8_initializer.h" #include "net/base/mime_util.h" #include "net/cookies/canonical_cookie.h" #include "net/cookies/cookie_options.h" @@ -2185,14 +2185,14 @@ void ChromeContentBrowserClient::GetAdditionalMappedFilesForChildProcess( #if defined(V8_USE_EXTERNAL_STARTUP_DATA) if (v8_snapshot_fd_.get() == -1 && v8_natives_fd_.get() == -1) { base::FilePath v8_data_path; - PathService::Get(gin::IsolateHolder::kV8SnapshotBasePathKey, &v8_data_path); + PathService::Get(gin::V8Initializer::kV8SnapshotBasePathKey, &v8_data_path); DCHECK(!v8_data_path.empty()); int file_flags = base::File::FLAG_OPEN | base::File::FLAG_READ; base::FilePath v8_natives_data_path = - v8_data_path.AppendASCII(gin::IsolateHolder::kNativesFileName); + v8_data_path.AppendASCII(gin::V8Initializer::kNativesFileName); base::FilePath v8_snapshot_data_path = - v8_data_path.AppendASCII(gin::IsolateHolder::kSnapshotFileName); + v8_data_path.AppendASCII(gin::V8Initializer::kSnapshotFileName); base::File v8_natives_data_file(v8_natives_data_path, file_flags); base::File v8_snapshot_data_file(v8_snapshot_data_path, file_flags); DCHECK(v8_natives_data_file.IsValid()); diff --git a/chrome/plugin/DEPS b/chrome/plugin/DEPS index eca72a4..8c270f4 100644 --- a/chrome/plugin/DEPS +++ b/chrome/plugin/DEPS @@ -1,5 +1,6 @@ include_rules = [ "+content/public/plugin", "+gin/public/isolate_holder.h", + "+gin/v8_initializer.h", "+media/base", ] diff --git a/chrome/plugin/chrome_content_plugin_client.cc b/chrome/plugin/chrome_content_plugin_client.cc index 87c20d0..fd4c013 100644 --- a/chrome/plugin/chrome_content_plugin_client.cc +++ b/chrome/plugin/chrome_content_plugin_client.cc @@ -18,14 +18,14 @@ #endif #ifdef V8_USE_EXTERNAL_STARTUP_DATA -#include "gin/public/isolate_holder.h" +#include "gin/v8_initializer.h" #endif namespace chrome { void ChromeContentPluginClient::PreSandboxInitialization() { #ifdef V8_USE_EXTERNAL_STARTUP_DATA - gin::IsolateHolder::LoadV8Snapshot(); + gin::V8Initializer::LoadV8Snapshot(); #endif #if defined(ENABLE_REMOTING) diff --git a/components/pdf/renderer/ppb_pdf_impl.cc b/components/pdf/renderer/ppb_pdf_impl.cc index 6349543..3553c47 100644 --- a/components/pdf/renderer/ppb_pdf_impl.cc +++ b/components/pdf/renderer/ppb_pdf_impl.cc @@ -18,7 +18,7 @@ #include "content/public/renderer/pepper_plugin_instance.h" #include "content/public/renderer/render_thread.h" #include "content/public/renderer/render_view.h" -#include "gin/public/isolate_holder.h" +#include "gin/v8_initializer.h" #include "ppapi/c/pp_resource.h" #include "ppapi/c/private/ppb_pdf.h" #include "ppapi/c/trusted/ppb_browser_font_trusted.h" @@ -343,8 +343,8 @@ void GetV8ExternalSnapshotData(PP_Instance instance_id, int* natives_size_out, const char** snapshot_data_out, int* snapshot_size_out) { - gin::IsolateHolder::GetV8ExternalSnapshotData(natives_data_out, - natives_size_out, snapshot_data_out, snapshot_size_out); + gin::V8Initializer::GetV8ExternalSnapshotData( + natives_data_out, natives_size_out, snapshot_data_out, snapshot_size_out); } const PPB_PDF ppb_pdf = { // diff --git a/content/app/DEPS b/content/app/DEPS index 61f7537..b6d8307 100644 --- a/content/app/DEPS +++ b/content/app/DEPS @@ -5,5 +5,6 @@ include_rules = [ # For loading V8's initial snapshot from external files. "+gin/public/isolate_holder.h", "+gin/public/snapshot_fd_data.h", + "+gin/v8_initializer.h", "+media/base", # For initializing media library. ] diff --git a/content/app/content_main_runner.cc b/content/app/content_main_runner.cc index 7c8e7eb..f14c6eb 100644 --- a/content/app/content_main_runner.cc +++ b/content/app/content_main_runner.cc @@ -51,7 +51,7 @@ #include "ui/base/ui_base_switches.h" #ifdef V8_USE_EXTERNAL_STARTUP_DATA -#include "gin/public/isolate_holder.h" +#include "gin/v8_initializer.h" #endif #if defined(USE_TCMALLOC) @@ -718,14 +718,14 @@ class ContentMainRunnerImpl : public ContentMainRunner { if (v8_natives_fd != -1 && v8_snapshot_fd != -1) { auto v8_natives_region = g_fds->GetRegion(kV8NativesDataDescriptor); auto v8_snapshot_region = g_fds->GetRegion(kV8SnapshotDataDescriptor); - CHECK(gin::IsolateHolder::LoadV8SnapshotFd( + CHECK(gin::V8Initializer::LoadV8SnapshotFromFD( v8_natives_fd, v8_natives_region.offset, v8_natives_region.size, v8_snapshot_fd, v8_snapshot_region.offset, v8_snapshot_region.size)); } else { - CHECK(gin::IsolateHolder::LoadV8Snapshot()); + CHECK(gin::V8Initializer::LoadV8Snapshot()); } #else - CHECK(gin::IsolateHolder::LoadV8Snapshot()); + CHECK(gin::V8Initializer::LoadV8Snapshot()); #endif // OS_POSIX && !OS_MACOSX #endif // V8_USE_EXTERNAL_STARTUP_DATA diff --git a/content/public/test/DEPS b/content/public/test/DEPS index 43a7d3c..e897ad3 100644 --- a/content/public/test/DEPS +++ b/content/public/test/DEPS @@ -9,7 +9,7 @@ specific_include_rules = { ".*\.cc": [ # Testing utilities can access anything in content/ "+content", - "+gin/public/isolate_holder.h", + "+gin/v8_initializer.h", "+media/base", "+third_party/iaccessible2", "+ui/base/resource/resource_bundle.h", diff --git a/content/public/test/content_test_suite_base.cc b/content/public/test/content_test_suite_base.cc index 81821b0..a19b03f 100644 --- a/content/public/test/content_test_suite_base.cc +++ b/content/public/test/content_test_suite_base.cc @@ -44,7 +44,7 @@ #endif #ifdef V8_USE_EXTERNAL_STARTUP_DATA -#include "gin/public/isolate_holder.h" +#include "gin/v8_initializer.h" #endif namespace content { @@ -73,7 +73,7 @@ void ContentTestSuiteBase::Initialize() { base::StatisticsRecorder::Initialize(); #ifdef V8_USE_EXTERNAL_STARTUP_DATA - gin::IsolateHolder::LoadV8Snapshot(); + gin::V8Initializer::LoadV8Snapshot(); #endif #if defined(OS_ANDROID) diff --git a/content/shell/DEPS b/content/shell/DEPS index a46f5cf..3dfeb6b 100644 --- a/content/shell/DEPS +++ b/content/shell/DEPS @@ -1,5 +1,6 @@ include_rules = [ "+gin/public", + "+gin/v8_initializer.h", "+v8/include", # For chromeos build config diff --git a/content/shell/browser/shell_content_browser_client.cc b/content/shell/browser/shell_content_browser_client.cc index c10761f..9cf8473 100644 --- a/content/shell/browser/shell_content_browser_client.cc +++ b/content/shell/browser/shell_content_browser_client.cc @@ -34,7 +34,7 @@ #include "content/shell/common/shell_messages.h" #include "content/shell/common/shell_switches.h" #include "content/shell/common/webkit_test_helpers.h" -#include "gin/public/isolate_holder.h" +#include "gin/v8_initializer.h" #include "net/url_request/url_request_context_getter.h" #include "url/gurl.h" @@ -335,14 +335,14 @@ void ShellContentBrowserClient::GetAdditionalMappedFilesForChildProcess( #if defined(V8_USE_EXTERNAL_STARTUP_DATA) if (v8_snapshot_fd_.get() == -1 && v8_natives_fd_.get() == -1) { base::FilePath v8_data_path; - PathService::Get(gin::IsolateHolder::kV8SnapshotBasePathKey, &v8_data_path); + PathService::Get(gin::V8Initializer::kV8SnapshotBasePathKey, &v8_data_path); DCHECK(!v8_data_path.empty()); int file_flags = base::File::FLAG_OPEN | base::File::FLAG_READ; base::FilePath v8_natives_data_path = - v8_data_path.AppendASCII(gin::IsolateHolder::kNativesFileName); + v8_data_path.AppendASCII(gin::V8Initializer::kNativesFileName); base::FilePath v8_snapshot_data_path = - v8_data_path.AppendASCII(gin::IsolateHolder::kSnapshotFileName); + v8_data_path.AppendASCII(gin::V8Initializer::kSnapshotFileName); base::File v8_natives_data_file(v8_natives_data_path, file_flags); base::File v8_snapshot_data_file(v8_snapshot_data_path, file_flags); DCHECK(v8_natives_data_file.IsValid()); diff --git a/content/test/DEPS b/content/test/DEPS index 7d1b3d9..b0b89c0 100644 --- a/content/test/DEPS +++ b/content/test/DEPS @@ -4,7 +4,7 @@ include_rules = [ # Testing utilities can access anything in content/ "+content", # For loading V8's initial snapshot from external files. - "+gin/public/isolate_holder.h", + "+gin/v8_initializer.h", "+media/audio", # For AudioParameters in WebRTC tests. "+media/base", # For ChannelLayout in WebRTC tests. "+ui/base/resource/data_pack.h", diff --git a/content/test/content_test_launcher.cc b/content/test/content_test_launcher.cc index 7c7eb27..9b30fdb 100644 --- a/content/test/content_test_launcher.cc +++ b/content/test/content_test_launcher.cc @@ -21,7 +21,7 @@ #include "testing/gtest/include/gtest/gtest.h" #ifdef V8_USE_EXTERNAL_STARTUP_DATA -#include "gin/public/isolate_holder.h" +#include "gin/v8_initializer.h" #endif #if defined(OS_ANDROID) @@ -57,7 +57,7 @@ class ContentBrowserTestSuite : public ContentTestSuiteBase { base::i18n::InitializeICU(); #ifdef V8_USE_EXTERNAL_STARTUP_DATA - gin::IsolateHolder::LoadV8Snapshot(); + gin::V8Initializer::LoadV8Snapshot(); #endif // This needs to be done before base::TestSuite::Initialize() is called, diff --git a/content/test/test_blink_web_unit_test_support.cc b/content/test/test_blink_web_unit_test_support.cc index 9010d0d..1326e7d 100644 --- a/content/test/test_blink_web_unit_test_support.cc +++ b/content/test/test_blink_web_unit_test_support.cc @@ -39,7 +39,7 @@ #endif #ifdef V8_USE_EXTERNAL_STARTUP_DATA -#include "gin/public/isolate_holder.h" +#include "gin/v8_initializer.h" #endif namespace content { @@ -53,7 +53,7 @@ TestBlinkWebUnitTestSupport::TestBlinkWebUnitTestSupport() { mock_clipboard_.reset(new MockWebClipboardImpl()); #ifdef V8_USE_EXTERNAL_STARTUP_DATA - gin::IsolateHolder::LoadV8Snapshot(); + gin::V8Initializer::LoadV8Snapshot(); #endif if (base::MessageLoopProxy::current()) { diff --git a/gin/BUILD.gn b/gin/BUILD.gn index 5446d56..9fe5a12 100644 --- a/gin/BUILD.gn +++ b/gin/BUILD.gn @@ -56,6 +56,8 @@ component("gin") { "shell_runner.h", "try_catch.cc", "try_catch.h", + "v8_initializer.cc", + "v8_initializer.h", "v8_platform.cc", "wrappable.cc", "wrappable.h", diff --git a/gin/gin.gyp b/gin/gin.gyp index 096c120..46afc97 100644 --- a/gin/gin.gyp +++ b/gin/gin.gyp @@ -73,6 +73,8 @@ 'shell_runner.h', 'try_catch.cc', 'try_catch.h', + 'v8_initializer.h', + 'v8_initializer.cc', 'v8_platform.cc', 'wrappable.cc', 'wrappable.h', diff --git a/gin/isolate_holder.cc b/gin/isolate_holder.cc index 317b582..3cc3238 100644 --- a/gin/isolate_holder.cc +++ b/gin/isolate_holder.cc @@ -7,214 +7,24 @@ #include <stdlib.h> #include <string.h> -#include "base/files/memory_mapped_file.h" #include "base/logging.h" #include "base/message_loop/message_loop.h" -#include "base/rand_util.h" -#include "base/strings/sys_string_conversions.h" #include "base/sys_info.h" -#include "crypto/sha2.h" -#include "gin/array_buffer.h" #include "gin/debug_impl.h" #include "gin/function_template.h" #include "gin/per_isolate_data.h" -#include "gin/public/v8_platform.h" #include "gin/run_microtasks_observer.h" - -#if defined(V8_USE_EXTERNAL_STARTUP_DATA) -#if defined(OS_MACOSX) -#include "base/mac/foundation_util.h" -#endif // OS_MACOSX -#include "base/path_service.h" -#endif // V8_USE_EXTERNAL_STARTUP_DATA +#include "gin/v8_initializer.h" namespace gin { namespace { - -v8::ArrayBuffer::Allocator* g_array_buffer_allocator = NULL; - -bool GenerateEntropy(unsigned char* buffer, size_t amount) { - base::RandBytes(buffer, amount); - return true; -} - -base::MemoryMappedFile* g_mapped_natives = NULL; -base::MemoryMappedFile* g_mapped_snapshot = NULL; - -#if defined(V8_USE_EXTERNAL_STARTUP_DATA) -bool MapV8Files(base::FilePath* natives_path, - base::FilePath* snapshot_path, - int natives_fd = -1, - int snapshot_fd = -1, - base::MemoryMappedFile::Region natives_region = - base::MemoryMappedFile::Region::kWholeFile, - base::MemoryMappedFile::Region snapshot_region = - base::MemoryMappedFile::Region::kWholeFile) { - int flags = base::File::FLAG_OPEN | base::File::FLAG_READ; - - g_mapped_natives = new base::MemoryMappedFile; - if (!g_mapped_natives->IsValid()) { -#if !defined(OS_WIN) - if (natives_fd == -1 - ? !g_mapped_natives->Initialize(base::File(*natives_path, flags), - natives_region) - : !g_mapped_natives->Initialize(base::File(natives_fd), - natives_region)) { -#else - if (!g_mapped_natives->Initialize(base::File(*natives_path, flags), - natives_region)) { -#endif // !OS_WIN - delete g_mapped_natives; - g_mapped_natives = NULL; - LOG(FATAL) << "Couldn't mmap v8 natives data file"; - return false; - } - } - - g_mapped_snapshot = new base::MemoryMappedFile; - if (!g_mapped_snapshot->IsValid()) { -#if !defined(OS_WIN) - if (snapshot_fd == -1 - ? !g_mapped_snapshot->Initialize(base::File(*snapshot_path, flags), - snapshot_region) - : !g_mapped_snapshot->Initialize(base::File(snapshot_fd), - snapshot_region)) { -#else - if (!g_mapped_snapshot->Initialize(base::File(*snapshot_path, flags), - snapshot_region)) { -#endif // !OS_WIN - delete g_mapped_snapshot; - g_mapped_snapshot = NULL; - LOG(ERROR) << "Couldn't mmap v8 snapshot data file"; - return false; - } - } - - return true; -} - -#if defined(V8_VERIFY_EXTERNAL_STARTUP_DATA) -bool VerifyV8SnapshotFile(base::MemoryMappedFile* snapshot_file, - const unsigned char* fingerprint) { - unsigned char output[crypto::kSHA256Length]; - crypto::SHA256HashString( - base::StringPiece(reinterpret_cast<const char*>(snapshot_file->data()), - snapshot_file->length()), - output, sizeof(output)); - return !memcmp(fingerprint, output, sizeof(output)); -} -#endif // V8_VERIFY_EXTERNAL_STARTUP_DATA -#endif // V8_USE_EXTERNAL_STARTUP_DATA - +v8::ArrayBuffer::Allocator* g_array_buffer_allocator = nullptr; } // namespace -#if defined(V8_USE_EXTERNAL_STARTUP_DATA) - -#if defined(V8_VERIFY_EXTERNAL_STARTUP_DATA) -// Defined in gen/gin/v8_snapshot_fingerprint.cc -extern const unsigned char g_natives_fingerprint[]; -extern const unsigned char g_snapshot_fingerprint[]; -#endif // V8_VERIFY_EXTERNAL_STARTUP_DATA - -#if !defined(OS_MACOSX) -const int IsolateHolder::kV8SnapshotBasePathKey = -#if defined(OS_ANDROID) - base::DIR_ANDROID_APP_DATA; -#elif defined(OS_POSIX) - base::DIR_EXE; -#elif defined(OS_WIN) - base::DIR_MODULE; -#endif // OS_ANDROID -#endif // !OS_MACOSX - -const char IsolateHolder::kNativesFileName[] = "natives_blob.bin"; -const char IsolateHolder::kSnapshotFileName[] = "snapshot_blob.bin"; - -// static -bool IsolateHolder::LoadV8Snapshot() { - if (g_mapped_natives && g_mapped_snapshot) - return true; - -#if !defined(OS_MACOSX) - base::FilePath data_path; - PathService::Get(kV8SnapshotBasePathKey, &data_path); - DCHECK(!data_path.empty()); - - base::FilePath natives_path = data_path.AppendASCII(kNativesFileName); - base::FilePath snapshot_path = data_path.AppendASCII(kSnapshotFileName); -#else // !defined(OS_MACOSX) - base::ScopedCFTypeRef<CFStringRef> natives_file_name( - base::SysUTF8ToCFStringRef(kNativesFileName)); - base::FilePath natives_path = base::mac::PathForFrameworkBundleResource( - natives_file_name); - base::ScopedCFTypeRef<CFStringRef> snapshot_file_name( - base::SysUTF8ToCFStringRef(kSnapshotFileName)); - base::FilePath snapshot_path = base::mac::PathForFrameworkBundleResource( - snapshot_file_name); - DCHECK(!natives_path.empty()); - DCHECK(!snapshot_path.empty()); -#endif // !defined(OS_MACOSX) - - if (!MapV8Files(&natives_path, &snapshot_path)) - return false; - -#if defined(V8_VERIFY_EXTERNAL_STARTUP_DATA) - return VerifyV8SnapshotFile(g_mapped_natives, g_natives_fingerprint) && - VerifyV8SnapshotFile(g_mapped_snapshot, g_snapshot_fingerprint); -#else - return true; -#endif // V8_VERIFY_EXTERNAL_STARTUP_DATA -} - -//static -bool IsolateHolder::LoadV8SnapshotFd(int natives_fd, - int64 natives_offset, - int64 natives_size, - int snapshot_fd, - int64 snapshot_offset, - int64 snapshot_size) { - if (g_mapped_natives && g_mapped_snapshot) - return true; - - base::MemoryMappedFile::Region natives_region = - base::MemoryMappedFile::Region::kWholeFile; - if (natives_size != 0 || natives_offset != 0) { - natives_region = - base::MemoryMappedFile::Region(natives_offset, natives_size); - } - - base::MemoryMappedFile::Region snapshot_region = - base::MemoryMappedFile::Region::kWholeFile; - if (natives_size != 0 || natives_offset != 0) { - snapshot_region = - base::MemoryMappedFile::Region(snapshot_offset, snapshot_size); - } - - return MapV8Files( - NULL, NULL, natives_fd, snapshot_fd, natives_region, snapshot_region); -} -#endif // V8_USE_EXTERNAL_STARTUP_DATA - -//static -void IsolateHolder::GetV8ExternalSnapshotData(const char** natives_data_out, - int* natives_size_out, - const char** snapshot_data_out, - int* snapshot_size_out) { - if (!g_mapped_natives || !g_mapped_snapshot) { - *natives_data_out = *snapshot_data_out = NULL; - *natives_size_out = *snapshot_size_out = 0; - return; - } - *natives_data_out = reinterpret_cast<const char*>(g_mapped_natives->data()); - *snapshot_data_out = reinterpret_cast<const char*>(g_mapped_snapshot->data()); - *natives_size_out = static_cast<int>(g_mapped_natives->length()); - *snapshot_size_out = static_cast<int>(g_mapped_snapshot->length()); -} - IsolateHolder::IsolateHolder() { - CHECK(g_array_buffer_allocator) - << "You need to invoke gin::IsolateHolder::Initialize first"; + v8::ArrayBuffer::Allocator* allocator = g_array_buffer_allocator; + CHECK(allocator) << "You need to invoke gin::IsolateHolder::Initialize first"; v8::Isolate::CreateParams params; params.entry_hook = DebugImpl::GetFunctionEntryHook(); params.code_event_handler = DebugImpl::GetJitCodeEventHandler(); @@ -222,7 +32,7 @@ IsolateHolder::IsolateHolder() { base::SysInfo::AmountOfVirtualMemory(), base::SysInfo::NumberOfProcessors()); isolate_ = v8::Isolate::New(params); - isolate_data_.reset(new PerIsolateData(isolate_, g_array_buffer_allocator)); + isolate_data_.reset(new PerIsolateData(isolate_, allocator)); #if defined(OS_WIN) { void* code_range; @@ -259,30 +69,8 @@ IsolateHolder::~IsolateHolder() { void IsolateHolder::Initialize(ScriptMode mode, v8::ArrayBuffer::Allocator* allocator) { CHECK(allocator); - static bool v8_is_initialized = false; - if (v8_is_initialized) - return; - v8::V8::InitializePlatform(V8Platform::Get()); - v8::V8::SetArrayBufferAllocator(allocator); + gin::V8Initializer::Initialize(mode, allocator); g_array_buffer_allocator = allocator; - if (mode == gin::IsolateHolder::kStrictMode) { - static const char use_strict[] = "--use_strict"; - v8::V8::SetFlagsFromString(use_strict, sizeof(use_strict) - 1); - } - v8::V8::SetEntropySource(&GenerateEntropy); -#if defined(V8_USE_EXTERNAL_STARTUP_DATA) - v8::StartupData natives; - natives.data = reinterpret_cast<const char*>(g_mapped_natives->data()); - natives.raw_size = static_cast<int>(g_mapped_natives->length()); - v8::V8::SetNativesDataBlob(&natives); - - v8::StartupData snapshot; - snapshot.data = reinterpret_cast<const char*>(g_mapped_snapshot->data()); - snapshot.raw_size = static_cast<int>(g_mapped_snapshot->length()); - v8::V8::SetSnapshotDataBlob(&snapshot); -#endif // V8_USE_EXTERNAL_STARTUP_DATA - v8::V8::Initialize(); - v8_is_initialized = true; } void IsolateHolder::AddRunMicrotasksObserver() { @@ -297,4 +85,13 @@ void IsolateHolder::RemoveRunMicrotasksObserver() { task_observer_.reset(); } +#if defined(V8_USE_EXTERNAL_STARTUP_DATA) + +// static +bool IsolateHolder::LoadV8Snapshot() { + return V8Initializer::LoadV8Snapshot(); +} + +#endif // V8_USE_EXTERNAL_STARTUP_DATA + } // namespace gin diff --git a/gin/public/isolate_holder.h b/gin/public/isolate_holder.h index 7968b1df..d2275ed 100644 --- a/gin/public/isolate_holder.h +++ b/gin/public/isolate_holder.h @@ -31,9 +31,10 @@ class GIN_EXPORT IsolateHolder { ~IsolateHolder(); // Should be invoked once before creating IsolateHolder instances to - // initialize V8 and Gin. In case V8_USE_EXTERNAL_STARTUP_DATA is defined, - // V8's initial snapshot should be loaded (by calling LoadV8Snapshot or - // LoadV8SnapshotFd) before calling Initialize. + // initialize V8 and Gin. In case V8_USE_EXTERNAL_STARTUP_DATA is + // defined, V8's initial snapshot should be loaded (by calling + // V8Initializer::LoadV8SnapshotFromFD or + // V8Initializer::LoadV8Snapshot) before calling this method. static void Initialize(ScriptMode mode, v8::ArrayBuffer::Allocator* allocator); @@ -52,22 +53,13 @@ class GIN_EXPORT IsolateHolder { void RemoveRunMicrotasksObserver(); #if defined(V8_USE_EXTERNAL_STARTUP_DATA) - static const int kV8SnapshotBasePathKey; - static const char kNativesFileName[]; - static const char kSnapshotFileName[]; - - static bool LoadV8SnapshotFd(int natives_fd, - int64 natives_offset, - int64 natives_size, - int snapshot_fd, - int64 snapshot_offset, - int64 snapshot_size); + + // Deprecated and to be removed: Use gin::V8Initializer::LoadV8Snapshot() + // Load V8 snapshot from default resources. Returns true on success or + // snapshot is already loaded, false otherwise. static bool LoadV8Snapshot(); + #endif // V8_USE_EXTERNAL_STARTUP_DATA - static void GetV8ExternalSnapshotData(const char** natives_data_out, - int* natives_size_out, - const char** snapshot_data_out, - int* snapshot_size_out); private: v8::Isolate* isolate_; diff --git a/gin/shell/gin_main.cc b/gin/shell/gin_main.cc index 532f996..d3bab72 100644 --- a/gin/shell/gin_main.cc +++ b/gin/shell/gin_main.cc @@ -13,6 +13,7 @@ #include "gin/modules/module_runner_delegate.h" #include "gin/public/isolate_holder.h" #include "gin/try_catch.h" +#include "gin/v8_initializer.h" namespace gin { namespace { @@ -60,7 +61,7 @@ int main(int argc, char** argv) { base::CommandLine::Init(argc, argv); base::i18n::InitializeICU(); #ifdef V8_USE_EXTERNAL_STARTUP_DATA - gin::IsolateHolder::LoadV8Snapshot(); + gin::V8Initializer::LoadV8Snapshot(); #endif gin::IsolateHolder::Initialize(gin::IsolateHolder::kStrictMode, diff --git a/gin/shell_runner_unittest.cc b/gin/shell_runner_unittest.cc index 7134de1..340b673 100644 --- a/gin/shell_runner_unittest.cc +++ b/gin/shell_runner_unittest.cc @@ -8,6 +8,7 @@ #include "gin/array_buffer.h" #include "gin/converter.h" #include "gin/public/isolate_holder.h" +#include "gin/v8_initializer.h" #include "testing/gtest/include/gtest/gtest.h" #ifdef V8_USE_EXTERNAL_STARTUP_DATA @@ -25,7 +26,7 @@ TEST(RunnerTest, Run) { std::string source = "this.result = 'PASS';\n"; #ifdef V8_USE_EXTERNAL_STARTUP_DATA - gin::IsolateHolder::LoadV8Snapshot(); + gin::V8Initializer::LoadV8Snapshot(); #endif gin::IsolateHolder::Initialize(gin::IsolateHolder::kStrictMode, diff --git a/gin/test/file_runner.cc b/gin/test/file_runner.cc index b639d06..723ab98 100644 --- a/gin/test/file_runner.cc +++ b/gin/test/file_runner.cc @@ -17,12 +17,9 @@ #include "gin/test/gc.h" #include "gin/test/gtest.h" #include "gin/try_catch.h" +#include "gin/v8_initializer.h" #include "testing/gtest/include/gtest/gtest.h" -#ifdef V8_USE_EXTERNAL_STARTUP_DATA -#include "gin/public/isolate_holder.h" -#endif - namespace gin { namespace { @@ -63,11 +60,12 @@ void RunTestFromFile(const base::FilePath& path, FileRunnerDelegate* delegate, base::MessageLoop message_loop; #ifdef V8_USE_EXTERNAL_STARTUP_DATA - gin::IsolateHolder::LoadV8Snapshot(); + gin::V8Initializer::LoadV8Snapshot(); #endif gin::IsolateHolder::Initialize(gin::IsolateHolder::kStrictMode, gin::ArrayBufferAllocator::SharedInstance()); + gin::IsolateHolder instance; gin::ShellRunner runner(delegate, instance.isolate()); { diff --git a/gin/test/v8_test.cc b/gin/test/v8_test.cc index a022511..aad291d 100644 --- a/gin/test/v8_test.cc +++ b/gin/test/v8_test.cc @@ -6,10 +6,7 @@ #include "gin/array_buffer.h" #include "gin/public/isolate_holder.h" - -#ifdef V8_USE_EXTERNAL_STARTUP_DATA -#include "gin/public/isolate_holder.h" -#endif +#include "gin/v8_initializer.h" using v8::Context; using v8::Local; @@ -25,10 +22,11 @@ V8Test::~V8Test() { void V8Test::SetUp() { #ifdef V8_USE_EXTERNAL_STARTUP_DATA - gin::IsolateHolder::LoadV8Snapshot(); + gin::V8Initializer::LoadV8Snapshot(); #endif gin::IsolateHolder::Initialize(gin::IsolateHolder::kStrictMode, gin::ArrayBufferAllocator::SharedInstance()); + instance_.reset(new gin::IsolateHolder); instance_->isolate()->Enter(); HandleScope handle_scope(instance_->isolate()); diff --git a/gin/v8_initializer.cc b/gin/v8_initializer.cc new file mode 100644 index 0000000..76f227d --- /dev/null +++ b/gin/v8_initializer.cc @@ -0,0 +1,221 @@ +// Copyright 2013 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "gin/v8_initializer.h" + +#include "base/basictypes.h" +#include "base/files/file.h" +#include "base/files/file_path.h" +#include "base/files/memory_mapped_file.h" +#include "base/logging.h" +#include "base/rand_util.h" +#include "base/strings/sys_string_conversions.h" +#include "crypto/sha2.h" + +#if defined(V8_USE_EXTERNAL_STARTUP_DATA) +#if defined(OS_MACOSX) +#include "base/mac/foundation_util.h" +#endif // OS_MACOSX +#include "base/path_service.h" +#endif // V8_USE_EXTERNAL_STARTUP_DATA + +namespace gin { + +namespace { + +bool GenerateEntropy(unsigned char* buffer, size_t amount) { + base::RandBytes(buffer, amount); + return true; +} + +base::MemoryMappedFile* g_mapped_natives = nullptr; +base::MemoryMappedFile* g_mapped_snapshot = nullptr; + +#if defined(V8_USE_EXTERNAL_STARTUP_DATA) +bool MapV8Files(base::File natives_file, + base::File snapshot_file, + base::MemoryMappedFile::Region natives_region = + base::MemoryMappedFile::Region::kWholeFile, + base::MemoryMappedFile::Region snapshot_region = + base::MemoryMappedFile::Region::kWholeFile) { + g_mapped_natives = new base::MemoryMappedFile; + if (!g_mapped_natives->IsValid()) { + if (!g_mapped_natives->Initialize(natives_file.Pass(), natives_region)) { + delete g_mapped_natives; + g_mapped_natives = NULL; + LOG(FATAL) << "Couldn't mmap v8 natives data file"; + return false; + } + } + + g_mapped_snapshot = new base::MemoryMappedFile; + if (!g_mapped_snapshot->IsValid()) { + if (!g_mapped_snapshot->Initialize(snapshot_file.Pass(), snapshot_region)) { + delete g_mapped_snapshot; + g_mapped_snapshot = NULL; + LOG(ERROR) << "Couldn't mmap v8 snapshot data file"; + return false; + } + } + + return true; +} + +#if defined(V8_VERIFY_EXTERNAL_STARTUP_DATA) +bool VerifyV8SnapshotFile(base::MemoryMappedFile* snapshot_file, + const unsigned char* fingerprint) { + unsigned char output[crypto::kSHA256Length]; + crypto::SHA256HashString( + base::StringPiece(reinterpret_cast<const char*>(snapshot_file->data()), + snapshot_file->length()), + output, sizeof(output)); + return !memcmp(fingerprint, output, sizeof(output)); +} +#endif // V8_VERIFY_EXTERNAL_STARTUP_DATA +#endif // V8_USE_EXTERNAL_STARTUP_DATA + +} // namespace + +#if defined(V8_USE_EXTERNAL_STARTUP_DATA) + +#if defined(V8_VERIFY_EXTERNAL_STARTUP_DATA) +// Defined in gen/gin/v8_snapshot_fingerprint.cc +extern const unsigned char g_natives_fingerprint[]; +extern const unsigned char g_snapshot_fingerprint[]; +#endif // V8_VERIFY_EXTERNAL_STARTUP_DATA + +#if !defined(OS_MACOSX) +const int V8Initializer::kV8SnapshotBasePathKey = +#if defined(OS_ANDROID) + base::DIR_ANDROID_APP_DATA; +#elif defined(OS_POSIX) + base::DIR_EXE; +#elif defined(OS_WIN) + base::DIR_MODULE; +#endif // OS_ANDROID +#endif // !OS_MACOSX + +const char V8Initializer::kNativesFileName[] = "natives_blob.bin"; +const char V8Initializer::kSnapshotFileName[] = "snapshot_blob.bin"; + +// static +bool V8Initializer::LoadV8Snapshot() { + if (g_mapped_natives && g_mapped_snapshot) + return true; + +#if !defined(OS_MACOSX) + base::FilePath data_path; + PathService::Get(kV8SnapshotBasePathKey, &data_path); + DCHECK(!data_path.empty()); + + base::FilePath natives_path = data_path.AppendASCII(kNativesFileName); + base::FilePath snapshot_path = data_path.AppendASCII(kSnapshotFileName); +#else // !defined(OS_MACOSX) + base::ScopedCFTypeRef<CFStringRef> natives_file_name( + base::SysUTF8ToCFStringRef(kNativesFileName)); + base::FilePath natives_path = + base::mac::PathForFrameworkBundleResource(natives_file_name); + base::ScopedCFTypeRef<CFStringRef> snapshot_file_name( + base::SysUTF8ToCFStringRef(kSnapshotFileName)); + base::FilePath snapshot_path = + base::mac::PathForFrameworkBundleResource(snapshot_file_name); + DCHECK(!natives_path.empty()); + DCHECK(!snapshot_path.empty()); +#endif // !defined(OS_MACOSX) + + int flags = base::File::FLAG_OPEN | base::File::FLAG_READ; + if (!MapV8Files(base::File(natives_path, flags), + base::File(snapshot_path, flags))) + return false; + +#if defined(V8_VERIFY_EXTERNAL_STARTUP_DATA) + return VerifyV8SnapshotFile(g_mapped_natives, g_natives_fingerprint) && + VerifyV8SnapshotFile(g_mapped_snapshot, g_snapshot_fingerprint); +#else + return true; +#endif // V8_VERIFY_EXTERNAL_STARTUP_DATA +} + +// static +bool V8Initializer::LoadV8SnapshotFromFD(base::PlatformFile natives_pf, + int64 natives_offset, + int64 natives_size, + base::PlatformFile snapshot_pf, + int64 snapshot_offset, + int64 snapshot_size) { + if (g_mapped_natives && g_mapped_snapshot) + return true; + + base::MemoryMappedFile::Region natives_region = + base::MemoryMappedFile::Region::kWholeFile; + if (natives_size != 0 || natives_offset != 0) { + natives_region = + base::MemoryMappedFile::Region(natives_offset, natives_size); + } + + base::MemoryMappedFile::Region snapshot_region = + base::MemoryMappedFile::Region::kWholeFile; + if (natives_size != 0 || natives_offset != 0) { + snapshot_region = + base::MemoryMappedFile::Region(snapshot_offset, snapshot_size); + } + + return MapV8Files(base::File(natives_pf), base::File(snapshot_pf), + natives_region, snapshot_region); +} + +#endif // V8_USE_EXTERNAL_STARTUP_DATA + +// static +void V8Initializer::Initialize(gin::IsolateHolder::ScriptMode mode, + v8::ArrayBuffer::Allocator* allocator) { + CHECK(allocator); + + static bool v8_is_initialized = false; + if (v8_is_initialized) + return; + + v8::V8::InitializePlatform(V8Platform::Get()); + v8::V8::SetArrayBufferAllocator(allocator); + + if (gin::IsolateHolder::kStrictMode == mode) { + static const char use_strict[] = "--use_strict"; + v8::V8::SetFlagsFromString(use_strict, sizeof(use_strict) - 1); + } + +#if defined(V8_USE_EXTERNAL_STARTUP_DATA) + v8::StartupData natives; + natives.data = reinterpret_cast<const char*>(g_mapped_natives->data()); + natives.raw_size = static_cast<int>(g_mapped_natives->length()); + v8::V8::SetNativesDataBlob(&natives); + + v8::StartupData snapshot; + snapshot.data = reinterpret_cast<const char*>(g_mapped_snapshot->data()); + snapshot.raw_size = static_cast<int>(g_mapped_snapshot->length()); + v8::V8::SetSnapshotDataBlob(&snapshot); +#endif // V8_USE_EXTERNAL_STARTUP_DATA + + v8::V8::SetEntropySource(&GenerateEntropy); + v8::V8::Initialize(); + + v8_is_initialized = true; +} + +// static +void V8Initializer::GetV8ExternalSnapshotData(const char** natives_data_out, + int* natives_size_out, + const char** snapshot_data_out, + int* snapshot_size_out) { + if (!g_mapped_natives || !g_mapped_snapshot) { + *natives_data_out = *snapshot_data_out = NULL; + *natives_size_out = *snapshot_size_out = 0; + return; + } + *natives_data_out = reinterpret_cast<const char*>(g_mapped_natives->data()); + *snapshot_data_out = reinterpret_cast<const char*>(g_mapped_snapshot->data()); + *natives_size_out = static_cast<int>(g_mapped_natives->length()); + *snapshot_size_out = static_cast<int>(g_mapped_snapshot->length()); +} + +} // namespace gin diff --git a/gin/v8_initializer.h b/gin/v8_initializer.h new file mode 100644 index 0000000..ad3ed6e --- /dev/null +++ b/gin/v8_initializer.h @@ -0,0 +1,57 @@ +// Copyright 2013 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef GIN_V8_INITIALIZER_H_ +#define GIN_V8_INITIALIZER_H_ + +#include "base/files/file.h" +#include "gin/array_buffer.h" +#include "gin/gin_export.h" +#include "gin/public/isolate_holder.h" +#include "gin/public/v8_platform.h" +#include "v8/include/v8.h" + +namespace gin { + +class GIN_EXPORT V8Initializer { + public: + static const int kV8SnapshotBasePathKey; + static const char kNativesFileName[]; + static const char kSnapshotFileName[]; + + // This should be called by IsolateHolder::Initialize(). + static void Initialize(gin::IsolateHolder::ScriptMode mode, + v8::ArrayBuffer::Allocator* allocator); + + // Get address and size information for currently loaded snapshot. + // If no snapshot is loaded, the return values are null for addresses + // and 0 for sizes. + static void GetV8ExternalSnapshotData(const char** natives_data_out, + int* natives_size_out, + const char** snapshot_data_out, + int* snapshot_size_out); + +#if defined(V8_USE_EXTERNAL_STARTUP_DATA) + + // Load V8 snapshot from user provided platform file descriptors. + // The offset and size arguments, if non-zero, specify the portions + // of the files to be loaded. This methods returns true on success + // (or if snapshot is already loaded), false otherwise. + static bool LoadV8SnapshotFromFD(base::PlatformFile natives_fd, + int64 natives_offset, + int64 natives_size, + base::PlatformFile snapshot_fd, + int64 snapshot_offset, + int64 snapshot_size); + + // Load V8 snapshot from default resources. Returns true on success or + // snapshot is already loaded, false otherwise. + static bool LoadV8Snapshot(); + +#endif // V8_USE_EXTERNAL_STARTUP_DATA +}; + +} // namespace gin + +#endif // GIN_V8_INITIALIZER_H_ diff --git a/net/proxy/proxy_resolver_v8.cc b/net/proxy/proxy_resolver_v8.cc index fcac10e..c41b190 100644 --- a/net/proxy/proxy_resolver_v8.cc +++ b/net/proxy/proxy_resolver_v8.cc @@ -18,6 +18,7 @@ #include "base/synchronization/lock.h" #include "gin/array_buffer.h" #include "gin/public/isolate_holder.h" +#include "gin/v8_initializer.h" #include "net/base/net_errors.h" #include "net/base/net_util.h" #include "net/log/net_log.h" @@ -365,7 +366,7 @@ class SharedIsolateFactory { // Do one-time initialization for V8. if (!has_initialized_v8_) { #ifdef V8_USE_EXTERNAL_STARTUP_DATA - gin::IsolateHolder::LoadV8Snapshot(); + gin::V8Initializer::LoadV8Snapshot(); #endif gin::IsolateHolder::Initialize( diff --git a/ppapi/proxy/pdf_resource.cc b/ppapi/proxy/pdf_resource.cc index e9d3ae8..384fdc9 100644 --- a/ppapi/proxy/pdf_resource.cc +++ b/ppapi/proxy/pdf_resource.cc @@ -10,7 +10,7 @@ #include "base/command_line.h" #include "base/metrics/histogram.h" #include "base/strings/utf_string_conversions.h" -#include "gin/public/isolate_holder.h" +#include "gin/v8_initializer.h" #include "ppapi/c/pp_errors.h" #include "ppapi/c/private/ppb_pdf.h" #include "ppapi/proxy/ppapi_messages.h" @@ -210,8 +210,8 @@ void PDFResource::GetV8ExternalSnapshotData(const char** natives_data_out, int* natives_size_out, const char** snapshot_data_out, int* snapshot_size_out) { - gin::IsolateHolder::GetV8ExternalSnapshotData(natives_data_out, - natives_size_out, snapshot_data_out, snapshot_size_out); + gin::V8Initializer::GetV8ExternalSnapshotData( + natives_data_out, natives_size_out, snapshot_data_out, snapshot_size_out); } } // namespace proxy |