From 2d15e8bf6fa169d4760552f2e0bb4e70bc32de8e Mon Sep 17 00:00:00 2001 From: Kaushik Iska Date: Mon, 30 Sep 2019 14:04:40 -0700 Subject: [PATCH] [flutter_runner] Verified Execution for read exec on Fuchsia (flutter/engine#12703) Note: `fdio_open_fd` does not default to `AT_FDCWD` so we need to specify absolute paths. --- .../platform/fuchsia/flutter/component.cc | 58 ++++++++++++++++--- 1 file changed, 51 insertions(+), 7 deletions(-) diff --git a/engine/src/flutter/shell/platform/fuchsia/flutter/component.cc b/engine/src/flutter/shell/platform/fuchsia/flutter/component.cc index 27f11cf065..a4e53e253d 100644 --- a/engine/src/flutter/shell/platform/fuchsia/flutter/component.cc +++ b/engine/src/flutter/shell/platform/fuchsia/flutter/component.cc @@ -17,12 +17,17 @@ #include #include #include +#include +#include #include #include +#include "flutter/fml/mapping.h" #include "flutter/fml/synchronization/waitable_event.h" +#include "flutter/fml/unique_fd.h" #include "flutter/runtime/dart_vm_lifecycle.h" #include "flutter/shell/common/switches.h" +#include "lib/fdio/io.h" #include "runtime/dart/utils/files.h" #include "runtime/dart/utils/handle_exception.h" #include "runtime/dart/utils/tempfs.h" @@ -32,6 +37,13 @@ #include "task_runner_adapter.h" #include "thread.h" +// TODO(kaushikiska): Use these constants from ::llcpp::fuchsia::io +// Can read from target object. +constexpr uint32_t OPEN_RIGHT_READABLE = 1u; + +// Connection can map target object executable. +constexpr uint32_t OPEN_RIGHT_EXECUTABLE = 8u; + namespace flutter_runner { constexpr char kDataKey[] = "data"; @@ -69,6 +81,30 @@ static std::string DebugLabelForURL(const std::string& url) { } } +static std::unique_ptr MakeReadExecMapping(const char* path) { + int fd = 0; + // The returned file descriptor is compatible with standard posix operations + // such as close, mmap, etc. We only need to treat open/open_at specially. + zx_status_t status = + fdio_open_fd(path, OPEN_RIGHT_READABLE | OPEN_RIGHT_EXECUTABLE, &fd); + + if (status != ZX_OK) { + return nullptr; + } + + using Protection = fml::FileMapping::Protection; + + auto mapping = std::make_unique( + fml::UniqueFD{fd}, std::initializer_list{ + Protection::kRead, Protection::kExecute}); + + if (!mapping->IsValid()) { + return nullptr; + } + + return mapping; +} + Application::Application( TerminationCallback termination_callback, fuchsia::sys::Package package, @@ -218,11 +254,16 @@ Application::Application( // Compare flutter_jit_runner in BUILD.gn. settings_.vm_snapshot_data_path = "pkg/data/vm_snapshot_data.bin"; - settings_.vm_snapshot_instr_path = "pkg/data/vm_snapshot_instructions.bin"; + settings_.vm_snapshot_instr = []() { + return MakeReadExecMapping("/pkg/data/vm_snapshot_instructions.bin"); + }; + settings_.isolate_snapshot_data_path = "pkg/data/isolate_core_snapshot_data.bin"; - settings_.isolate_snapshot_instr_path = - "pkg/data/isolate_core_snapshot_instructions.bin"; + settings_.isolate_snapshot_instr = []() { + return MakeReadExecMapping( + "/pkg/data/isolate_core_snapshot_instructions.bin"); + }; { // Check if we can use the snapshot with the framework already loaded. @@ -236,12 +277,15 @@ Application::Application( (runner_framework.compare(app_framework) == 0)) { settings_.vm_snapshot_data_path = "pkg/data/framework_vm_snapshot_data.bin"; - settings_.vm_snapshot_instr_path = - "pkg/data/framework_vm_snapshot_instructions.bin"; + settings_.vm_snapshot_instr = []() { + return MakeReadExecMapping("/pkg/data/vm_snapshot_instructions.bin"); + }; settings_.isolate_snapshot_data_path = "pkg/data/framework_isolate_core_snapshot_data.bin"; - settings_.isolate_snapshot_instr_path = - "pkg/data/framework_isolate_core_snapshot_instructions.bin"; + settings_.isolate_snapshot_instr = []() { + return MakeReadExecMapping( + "/pkg/data/isolate_core_snapshot_instructions.bin"); + }; FML_LOG(INFO) << "Using snapshot with framework for " << package.resolved_url;