Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
vmo.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
5#include "vmo.h"
6
7#include <fcntl.h>
8#include <sys/stat.h>
9
10#include <fuchsia/io/cpp/fidl.h>
11#include <fuchsia/mem/cpp/fidl.h>
12#include <lib/fdio/directory.h>
13#include <lib/fdio/io.h>
14#include <zircon/status.h>
15
16#include <ios>
17
18#include "flutter/fml/logging.h"
19
20namespace {
21
22bool VmoFromFd(int fd, bool executable, fuchsia::mem::Buffer* buffer) {
23 if (!buffer) {
24 FML_LOG(FATAL) << "Invalid buffer pointer";
25 }
26
27 struct stat stat_struct;
28 if (fstat(fd, &stat_struct) == -1) {
29 FML_LOG(ERROR) << "fstat failed: " << strerror(errno);
30 return false;
31 }
32
33 zx_handle_t result = ZX_HANDLE_INVALID;
34 zx_status_t status;
35 if (executable) {
36 status = fdio_get_vmo_exec(fd, &result);
37 } else {
38 status = fdio_get_vmo_copy(fd, &result);
39 }
40
41 if (status != ZX_OK) {
42 FML_LOG(ERROR) << (executable ? "fdio_get_vmo_exec" : "fdio_get_vmo_copy")
43 << " failed: " << zx_status_get_string(status);
44 return false;
45 }
46
47 buffer->vmo = zx::vmo(result);
48 buffer->size = stat_struct.st_size;
49
50 return true;
51}
52
53} // namespace
54
55namespace dart_utils {
56
57bool VmoFromFilename(const std::string& filename,
58 bool executable,
59 fuchsia::mem::Buffer* buffer) {
60 // Note: the implementation here cannot be shared with VmoFromFilenameAt
61 // because fdio_open_fd_at does not aim to provide POSIX compatibility, and
62 // thus does not handle AT_FDCWD as dirfd.
63 auto flags = fuchsia::io::OpenFlags::RIGHT_READABLE;
64 if (executable) {
65 flags |= fuchsia::io::OpenFlags::RIGHT_EXECUTABLE;
66 }
67
68 int fd;
69 const zx_status_t status =
70 fdio_open_fd(filename.c_str(), static_cast<uint32_t>(flags), &fd);
71 if (status != ZX_OK) {
72 FML_LOG(ERROR) << "fdio_open_fd(\"" << filename << "\", " << std::hex
73 << static_cast<uint32_t>(flags)
74 << ") failed: " << zx_status_get_string(status);
75 return false;
76 }
77 bool result = VmoFromFd(fd, executable, buffer);
78 close(fd);
79 return result;
80}
81
82bool VmoFromFilenameAt(int dirfd,
83 const std::string& filename,
84 bool executable,
85 fuchsia::mem::Buffer* buffer) {
86 auto flags = fuchsia::io::OpenFlags::RIGHT_READABLE;
87 if (executable) {
88 flags |= fuchsia::io::OpenFlags::RIGHT_EXECUTABLE;
89 }
90
91 int fd;
92 const zx_status_t status = fdio_open_fd_at(dirfd, filename.c_str(),
93 static_cast<uint32_t>(flags), &fd);
94 if (status != ZX_OK) {
95 FML_LOG(ERROR) << "fdio_open_fd_at(" << dirfd << ", \"" << filename
96 << "\", " << std::hex << static_cast<uint32_t>(flags)
97 << ") failed: " << zx_status_get_string(status);
98 return false;
99 }
100 bool result = VmoFromFd(fd, executable, buffer);
101 close(fd);
102 return result;
103}
104
105zx_status_t IsSizeValid(const fuchsia::mem::Buffer& buffer, bool* is_valid) {
106 size_t vmo_size;
107 zx_status_t status = buffer.vmo.get_size(&vmo_size);
108 if (status == ZX_OK) {
109 *is_valid = vmo_size >= buffer.size;
110 } else {
111 *is_valid = false;
112 }
113 return status;
114}
115
116} // namespace dart_utils
static bool is_valid(SkISize dim)
#define FATAL(error)
FlutterSemanticsFlag flags
static const uint8_t buffer[]
GAsyncResult * result
#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
bool VmoFromFilename(const std::string &filename, bool executable, fuchsia::mem::Buffer *buffer)
Definition vmo.cc:57
zx_status_t IsSizeValid(const fuchsia::mem::Buffer &buffer, bool *is_valid)
Definition vmo.cc:105
#define ERROR(message)