Flutter Engine
 
Loading...
Searching...
No Matches
dart_snapshot.cc File Reference
#include "flutter/runtime/dart_snapshot.h"
#include <sstream>
#include "flutter/fml/native_library.h"
#include "flutter/fml/paths.h"
#include "flutter/fml/trace_event.h"
#include "flutter/lib/snapshot/snapshot.h"
#include "flutter/runtime/dart_vm.h"
#include "third_party/dart/runtime/include/dart_api.h"

Go to the source code of this file.

Namespaces

namespace  flutter
 

Macros

#define DART_SNAPSHOT_STATIC_LINK    ((FML_OS_WIN || FML_OS_ANDROID) && FLUTTER_JIT_RUNTIME)
 

Functions

static std::unique_ptr< const fml::Mappingflutter::GetFileMapping (const std::string &path, bool executable)
 
static std::shared_ptr< const fml::Mappingflutter::SearchMapping (const MappingCallback &embedder_mapping_callback, const std::string &file_path, const std::vector< std::string > &native_library_paths, const char *native_library_symbol_name, bool is_executable)
 
static std::shared_ptr< const fml::Mappingflutter::ResolveVMData (const Settings &settings)
 
static std::shared_ptr< const fml::Mappingflutter::ResolveVMInstructions (const Settings &settings)
 
static std::shared_ptr< const fml::Mappingflutter::ResolveIsolateData (const Settings &settings)
 
static std::shared_ptr< const fml::Mappingflutter::ResolveIsolateInstructions (const Settings &settings)
 

Macro Definition Documentation

◆ DART_SNAPSHOT_STATIC_LINK

#define DART_SNAPSHOT_STATIC_LINK    ((FML_OS_WIN || FML_OS_ANDROID) && FLUTTER_JIT_RUNTIME)

Definition at line 27 of file dart_snapshot.cc.

33 {
34 if (executable) {
36 } else {
38 }
39}
40
41// The first party embedders don't yet use the stable embedder API and depend on
42// the engine figuring out the locations of the various heap and instructions
43// buffers. Consequently, the engine had baked in opinions about where these
44// buffers would reside and how they would be packaged (examples, in an external
45// dylib, in the same dylib, at a path, at a path relative to and FD, etc..). As
46// the needs of the platforms changed, the lack of an API meant that the engine
47// had to be patched to look for new fields in the settings object. This grew
48// untenable and with the addition of the new Fuchsia embedder and the generic C
49// embedder API, embedders could specify the mapping directly. Once everyone
50// moves to the embedder API, this method can effectively be reduced to just
51// invoking the embedder_mapping_callback directly.
52static std::shared_ptr<const fml::Mapping> SearchMapping(
53 const MappingCallback& embedder_mapping_callback,
54 const std::string& file_path,
55 const std::vector<std::string>& native_library_paths,
56 const char* native_library_symbol_name,
57 bool is_executable) {
58 // Ask the embedder. There is no fallback as we expect the embedders (via
59 // their embedding APIs) to just specify the mappings directly.
60 if (embedder_mapping_callback) {
61 // Note that mapping will be nullptr if the mapping callback returns an
62 // invalid mapping. If all the other methods for resolving the data also
63 // fail, the engine will stop with accompanying error logs.
64 if (auto mapping = embedder_mapping_callback()) {
65 return mapping;
66 }
67 }
68
69 // Attempt to open file at path specified.
70 if (!file_path.empty()) {
71 if (auto file_mapping = GetFileMapping(file_path, is_executable)) {
72 return file_mapping;
73 }
74 }
75
76 // Look in application specified native library if specified.
77 for (const std::string& path : native_library_paths) {
78 auto native_library = fml::NativeLibrary::Create(path.c_str());
79 auto symbol_mapping = std::make_unique<const fml::SymbolMapping>(
80 native_library, native_library_symbol_name);
81 if (symbol_mapping->GetMapping() != nullptr) {
82 return symbol_mapping;
83 }
84 }
85
86 // Look inside the currently loaded process.
87 {
88 auto loaded_process = fml::NativeLibrary::CreateForCurrentProcess();
89 auto symbol_mapping = std::make_unique<const fml::SymbolMapping>(
90 loaded_process, native_library_symbol_name);
91 if (symbol_mapping->GetMapping() != nullptr) {
92 return symbol_mapping;
93 }
94 }
95
96 return nullptr;
97}
98
99#endif // !DART_SNAPSHOT_STATIC_LINK
100
101static std::shared_ptr<const fml::Mapping> ResolveVMData(
102 const Settings& settings) {
103#if DART_SNAPSHOT_STATIC_LINK
104 return std::make_unique<fml::NonOwnedMapping>(kDartVmSnapshotData,
105 0, // size
106 nullptr, // release_func
107 true // dontneed_safe
108 );
109#else // DART_SNAPSHOT_STATIC_LINK
110 return SearchMapping(
111 settings.vm_snapshot_data, // embedder_mapping_callback
112 settings.vm_snapshot_data_path, // file_path
113 settings.application_library_paths, // native_library_paths
114 DartSnapshot::kVMDataSymbol, // native_library_symbol_name
115 false // is_executable
116 );
117#endif // DART_SNAPSHOT_STATIC_LINK
118}
119
120static std::shared_ptr<const fml::Mapping> ResolveVMInstructions(
121 const Settings& settings) {
122#if DART_SNAPSHOT_STATIC_LINK
123 return std::make_unique<fml::NonOwnedMapping>(kDartVmSnapshotInstructions,
124 0, // size
125 nullptr, // release_func
126 true // dontneed_safe
127 );
128#else // DART_SNAPSHOT_STATIC_LINK
129 return SearchMapping(
130 settings.vm_snapshot_instr, // embedder_mapping_callback
131 settings.vm_snapshot_instr_path, // file_path
132 settings.application_library_paths, // native_library_paths
133 DartSnapshot::kVMInstructionsSymbol, // native_library_symbol_name
134 true // is_executable
135 );
136#endif // DART_SNAPSHOT_STATIC_LINK
137}
138
139static std::shared_ptr<const fml::Mapping> ResolveIsolateData(
140 const Settings& settings) {
141#if DART_SNAPSHOT_STATIC_LINK
142 return std::make_unique<fml::NonOwnedMapping>(kDartIsolateSnapshotData,
143 0, // size
144 nullptr, // release_func
145 true // dontneed_safe
146 );
147#else // DART_SNAPSHOT_STATIC_LINK
148 return SearchMapping(
149 settings.isolate_snapshot_data, // embedder_mapping_callback
150 settings.isolate_snapshot_data_path, // file_path
151 settings.application_library_paths, // native_library_paths
152 DartSnapshot::kIsolateDataSymbol, // native_library_symbol_name
153 false // is_executable
154 );
155#endif // DART_SNAPSHOT_STATIC_LINK
156}
157
158static std::shared_ptr<const fml::Mapping> ResolveIsolateInstructions(
159 const Settings& settings) {
160#if DART_SNAPSHOT_STATIC_LINK
161 return std::make_unique<fml::NonOwnedMapping>(
163 0, // size
164 nullptr, // release_func
165 true // dontneed_safe
166 );
167#else // DART_SNAPSHOT_STATIC_LINK
168 return SearchMapping(
169 settings.isolate_snapshot_instr, // embedder_mapping_callback
170 settings.isolate_snapshot_instr_path, // file_path
171 settings.application_library_paths, // native_library_paths
172 DartSnapshot::kIsolateInstructionsSymbol, // native_library_symbol_name
173 true // is_executable
174 );
175#endif // DART_SNAPSHOT_STATIC_LINK
176}
177
178fml::RefPtr<const DartSnapshot> DartSnapshot::VMSnapshotFromSettings(
179 const Settings& settings) {
180 TRACE_EVENT0("flutter", "DartSnapshot::VMSnapshotFromSettings");
181 auto snapshot =
182 fml::MakeRefCounted<DartSnapshot>(ResolveVMData(settings), //
183 ResolveVMInstructions(settings) //
184 );
185 if (snapshot->IsValid()) {
186 return snapshot;
187 }
188 return nullptr;
189}
190
191fml::RefPtr<const DartSnapshot> DartSnapshot::IsolateSnapshotFromSettings(
192 const Settings& settings) {
193 TRACE_EVENT0("flutter", "DartSnapshot::IsolateSnapshotFromSettings");
194 auto snapshot =
195 fml::MakeRefCounted<DartSnapshot>(ResolveIsolateData(settings), //
196 ResolveIsolateInstructions(settings) //
197 );
198 if (snapshot->IsValid()) {
199 return snapshot;
200 }
201 return nullptr;
202}
203
204fml::RefPtr<DartSnapshot> DartSnapshot::IsolateSnapshotFromMappings(
205 const std::shared_ptr<const fml::Mapping>& snapshot_data,
206 const std::shared_ptr<const fml::Mapping>& snapshot_instructions) {
207 auto snapshot =
208 fml::MakeRefCounted<DartSnapshot>(snapshot_data, snapshot_instructions);
209 if (snapshot->IsValid()) {
210 return snapshot;
211 }
212 return nullptr;
213 // NOLINTNEXTLINE(clang-analyzer-cplusplus.NewDeleteLeaks)
214}
215
216fml::RefPtr<DartSnapshot> DartSnapshot::VMServiceIsolateSnapshotFromSettings(
217 const Settings& settings) {
218#if DART_SNAPSHOT_STATIC_LINK
219 return nullptr;
220#else // DART_SNAPSHOT_STATIC_LINK
221 if (settings.vmservice_snapshot_library_path.empty()) {
222 return nullptr;
223 }
224
225 std::shared_ptr<const fml::Mapping> snapshot_data =
226 SearchMapping(nullptr, "", settings.vmservice_snapshot_library_path,
227 DartSnapshot::kIsolateDataSymbol, false);
228 std::shared_ptr<const fml::Mapping> snapshot_instructions =
229 SearchMapping(nullptr, "", settings.vmservice_snapshot_library_path,
230 DartSnapshot::kIsolateInstructionsSymbol, true);
231 return IsolateSnapshotFromMappings(snapshot_data, snapshot_instructions);
232#endif // DART_SNAPSHOT_STATIC_LINK
233}
234
235DartSnapshot::DartSnapshot(std::shared_ptr<const fml::Mapping> data,
236 std::shared_ptr<const fml::Mapping> instructions)
237 : data_(std::move(data)), instructions_(std::move(instructions)) {}
238
239DartSnapshot::~DartSnapshot() = default;
240
241bool DartSnapshot::IsValid() const {
242 return static_cast<bool>(data_);
243}
244
245bool DartSnapshot::IsValidForAOT() const {
246 return data_ && instructions_;
247}
248
249const uint8_t* DartSnapshot::GetDataMapping() const {
250 return data_ ? data_->GetMapping() : nullptr;
251}
252
253const uint8_t* DartSnapshot::GetInstructionsMapping() const {
254 return instructions_ ? instructions_->GetMapping() : nullptr;
255}
256
257bool DartSnapshot::IsDontNeedSafe() const {
258 if (data_ && !data_->IsDontNeedSafe()) {
259 return false;
260 }
261 if (instructions_ && !instructions_->IsDontNeedSafe()) {
262 return false;
263 }
264 return true;
265}
266
267bool DartSnapshot::IsNullSafetyEnabled(const fml::Mapping* kernel) const {
268 return ::Dart_DetectNullSafety(
269 nullptr, // script_uri (unsupported by Flutter)
270 nullptr, // package_config (package resolution of parent used)
271 nullptr, // original_working_directory (no package config)
272 GetDataMapping(), // snapshot_data
273 GetInstructionsMapping(), // snapshot_instructions
274 kernel ? kernel->GetMapping() : nullptr, // kernel_buffer
275 kernel ? kernel->GetSize() : 0u // kernel_buffer_size
276 );
277}
278
279} // namespace flutter
static std::unique_ptr< FileMapping > CreateReadExecute(const std::string &path)
Definition mapping.cc:44
static std::unique_ptr< FileMapping > CreateReadOnly(const std::string &path)
Definition mapping.cc:20
virtual const uint8_t * GetMapping() const =0
static fml::RefPtr< NativeLibrary > CreateForCurrentProcess()
static fml::RefPtr< NativeLibrary > Create(const char *path)
const uint8_t kDartVmSnapshotData[]
const uint8_t kDartVmSnapshotInstructions[]
const uint8_t kDartIsolateSnapshotData[]
const uint8_t kDartIsolateSnapshotInstructions[]
static std::shared_ptr< const fml::Mapping > ResolveIsolateData(const Settings &settings)
static std::shared_ptr< const fml::Mapping > SearchMapping(const MappingCallback &embedder_mapping_callback, const std::string &file_path, const std::vector< std::string > &native_library_paths, const char *native_library_symbol_name, bool is_executable)
static std::shared_ptr< const fml::Mapping > ResolveVMInstructions(const Settings &settings)
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot The VM snapshot data that will be memory mapped as read only SnapshotAssetPath must be present isolate snapshot The isolate snapshot data that will be memory mapped as read only SnapshotAssetPath must be present cache dir path
Definition switch_defs.h:52
static std::shared_ptr< const fml::Mapping > ResolveVMData(const Settings &settings)
static std::unique_ptr< const fml::Mapping > GetFileMapping(const std::string &path, bool executable)
static std::shared_ptr< const fml::Mapping > ResolveIsolateInstructions(const Settings &settings)
Definition ref_ptr.h:261
std::shared_ptr< const fml::Mapping > data
#define TRACE_EVENT0(category_group, name)