Flutter Engine
The Flutter Engine
file_in_namespace_buffer.cc
Go to the documentation of this file.
1// Copyright 2013 The Flutter Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
6
7#include <fuchsia/io/cpp/fidl.h>
8#include <lib/fdio/directory.h>
9#include <zircon/status.h>
10
11#include "flutter/fml/trace_event.h"
17
18namespace flutter_runner {
19
21 const char* path,
22 bool executable)
23 : address_(nullptr), size_(0) {
24 fuchsia::mem::Buffer buffer;
25 if (!dart_utils::VmoFromFilenameAt(namespace_fd, path, executable, &buffer) ||
26 buffer.size == 0) {
27 return;
28 }
29
30 uint32_t flags = ZX_VM_PERM_READ;
31 if (executable) {
32 flags |= ZX_VM_PERM_EXECUTE;
33 }
34
35 uintptr_t addr;
36 const zx_status_t status =
37 zx::vmar::root_self()->map(flags, 0, buffer.vmo, 0, buffer.size, &addr);
38 if (status != ZX_OK) {
39 FML_LOG(FATAL) << "Failed to map " << path << ": "
40 << zx_status_get_string(status);
41 }
42
43 address_ = reinterpret_cast<void*>(addr);
44 size_ = buffer.size;
45}
46
48 if (address_ != nullptr) {
49 zx::vmar::root_self()->unmap(reinterpret_cast<uintptr_t>(address_), size_);
50 address_ = nullptr;
51 size_ = 0;
52 }
53}
54
55const uint8_t* FileInNamespaceBuffer::GetMapping() const {
56 return reinterpret_cast<const uint8_t*>(address_);
57}
58
60 return size_;
61}
62
64 return true;
65}
66
67std::unique_ptr<fml::Mapping> LoadFile(int namespace_fd,
68 const char* path,
69 bool executable) {
70 FML_TRACE_EVENT("flutter", "LoadFile", "path", path);
71 return std::make_unique<FileInNamespaceBuffer>(namespace_fd, path,
72 executable);
73}
74
75std::unique_ptr<fml::FileMapping> MakeFileMapping(const char* path,
76 bool executable) {
77 auto flags = fuchsia::io::OpenFlags::RIGHT_READABLE;
78 if (executable) {
79 flags |= fuchsia::io::OpenFlags::RIGHT_EXECUTABLE;
80 }
81
82 // The returned file descriptor is compatible with standard posix operations
83 // such as close, mmap, etc. We only need to treat open/open_at specially.
84 int fd;
85 const zx_status_t status =
86 fdio_open_fd(path, static_cast<uint32_t>(flags), &fd);
87 if (status != ZX_OK) {
88 return nullptr;
89 }
90
91 using Protection = fml::FileMapping::Protection;
92
93 std::initializer_list<Protection> protection_execute = {Protection::kRead,
94 Protection::kExecute};
95 std::initializer_list<Protection> protection_read = {Protection::kRead};
96 auto mapping = std::make_unique<fml::FileMapping>(
97 fml::UniqueFD{fd}, executable ? protection_execute : protection_read);
98
99 if (!mapping->IsValid()) {
100 return nullptr;
101 }
102 return mapping;
103}
104
105} // namespace flutter_runner
FileInNamespaceBuffer(int namespace_fd, const char *path, bool executable)
const uint8_t * GetMapping() const override
#define FATAL(error)
FlutterSemanticsFlag flags
#define FML_LOG(severity)
Definition: logging.h:82
bool VmoFromFilenameAt(int dirfd, const std::string &filename, bool executable, fuchsia::mem::Buffer *buffer)
Definition: vmo.cc:82
std::unique_ptr< fml::FileMapping > MakeFileMapping(const char *path, bool executable)
std::unique_ptr< fml::Mapping > LoadFile(int namespace_fd, const char *path, bool executable)
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot The VM snapshot data that will be memory mapped as read only SnapshotAssetPath must be present isolate snapshot The isolate snapshot data that will be memory mapped as read only SnapshotAssetPath must be present cache dir path
Definition: switches.h:57
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot The VM snapshot data that will be memory mapped as read only SnapshotAssetPath must be present isolate snapshot The isolate snapshot data that will be memory mapped as read only SnapshotAssetPath must be present cache dir Path to the cache directory This is different from the persistent_cache_path in embedder which is used for Skia shader cache icu native lib Path to the library file that exports the ICU data vm service The hostname IP address on which the Dart VM Service should be served If not defaults to or::depending on whether ipv6 is specified vm service A custom Dart VM Service port The default is to pick a randomly available open port disable vm Disable the Dart VM Service The Dart VM Service is never available in release mode disable vm service Disable mDNS Dart VM Service publication Bind to the IPv6 localhost address for the Dart VM Service Ignored if vm service host is set endless trace buffer
Definition: switches.h:126
#define FML_TRACE_EVENT(category_group, name,...)
Definition: trace_event.h:121