Flutter Engine
 
Loading...
Searching...
No Matches
dart_runner.cc
Go to the documentation of this file.
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 "dart_runner.h"
6
7#include <lib/async-loop/loop.h>
8#include <lib/async/default.h>
9#include <lib/vfs/cpp/pseudo_dir.h>
10#include <sys/stat.h>
11#include <zircon/status.h>
12#include <zircon/syscalls.h>
13
14#include <cerrno>
15#include <memory>
16#include <thread>
17#include <utility>
18
21#include "flutter/fml/logging.h"
25#include "service_isolate.h"
26#include "third_party/dart/runtime/include/bin/dart_io_api.h"
29
30#if defined(AOT_RUNTIME)
31extern "C" uint8_t _kDartVmSnapshotData[];
32extern "C" uint8_t _kDartVmSnapshotInstructions[];
33#endif
34
35namespace dart_runner {
36
37namespace {
38
39const char* kDartVMArgs[] = {
40 // clang-format off
41
42 "--timeline_recorder=systrace",
43 "--timeline_streams=Compiler,Dart,Debugger,Embedder,GC,Isolate,VM",
44
45#if defined(AOT_RUNTIME)
46 "--precompilation",
47#else
48 "--enable_mirrors=false",
49#endif
50
51 // No asserts in debug or release product.
52 // No asserts in release with flutter_profile=true (non-product)
53 // Yes asserts in non-product debug.
54#if !defined(DART_PRODUCT) && (!defined(FLUTTER_PROFILE) || !defined(NDEBUG))
55 "--enable_asserts",
56#endif
57};
58
59Dart_Isolate IsolateGroupCreateCallback(const char* uri,
60 const char* name,
61 const char* package_root,
62 const char* package_config,
63 Dart_IsolateFlags* flags,
64 void* callback_data,
65 char** error) {
66 if (std::string(uri) == DART_VM_SERVICE_ISOLATE_NAME) {
67#if defined(DART_PRODUCT)
68 *error = strdup("The service isolate is not implemented in product mode");
69 return NULL;
70#else
71 return CreateServiceIsolate(uri, flags, error);
72#endif
73 }
74
75 *error = strdup("Isolate spawning is not implemented in dart_runner");
76 return NULL;
77}
78
79void IsolateShutdownCallback(void* isolate_group_data, void* isolate_data) {
80 // The service isolate (and maybe later the kernel isolate) doesn't have an
81 // async loop.
82 auto dispatcher = async_get_default_dispatcher();
83 auto loop = async_loop_from_dispatcher(dispatcher);
84 if (loop) {
87 if (queue) {
88 queue->Destroy();
89 }
90
91 async_loop_quit(loop);
92 }
93
94 auto state =
95 static_cast<std::shared_ptr<tonic::DartState>*>(isolate_group_data);
96 state->get()->SetIsShuttingDown();
97}
98
99void IsolateGroupCleanupCallback(void* isolate_group_data) {
100 delete static_cast<std::shared_ptr<tonic::DartState>*>(isolate_group_data);
101}
102
103// Runs the application for a Dart component.
104void RunApplication(
105 DartRunner* runner,
106 fuchsia::component::runner::ComponentStartInfo start_info,
107 std::shared_ptr<sys::ServiceDirectory> runner_incoming_services,
108 fidl::InterfaceRequest<fuchsia::component::runner::ComponentController>
109 controller) {
110 const int64_t start = Dart_TimelineGetMicros();
111
112 DartComponentController app(std::move(start_info), runner_incoming_services,
113 std::move(controller));
114 const bool success = app.SetUp();
115
116 const int64_t end = Dart_TimelineGetMicros();
117 Dart_RecordTimelineEvent(
118 "DartComponentController::SetUp", start, end, 0, nullptr,
119 Dart_Timeline_Event_Duration, 0, NULL, NULL);
120 if (success) {
121 app.Run();
122 }
123
124 if (Dart_CurrentIsolate()) {
125 Dart_ShutdownIsolate();
126 }
127}
128
129void RunTestApplication(
130 DartRunner* runner,
131 fuchsia::component::runner::ComponentStartInfo start_info,
132 std::shared_ptr<sys::ServiceDirectory> runner_incoming_services,
133 fidl::InterfaceRequest<fuchsia::component::runner::ComponentController>
134 controller,
135 fit::function<void(std::shared_ptr<DartTestComponentController>)>
136 component_created_callback,
137 fit::function<void(DartTestComponentController*)> done_callback) {
138 const int64_t start = Dart_TimelineGetMicros();
139
140 auto test_component = std::make_shared<DartTestComponentController>(
141 std::move(start_info), runner_incoming_services, std::move(controller),
142 std::move(done_callback));
143
144 component_created_callback(test_component);
145
146 // Start up the dart isolate and serve the suite protocol.
147 test_component->SetUp();
148
149 const int64_t end = Dart_TimelineGetMicros();
150 Dart_RecordTimelineEvent(
151 "DartTestComponentController::SetUp", start, end, 0, nullptr,
152 Dart_Timeline_Event_Duration, 0, NULL, NULL);
153}
154
155bool EntropySource(uint8_t* buffer, intptr_t count) {
156 zx_cprng_draw(buffer, count);
157 return true;
158}
159
160} // namespace
161
162// "args" are how the component specifies arguments to the runner.
163constexpr char kArgsKey[] = "args";
164
165/// Parses the |args| field from the "program" field to determine
166/// if a test component is being executed.
167bool IsTestProgram(const fuchsia::data::Dictionary& program_metadata) {
168 for (const auto& entry : program_metadata.entries()) {
169 if (entry.key.compare(kArgsKey) != 0 || entry.value == nullptr) {
170 continue;
171 }
172 auto args = entry.value->str_vec();
173
174 // fml::CommandLine expects the first argument to be the name of the
175 // program, so we prepend a dummy argument so we can use fml::CommandLine to
176 // parse the arguments for us.
177 std::vector<std::string> command_line_args = {""};
178 command_line_args.insert(command_line_args.end(), args.begin(), args.end());
180 command_line_args.begin(), command_line_args.end());
181
182 std::string is_test_str;
183 return parsed_args.GetOptionValue("is_test", &is_test_str) &&
184 is_test_str == "true";
185 }
186 return false;
187}
188
189DartRunner::DartRunner(sys::ComponentContext* context) : context_(context) {
190 context_->outgoing()
191 ->AddPublicService<fuchsia::component::runner::ComponentRunner>(
192 [this](fidl::InterfaceRequest<
193 fuchsia::component::runner::ComponentRunner> request) {
194 component_runner_bindings_.AddBinding(this, std::move(request));
195 });
196
197#if !defined(DART_PRODUCT)
198 // The VM service isolate uses the process-wide namespace. It writes the
199 // vm service protocol port under /tmp. The VMServiceObject exposes that
200 // port number to The Hub.
201 context_->outgoing()->debug_dir()->AddEntry(
203 std::make_unique<dart_utils::VMServiceObject>());
204
205#endif // !defined(DART_PRODUCT)
206
207 dart::bin::BootstrapDartIo();
208
209 char* error =
210 Dart_SetVMFlags(dart_utils::ArraySize(kDartVMArgs), kDartVMArgs);
211 if (error) {
212 FML_LOG(FATAL) << "Dart_SetVMFlags failed: " << error;
213 }
214
215 Dart_InitializeParams params = {};
216 params.version = DART_INITIALIZE_PARAMS_CURRENT_VERSION;
217#if defined(AOT_RUNTIME)
218 params.vm_snapshot_data = ::_kDartVmSnapshotData;
219 params.vm_snapshot_instructions = ::_kDartVmSnapshotInstructions;
220#else
222 nullptr, "/pkg/data/vm_snapshot_data.bin", vm_snapshot_data_)) {
223 FML_LOG(FATAL) << "Failed to load vm snapshot data";
224 }
225 params.vm_snapshot_data = vm_snapshot_data_.address();
226#endif
227 params.create_group = IsolateGroupCreateCallback;
228 params.shutdown_isolate = IsolateShutdownCallback;
229 params.cleanup_group = IsolateGroupCleanupCallback;
230 params.entropy_source = EntropySource;
231 error = Dart_Initialize(&params);
232 if (error)
233 FML_LOG(FATAL) << "Dart_Initialize failed: " << error;
234}
235
237 char* error = Dart_Cleanup();
238 if (error)
239 FML_LOG(FATAL) << "Dart_Cleanup failed: " << error;
240}
241
242void DartRunner::Start(
243 fuchsia::component::runner::ComponentStartInfo start_info,
244 fidl::InterfaceRequest<fuchsia::component::runner::ComponentController>
245 controller) {
246 // Parse the program field of the component's cml and check if it is a test
247 // component. If so, serve the |fuchsia.test.Suite| protocol from the
248 // component's outgoing directory, via DartTestComponentController.
249 if (IsTestProgram(start_info.program())) {
250 std::string url_copy = start_info.resolved_url();
251 TRACE_EVENT1("dart", "Start", "url", url_copy.c_str());
252 std::thread thread(
253 RunTestApplication, this, std::move(start_info), context_->svc(),
254 std::move(controller),
255 // component_created_callback
256 [this](std::shared_ptr<DartTestComponentController> ptr) {
257 test_components_.emplace(ptr.get(), std::move(ptr));
258 },
259 // done_callback
260 [this](DartTestComponentController* ptr) {
261 auto it = test_components_.find(ptr);
262 if (it != test_components_.end()) {
263 test_components_.erase(it);
264 }
265 });
266 thread.detach();
267 } else {
268 std::string url_copy = start_info.resolved_url();
269 TRACE_EVENT1("dart", "Start", "url", url_copy.c_str());
270 std::thread thread(RunApplication, this, std::move(start_info),
271 context_->svc(), std::move(controller));
272 thread.detach();
273 }
274}
275
276void DartRunner::handle_unknown_method(uint64_t ordinal,
277 bool method_has_response) {
278 FML_LOG(ERROR) << "Unknown method called on DartRunner. Ordinal: "
279 << ordinal;
280}
281
282} // namespace dart_runner
DartRunner(sys::ComponentContext *context)
static bool LoadFromNamespace(fdio_ns_t *namespc, const std::string &path, MappedResource &resource, bool executable=false)
const uint8_t * address() const
static constexpr const char * kPortDirName
bool GetOptionValue(std::string_view name, std::string *value) const
static DartMicrotaskQueue * GetForCurrentThread()
const EmbeddedViewParams * params
VkQueue queue
Definition main.cc:71
G_BEGIN_DECLS G_MODULE_EXPORT FlValue * args
const uint8_t uint32_t uint32_t GError ** error
#define FML_LOG(severity)
Definition logging.h:101
const char * name
Definition fuchsia.cc:49
constexpr char kArgsKey[]
bool IsTestProgram(const fuchsia::data::Dictionary &program_metadata)
Dart_Isolate CreateServiceIsolate(const char *uri, Dart_IsolateFlags *flags_unused, char **error)
size_t ArraySize(T(&array)[SIZE])
Definition inlines.h:11
char * strdup(const char *str1)
CommandLine CommandLineFromIterators(InputIterator first, InputIterator last)
const size_t start
const size_t end
#define TRACE_EVENT1(category_group, name, arg1_name, arg1_val)