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 "flutter/fml/mapping.h"
6
7#include <algorithm>
8#include <cstring>
9#include <memory>
10#include <sstream>
11
12namespace fml {
13
14// FileMapping
15
16uint8_t* FileMapping::GetMutableMapping() {
17 return mutable_mapping_;
18}
19
20std::unique_ptr<FileMapping> FileMapping::CreateReadOnly(
21 const std::string& path) {
22 return CreateReadOnly(base_fd: OpenFile(path: path.c_str(), create_if_necessary: false, permission: FilePermission::kRead),
23 sub_path: "");
24}
25
26std::unique_ptr<FileMapping> FileMapping::CreateReadOnly(
27 const fml::UniqueFD& base_fd,
28 const std::string& sub_path) {
29 if (!sub_path.empty()) {
30 return CreateReadOnly(
31 base_fd: OpenFile(base_directory: base_fd, path: sub_path.c_str(), create_if_necessary: false, permission: FilePermission::kRead), sub_path: "");
32 }
33
34 auto mapping = std::make_unique<FileMapping>(
35 args: base_fd, args: std::initializer_list<Protection>{Protection::kRead});
36
37 if (!mapping->IsValid()) {
38 return nullptr;
39 }
40
41 return mapping;
42}
43
44std::unique_ptr<FileMapping> FileMapping::CreateReadExecute(
45 const std::string& path) {
46 return CreateReadExecute(
47 base_fd: OpenFile(path: path.c_str(), create_if_necessary: false, permission: FilePermission::kRead));
48}
49
50std::unique_ptr<FileMapping> FileMapping::CreateReadExecute(
51 const fml::UniqueFD& base_fd,
52 const std::string& sub_path) {
53 if (!sub_path.empty()) {
54 return CreateReadExecute(
55 base_fd: OpenFile(base_directory: base_fd, path: sub_path.c_str(), create_if_necessary: false, permission: FilePermission::kRead), sub_path: "");
56 }
57
58 auto mapping = std::make_unique<FileMapping>(
59 args: base_fd, args: std::initializer_list<Protection>{Protection::kRead,
60 Protection::kExecute});
61
62 if (!mapping->IsValid()) {
63 return nullptr;
64 }
65
66 return mapping;
67}
68
69// Data Mapping
70
71DataMapping::DataMapping(std::vector<uint8_t> data) : data_(std::move(data)) {}
72
73DataMapping::DataMapping(const std::string& string)
74 : data_(string.begin(), string.end()) {}
75
76DataMapping::~DataMapping() = default;
77
78size_t DataMapping::GetSize() const {
79 return data_.size();
80}
81
82const uint8_t* DataMapping::GetMapping() const {
83 return data_.data();
84}
85
86bool DataMapping::IsDontNeedSafe() const {
87 return false;
88}
89
90// NonOwnedMapping
91NonOwnedMapping::NonOwnedMapping(const uint8_t* data,
92 size_t size,
93 const ReleaseProc& release_proc,
94 bool dontneed_safe)
95 : data_(data),
96 size_(size),
97 release_proc_(release_proc),
98 dontneed_safe_(dontneed_safe) {}
99
100NonOwnedMapping::~NonOwnedMapping() {
101 if (release_proc_) {
102 release_proc_(data_, size_);
103 }
104}
105
106size_t NonOwnedMapping::GetSize() const {
107 return size_;
108}
109
110const uint8_t* NonOwnedMapping::GetMapping() const {
111 return data_;
112}
113
114bool NonOwnedMapping::IsDontNeedSafe() const {
115 return dontneed_safe_;
116}
117
118// MallocMapping
119MallocMapping::MallocMapping() : data_(nullptr), size_(0) {}
120
121MallocMapping::MallocMapping(uint8_t* data, size_t size)
122 : data_(data), size_(size) {}
123
124MallocMapping::MallocMapping(fml::MallocMapping&& mapping)
125 : data_(mapping.data_), size_(mapping.size_) {
126 mapping.data_ = nullptr;
127 mapping.size_ = 0;
128}
129
130MallocMapping::~MallocMapping() {
131 free(ptr: data_);
132 data_ = nullptr;
133}
134
135MallocMapping MallocMapping::Copy(const void* begin, size_t length) {
136 auto result =
137 MallocMapping(reinterpret_cast<uint8_t*>(malloc(size: length)), length);
138 FML_CHECK(result.GetMapping() != nullptr);
139 memcpy(dest: const_cast<uint8_t*>(result.GetMapping()), src: begin, n: length);
140 return result;
141}
142
143size_t MallocMapping::GetSize() const {
144 return size_;
145}
146
147const uint8_t* MallocMapping::GetMapping() const {
148 return data_;
149}
150
151bool MallocMapping::IsDontNeedSafe() const {
152 return false;
153}
154
155uint8_t* MallocMapping::Release() {
156 uint8_t* result = data_;
157 data_ = nullptr;
158 size_ = 0;
159 return result;
160}
161
162// Symbol Mapping
163
164SymbolMapping::SymbolMapping(fml::RefPtr<fml::NativeLibrary> native_library,
165 const char* symbol_name)
166 : native_library_(std::move(native_library)) {
167 if (native_library_ && symbol_name != nullptr) {
168 mapping_ = native_library_->ResolveSymbol(symbol: symbol_name);
169
170 if (mapping_ == nullptr) {
171 // Apparently, dart_bootstrap seems to account for the Mac behavior of
172 // requiring the underscore prefixed symbol name on non-Mac platforms as
173 // well. As a fallback, check the underscore prefixed variant of the
174 // symbol name and allow callers to not have handle this on a per platform
175 // toolchain quirk basis.
176
177 std::stringstream underscore_symbol_name;
178 underscore_symbol_name << "_" << symbol_name;
179 mapping_ =
180 native_library_->ResolveSymbol(symbol: underscore_symbol_name.str().c_str());
181 }
182 }
183}
184
185SymbolMapping::~SymbolMapping() = default;
186
187size_t SymbolMapping::GetSize() const {
188 return 0;
189}
190
191const uint8_t* SymbolMapping::GetMapping() const {
192 return mapping_;
193}
194
195bool SymbolMapping::IsDontNeedSafe() const {
196 return true;
197}
198
199} // namespace fml
200

source code of flutter_engine/flutter/fml/mapping.cc