Flutter Engine Uber Docs
Docs for the entire Flutter Engine repo.
 
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 26 of file dart_snapshot.cc.

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