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 <sstream>
8
9 namespace fml {
10
11 // FileMapping
12
GetMutableMapping()13 uint8_t* FileMapping::GetMutableMapping() {
14 return mutable_mapping_;
15 }
16
CreateReadOnly(const std::string & path)17 std::unique_ptr<FileMapping> FileMapping::CreateReadOnly(
18 const std::string& path) {
19 return CreateReadOnly(OpenFile(path.c_str(), false, FilePermission::kRead),
20 "");
21 }
22
CreateReadOnly(const fml::UniqueFD & base_fd,const std::string & sub_path)23 std::unique_ptr<FileMapping> FileMapping::CreateReadOnly(
24 const fml::UniqueFD& base_fd,
25 const std::string& sub_path) {
26 if (sub_path.size() != 0) {
27 return CreateReadOnly(
28 OpenFile(base_fd, sub_path.c_str(), false, FilePermission::kRead), "");
29 }
30
31 auto mapping = std::make_unique<FileMapping>(
32 base_fd, std::initializer_list<Protection>{Protection::kRead});
33
34 if (!mapping->IsValid()) {
35 return nullptr;
36 }
37
38 return mapping;
39 }
40
CreateReadExecute(const std::string & path)41 std::unique_ptr<FileMapping> FileMapping::CreateReadExecute(
42 const std::string& path) {
43 return CreateReadExecute(
44 OpenFile(path.c_str(), false, FilePermission::kRead));
45 }
46
CreateReadExecute(const fml::UniqueFD & base_fd,const std::string & sub_path)47 std::unique_ptr<FileMapping> FileMapping::CreateReadExecute(
48 const fml::UniqueFD& base_fd,
49 const std::string& sub_path) {
50 if (sub_path.size() != 0) {
51 return CreateReadExecute(
52 OpenFile(base_fd, sub_path.c_str(), false, FilePermission::kRead), "");
53 }
54
55 auto mapping = std::make_unique<FileMapping>(
56 base_fd, std::initializer_list<Protection>{Protection::kRead,
57 Protection::kExecute});
58
59 if (!mapping->IsValid()) {
60 return nullptr;
61 }
62
63 return mapping;
64 }
65
66 // Data Mapping
67
DataMapping(std::vector<uint8_t> data)68 DataMapping::DataMapping(std::vector<uint8_t> data) : data_(std::move(data)) {}
69
70 DataMapping::~DataMapping() = default;
71
GetSize() const72 size_t DataMapping::GetSize() const {
73 return data_.size();
74 }
75
GetMapping() const76 const uint8_t* DataMapping::GetMapping() const {
77 return data_.data();
78 }
79
80 // NonOwnedMapping
81
NonOwnedMapping(const uint8_t * data,size_t size,ReleaseProc release_proc)82 NonOwnedMapping::NonOwnedMapping(const uint8_t* data,
83 size_t size,
84 ReleaseProc release_proc)
85 : data_(data), size_(size), release_proc_(release_proc) {}
86
~NonOwnedMapping()87 NonOwnedMapping::~NonOwnedMapping() {
88 if (release_proc_) {
89 release_proc_(data_, size_);
90 }
91 }
92
GetSize() const93 size_t NonOwnedMapping::GetSize() const {
94 return size_;
95 }
96
GetMapping() const97 const uint8_t* NonOwnedMapping::GetMapping() const {
98 return data_;
99 }
100
101 // Symbol Mapping
102
SymbolMapping(fml::RefPtr<fml::NativeLibrary> native_library,const char * symbol_name)103 SymbolMapping::SymbolMapping(fml::RefPtr<fml::NativeLibrary> native_library,
104 const char* symbol_name)
105 : native_library_(std::move(native_library)) {
106 if (native_library_ && symbol_name != nullptr) {
107 mapping_ = native_library_->ResolveSymbol(symbol_name);
108
109 if (mapping_ == nullptr) {
110 // Apparently, dart_bootstrap seems to account for the Mac behavior of
111 // requiring the underscore prefixed symbol name on non-Mac platforms as
112 // well. As a fallback, check the underscore prefixed variant of the
113 // symbol name and allow callers to not have handle this on a per platform
114 // toolchain quirk basis.
115
116 std::stringstream underscore_symbol_name;
117 underscore_symbol_name << "_" << symbol_name;
118 mapping_ =
119 native_library_->ResolveSymbol(underscore_symbol_name.str().c_str());
120 }
121 }
122 }
123
124 SymbolMapping::~SymbolMapping() = default;
125
GetSize() const126 size_t SymbolMapping::GetSize() const {
127 return 0;
128 }
129
GetMapping() const130 const uint8_t* SymbolMapping::GetMapping() const {
131 return mapping_;
132 }
133
134 } // namespace fml
135