From b8b7e8e829c3ad42d2870f369a8ec460896fc47f Mon Sep 17 00:00:00 2001 From: Ryan Macnak Date: Wed, 25 Apr 2018 13:31:13 -0700 Subject: [PATCH] [fuchsia] Changes for running Dart 2 on flutter_runner. (flutter/engine#5080) - Remove assumption that we're in Dart 2 mode only if there's a platform kernel. - Load core snapshots from the package instead of linking them in. --- engine/src/flutter/BUILD.gn | 158 +++++++++++++++--- engine/src/flutter/common/settings.cc | 11 +- engine/src/flutter/common/settings.h | 11 +- engine/src/flutter/runtime/BUILD.gn | 3 +- engine/src/flutter/runtime/dart_isolate.cc | 14 +- engine/src/flutter/runtime/dart_snapshot.cc | 28 ++-- engine/src/flutter/runtime/dart_vm.cc | 4 +- engine/src/flutter/shell/common/switches.cc | 27 ++- .../shell/platform/android/flutter_main.cc | 7 +- .../framework/Source/FlutterDartProject.mm | 2 +- .../shell/platform/embedder/embedder.cc | 9 +- .../src/flutter/shell/testing/tester_main.cc | 2 +- 12 files changed, 203 insertions(+), 73 deletions(-) diff --git a/engine/src/flutter/BUILD.gn b/engine/src/flutter/BUILD.gn index 6560972bc5..b5be7064e9 100644 --- a/engine/src/flutter/BUILD.gn +++ b/engine/src/flutter/BUILD.gn @@ -79,47 +79,165 @@ if (is_fuchsia) { "$flutter_root/content_handler:aot", ] if (flutter_runtime_mode != "release") { - deps += [ "//third_party/dart/runtime/observatory:embedded_archive_observatory" ] + deps += [ + "//third_party/dart/runtime/observatory:embedded_archive_observatory", + ] } binary = "flutter_aot_runner" if (flutter_runtime_mode != "release") { - resources = [ { - path = rebase_path( - "$root_gen_dir/observatory/embedded_archive_observatory.tar") - dest = "observatory.tar" - } ] + resources = [ + { + path = rebase_path( + "$root_gen_dir/observatory/embedded_archive_observatory.tar") + dest = "observatory.tar" + }, + ] } - meta = [ { - path = rebase_path("content_handler/meta/sandbox") - dest = "sandbox" - } ] + meta = [ + { + path = rebase_path("content_handler/meta/sandbox") + dest = "sandbox" + }, + ] + } + + package("flutter2_aot_runner") { + deps = [ + "$flutter_root/content_handler:aot", + ] + if (flutter_runtime_mode != "release") { + deps += [ + "//third_party/dart/runtime/observatory:embedded_archive_observatory", + ] + } + + binary = "flutter_aot_runner" + + if (flutter_runtime_mode != "release") { + resources = [ + { + path = rebase_path( + "$root_gen_dir/observatory/embedded_archive_observatory.tar") + dest = "observatory.tar" + }, + ] + } + + meta = [ + { + path = rebase_path("content_handler/meta/sandbox") + dest = "sandbox" + }, + ] } package("flutter_jit_runner") { + snapshot_label = "$flutter_root/lib/snapshot:generate_snapshot_bin" + snapshot_gen_dir = get_label_info(snapshot_label, "target_gen_dir") + deps = [ "$flutter_root/content_handler:jit", + snapshot_label, ] if (flutter_runtime_mode != "release") { - deps += [ "//third_party/dart/runtime/observatory:embedded_archive_observatory" ] + deps += [ + "//third_party/dart/runtime/observatory:embedded_archive_observatory", + ] } binary = "flutter_jit_runner" + resources = [ + { + path = rebase_path("$snapshot_gen_dir/vm_isolate_snapshot.bin") + dest = "vm_snapshot_data.bin" + }, + { + path = rebase_path("$snapshot_gen_dir/vm_snapshot_instructions.bin") + dest = "vm_snapshot_instructions.bin" + }, + { + path = rebase_path("$snapshot_gen_dir/isolate_snapshot.bin") + dest = "isolate_core_snapshot_data.bin" + }, + { + path = + rebase_path("$snapshot_gen_dir/isolate_snapshot_instructions.bin") + dest = "isolate_core_snapshot_instructions.bin" + }, + ] if (flutter_runtime_mode != "release") { - resources = [ { - path = rebase_path( - "$root_gen_dir/observatory/embedded_archive_observatory.tar") - dest = "observatory.tar" - } ] + resources += [ + { + path = rebase_path( + "$root_gen_dir/observatory/embedded_archive_observatory.tar") + dest = "observatory.tar" + }, + ] } - meta = [ { - path = rebase_path("content_handler/meta/sandbox") - dest = "sandbox" - } ] + meta = [ + { + path = rebase_path("content_handler/meta/sandbox") + dest = "sandbox" + }, + ] + } + + package("flutter2_jit_runner") { + snapshot_label = "//topaz/runtime/flutter_runner/kernel:kernel_core_snapshot" + snapshot_gen_dir = get_label_info(snapshot_label, "target_gen_dir") + + deps = [ + "$flutter_root/content_handler:jit", + snapshot_label, + ] + if (flutter_runtime_mode != "release") { + deps += [ + "//third_party/dart/runtime/observatory:embedded_archive_observatory", + ] + } + + binary = "flutter_jit_runner" + + resources = [ + { + path = rebase_path("$snapshot_gen_dir/vm_isolate_snapshot.bin") + dest = "vm_snapshot_data.bin" + }, + { + path = rebase_path("$snapshot_gen_dir/vm_snapshot_instructions.bin") + dest = "vm_snapshot_instructions.bin" + }, + { + path = rebase_path("$snapshot_gen_dir/isolate_snapshot.bin") + dest = "isolate_core_snapshot_data.bin" + }, + { + path = + rebase_path("$snapshot_gen_dir/isolate_snapshot_instructions.bin") + dest = "isolate_core_snapshot_instructions.bin" + }, + ] + if (flutter_runtime_mode != "release") { + resources += [ + { + path = rebase_path( + "$root_gen_dir/observatory/embedded_archive_observatory.tar") + dest = "observatory.tar" + }, + ] + } + + meta = [ + { + path = rebase_path("content_handler/meta/sandbox") + dest = "sandbox" + }, + ] } } else { group("dist") { diff --git a/engine/src/flutter/common/settings.cc b/engine/src/flutter/common/settings.cc index 85523e1b9a..e6a9f24ba7 100644 --- a/engine/src/flutter/common/settings.cc +++ b/engine/src/flutter/common/settings.cc @@ -11,16 +11,13 @@ namespace blink { std::string Settings::ToString() const { std::stringstream stream; stream << "Settings: " << std::endl; - stream << "aot_snapshot_path: " << aot_snapshot_path << std::endl; stream << "script_snapshot_path: " << script_snapshot_path << std::endl; - stream << "aot_vm_snapshot_data_filename: " << aot_vm_snapshot_data_filename + stream << "vm_snapshot_data_path: " << vm_snapshot_data_path << std::endl; + stream << "vm_snapshot_instr_path: " << vm_snapshot_instr_path << std::endl; + stream << "isolate_snapshot_data_path: " << isolate_snapshot_data_path << std::endl; - stream << "aot_vm_snapshot_instr_filename: " << aot_vm_snapshot_instr_filename + stream << "isolate_snapshot_instr_path: " << isolate_snapshot_instr_path << std::endl; - stream << "aot_isolate_snapshot_data_filename: " - << aot_isolate_snapshot_data_filename << std::endl; - stream << "aot_isolate_snapshot_instr_filename: " - << aot_isolate_snapshot_instr_filename << std::endl; stream << "application_library_path: " << application_library_path << std::endl; stream << "main_dart_file_path: " << main_dart_file_path << std::endl; diff --git a/engine/src/flutter/common/settings.h b/engine/src/flutter/common/settings.h index ffb80131f0..4f035d39db 100644 --- a/engine/src/flutter/common/settings.h +++ b/engine/src/flutter/common/settings.h @@ -24,13 +24,12 @@ using TaskObserverRemove = std::function; struct Settings { // VM settings std::string script_snapshot_path; - std::string kernel_snapshot_path; + std::string platform_kernel_path; - std::string aot_snapshot_path; - std::string aot_vm_snapshot_data_filename; - std::string aot_vm_snapshot_instr_filename; - std::string aot_isolate_snapshot_data_filename; - std::string aot_isolate_snapshot_instr_filename; + std::string vm_snapshot_data_path; + std::string vm_snapshot_instr_path; + std::string isolate_snapshot_data_path; + std::string isolate_snapshot_instr_path; std::string application_library_path; std::string application_kernel_asset; diff --git a/engine/src/flutter/runtime/BUILD.gn b/engine/src/flutter/runtime/BUILD.gn index c607bfd1fa..8ed7952eaa 100644 --- a/engine/src/flutter/runtime/BUILD.gn +++ b/engine/src/flutter/runtime/BUILD.gn @@ -114,7 +114,8 @@ source_set("runtime") { # In AOT mode, precompiled snapshots contain the instruction buffer. # Generation of the same requires all application specific script code to be # specified up front. In such cases, there can be no generic snapshot. - if (!flutter_aot) { + # In Fuchsia, we load from a file instead of linking. + if (!flutter_aot && !is_fuchsia) { deps += [ "$flutter_root/lib/snapshot" ] } } diff --git a/engine/src/flutter/runtime/dart_isolate.cc b/engine/src/flutter/runtime/dart_isolate.cc index 7190a0b98a..05b34b979e 100644 --- a/engine/src/flutter/runtime/dart_isolate.cc +++ b/engine/src/flutter/runtime/dart_isolate.cc @@ -23,6 +23,7 @@ #include "lib/tonic/file_loader/file_loader.h" #include "lib/tonic/scopes/dart_api_scope.h" #include "lib/tonic/scopes/dart_isolate_scope.h" +#include "third_party/dart/runtime/include/dart_api.h" #include "third_party/dart/runtime/include/dart_tools_api.h" #ifdef ERROR @@ -290,9 +291,8 @@ static bool LoadKernelSnapshot(std::shared_ptr mapping) { return true; } -static bool LoadSnapshot(std::shared_ptr mapping, - bool is_kernel) { - if (is_kernel) { +static bool LoadSnapshot(std::shared_ptr mapping) { + if (Dart_IsKernel(mapping->GetMapping(), mapping->GetSize())) { return LoadKernelSnapshot(std::move(mapping)); } else { return LoadScriptSnapshot(std::move(mapping)); @@ -322,7 +322,7 @@ bool DartIsolate::PrepareForRunningFromSnapshot( return false; } - if (!LoadSnapshot(mapping, vm_->GetPlatformKernel() != nullptr)) { + if (!LoadSnapshot(mapping)) { return false; } @@ -539,8 +539,12 @@ Dart_Isolate DartIsolate::DartCreateAndStartServiceIsolate( // thread. service_isolate->ResetWeakPtrFactory(); + const bool isolate_snapshot_is_dart_2 = + Dart_IsDart2Snapshot(vm->GetIsolateSnapshot()->GetData()->GetSnapshotPointer()); + const bool is_preview_dart2 = + vm->GetPlatformKernel() != nullptr || isolate_snapshot_is_dart_2; const bool running_from_sources = - !DartVM::IsRunningPrecompiledCode() && vm->GetPlatformKernel() == nullptr; + !DartVM::IsRunningPrecompiledCode() && !is_preview_dart2; tonic::DartState::Scope scope(service_isolate); if (!DartServiceIsolate::Startup( diff --git a/engine/src/flutter/runtime/dart_snapshot.cc b/engine/src/flutter/runtime/dart_snapshot.cc index 9a70d95c2f..4483ecd6c7 100644 --- a/engine/src/flutter/runtime/dart_snapshot.cc +++ b/engine/src/flutter/runtime/dart_snapshot.cc @@ -22,11 +22,9 @@ const char* DartSnapshot::kIsolateInstructionsSymbol = "kDartIsolateSnapshotInstructions"; std::unique_ptr ResolveVMData(const Settings& settings) { - if (settings.aot_snapshot_path.size() > 0) { - auto path = fml::paths::JoinPaths( - {settings.aot_snapshot_path, settings.aot_vm_snapshot_data_filename}); + if (settings.vm_snapshot_data_path.size() > 0) { if (auto source = DartSnapshotBuffer::CreateWithContentsOfFile( - path.c_str(), false /* executable */)) { + settings.vm_snapshot_data_path.c_str(), false /* executable */)) { return source; } } @@ -38,11 +36,9 @@ std::unique_ptr ResolveVMData(const Settings& settings) { std::unique_ptr ResolveVMInstructions( const Settings& settings) { - if (settings.aot_snapshot_path.size() > 0) { - auto path = fml::paths::JoinPaths( - {settings.aot_snapshot_path, settings.aot_vm_snapshot_instr_filename}); + if (settings.vm_snapshot_instr_path.size() > 0) { if (auto source = DartSnapshotBuffer::CreateWithContentsOfFile( - path.c_str(), true /* executable */)) { + settings.vm_snapshot_instr_path.c_str(), true /* executable */)) { return source; } } @@ -63,12 +59,10 @@ std::unique_ptr ResolveVMInstructions( std::unique_ptr ResolveIsolateData( const Settings& settings) { - if (settings.aot_snapshot_path.size() > 0) { - auto path = - fml::paths::JoinPaths({settings.aot_snapshot_path, - settings.aot_isolate_snapshot_data_filename}); + if (settings.isolate_snapshot_data_path.size() > 0) { if (auto source = DartSnapshotBuffer::CreateWithContentsOfFile( - path.c_str(), false /* executable */)) { + settings.isolate_snapshot_data_path.c_str(), + false /* executable */)) { return source; } } @@ -80,12 +74,10 @@ std::unique_ptr ResolveIsolateData( std::unique_ptr ResolveIsolateInstructions( const Settings& settings) { - if (settings.aot_snapshot_path.size() > 0) { - auto path = - fml::paths::JoinPaths({settings.aot_snapshot_path, - settings.aot_isolate_snapshot_instr_filename}); + if (settings.isolate_snapshot_instr_path.size() > 0) { if (auto source = DartSnapshotBuffer::CreateWithContentsOfFile( - path.c_str(), true /* executable */)) { + settings.isolate_snapshot_instr_path.c_str(), + true /* executable */)) { return source; } } diff --git a/engine/src/flutter/runtime/dart_vm.cc b/engine/src/flutter/runtime/dart_vm.cc index f58563f9aa..7a3245bd4c 100644 --- a/engine/src/flutter/runtime/dart_vm.cc +++ b/engine/src/flutter/runtime/dart_vm.cc @@ -266,7 +266,7 @@ DartVM::DartVM(const Settings& settings, vm_snapshot_(std::move(vm_snapshot)), isolate_snapshot_(std::move(isolate_snapshot)), platform_kernel_mapping_( - std::make_unique(settings.kernel_snapshot_path)), + std::make_unique(settings.platform_kernel_path)), weak_factory_(this) { TRACE_EVENT0("flutter", "DartVMInitializer"); FXL_DLOG(INFO) << "Attempting Dart VM launch for mode: " @@ -342,7 +342,7 @@ DartVM::DartVM(const Settings& settings, const bool is_preview_dart2 = platform_kernel_ != nullptr || isolate_snapshot_is_dart_2; - FXL_DLOG(INFO) << "Dart 2 " << (is_preview_dart2 ? " is" : "is NOT") + FXL_DLOG(INFO) << "Dart 2 " << (is_preview_dart2 ? "is" : "is NOT") << " enabled. Platform kernel: " << static_cast(platform_kernel_) << " Isolate Snapshot is Dart 2: " diff --git a/engine/src/flutter/shell/common/switches.cc b/engine/src/flutter/shell/common/switches.cc index f8883a2a3f..bd12301c7b 100644 --- a/engine/src/flutter/shell/common/switches.cc +++ b/engine/src/flutter/shell/common/switches.cc @@ -9,6 +9,7 @@ #include #include +#include "flutter/fml/paths.h" #include "lib/fxl/strings/string_view.h" // Include once for the default enum definition. @@ -172,21 +173,37 @@ blink::Settings SettingsFromCommandLine(const fxl::CommandLine& command_line) { command_line.GetOptionValue(FlagForSwitch(Switch::Packages), &settings.packages_file_path); + std::string aot_snapshot_path; command_line.GetOptionValue(FlagForSwitch(Switch::AotSnapshotPath), - &settings.aot_snapshot_path); + &aot_snapshot_path); + std::string aot_vm_snapshot_data_filename; command_line.GetOptionValue(FlagForSwitch(Switch::AotVmSnapshotData), - &settings.aot_vm_snapshot_data_filename); + &aot_vm_snapshot_data_filename); + std::string aot_vm_snapshot_instr_filename; command_line.GetOptionValue(FlagForSwitch(Switch::AotVmSnapshotInstructions), - &settings.aot_vm_snapshot_instr_filename); + &aot_vm_snapshot_instr_filename); + std::string aot_isolate_snapshot_data_filename; command_line.GetOptionValue(FlagForSwitch(Switch::AotIsolateSnapshotData), - &settings.aot_isolate_snapshot_data_filename); + &aot_isolate_snapshot_data_filename); + std::string aot_isolate_snapshot_instr_filename; command_line.GetOptionValue( FlagForSwitch(Switch::AotIsolateSnapshotInstructions), - &settings.aot_isolate_snapshot_instr_filename); + &aot_isolate_snapshot_instr_filename); + + if (aot_snapshot_path.size() > 0) { + settings.vm_snapshot_data_path = fml::paths::JoinPaths( + {aot_snapshot_path, aot_vm_snapshot_data_filename}); + settings.vm_snapshot_instr_path = fml::paths::JoinPaths( + {aot_snapshot_path, aot_vm_snapshot_instr_filename}); + settings.isolate_snapshot_data_path = fml::paths::JoinPaths( + {aot_snapshot_path, aot_isolate_snapshot_data_filename}); + settings.isolate_snapshot_instr_path = fml::paths::JoinPaths( + {aot_snapshot_path, aot_isolate_snapshot_instr_filename}); + } command_line.GetOptionValue(FlagForSwitch(Switch::CacheDirPath), &settings.temp_directory_path); diff --git a/engine/src/flutter/shell/platform/android/flutter_main.cc b/engine/src/flutter/shell/platform/android/flutter_main.cc index 08d97add93..1849c83984 100644 --- a/engine/src/flutter/shell/platform/android/flutter_main.cc +++ b/engine/src/flutter/shell/platform/android/flutter_main.cc @@ -64,10 +64,11 @@ void FlutterMain::Init(JNIEnv* env, auto application_kernel_path = fml::paths::JoinPaths({settings.assets_path, "kernel_blob.bin"}); - if (files::IsFile(platform_kernel_path) && - files::IsFile(application_kernel_path)) { - settings.kernel_snapshot_path = platform_kernel_path; + if (files::IsFile(application_kernel_path)) { settings.application_kernel_asset = application_kernel_path; + if (files::IsFile(platform_kernel_path)) { + settings.platform_kernel_path = platform_kernel_path; + } } } diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterDartProject.mm b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterDartProject.mm index 402d7dfd33..b1410547e7 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterDartProject.mm +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterDartProject.mm @@ -97,7 +97,7 @@ static blink::Settings DefaultSettingsForProcess() { NSURL* vmKernelSnapshotURL = [NSURL URLWithString:@(kVMKernelSnapshotFileName) relativeToURL:[NSURL fileURLWithPath:assetsPath]]; if ([[NSFileManager defaultManager] fileExistsAtPath:vmKernelSnapshotURL.path]) { - settings.kernel_snapshot_path = vmKernelSnapshotURL.path.UTF8String; + settings.platform_kernel_path = vmKernelSnapshotURL.path.UTF8String; } } diff --git a/engine/src/flutter/shell/platform/embedder/embedder.cc b/engine/src/flutter/shell/platform/embedder/embedder.cc index 8a68aacfaf..5279f152b2 100644 --- a/engine/src/flutter/shell/platform/embedder/embedder.cc +++ b/engine/src/flutter/shell/platform/embedder/embedder.cc @@ -154,11 +154,12 @@ FlutterResult FlutterEngineRun(size_t version, fml::paths::JoinPaths({settings.assets_path, "platform.dill"}); std::string application_kernel_path = fml::paths::JoinPaths( {settings.assets_path, kApplicationKernelSnapshotFileName}); - if (files::IsFile(platform_kernel_path) && - files::IsFile(application_kernel_path)) { + if (files::IsFile(application_kernel_path)) { // Run from a kernel snapshot. - settings.application_kernel_asset = kApplicationKernelSnapshotFileName; - settings.kernel_snapshot_path = platform_kernel_path; + settings.platform_kernel_path = platform_kernel_path; + if (files::IsFile(platform_kernel_path)) { + settings.application_kernel_asset = kApplicationKernelSnapshotFileName; + } } else { // Run from a main Dart file. settings.main_dart_file_path = args->main_path; diff --git a/engine/src/flutter/shell/testing/tester_main.cc b/engine/src/flutter/shell/testing/tester_main.cc index 97af6dd847..5258d80a86 100644 --- a/engine/src/flutter/shell/testing/tester_main.cc +++ b/engine/src/flutter/shell/testing/tester_main.cc @@ -254,7 +254,7 @@ int main(int argc, char* argv[]) { settings.icu_data_path = "icudtl.dat"; - settings.kernel_snapshot_path = + settings.platform_kernel_path = fml::paths::JoinPaths({settings.assets_path, "platform.dill"}); // The tools that read logs get confused if there is a log tag specified.