fuchsia: remove use of replace_as_executable (second try) (flutter/engine#17313)

On Fuchsia, we can now get executable VMOs from trusted backing
filesystems.  This allows us to remove the use of replace_as_executable
in favor of opening files with `fdio_open_fd_at` with the
`OPEN_RIGHT_EXECUTABLE` flag and getting VMOs by calling
`fdio_get_vmo_exec`.

By moving the responsibility for executability into the filesystem, we
are able to remove `deprecated-ambient-replace-as-executable` from
component manifests for non-JIT runners (the JIT runners still call
replace_as_executable in Dart's allocator).  It wasn't abundantly clear
whether .cmx files for tests were used purely in AOT runtime
environments or also saw JIT usage, so I left those as-is.

For context: this is a second attempt at #16690, which was reverted
because it broke the Dart JIT runner.  The primary difference is that
this time around, we correctly handle absolute vs relative paths,
depending on whether library loading bottoms out in `fdio_open_fd` or
`fdio_open_fd_at`.  I've added additional assertions to help ensure any
new usages use the correct shape of path.

Testing: I verified locally that the flutter product runner works on
Astro, and also successfully ran the Dart JIT example test (which was
the thing blocking the google3 roll with the previous attempt at this
patchset).

Co-authored-by: Drew Fisher <zarvox@google.com>
This commit is contained in:
Drew Fisher
2020-03-25 13:17:28 -07:00
committed by GitHub
parent 95ef21485b
commit abfe606076
13 changed files with 82 additions and 62 deletions

View File

@@ -201,12 +201,12 @@ bool DartComponentController::SetupFromKernel() {
}
if (!dart_utils::MappedResource::LoadFromNamespace(
nullptr, "pkg/data/isolate_core_snapshot_data.bin",
nullptr, "/pkg/data/isolate_core_snapshot_data.bin",
isolate_snapshot_data_)) {
return false;
}
if (!dart_utils::MappedResource::LoadFromNamespace(
nullptr, "pkg/data/isolate_core_snapshot_instructions.bin",
nullptr, "/pkg/data/isolate_core_snapshot_instructions.bin",
isolate_snapshot_instructions_, true /* executable */)) {
return false;
}

View File

@@ -158,11 +158,11 @@ DartRunner::DartRunner() : context_(sys::ComponentContext::Create()) {
params.vm_snapshot_instructions = ::_kDartVmSnapshotInstructions;
#else
if (!dart_utils::MappedResource::LoadFromNamespace(
nullptr, "pkg/data/vm_snapshot_data.bin", vm_snapshot_data_)) {
nullptr, "/pkg/data/vm_snapshot_data.bin", vm_snapshot_data_)) {
FX_LOG(FATAL, LOG_TAG, "Failed to load vm snapshot data");
}
if (!dart_utils::MappedResource::LoadFromNamespace(
nullptr, "pkg/data/vm_snapshot_instructions.bin",
nullptr, "/pkg/data/vm_snapshot_instructions.bin",
vm_snapshot_instructions_, true /* executable */)) {
FX_LOG(FATAL, LOG_TAG, "Failed to load vm snapshot instructions");
}

View File

@@ -3,9 +3,6 @@
"data": "data/goodbye_dart_aot"
},
"sandbox": {
"features": [
"deprecated-ambient-replace-as-executable"
],
"services": [
"fuchsia.intl.PropertyProvider",
"fuchsia.sys.Environment"

View File

@@ -5,7 +5,6 @@
"sandbox": {
"features": [
"config-data",
"deprecated-ambient-replace-as-executable",
"root-ssl-certificates"
],
"services": [

View File

@@ -5,7 +5,6 @@
"sandbox": {
"features": [
"config-data",
"deprecated-ambient-replace-as-executable",
"root-ssl-certificates"
],
"services": [

View File

@@ -82,7 +82,7 @@ Dart_Isolate CreateServiceIsolate(const char* uri,
#if defined(AOT_RUNTIME)
// The VM service was compiled as a separate app.
const char* snapshot_path = "pkg/data/vmservice_snapshot.so";
const char* snapshot_path = "/pkg/data/vmservice_snapshot.so";
if (elf_snapshot.Load(nullptr, snapshot_path)) {
vmservice_data = elf_snapshot.IsolateData();
vmservice_instructions = elf_snapshot.IsolateInstrs();
@@ -92,14 +92,14 @@ Dart_Isolate CreateServiceIsolate(const char* uri,
} else {
// The VM service was compiled as a separate app.
const char* snapshot_data_path =
"pkg/data/vmservice_isolate_snapshot_data.bin";
"/pkg/data/vmservice_isolate_snapshot_data.bin";
const char* snapshot_instructions_path =
"pkg/data/vmservice_isolate_snapshot_instructions.bin";
"/pkg/data/vmservice_isolate_snapshot_instructions.bin";
#else
// The VM service is embedded in the core snapshot.
const char* snapshot_data_path = "pkg/data/isolate_core_snapshot_data.bin";
const char* snapshot_data_path = "/pkg/data/isolate_core_snapshot_data.bin";
const char* snapshot_instructions_path =
"pkg/data/isolate_core_snapshot_instructions.bin";
"/pkg/data/isolate_core_snapshot_instructions.bin";
#endif
if (!dart_utils::MappedResource::LoadFromNamespace(
@@ -196,7 +196,7 @@ Dart_Isolate CreateServiceIsolate(const char* uri,
Dart_Handle GetVMServiceAssetsArchiveCallback() {
dart_utils::MappedResource observatory_tar;
if (!dart_utils::MappedResource::LoadFromNamespace(
nullptr, "pkg/data/observatory.tar", observatory_tar)) {
nullptr, "/pkg/data/observatory.tar", observatory_tar)) {
FX_LOG(ERROR, LOG_TAG, "Failed to load Observatory assets");
return nullptr;
}

View File

@@ -431,7 +431,8 @@ class FileInNamespaceBuffer final : public fml::Mapping {
FileInNamespaceBuffer(int namespace_fd, const char* path, bool executable)
: address_(nullptr), size_(0) {
fuchsia::mem::Buffer buffer;
if (!dart_utils::VmoFromFilenameAt(namespace_fd, path, &buffer)) {
if (!dart_utils::VmoFromFilenameAt(namespace_fd, path, executable,
&buffer)) {
return;
}
if (buffer.size == 0) {
@@ -441,17 +442,6 @@ class FileInNamespaceBuffer final : public fml::Mapping {
uint32_t flags = ZX_VM_PERM_READ;
if (executable) {
flags |= ZX_VM_PERM_EXECUTE;
// VmoFromFilenameAt will return VMOs without ZX_RIGHT_EXECUTE,
// so we need replace_as_executable to be able to map them as
// ZX_VM_PERM_EXECUTE.
// TODO(mdempsky): Update comment once SEC-42 is fixed.
zx_status_t status =
buffer.vmo.replace_as_executable(zx::handle(), &buffer.vmo);
if (status != ZX_OK) {
FML_LOG(FATAL) << "Failed to make VMO executable: "
<< zx_status_get_string(status);
}
}
uintptr_t addr;
zx_status_t status =

View File

@@ -5,7 +5,6 @@
"sandbox": {
"features": [
"config-data",
"deprecated-ambient-replace-as-executable",
"root-ssl-certificates",
"vulkan"
],

View File

@@ -5,7 +5,6 @@
"sandbox": {
"features": [
"config-data",
"deprecated-ambient-replace-as-executable",
"root-ssl-certificates",
"vulkan"
],

View File

@@ -87,7 +87,7 @@ bool InitializeICU() {
const char* data_path = kIcuDataPath;
fuchsia::mem::Buffer icu_data;
if (!dart_utils::VmoFromFilename(data_path, &icu_data)) {
if (!dart_utils::VmoFromFilename(data_path, false, &icu_data)) {
return false;
}

View File

@@ -32,41 +32,31 @@ static bool OpenVmo(fuchsia::mem::Buffer* resource_vmo,
bool executable) {
TRACE_DURATION("dart", "LoadFromNamespace", "path", path);
// openat of a path with a leading '/' ignores the namespace fd.
dart_utils::Check(path[0] != '/', LOG_TAG);
if (namespc == nullptr) {
if (!VmoFromFilename(path, resource_vmo)) {
// Opening a file in the root namespace expects an absolute path.
dart_utils::Check(path[0] == '/', LOG_TAG);
if (!VmoFromFilename(path, executable, resource_vmo)) {
return false;
}
} else {
// openat of a path with a leading '/' ignores the namespace fd.
// require a relative path.
dart_utils::Check(path[0] != '/', LOG_TAG);
auto root_dir = fdio_ns_opendir(namespc);
if (root_dir < 0) {
FX_LOG(ERROR, LOG_TAG, "Failed to open namespace directory");
return false;
}
bool result = dart_utils::VmoFromFilenameAt(root_dir, path, resource_vmo);
bool result =
dart_utils::VmoFromFilenameAt(root_dir, path, executable, resource_vmo);
close(root_dir);
if (!result) {
return result;
}
}
if (executable) {
// VmoFromFilenameAt will return VMOs without ZX_RIGHT_EXECUTE,
// so we need replace_as_executable to be able to map them as
// ZX_VM_PERM_EXECUTE.
// TODO(mdempsky): Update comment once SEC-42 is fixed.
zx_status_t status = resource_vmo->vmo.replace_as_executable(
zx::handle(), &resource_vmo->vmo);
if (status != ZX_OK) {
FX_LOGF(ERROR, LOG_TAG, "Failed to make VMO executable: %s",
zx_status_get_string(status));
return false;
}
}
return true;
}
@@ -114,10 +104,22 @@ MappedResource::~MappedResource() {
static int OpenFdExec(const std::string& path, int dirfd) {
int fd = -1;
zx_status_t result = fdio_open_fd_at(
dirfd, path.c_str(),
fuchsia::io::OPEN_RIGHT_READABLE | fuchsia::io::OPEN_RIGHT_EXECUTABLE,
&fd);
zx_status_t result;
if (dirfd == AT_FDCWD) {
// fdio_open_fd_at does not support AT_FDCWD, by design. Use fdio_open_fd
// and expect an absolute path for that usage pattern.
dart_utils::Check(path[0] == '/', LOG_TAG);
result = fdio_open_fd(
path.c_str(),
fuchsia::io::OPEN_RIGHT_READABLE | fuchsia::io::OPEN_RIGHT_EXECUTABLE,
&fd);
} else {
dart_utils::Check(path[0] != '/', LOG_TAG);
result = fdio_open_fd_at(
dirfd, path.c_str(),
fuchsia::io::OPEN_RIGHT_READABLE | fuchsia::io::OPEN_RIGHT_EXECUTABLE,
&fd);
}
if (result != ZX_OK) {
FX_LOGF(ERROR, LOG_TAG, "fdio_open_fd_at(%s) failed: %s", path.c_str(),
zx_status_get_string(result));

View File

@@ -7,15 +7,18 @@
#include <fcntl.h>
#include <sys/stat.h>
#include <fuchsia/io/cpp/fidl.h>
#include <fuchsia/mem/cpp/fidl.h>
#include <lib/fdio/directory.h>
#include <lib/fdio/io.h>
#include <lib/syslog/global.h>
#include <zircon/status.h>
#include "runtime/dart/utils/logging.h"
namespace {
bool VmoFromFd(int fd, fuchsia::mem::Buffer* buffer) {
bool VmoFromFd(int fd, bool executable, fuchsia::mem::Buffer* buffer) {
if (!buffer) {
FX_LOG(FATAL, LOG_TAG, "Invalid buffer pointer");
}
@@ -27,7 +30,14 @@ bool VmoFromFd(int fd, fuchsia::mem::Buffer* buffer) {
}
zx_handle_t result = ZX_HANDLE_INVALID;
if (fdio_get_vmo_copy(fd, &result) != ZX_OK) {
zx_status_t status;
if (executable) {
status = fdio_get_vmo_exec(fd, &result);
} else {
status = fdio_get_vmo_copy(fd, &result);
}
if (status != ZX_OK) {
return false;
}
@@ -42,20 +52,42 @@ bool VmoFromFd(int fd, fuchsia::mem::Buffer* buffer) {
namespace dart_utils {
bool VmoFromFilename(const std::string& filename,
bool executable,
fuchsia::mem::Buffer* buffer) {
return VmoFromFilenameAt(AT_FDCWD, filename, buffer);
// Note: the implementation here cannot be shared with VmoFromFilenameAt
// because fdio_open_fd_at does not aim to provide POSIX compatibility, and
// thus does not handle AT_FDCWD as dirfd.
uint32_t flags = fuchsia::io::OPEN_RIGHT_READABLE |
(executable ? fuchsia::io::OPEN_RIGHT_EXECUTABLE : 0);
zx_status_t status;
int fd;
status = fdio_open_fd(filename.c_str(), flags, &fd);
if (status != ZX_OK) {
FX_LOGF(ERROR, LOG_TAG, "fdio_open_fd(\"%s\", %08x) failed: %s",
filename.c_str(), flags, zx_status_get_string(status));
return false;
}
bool result = VmoFromFd(fd, executable, buffer);
close(fd);
return result;
}
bool VmoFromFilenameAt(int dirfd,
const std::string& filename,
bool executable,
fuchsia::mem::Buffer* buffer) {
int fd = openat(dirfd, filename.c_str(), O_RDONLY);
if (fd == -1) {
FX_LOGF(ERROR, LOG_TAG, "openat(\"%s\") failed: %s", filename.c_str(),
strerror(errno));
uint32_t flags = fuchsia::io::OPEN_RIGHT_READABLE |
(executable ? fuchsia::io::OPEN_RIGHT_EXECUTABLE : 0);
zx_status_t status;
int fd;
status = fdio_open_fd_at(dirfd, filename.c_str(), flags, &fd);
if (status != ZX_OK) {
FX_LOGF(ERROR, LOG_TAG, "fdio_open_fd_at(%d, \"%s\", %08x) failed: %s",
dirfd, filename.c_str(), flags, zx_status_get_string(status));
return false;
}
bool result = VmoFromFd(fd, buffer);
bool result = VmoFromFd(fd, executable, buffer);
close(fd);
return result;
}

View File

@@ -11,10 +11,13 @@
namespace dart_utils {
bool VmoFromFilename(const std::string& filename, fuchsia::mem::Buffer* buffer);
bool VmoFromFilename(const std::string& filename,
bool executable,
fuchsia::mem::Buffer* buffer);
bool VmoFromFilenameAt(int dirfd,
const std::string& filename,
bool executable,
fuchsia::mem::Buffer* buffer);
zx_status_t IsSizeValid(const fuchsia::mem::Buffer& buffer, bool* is_valid);