Flutter Engine
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 <lib/syslog/global.h>
15 #include <zircon/status.h>
16 
17 #include "logging.h"
18 
19 namespace {
20 
21 bool VmoFromFd(int fd, bool executable, fuchsia::mem::Buffer* buffer) {
22  if (!buffer) {
23  FX_LOG(FATAL, LOG_TAG, "Invalid buffer pointer");
24  }
25 
26  struct stat stat_struct;
27  if (fstat(fd, &stat_struct) == -1) {
28  FX_LOGF(ERROR, LOG_TAG, "fstat failed: %s", strerror(errno));
29  return false;
30  }
31 
32  zx_handle_t result = ZX_HANDLE_INVALID;
33  zx_status_t status;
34  if (executable) {
35  status = fdio_get_vmo_exec(fd, &result);
36  } else {
37  status = fdio_get_vmo_copy(fd, &result);
38  }
39 
40  if (status != ZX_OK) {
41  return false;
42  }
43 
44  buffer->vmo = zx::vmo(result);
45  buffer->size = stat_struct.st_size;
46 
47  return true;
48 }
49 
50 } // namespace
51 
52 namespace dart_utils {
53 
54 bool VmoFromFilename(const std::string& filename,
55  bool executable,
56  fuchsia::mem::Buffer* buffer) {
57  // Note: the implementation here cannot be shared with VmoFromFilenameAt
58  // because fdio_open_fd_at does not aim to provide POSIX compatibility, and
59  // thus does not handle AT_FDCWD as dirfd.
61  (executable ? fuchsia::io::OPEN_RIGHT_EXECUTABLE : 0);
62  zx_status_t status;
63  int fd;
64 
65  status = fdio_open_fd(filename.c_str(), flags, &fd);
66  if (status != ZX_OK) {
67  FX_LOGF(ERROR, LOG_TAG, "fdio_open_fd(\"%s\", %08x) failed: %s",
68  filename.c_str(), flags, zx_status_get_string(status));
69  return false;
70  }
71  bool result = VmoFromFd(fd, executable, buffer);
72  close(fd);
73  return result;
74 }
75 
76 bool VmoFromFilenameAt(int dirfd,
77  const std::string& filename,
78  bool executable,
79  fuchsia::mem::Buffer* buffer) {
81  (executable ? fuchsia::io::OPEN_RIGHT_EXECUTABLE : 0);
82  zx_status_t status;
83  int fd;
84  status = fdio_open_fd_at(dirfd, filename.c_str(), flags, &fd);
85  if (status != ZX_OK) {
86  FX_LOGF(ERROR, LOG_TAG, "fdio_open_fd_at(%d, \"%s\", %08x) failed: %s",
87  dirfd, filename.c_str(), flags, zx_status_get_string(status));
88  return false;
89  }
90  bool result = VmoFromFd(fd, executable, buffer);
91  close(fd);
92  return result;
93 }
94 
95 zx_status_t IsSizeValid(const fuchsia::mem::Buffer& buffer, bool* is_valid) {
96  size_t vmo_size;
97  zx_status_t status = buffer.vmo.get_size(&vmo_size);
98  if (status == ZX_OK) {
99  *is_valid = vmo_size >= buffer.size;
100  } else {
101  *is_valid = false;
102  }
103  return status;
104 }
105 
106 } // namespace dart_utils
constexpr uint32_t OPEN_RIGHT_EXECUTABLE
Definition: component.cc:50
bool VmoFromFilenameAt(int dirfd, const std::string &filename, bool executable, fuchsia::mem::Buffer *buffer)
Definition: vmo.cc:76
#define LOG_TAG
Definition: logging.h:11
zx_status_t IsSizeValid(const fuchsia::mem::Buffer &buffer, bool *is_valid)
Definition: vmo.cc:95
bool VmoFromFilename(const std::string &filename, bool executable, fuchsia::mem::Buffer *buffer)
Definition: vmo.cc:54
constexpr uint32_t OPEN_RIGHT_READABLE
Definition: component.cc:47
DEF_SWITCHES_START snapshot asset Path to the directory containing the four files specified by VmSnapshotInstructions and IsolateSnapshotInstructions vm snapshot The VM instructions snapshot that will be memory mapped as read and executable SnapshotAssetPath must be present isolate snapshot The isolate instructions snapshot that will be memory mapped as read and executable SnapshotAssetPath must be present icu symbol Prefix for the symbols representing ICU data linked into the Flutter library dart flags
Definition: switches.h:66