Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
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
static const uint8_t buffer[]
#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)
#define FML_TRACE_EVENT(category_group, name,...)