Flutter Engine
dart_runner Namespace Reference

Classes

class  DartComponentController
 
class  DartRunner
 

Functions

void InitBuiltinLibrariesForIsolate (const std::string &script_uri, fdio_ns_t *namespc, int stdoutfd, int stderrfd, fidl::InterfaceHandle< fuchsia::sys::Environment > environment, zx::channel directory_request, bool service_isolate)
 
Dart_Isolate CreateServiceIsolate (const char *uri, Dart_IsolateFlags *flags, char **error)
 
Dart_Handle GetVMServiceAssetsArchiveCallback ()
 

Variables

constexpr char kDataKey [] = "data"
 
constexpr char kTmpPath [] = "/tmp"
 
constexpr char kServiceRootPath [] = "/svc"
 
const zx::duration kIdleWaitDuration = zx::sec(2)
 
const zx::duration kIdleNotifyDuration = zx::msec(500)
 
const zx::duration kIdleSlack = zx::sec(1)
 
uint8_t const *const vm_isolate_snapshot_buffer
 
uint8_t const *const isolate_snapshot_buffer
 

Function Documentation

◆ CreateServiceIsolate()

Dart_Isolate dart_runner::CreateServiceIsolate ( const char *  uri,
Dart_IsolateFlags *  flags,
char **  error 
)

Definition at line 76 of file service_isolate.cc.

References dart_utils::MappedResource::address(), flutter::EmbedderInformationCallback(), InitBuiltinLibrariesForIsolate(), dart_utils::ElfSnapshot::IsolateData(), dart_utils::ElfSnapshot::IsolateInstrs(), dart_utils::ElfSnapshot::Load(), dart_utils::MappedResource::LoadFromNamespace(), LOG_TAG, tonic::DartLibraryNatives::Register(), SHUTDOWN_ON_ERROR, and fml::strdup().

78  {
79  Dart_SetEmbedderInformationCallback(EmbedderInformationCallback);
80 
81  const uint8_t *vmservice_data = nullptr, *vmservice_instructions = nullptr;
82 
83 #if defined(AOT_RUNTIME)
84  // The VM service was compiled as a separate app.
85  const char* snapshot_path = "/pkg/data/vmservice_snapshot.so";
86  if (elf_snapshot.Load(nullptr, snapshot_path)) {
87  vmservice_data = elf_snapshot.IsolateData();
88  vmservice_instructions = elf_snapshot.IsolateInstrs();
89  if (vmservice_data == nullptr || vmservice_instructions == nullptr) {
90  return nullptr;
91  }
92  } else {
93  // The VM service was compiled as a separate app.
94  const char* snapshot_data_path =
95  "/pkg/data/vmservice_isolate_snapshot_data.bin";
96  const char* snapshot_instructions_path =
97  "/pkg/data/vmservice_isolate_snapshot_instructions.bin";
98 #else
99  // The VM service is embedded in the core snapshot.
100  const char* snapshot_data_path = "/pkg/data/isolate_core_snapshot_data.bin";
101  const char* snapshot_instructions_path =
102  "/pkg/data/isolate_core_snapshot_instructions.bin";
103 #endif
104 
106  nullptr, snapshot_data_path, mapped_isolate_snapshot_data)) {
107  *error = strdup("Failed to load snapshot for service isolate");
108  FX_LOG(ERROR, LOG_TAG, *error);
109  return nullptr;
110  }
112  nullptr, snapshot_instructions_path,
113  mapped_isolate_snapshot_instructions, true /* executable */)) {
114  *error = strdup("Failed to load snapshot for service isolate");
115  FX_LOG(ERROR, LOG_TAG, *error);
116  return nullptr;
117  }
118 
119  vmservice_data = mapped_isolate_snapshot_data.address();
120  vmservice_instructions = mapped_isolate_snapshot_instructions.address();
121 #if defined(AOT_RUNTIME)
122  }
123 #endif
124 
125  auto state = new std::shared_ptr<tonic::DartState>(new tonic::DartState());
126  Dart_Isolate isolate = Dart_CreateIsolateGroup(
127  uri, DART_VM_SERVICE_ISOLATE_NAME, vmservice_data, vmservice_instructions,
128  nullptr /* flags */, state, state, error);
129  if (!isolate) {
130  FX_LOGF(ERROR, LOG_TAG, "Dart_CreateIsolateGroup failed: %s", *error);
131  return nullptr;
132  }
133 
134  state->get()->SetIsolate(isolate);
135 
136  // Setup native entries.
137  service_natives = new tonic::DartLibraryNatives();
138  service_natives->Register({
139  {"VMServiceIO_NotifyServerState", NotifyServerState, 1, true},
140  {"VMServiceIO_Shutdown", Shutdown, 0, true},
141  });
142 
143  Dart_EnterScope();
144 
145  Dart_Handle library =
146  Dart_LookupLibrary(Dart_NewStringFromCString("dart:vmservice_io"));
147  SHUTDOWN_ON_ERROR(library);
148  Dart_Handle result = Dart_SetRootLibrary(library);
149  SHUTDOWN_ON_ERROR(result);
150  result = Dart_SetNativeResolver(library, GetNativeFunction, GetSymbol);
151  SHUTDOWN_ON_ERROR(result);
152 
153  // _ip = '127.0.0.1'
154  result = Dart_SetField(library, Dart_NewStringFromCString("_ip"),
155  Dart_NewStringFromCString("127.0.0.1"));
156  SHUTDOWN_ON_ERROR(result);
157 
158  // _port = 0
159  result = Dart_SetField(library, Dart_NewStringFromCString("_port"),
160  Dart_NewInteger(0));
161  SHUTDOWN_ON_ERROR(result);
162 
163  // _autoStart = true
164  result = Dart_SetField(library, Dart_NewStringFromCString("_autoStart"),
165  Dart_NewBoolean(true));
166  SHUTDOWN_ON_ERROR(result);
167 
168  // _originCheckDisabled = false
169  result =
170  Dart_SetField(library, Dart_NewStringFromCString("_originCheckDisabled"),
171  Dart_NewBoolean(false));
172  SHUTDOWN_ON_ERROR(result);
173 
174  // _authCodesDisabled = false
175  result =
176  Dart_SetField(library, Dart_NewStringFromCString("_authCodesDisabled"),
177  Dart_NewBoolean(false));
178  SHUTDOWN_ON_ERROR(result);
179 
180  InitBuiltinLibrariesForIsolate(std::string(uri), nullptr, fileno(stdout),
181  fileno(stderr), nullptr, zx::channel(), true);
182 
183  // Make runnable.
184  Dart_ExitScope();
185  Dart_ExitIsolate();
186  *error = Dart_IsolateMakeRunnable(isolate);
187  if (*error != nullptr) {
188  FX_LOG(ERROR, LOG_TAG, *error);
189  Dart_EnterIsolate(isolate);
190  Dart_ShutdownIsolate();
191  return nullptr;
192  }
193  return isolate;
194 } // namespace dart_runner
FlMethodResponse GError ** error
static void EmbedderInformationCallback(Dart_EmbedderInformation *info)
Definition: dart_vm.cc:233
static bool LoadFromNamespace(fdio_ns_t *namespc, const std::string &path, MappedResource &resource, bool executable=false)
void InitBuiltinLibrariesForIsolate(const std::string &script_uri, fdio_ns_t *namespc, int stdoutfd, int stderrfd, fidl::InterfaceHandle< fuchsia::sys::Environment > environment, zx::channel directory_request, bool service_isolate)
#define SHUTDOWN_ON_ERROR(handle)
#define LOG_TAG
Definition: logging.h:11
char * strdup(const char *str1)

◆ GetVMServiceAssetsArchiveCallback()

Dart_Handle dart_runner::GetVMServiceAssetsArchiveCallback ( )

Definition at line 196 of file service_isolate.cc.

References dart_utils::MappedResource::address(), dart_utils::MappedResource::LoadFromNamespace(), LOG_TAG, dart_utils::MappedResource::size(), and tonic::ToDart().

Referenced by dart_runner::DartRunner::DartRunner().

196  {
197  dart_utils::MappedResource observatory_tar;
199  nullptr, "/pkg/data/observatory.tar", observatory_tar)) {
200  FX_LOG(ERROR, LOG_TAG, "Failed to load Observatory assets");
201  return nullptr;
202  }
203  // TODO(rmacnak): Should we avoid copying the tar? Or does the service
204  // library not hold onto it anyway?
206  reinterpret_cast<const uint8_t*>(observatory_tar.address()),
207  observatory_tar.size());
208 }
static bool LoadFromNamespace(fdio_ns_t *namespc, const std::string &path, MappedResource &resource, bool executable=false)
const uint8_t * address() const
#define LOG_TAG
Definition: logging.h:11
Dart_Handle ToDart(const T &object)

◆ InitBuiltinLibrariesForIsolate()

void dart_runner::InitBuiltinLibrariesForIsolate ( const std::string &  script_uri,
fdio_ns_t *  namespc,
int  stdoutfd,
int  stderrfd,
fidl::InterfaceHandle< fuchsia::sys::Environment >  environment,
zx::channel  directory_request,
bool  service_isolate 
)

Definition at line 99 of file builtin_libraries.cc.

References FML_CHECK, fuchsia::dart::Initialize(), tonic::LogIfError(), and tonic::ToDart().

Referenced by CreateServiceIsolate(), and dart_runner::DartComponentController::Main().

106  {
107  // dart:fuchsia --------------------------------------------------------------
108  // dart runner doesn't care about scenic view ref.
109  if (!service_isolate) {
110  fuchsia::dart::Initialize(std::move(environment),
111  std::move(directory_request), std::nullopt);
112  }
113 
114  // dart:fuchsia.builtin ------------------------------------------------------
115 
116  Dart_Handle builtin_lib = Dart_LookupLibrary(ToDart("dart:fuchsia.builtin"));
117  FML_CHECK(!tonic::LogIfError(builtin_lib));
118  Dart_Handle result = Dart_SetNativeResolver(builtin_lib, BuiltinNativeLookup,
119  BuiltinNativeSymbol);
120  FML_CHECK(!tonic::LogIfError(result));
121 
122  // dart:io -------------------------------------------------------------------
123 
124  Dart_Handle io_lib = Dart_LookupLibrary(ToDart("dart:io"));
125  FML_CHECK(!tonic::LogIfError(io_lib));
126  result = Dart_SetNativeResolver(io_lib, dart::bin::IONativeLookup,
127  dart::bin::IONativeSymbol);
128  FML_CHECK(!tonic::LogIfError(result));
129 
130  // dart:zircon ---------------------------------------------------------------
131 
132  Dart_Handle zircon_lib = Dart_LookupLibrary(ToDart("dart:zircon"));
133  FML_CHECK(!tonic::LogIfError(zircon_lib));
134  // NativeResolver already set by fuchsia::dart::Initialize().
135 
136  // Core libraries ------------------------------------------------------------
137 
138  Dart_Handle async_lib = Dart_LookupLibrary(ToDart("dart:async"));
139  FML_CHECK(!tonic::LogIfError(async_lib));
140 
141  Dart_Handle core_lib = Dart_LookupLibrary(ToDart("dart:core"));
142  FML_CHECK(!tonic::LogIfError(core_lib));
143 
144  Dart_Handle internal_lib = Dart_LookupLibrary(ToDart("dart:_internal"));
145  FML_CHECK(!tonic::LogIfError(internal_lib));
146 
147  Dart_Handle isolate_lib = Dart_LookupLibrary(ToDart("dart:isolate"));
148  FML_CHECK(!tonic::LogIfError(isolate_lib));
149 
150 #if !defined(AOT_RUNTIME)
151  // AOT: These steps already happened at compile time in gen_snapshot.
152 
153  // We need to ensure that all the scripts loaded so far are finalized
154  // as we are about to invoke some Dart code below to setup closures.
155  result = Dart_FinalizeLoading(false);
156  FML_CHECK(!tonic::LogIfError(result));
157 #endif
158 
159  // Setup the internal library's 'internalPrint' function.
160  Dart_Handle print =
161  Dart_Invoke(builtin_lib, ToDart("_getPrintClosure"), 0, nullptr);
162  FML_CHECK(!tonic::LogIfError(print));
163 
164  result = Dart_SetField(internal_lib, ToDart("_printClosure"), print);
165  FML_CHECK(!tonic::LogIfError(result));
166 
167  // Set up the 'scheduleImmediate' closure.
168  Dart_Handle schedule_immediate_closure;
169  if (service_isolate) {
170  // Running on dart::ThreadPool.
171  schedule_immediate_closure = Dart_Invoke(
172  isolate_lib, ToDart("_getIsolateScheduleImmediateClosure"), 0, nullptr);
173  } else {
174  // Running on async::Loop.
175  schedule_immediate_closure = Dart_Invoke(
176  builtin_lib, ToDart("_getScheduleMicrotaskClosure"), 0, nullptr);
177  }
178  FML_CHECK(!tonic::LogIfError(schedule_immediate_closure));
179 
180  Dart_Handle schedule_args[1];
181  schedule_args[0] = schedule_immediate_closure;
182  result = Dart_Invoke(async_lib, ToDart("_setScheduleImmediateClosure"), 1,
183  schedule_args);
184  FML_CHECK(!tonic::LogIfError(result));
185 
186  // Set up the namespace in dart:io.
187  Dart_Handle namespace_type =
188  Dart_GetType(io_lib, ToDart("_Namespace"), 0, nullptr);
189  FML_CHECK(!tonic::LogIfError(namespace_type));
190 
191  Dart_Handle namespace_args[1];
192  namespace_args[0] = ToDart(reinterpret_cast<intptr_t>(namespc));
193  result =
194  Dart_Invoke(namespace_type, ToDart("_setupNamespace"), 1, namespace_args);
195  FML_CHECK(!tonic::LogIfError(result));
196 
197  // Set up the namespace in dart:zircon.
198  namespace_type = Dart_GetType(zircon_lib, ToDart("_Namespace"), 0, nullptr);
199  FML_CHECK(!tonic::LogIfError(namespace_type));
200 
201  result = Dart_SetField(namespace_type, ToDart("_namespace"),
202  ToDart(reinterpret_cast<intptr_t>(namespc)));
203  FML_CHECK(!tonic::LogIfError(result));
204 
205  // Set up stdout and stderr.
206  Dart_Handle stdio_args[3];
207  stdio_args[0] = Dart_NewInteger(0);
208  stdio_args[1] = Dart_NewInteger(stdoutfd);
209  stdio_args[2] = Dart_NewInteger(stderrfd);
210  result = Dart_Invoke(io_lib, ToDart("_setStdioFDs"), 3, stdio_args);
211  FML_CHECK(!tonic::LogIfError(result));
212 
213  // Disable some dart:io operations.
214  Dart_Handle embedder_config_type =
215  Dart_GetType(io_lib, ToDart("_EmbedderConfig"), 0, nullptr);
216  FML_CHECK(!tonic::LogIfError(embedder_config_type));
217 
218  result =
219  Dart_SetField(embedder_config_type, ToDart("_mayExit"), Dart_False());
220  FML_CHECK(!tonic::LogIfError(result));
221 
222  // Set the script location.
223  result = Dart_SetField(builtin_lib, ToDart("_rawScript"), ToDart(script_uri));
224  FML_CHECK(!tonic::LogIfError(result));
225 
226  // Setup the uriBase with the base uri of the fidl app.
227  Dart_Handle uri_base =
228  Dart_Invoke(io_lib, ToDart("_getUriBaseClosure"), 0, nullptr);
229  FML_CHECK(!tonic::LogIfError(uri_base));
230 
231  result = Dart_SetField(core_lib, ToDart("_uriBaseClosure"), uri_base);
232  FML_CHECK(!tonic::LogIfError(result));
233 
234  Dart_Handle setup_hooks = ToDart("_setupHooks");
235  result = Dart_Invoke(builtin_lib, setup_hooks, 0, nullptr);
236  FML_CHECK(!tonic::LogIfError(result));
237  result = Dart_Invoke(io_lib, setup_hooks, 0, nullptr);
238  FML_CHECK(!tonic::LogIfError(result));
239  result = Dart_Invoke(isolate_lib, setup_hooks, 0, nullptr);
240  FML_CHECK(!tonic::LogIfError(result));
241 }
void Initialize(fidl::InterfaceHandle< fuchsia::sys::Environment > environment, zx::channel directory_request, std::optional< zx::eventpair > view_ref)
Definition: fuchsia.cc:103
#define FML_CHECK(condition)
Definition: logging.h:68
Dart_Handle ToDart(const T &object)
bool LogIfError(Dart_Handle handle)
Definition: dart_error.cc:15

Variable Documentation

◆ isolate_snapshot_buffer

uint8_t const* const dart_runner::isolate_snapshot_buffer

◆ kDataKey

constexpr char dart_runner::kDataKey[] = "data"

Definition at line 46 of file dart_component_controller.cc.

◆ kIdleNotifyDuration

const zx::duration dart_runner::kIdleNotifyDuration = zx::msec(500)

Definition at line 466 of file dart_component_controller.cc.

◆ kIdleSlack

const zx::duration dart_runner::kIdleSlack = zx::sec(1)

Definition at line 467 of file dart_component_controller.cc.

◆ kIdleWaitDuration

const zx::duration dart_runner::kIdleWaitDuration = zx::sec(2)

Definition at line 465 of file dart_component_controller.cc.

◆ kServiceRootPath

constexpr char dart_runner::kServiceRootPath[] = "/svc"

◆ kTmpPath

constexpr char dart_runner::kTmpPath[] = "/tmp"

◆ vm_isolate_snapshot_buffer

uint8_t const* const dart_runner::vm_isolate_snapshot_buffer