Flutter Engine
The Flutter Engine
service_isolate.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 "service_isolate.h"
6
7#include "flutter/fml/logging.h"
8#include "third_party/dart/runtime/include/bin/dart_io_api.h"
14
15#include "builtin_libraries.h"
17
18namespace dart_runner {
19namespace {
20
21dart_utils::ElfSnapshot elf_snapshot; // AOT snapshot
22dart_utils::MappedResource mapped_isolate_snapshot_data; // JIT snapshot
24 mapped_isolate_snapshot_instructions; // JIT snapshot
25tonic::DartLibraryNatives* service_natives = nullptr;
26
27Dart_NativeFunction GetNativeFunction(Dart_Handle name,
29 bool* auto_setup_scope) {
30 FML_CHECK(service_natives);
31 return service_natives->GetNativeFunction(name, argument_count,
32 auto_setup_scope);
33}
34
35const uint8_t* GetSymbol(Dart_NativeFunction native_function) {
36 FML_CHECK(service_natives);
37 return service_natives->GetSymbol(native_function);
38}
39
40#define SHUTDOWN_ON_ERROR(handle) \
41 if (Dart_IsError(handle)) { \
42 *error = strdup(Dart_GetError(handle)); \
43 FML_LOG(ERROR) << error; \
44 Dart_ExitScope(); \
45 Dart_ShutdownIsolate(); \
46 return nullptr; \
47 }
48
50 // NOP.
51}
52
54 // NOP.
55}
56
59 info->name = "dart_runner";
60 info->current_rss = -1;
61 info->max_rss = -1;
62
63 zx_info_task_stats_t task_stats;
64 zx_handle_t process = zx_process_self();
65 zx_status_t status = zx_object_get_info(
66 process, ZX_INFO_TASK_STATS, &task_stats, sizeof(task_stats), NULL, NULL);
67 if (status == ZX_OK) {
68 info->current_rss =
69 task_stats.mem_private_bytes + task_stats.mem_shared_bytes;
70 }
71}
72
73} // namespace
74
76 const char* uri,
77 Dart_IsolateFlags* flags_unused, // These flags are currently unused
78 char** error) {
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 FML_LOG(ERROR) << *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 FML_LOG(ERROR) << *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
127 flags.null_safety = true;
128
129 auto state = new std::shared_ptr<tonic::DartState>(new tonic::DartState());
131 uri, DART_VM_SERVICE_ISOLATE_NAME, vmservice_data, vmservice_instructions,
132 &flags, state, state, error);
133 if (!isolate) {
134 FML_LOG(ERROR) << "Dart_CreateIsolateGroup failed: " << *error;
135 return nullptr;
136 }
137
138 state->get()->SetIsolate(isolate);
139
140 // Setup native entries.
141 service_natives = new tonic::DartLibraryNatives();
142 service_natives->Register({
143 {"VMServiceIO_NotifyServerState", NotifyServerState, 1, true},
144 {"VMServiceIO_Shutdown", Shutdown, 0, true},
145 });
146
148
149 Dart_Handle library =
150 Dart_LookupLibrary(Dart_NewStringFromCString("dart:vmservice_io"));
151 SHUTDOWN_ON_ERROR(library);
154 result = Dart_SetNativeResolver(library, GetNativeFunction, GetSymbol);
156
157 // _ip = '127.0.0.1'
159 Dart_NewStringFromCString("127.0.0.1"));
161
162 // _port = 0
164 Dart_NewInteger(0));
166
167 // _autoStart = true
168 result = Dart_SetField(library, Dart_NewStringFromCString("_autoStart"),
169 Dart_NewBoolean(true));
171
172 // _originCheckDisabled = false
173 result =
174 Dart_SetField(library, Dart_NewStringFromCString("_originCheckDisabled"),
175 Dart_NewBoolean(false));
177
178 // _authCodesDisabled = false
179 result =
180 Dart_SetField(library, Dart_NewStringFromCString("_authCodesDisabled"),
181 Dart_NewBoolean(false));
183
184 InitBuiltinLibrariesForIsolate(std::string(uri), nullptr, fileno(stdout),
185 fileno(stderr), zx::channel(), true);
186
187 // Make runnable.
191 if (*error != nullptr) {
192 FML_LOG(ERROR) << *error;
193 Dart_EnterIsolate(isolate);
195 return nullptr;
196 }
197 return isolate;
198} // namespace dart_runner
199
201 dart_utils::MappedResource vm_service_tar;
203 nullptr, "/pkg/data/observatory.tar", vm_service_tar)) {
204 FML_LOG(ERROR) << "Failed to load Observatory assets";
205 return nullptr;
206 }
207 // TODO(rmacnak): Should we avoid copying the tar? Or does the service
208 // library not hold onto it anyway?
210 reinterpret_cast<const uint8_t*>(vm_service_tar.address()),
211 vm_service_tar.size());
212}
213
214} // namespace dart_runner
static void info(const char *fmt,...) SK_PRINTF_LIKE(1
Definition: DM.cpp:213
bool Load(fdio_ns_t *namespc, const std::string &path)
const uint8_t * IsolateData() const
const uint8_t * IsolateInstrs() const
static bool LoadFromNamespace(fdio_ns_t *namespc, const std::string &path, MappedResource &resource, bool executable=false)
const uint8_t * address() const
void Register(std::initializer_list< Entry > entries)
const uint8_t * GetSymbol(Dart_NativeFunction native_function)
Dart_NativeFunction GetNativeFunction(Dart_Handle name, int argument_count, bool *auto_setup_scope)
DART_EXPORT Dart_Isolate Dart_CreateIsolateGroup(const char *script_uri, const char *name, const uint8_t *isolate_snapshot_data, const uint8_t *isolate_snapshot_instructions, Dart_IsolateFlags *flags, void *isolate_group_data, void *isolate_data, char **error)
DART_EXPORT void Dart_IsolateFlagsInitialize(Dart_IsolateFlags *flags)
DART_EXPORT DART_WARN_UNUSED_RESULT char * Dart_IsolateMakeRunnable(Dart_Isolate isolate)
DART_EXPORT Dart_Handle Dart_SetNativeResolver(Dart_Handle library, Dart_NativeEntryResolver resolver, Dart_NativeEntrySymbol symbol)
DART_EXPORT void Dart_EnterScope(void)
DART_EXPORT void Dart_ExitScope(void)
DART_EXPORT void Dart_ShutdownIsolate(void)
struct _Dart_Handle * Dart_Handle
Definition: dart_api.h:258
struct _Dart_Isolate * Dart_Isolate
Definition: dart_api.h:88
DART_EXPORT Dart_Handle Dart_LookupLibrary(Dart_Handle url)
struct _Dart_NativeArguments * Dart_NativeArguments
Definition: dart_api.h:3019
DART_EXPORT DART_WARN_UNUSED_RESULT Dart_Handle Dart_SetField(Dart_Handle container, Dart_Handle name, Dart_Handle value)
DART_EXPORT Dart_Handle Dart_NewStringFromCString(const char *str)
void(* Dart_NativeFunction)(Dart_NativeArguments arguments)
Definition: dart_api.h:3207
DART_EXPORT Dart_Handle Dart_NewInteger(int64_t value)
DART_EXPORT Dart_Handle Dart_NewBoolean(bool value)
DART_EXPORT void Dart_EnterIsolate(Dart_Isolate isolate)
#define DART_VM_SERVICE_ISOLATE_NAME
Definition: dart_api.h:3888
DART_EXPORT void Dart_ExitIsolate(void)
DART_EXPORT Dart_Handle Dart_SetRootLibrary(Dart_Handle library)
DART_EXPORT void Dart_SetEmbedderInformationCallback(Dart_EmbedderInformationCallback callback)
#define DART_EMBEDDER_INFORMATION_CURRENT_VERSION
AtkStateType state
FlutterSemanticsFlag flags
G_BEGIN_DECLS G_MODULE_EXPORT FlValue * args
const uint8_t uint32_t uint32_t GError ** error
GAsyncResult * result
#define FML_LOG(severity)
Definition: logging.h:82
#define FML_CHECK(condition)
Definition: logging.h:85
int argument_count
Definition: fuchsia.cc:52
static void Shutdown(Dart_NativeArguments args)
void NotifyServerState(Dart_NativeArguments args)
void InitBuiltinLibrariesForIsolate(const std::string &script_uri, fdio_ns_t *namespc, int stdoutfd, int stderrfd, zx::channel directory_request, bool service_isolate)
Dart_Handle GetVMServiceAssetsArchiveCallback()
Dart_Isolate CreateServiceIsolate(const char *uri, Dart_IsolateFlags *flags_unused, char **error)
DEF_SWITCHES_START aot vmservice shared library name
Definition: switches.h:32
static void EmbedderInformationCallback(Dart_EmbedderInformation *info)
Definition: dart_vm.cc:253
char * strdup(const char *str1)
Dart_Handle ToDart(const T &object)
#define SHUTDOWN_ON_ERROR(handle)
static void process(const char *inPath, const char *lexer, const char *token, const char *hPath, const char *cppPath)
Definition: Main.cpp:114
#define ERROR(message)
Definition: elf_loader.cc:260