Flutter Engine
mapped_resource.h
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 #ifndef APPS_DART_RUNNER_MAPPED_RESOURCE_H_
6 #define APPS_DART_RUNNER_MAPPED_RESOURCE_H_
7 
8 #include <fuchsia/mem/cpp/fidl.h>
9 #include <lib/fdio/namespace.h>
10 
11 #include "third_party/dart/runtime/bin/elf_loader.h"
12 
13 namespace dart_utils {
14 
15 class ElfSnapshot {
16  public:
18  ~ElfSnapshot();
19  ElfSnapshot(ElfSnapshot&& other) : handle_(other.handle_) {
20  other.handle_ = nullptr;
21  }
23  std::swap(handle_, other.handle_);
24  return *this;
25  }
26 
27  bool Load(fdio_ns_t* namespc, const std::string& path);
28  bool Load(int dirfd, const std::string& path);
29 
30  const uint8_t* VmData() const { return vm_data_; }
31  const uint8_t* VmInstrs() const { return vm_instrs_; }
32  const uint8_t* IsolateData() const { return isolate_data_; }
33  const uint8_t* IsolateInstrs() const { return isolate_instrs_; }
34 
35  private:
36  bool Load(int fd);
37 
38  Dart_LoadedElf* handle_ = nullptr;
39 
40  const uint8_t* vm_data_ = nullptr;
41  const uint8_t* vm_instrs_ = nullptr;
42  const uint8_t* isolate_data_ = nullptr;
43  const uint8_t* isolate_instrs_ = nullptr;
44 
45  // Disallow copy and assignment.
46  ElfSnapshot(const ElfSnapshot&) = delete;
47  ElfSnapshot& operator=(const ElfSnapshot&) = delete;
48 };
49 
51  public:
52  MappedResource() : address_(nullptr), size_(0) {}
54  : address_(other.address_), size_(other.size_) {
55  other.address_ = nullptr;
56  other.size_ = 0;
57  }
59  address_ = other.address_;
60  size_ = other.size_;
61  other.address_ = nullptr;
62  other.size_ = 0;
63  return *this;
64  }
65  ~MappedResource();
66 
67  // Loads the content of a file from the given namespace and maps it into the
68  // current process's address space. If namespace is null, the fdio "global"
69  // namespace is used (in which case, ./pkg means the dart_runner's package).
70  // The content is unmapped when the MappedResource goes out of scope. Returns
71  // true on success.
72  static bool LoadFromNamespace(fdio_ns_t* namespc,
73  const std::string& path,
74  MappedResource& resource,
75  bool executable = false);
76 
77  // Same as LoadFromNamespace, but takes a file descriptor to an opened
78  // directory instead of a namespace.
79  static bool LoadFromDir(int dirfd,
80  const std::string& path,
81  MappedResource& resource,
82  bool executable = false);
83 
84  // Maps a VMO into the current process's address space. The content is
85  // unmapped when the MappedResource goes out of scope. Returns true on
86  // success. The path is used only for error messages.
87  static bool LoadFromVmo(const std::string& path,
88  fuchsia::mem::Buffer resource_vmo,
89  MappedResource& resource,
90  bool executable = false);
91 
92  const uint8_t* address() const {
93  return reinterpret_cast<const uint8_t*>(address_);
94  }
95  size_t size() const { return size_; }
96 
97  private:
98  void* address_;
99  size_t size_;
100 
101  // Disallow copy and assignment.
102  MappedResource(const MappedResource&) = delete;
103  MappedResource& operator=(const MappedResource&) = delete;
104 };
105 
106 } // namespace dart_utils
107 
108 #endif // APPS_DART_RUNNER_MAPPED_RESOURCE_H_
DEF_SWITCHES_START snapshot asset path
Definition: switches.h:32
const uint8_t * IsolateData() const
MappedResource & operator=(MappedResource &&other)
ElfSnapshot(ElfSnapshot &&other)
void swap(scoped_nsprotocol< C > &p1, scoped_nsprotocol< C > &p2)
MappedResource(MappedResource &&other)
bool Load(fdio_ns_t *namespc, const std::string &path)
const uint8_t * address() const
ElfSnapshot & operator=(ElfSnapshot &&other)
const uint8_t * VmInstrs() const
const uint8_t * VmData() const
const uint8_t * IsolateInstrs() const