| 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 FLUTTER_FML_MAPPING_H_ |
| 6 | #define FLUTTER_FML_MAPPING_H_ |
| 7 | |
| 8 | #include <initializer_list> |
| 9 | #include <memory> |
| 10 | #include <string> |
| 11 | #include <vector> |
| 12 | |
| 13 | #include "flutter/fml/build_config.h" |
| 14 | #include "flutter/fml/file.h" |
| 15 | #include "flutter/fml/macros.h" |
| 16 | #include "flutter/fml/native_library.h" |
| 17 | #include "flutter/fml/unique_fd.h" |
| 18 | |
| 19 | namespace fml { |
| 20 | |
| 21 | class Mapping { |
| 22 | public: |
| 23 | Mapping(); |
| 24 | |
| 25 | virtual ~Mapping(); |
| 26 | |
| 27 | virtual size_t GetSize() const = 0; |
| 28 | |
| 29 | virtual const uint8_t* GetMapping() const = 0; |
| 30 | |
| 31 | // Whether calling madvise(DONTNEED) on the mapping is non-destructive. |
| 32 | // Generally true for file-mapped memory and false for anonymous memory. |
| 33 | virtual bool IsDontNeedSafe() const = 0; |
| 34 | |
| 35 | private: |
| 36 | FML_DISALLOW_COPY_AND_ASSIGN(Mapping); |
| 37 | }; |
| 38 | |
| 39 | class FileMapping final : public Mapping { |
| 40 | public: |
| 41 | enum class Protection { |
| 42 | kRead, |
| 43 | kWrite, |
| 44 | kExecute, |
| 45 | }; |
| 46 | |
| 47 | explicit FileMapping(const fml::UniqueFD& fd, |
| 48 | std::initializer_list<Protection> protection = { |
| 49 | Protection::kRead}); |
| 50 | |
| 51 | ~FileMapping() override; |
| 52 | |
| 53 | static std::unique_ptr<FileMapping> CreateReadOnly(const std::string& path); |
| 54 | |
| 55 | static std::unique_ptr<FileMapping> CreateReadOnly( |
| 56 | const fml::UniqueFD& base_fd, |
| 57 | const std::string& sub_path = "" ); |
| 58 | |
| 59 | static std::unique_ptr<FileMapping> CreateReadExecute( |
| 60 | const std::string& path); |
| 61 | |
| 62 | static std::unique_ptr<FileMapping> CreateReadExecute( |
| 63 | const fml::UniqueFD& base_fd, |
| 64 | const std::string& sub_path = "" ); |
| 65 | |
| 66 | // |Mapping| |
| 67 | size_t GetSize() const override; |
| 68 | |
| 69 | // |Mapping| |
| 70 | const uint8_t* GetMapping() const override; |
| 71 | |
| 72 | // |Mapping| |
| 73 | bool IsDontNeedSafe() const override; |
| 74 | |
| 75 | uint8_t* GetMutableMapping(); |
| 76 | |
| 77 | bool IsValid() const; |
| 78 | |
| 79 | private: |
| 80 | bool valid_ = false; |
| 81 | size_t size_ = 0; |
| 82 | uint8_t* mapping_ = nullptr; |
| 83 | uint8_t* mutable_mapping_ = nullptr; |
| 84 | |
| 85 | #if FML_OS_WIN |
| 86 | fml::UniqueFD mapping_handle_; |
| 87 | #endif |
| 88 | |
| 89 | FML_DISALLOW_COPY_AND_ASSIGN(FileMapping); |
| 90 | }; |
| 91 | |
| 92 | class DataMapping final : public Mapping { |
| 93 | public: |
| 94 | explicit DataMapping(std::vector<uint8_t> data); |
| 95 | |
| 96 | explicit DataMapping(const std::string& string); |
| 97 | |
| 98 | ~DataMapping() override; |
| 99 | |
| 100 | // |Mapping| |
| 101 | size_t GetSize() const override; |
| 102 | |
| 103 | // |Mapping| |
| 104 | const uint8_t* GetMapping() const override; |
| 105 | |
| 106 | // |Mapping| |
| 107 | bool IsDontNeedSafe() const override; |
| 108 | |
| 109 | private: |
| 110 | std::vector<uint8_t> data_; |
| 111 | |
| 112 | FML_DISALLOW_COPY_AND_ASSIGN(DataMapping); |
| 113 | }; |
| 114 | |
| 115 | class NonOwnedMapping final : public Mapping { |
| 116 | public: |
| 117 | using ReleaseProc = std::function<void(const uint8_t* data, size_t size)>; |
| 118 | NonOwnedMapping(const uint8_t* data, |
| 119 | size_t size, |
| 120 | const ReleaseProc& release_proc = nullptr, |
| 121 | bool dontneed_safe = false); |
| 122 | |
| 123 | ~NonOwnedMapping() override; |
| 124 | |
| 125 | // |Mapping| |
| 126 | size_t GetSize() const override; |
| 127 | |
| 128 | // |Mapping| |
| 129 | const uint8_t* GetMapping() const override; |
| 130 | |
| 131 | // |Mapping| |
| 132 | bool IsDontNeedSafe() const override; |
| 133 | |
| 134 | private: |
| 135 | const uint8_t* const data_; |
| 136 | const size_t size_; |
| 137 | const ReleaseProc release_proc_; |
| 138 | const bool dontneed_safe_; |
| 139 | |
| 140 | FML_DISALLOW_COPY_AND_ASSIGN(NonOwnedMapping); |
| 141 | }; |
| 142 | |
| 143 | /// A Mapping like NonOwnedMapping, but uses Free as its release proc. |
| 144 | class MallocMapping final : public Mapping { |
| 145 | public: |
| 146 | MallocMapping(); |
| 147 | |
| 148 | /// Creates a MallocMapping for a region of memory (without copying it). |
| 149 | /// The function will `abort()` if the malloc fails. |
| 150 | /// @param data The starting address of the mapping. |
| 151 | /// @param size The size of the mapping in bytes. |
| 152 | MallocMapping(uint8_t* data, size_t size); |
| 153 | |
| 154 | MallocMapping(fml::MallocMapping&& mapping); |
| 155 | |
| 156 | ~MallocMapping() override; |
| 157 | |
| 158 | /// Copies the data from `begin` to `end`. |
| 159 | /// It's templated since void* arithemetic isn't allowed and we want support |
| 160 | /// for `uint8_t` and `char`. |
| 161 | template <typename T> |
| 162 | static MallocMapping Copy(const T* begin, const T* end) { |
| 163 | FML_DCHECK(end >= begin); |
| 164 | size_t length = end - begin; |
| 165 | return Copy(begin, length); |
| 166 | } |
| 167 | |
| 168 | /// Copies a region of memory into a MallocMapping. |
| 169 | /// The function will `abort()` if the malloc fails. |
| 170 | /// @param begin The starting address of where we will copy. |
| 171 | /// @param length The length of the region to copy in bytes. |
| 172 | static MallocMapping Copy(const void* begin, size_t length); |
| 173 | |
| 174 | // |Mapping| |
| 175 | size_t GetSize() const override; |
| 176 | |
| 177 | // |Mapping| |
| 178 | const uint8_t* GetMapping() const override; |
| 179 | |
| 180 | // |Mapping| |
| 181 | bool IsDontNeedSafe() const override; |
| 182 | |
| 183 | /// Removes ownership of the data buffer. |
| 184 | /// After this is called; the mapping will point to nullptr. |
| 185 | [[nodiscard]] uint8_t* Release(); |
| 186 | |
| 187 | private: |
| 188 | uint8_t* data_; |
| 189 | size_t size_; |
| 190 | |
| 191 | FML_DISALLOW_COPY_AND_ASSIGN(MallocMapping); |
| 192 | }; |
| 193 | |
| 194 | class SymbolMapping final : public Mapping { |
| 195 | public: |
| 196 | SymbolMapping(fml::RefPtr<fml::NativeLibrary> native_library, |
| 197 | const char* symbol_name); |
| 198 | |
| 199 | ~SymbolMapping() override; |
| 200 | |
| 201 | // |Mapping| |
| 202 | size_t GetSize() const override; |
| 203 | |
| 204 | // |Mapping| |
| 205 | const uint8_t* GetMapping() const override; |
| 206 | |
| 207 | // |Mapping| |
| 208 | bool IsDontNeedSafe() const override; |
| 209 | |
| 210 | private: |
| 211 | fml::RefPtr<fml::NativeLibrary> native_library_; |
| 212 | const uint8_t* mapping_ = nullptr; |
| 213 | |
| 214 | FML_DISALLOW_COPY_AND_ASSIGN(SymbolMapping); |
| 215 | }; |
| 216 | |
| 217 | } // namespace fml |
| 218 | |
| 219 | #endif // FLUTTER_FML_MAPPING_H_ |
| 220 | |