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