Flutter Engine Uber Docs
Docs for the entire Flutter Engine repo.
 
Loading...
Searching...
No Matches
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
6
10#include "third_party/dart/runtime/include/bin/dart_io_api.h"
16
17namespace dart_runner {
18namespace {
19
20dart_utils::ElfSnapshot elf_snapshot; // AOT snapshot
21dart_utils::MappedResource mapped_isolate_snapshot_data; // JIT snapshot
23 mapped_isolate_snapshot_instructions; // JIT snapshot
24tonic::DartLibraryNatives* service_natives = nullptr;
25
26Dart_NativeFunction GetNativeFunction(Dart_Handle name,
28 bool* auto_setup_scope) {
29 FML_CHECK(service_natives);
30 return service_natives->GetNativeFunction(name, argument_count,
31 auto_setup_scope);
32}
33
34const uint8_t* GetSymbol(Dart_NativeFunction native_function) {
35 FML_CHECK(service_natives);
36 return service_natives->GetSymbol(native_function);
37}
38
39#define SHUTDOWN_ON_ERROR(handle) \
40 if (Dart_IsError(handle)) { \
41 *error = strdup(Dart_GetError(handle)); \
42 FML_LOG(ERROR) << error; \
43 Dart_ExitScope(); \
44 Dart_ShutdownIsolate(); \
45 return nullptr; \
46 }
47
48void NotifyServerState(Dart_NativeArguments args) {
49 // NOP.
50}
51
52void Shutdown(Dart_NativeArguments args) {
53 // NOP.
54}
55
56void EmbedderInformationCallback(Dart_EmbedderInformation* info) {
57 info->version = DART_EMBEDDER_INFORMATION_CURRENT_VERSION;
58 info->name = "dart_runner";
59 info->current_rss = -1;
60 info->max_rss = -1;
61
62 zx_info_task_stats_t task_stats;
63 zx_handle_t process = zx_process_self();
64 zx_status_t status = zx_object_get_info(
65 process, ZX_INFO_TASK_STATS, &task_stats, sizeof(task_stats), NULL, NULL);
66 if (status == ZX_OK) {
67 info->current_rss =
68 task_stats.mem_private_bytes + task_stats.mem_shared_bytes;
69 }
70}
71
72} // namespace
73
75 const char* uri,
76 Dart_IsolateFlags* flags_unused, // These flags are currently unused
77 char** error) {
78 Dart_SetEmbedderInformationCallback(EmbedderInformationCallback);
79
80 const uint8_t *vmservice_data = nullptr, *vmservice_instructions = nullptr;
81
82#if defined(AOT_RUNTIME)
83 // The VM service was compiled as a separate app.
84 const char* snapshot_path = "/pkg/data/vmservice_snapshot.so";
85 if (elf_snapshot.Load(nullptr, snapshot_path)) {
86 vmservice_data = elf_snapshot.IsolateData();
87 vmservice_instructions = elf_snapshot.IsolateInstrs();
88 if (vmservice_data == nullptr || vmservice_instructions == nullptr) {
89 return nullptr;
90 }
91 } else {
92 // The VM service was compiled as a separate app.
93 const char* snapshot_data_path =
94 "/pkg/data/vmservice_isolate_snapshot_data.bin";
95 const char* snapshot_instructions_path =
96 "/pkg/data/vmservice_isolate_snapshot_instructions.bin";
97#else
98 // The VM service is embedded in the core snapshot.
99 const char* snapshot_data_path = "/pkg/data/isolate_core_snapshot_data.bin";
100 const char* snapshot_instructions_path =
101 "/pkg/data/isolate_core_snapshot_instructions.bin";
102#endif
103
105 nullptr, snapshot_data_path, mapped_isolate_snapshot_data)) {
106 *error = strdup("Failed to load snapshot for service isolate");
107 FML_LOG(ERROR) << *error;
108 return nullptr;
109 }
111 nullptr, snapshot_instructions_path,
112 mapped_isolate_snapshot_instructions, true /* executable */)) {
113 *error = strdup("Failed to load snapshot for service isolate");
114 FML_LOG(ERROR) << *error;
115 return nullptr;
116 }
117
118 vmservice_data = mapped_isolate_snapshot_data.address();
119 vmservice_instructions = mapped_isolate_snapshot_instructions.address();
120#if defined(AOT_RUNTIME)
121 }
122#endif
123
124 Dart_IsolateFlags flags;
125 Dart_IsolateFlagsInitialize(&flags);
126 flags.null_safety = true;
127
128 auto state = new std::shared_ptr<tonic::DartState>(new tonic::DartState());
129 Dart_Isolate isolate = Dart_CreateIsolateGroup(
130 uri, DART_VM_SERVICE_ISOLATE_NAME, vmservice_data, vmservice_instructions,
131 &flags, state, state, error);
132 if (!isolate) {
133 FML_LOG(ERROR) << "Dart_CreateIsolateGroup failed: " << *error;
134 return nullptr;
135 }
136
137 state->get()->SetIsolate(isolate);
138
139 // Setup native entries.
140 service_natives = new tonic::DartLibraryNatives();
141 service_natives->Register({
142 {"VMServiceIO_NotifyServerState", NotifyServerState, 1, true},
143 {"VMServiceIO_Shutdown", Shutdown, 0, true},
144 });
145
146 Dart_EnterScope();
147
148 Dart_Handle library =
149 Dart_LookupLibrary(Dart_NewStringFromCString("dart:vmservice_io"));
150 SHUTDOWN_ON_ERROR(library);
151 Dart_Handle result = Dart_SetRootLibrary(library);
152 SHUTDOWN_ON_ERROR(result);
153 result = Dart_SetNativeResolver(library, GetNativeFunction, GetSymbol);
154 SHUTDOWN_ON_ERROR(result);
155
156 // _ip = '127.0.0.1'
157 result = Dart_SetField(library, Dart_NewStringFromCString("_ip"),
158 Dart_NewStringFromCString("127.0.0.1"));
159 SHUTDOWN_ON_ERROR(result);
160
161 // _port = 0
162 result = Dart_SetField(library, Dart_NewStringFromCString("_port"),
163 Dart_NewInteger(0));
164 SHUTDOWN_ON_ERROR(result);
165
166 // _autoStart = true
167 result = Dart_SetField(library, Dart_NewStringFromCString("_autoStart"),
168 Dart_NewBoolean(true));
169 SHUTDOWN_ON_ERROR(result);
170
171 // _originCheckDisabled = false
172 result =
173 Dart_SetField(library, Dart_NewStringFromCString("_originCheckDisabled"),
174 Dart_NewBoolean(false));
175 SHUTDOWN_ON_ERROR(result);
176
177 // _authCodesDisabled = false
178 result =
179 Dart_SetField(library, Dart_NewStringFromCString("_authCodesDisabled"),
180 Dart_NewBoolean(false));
181 SHUTDOWN_ON_ERROR(result);
182
183 InitBuiltinLibrariesForIsolate(std::string(uri), nullptr, fileno(stdout),
184 fileno(stderr), zx::channel(), true);
185
186 // Make runnable.
187 Dart_ExitScope();
188 Dart_ExitIsolate();
189 *error = Dart_IsolateMakeRunnable(isolate);
190 if (*error != nullptr) {
191 FML_LOG(ERROR) << *error;
192 Dart_EnterIsolate(isolate);
193 Dart_ShutdownIsolate();
194 return nullptr;
195 }
196 return isolate;
197} // namespace dart_runner
198
199} // namespace dart_runner
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)
#define SHUTDOWN_ON_ERROR(handle)
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
#define FML_CHECK(condition)
Definition logging.h:104
const char * name
Definition fuchsia.cc:50
int argument_count
Definition fuchsia.cc:52
void InitBuiltinLibrariesForIsolate(const std::string &script_uri, fdio_ns_t *namespc, int stdoutfd, int stderrfd, zx::channel directory_request, bool service_isolate)
Dart_Isolate CreateServiceIsolate(const char *uri, Dart_IsolateFlags *flags_unused, char **error)