9#include <fuchsia/io/cpp/fidl.h>
10#include <lib/fdio/directory.h>
11#include <lib/fdio/fd.h>
12#include <lib/fdio/io.h>
13#include <lib/fdio/limits.h>
14#include <lib/fdio/namespace.h>
15#include <lib/zx/channel.h>
18#include <zircon/process.h>
19#include <zircon/processargs.h>
21#include "flutter/fml/unique_fd.h"
32constexpr char kGetSizeResult[] =
"GetSizeResult";
33constexpr char kHandlePairResult[] =
"HandlePairResult";
34constexpr char kHandleResult[] =
"HandleResult";
35constexpr char kReadResult[] =
"ReadResult";
36constexpr char kHandleInfo[] =
"HandleInfo";
37constexpr char kReadEtcResult[] =
"ReadEtcResult";
38constexpr char kWriteResult[] =
"WriteResult";
39constexpr char kFromFileResult[] =
"FromFileResult";
40constexpr char kMapResult[] =
"MapResult";
44 explicit ByteDataScope(
Dart_Handle dart_handle) : dart_handle_(dart_handle) {
48 explicit ByteDataScope(
size_t size) {
61 void*
data()
const {
return data_; }
62 size_t size()
const {
return size_; }
63 Dart_Handle dart_handle()
const {
return dart_handle_; }
64 bool is_valid()
const {
return is_valid_; }
95 bool is_valid_ =
false;
97 void* data_ =
nullptr;
100Dart_Handle MakeHandleList(
const std::vector<zx_handle_t>& in_handles) {
108 for (
size_t i = 0;
i < in_handles.size();
i++) {
117template <
class... Args>
128 std::array<
Dart_Handle,
sizeof...(Args)> args_array{
129 {std::forward<Args>(
args)...}};
137 const std::vector<zx_handle_info_t>& in_handles) {
141 Dart_Handle empty_handle_info = ConstructDartObject(
144 handle_info_type, empty_handle_info, in_handles.size());
147 for (
size_t i = 0;
i < in_handles.size();
i++) {
151 ConstructDartObject(kHandleInfo, handle,
ToDart(in_handles[
i].
type),
152 ToDart(in_handles[
i].rights)));
159fdio_ns_t* GetNamespace() {
169 uint64_t fdio_ns_ptr;
173 return reinterpret_cast<fdio_ns_t*
>(fdio_ns_ptr);
178 if (!dir_fd.is_valid()) {
182 if (
path !=
nullptr && *
path ==
'/') {
186 if (zx_status_t status = fdio_open_fd_at(
188 static_cast<uint32_t
>(fuchsia::io::OpenFlags::RIGHT_READABLE),
202 zx_handle_t out0 = 0, out1 = 0;
203 zx_status_t status = zx_channel_create(
options, &out0, &out1);
204 if (status != ZX_OK) {
205 return ConstructDartObject(kHandlePairResult,
ToDart(status));
207 return ConstructDartObject(kHandlePairResult,
ToDart(status),
215 return fdio_ns_service_connect(GetNamespace(),
path.c_str(),
216 channel->ReleaseHandle());
221 if (zx_status_t status = FdFromPath(
path.c_str(), fd); status != ZX_OK) {
222 return ConstructDartObject(kHandleResult,
ToDart(status));
226 if (zx_status_t status =
227 fdio_fd_transfer(fd.
release(), handle.reset_and_get_address());
229 return ConstructDartObject(kHandleResult,
ToDart(status));
231 zx_info_handle_basic_t
info;
232 if (zx_status_t status = handle.get_info(ZX_INFO_HANDLE_BASIC, &
info,
233 sizeof(
info),
nullptr,
nullptr);
235 return ConstructDartObject(kHandleResult,
ToDart(status));
237 if (
info.type != ZX_OBJ_TYPE_CHANNEL) {
238 return ConstructDartObject(kHandleResult,
ToDart(ZX_ERR_WRONG_TYPE));
241 return ConstructDartObject(kHandleResult,
ToDart(ZX_OK),
247 std::vector<Handle*> handles) {
248 if (!channel || !channel->is_valid()) {
250 return ZX_ERR_BAD_HANDLE;
253 std::vector<zx_handle_t> zx_handles;
254 for (
Handle* handle : handles) {
255 zx_handles.push_back(handle->handle());
258 zx_status_t status = zx_channel_write(channel->handle(), 0,
data.data(),
259 data.length_in_bytes(),
260 zx_handles.data(), zx_handles.size());
262 for (
Handle* handle : handles) {
263 handle->ReleaseHandle();
273 std::vector<HandleDisposition*> handle_dispositions) {
274 if (!channel || !channel->is_valid()) {
276 return ZX_ERR_BAD_HANDLE;
279 std::vector<zx_handle_disposition_t> zx_handle_dispositions;
282 zx_handle_dispositions.push_back({.operation = handle->operation(),
283 .handle = handle->handle()->handle(),
284 .type = handle->type(),
285 .rights = handle->rights(),
289 zx_status_t status = zx_channel_write_etc(
290 channel->handle(), 0,
data.data(),
data.length_in_bytes(),
291 zx_handle_dispositions.data(), zx_handle_dispositions.size());
293 for (
size_t i = 0;
i < handle_dispositions.size(); ++
i) {
294 handle_dispositions[
i]->set_result(zx_handle_dispositions[
i].
result);
297 if (handle_dispositions[
i]->
operation() != ZX_HANDLE_OP_DUPLICATE) {
298 handle_dispositions[
i]->handle()->ReleaseHandle();
307 if (!channel || !channel->is_valid()) {
308 return ConstructDartObject(kReadResult,
ToDart(ZX_ERR_BAD_HANDLE));
311 uint32_t actual_bytes = 0;
312 uint32_t actual_handles = 0;
315 zx_status_t status = zx_channel_read(channel->handle(), 0,
nullptr,
nullptr,
316 0, 0, &actual_bytes, &actual_handles);
317 if (status != ZX_ERR_BUFFER_TOO_SMALL) {
319 return ConstructDartObject(kReadResult,
ToDart(status));
323 ByteDataScope bytes(actual_bytes);
325 std::vector<zx_handle_t> handles(actual_handles);
328 status = zx_channel_read(channel->handle(), 0, bytes.data(), handles.data(),
329 bytes.size(), handles.size(), &actual_bytes,
331 FML_DCHECK(status != ZX_OK || bytes.size() == actual_bytes);
335 if (status == ZX_OK) {
339 return ConstructDartObject(kReadResult,
ToDart(status), bytes.dart_handle(),
340 ToDart(actual_bytes), MakeHandleList(handles));
342 return ConstructDartObject(kReadResult,
ToDart(status));
347 if (!channel || !channel->is_valid()) {
348 return ConstructDartObject(kReadEtcResult,
ToDart(ZX_ERR_BAD_HANDLE));
351 uint32_t actual_bytes = 0;
352 uint32_t actual_handles = 0;
355 zx_status_t status = zx_channel_read(channel->handle(), 0,
nullptr,
nullptr,
356 0, 0, &actual_bytes, &actual_handles);
357 if (status != ZX_ERR_BUFFER_TOO_SMALL) {
359 return ConstructDartObject(kReadEtcResult,
ToDart(status));
363 ByteDataScope bytes(actual_bytes);
365 std::vector<zx_handle_info_t> handles(actual_handles);
368 status = zx_channel_read_etc(channel->handle(), 0, bytes.data(),
369 handles.data(), bytes.size(), handles.size(),
370 &actual_bytes, &actual_handles);
371 FML_DCHECK(status != ZX_OK || bytes.size() == actual_bytes);
375 if (status == ZX_OK) {
379 return ConstructDartObject(kReadEtcResult,
ToDart(status),
380 bytes.dart_handle(),
ToDart(actual_bytes),
381 MakeHandleInfoList(handles));
383 return ConstructDartObject(kReadEtcResult,
ToDart(status));
388 zx_handle_t out0 = 0, out1 = 0;
389 zx_status_t status = zx_eventpair_create(0, &out0, &out1);
390 if (status != ZX_OK) {
391 return ConstructDartObject(kHandlePairResult,
ToDart(status));
393 return ConstructDartObject(kHandlePairResult,
ToDart(status),
400 zx_handle_t out0 = 0, out1 = 0;
401 zx_status_t status = zx_socket_create(
options, &out0, &out1);
402 if (status != ZX_OK) {
403 return ConstructDartObject(kHandlePairResult,
ToDart(status));
405 return ConstructDartObject(kHandlePairResult,
ToDart(status),
414 if (!socket || !socket->is_valid()) {
416 return ConstructDartObject(kWriteResult,
ToDart(ZX_ERR_BAD_HANDLE));
420 zx_status_t status = zx_socket_write(socket->handle(),
options,
data.data(),
421 data.length_in_bytes(), &actual);
423 return ConstructDartObject(kWriteResult,
ToDart(status),
ToDart(actual));
427 if (!socket || !socket->is_valid()) {
428 return ConstructDartObject(kReadResult,
ToDart(ZX_ERR_BAD_HANDLE));
431 ByteDataScope bytes(
size);
434 zx_socket_read(socket->handle(), 0, bytes.data(),
size, &actual);
436 if (status == ZX_OK) {
438 return ConstructDartObject(kReadResult,
ToDart(status), bytes.dart_handle(),
442 return ConstructDartObject(kReadResult,
ToDart(status));
446 zx_handle_t vmo = ZX_HANDLE_INVALID;
447 zx_status_t status = zx_vmo_create(
size,
options, &vmo);
448 if (status != ZX_OK) {
449 return ConstructDartObject(kHandleResult,
ToDart(status));
451 return ConstructDartObject(kHandleResult,
ToDart(status),
458 if (zx_status_t status = FdFromPath(
path.c_str(), fd); status != ZX_OK) {
459 return ConstructDartObject(kHandleResult,
ToDart(status));
462 struct stat stat_struct;
463 if (fstat(fd.
get(), &stat_struct) != 0) {
465 return ConstructDartObject(kFromFileResult,
ToDart(ZX_ERR_IO));
468 if (zx_status_t status =
469 fdio_get_vmo_clone(fd.
get(), vmo.reset_and_get_address());
471 return ConstructDartObject(kFromFileResult,
ToDart(status));
474 return ConstructDartObject(kFromFileResult,
ToDart(ZX_OK),
476 ToDart(stat_struct.st_size));
480 if (!vmo || !vmo->is_valid()) {
481 return ConstructDartObject(kGetSizeResult,
ToDart(ZX_ERR_BAD_HANDLE));
485 zx_status_t status = zx_vmo_get_size(vmo->handle(), &
size);
491 if (!vmo || !vmo->is_valid()) {
492 return ZX_ERR_BAD_HANDLE;
494 return zx_vmo_set_size(vmo->handle(),
size);
500 if (!vmo || !vmo->is_valid()) {
502 return ZX_ERR_BAD_HANDLE;
506 zx_vmo_write(vmo->handle(),
data.data(),
offset,
data.length_in_bytes());
515 if (!vmo || !vmo->is_valid()) {
516 return ConstructDartObject(kReadResult,
ToDart(ZX_ERR_BAD_HANDLE));
520 ByteDataScope bytes(
size);
521 zx_status_t status = zx_vmo_read(vmo->handle(), bytes.data(),
offset,
size);
523 if (status == ZX_OK) {
524 return ConstructDartObject(kReadResult,
ToDart(status), bytes.dart_handle(),
527 return ConstructDartObject(kReadResult,
ToDart(status));
536void System::VmoMapFinalizer(
void* isolate_callback_data,
void* peer) {
538 zx_vmar_unmap(zx_vmar_root_self(),
reinterpret_cast<uintptr_t
>(r->
region),
544 if (!vmo || !vmo->is_valid())
545 return ConstructDartObject(kMapResult,
ToDart(ZX_ERR_BAD_HANDLE));
548 zx_status_t status = zx_vmo_get_size(vmo->handle(), &
size);
550 return ConstructDartObject(kMapResult,
ToDart(status));
552 uintptr_t mapped_addr;
553 status = zx_vmar_map(zx_vmar_root_self(), ZX_VM_PERM_READ, 0, vmo->handle(),
554 0,
size, &mapped_addr);
556 return ConstructDartObject(kMapResult,
ToDart(status));
558 void*
data =
reinterpret_cast<void*
>(mapped_addr);
560 static_cast<intptr_t
>(
size));
565 static_cast<intptr_t
>(
size) +
sizeof(*r),
566 System::VmoMapFinalizer);
568 return ConstructDartObject(kMapResult,
ToDart(ZX_OK),
object);
572 return zx_clock_get_monotonic();
577#define FOR_EACH_STATIC_BINDING(V) \
578 V(System, ChannelCreate) \
579 V(System, ChannelFromFile) \
580 V(System, ChannelWrite) \
581 V(System, ChannelWriteEtc) \
582 V(System, ChannelQueryAndRead) \
583 V(System, ChannelQueryAndReadEtc) \
584 V(System, EventpairCreate) \
585 V(System, ConnectToService) \
586 V(System, SocketCreate) \
587 V(System, SocketWrite) \
588 V(System, SocketRead) \
589 V(System, VmoCreate) \
590 V(System, VmoFromFile) \
591 V(System, VmoGetSize) \
592 V(System, VmoSetSize) \
594 V(System, VmoWrite) \
596 V(System, ClockGetMonotonic)
601#define DART_REGISTER_NATIVE_STATIC_(CLASS, METHOD) \
602 DART_REGISTER_NATIVE_STATIC(CLASS, METHOD),
static void info(const char *fmt,...) SK_PRINTF_LIKE(1
static void operation(T operation, uint32_t &a, uint32_t b, uint32_t c, uint32_t d, uint32_t x, uint8_t s, uint32_t t)
static bool is_valid(SkISize dim)
void reset(const T &value=Traits::InvalidValue())
Dart_PersistentHandle GetClass(const DartWrapperInfo &info)
static DartState * Current()
DartClassLibrary & class_library()
static Dart_Handle CreateInvalid()
static fml::RefPtr< Handle > Create(zx_handle_t handle)
static Dart_Handle ChannelCreate(uint32_t options)
static Dart_Handle SocketCreate(uint32_t options)
static Dart_Handle VmoCreate(uint64_t size, uint32_t options)
static Dart_Handle VmoMap(fml::RefPtr< Handle > vmo)
static zx_status_t VmoWrite(fml::RefPtr< Handle > vmo, uint64_t offset, const tonic::DartByteData &data)
static Dart_Handle SocketWrite(fml::RefPtr< Handle > socket, const tonic::DartByteData &data, int options)
static Dart_Handle VmoRead(fml::RefPtr< Handle > vmo, uint64_t offset, size_t size)
static uint64_t ClockGetMonotonic()
static Dart_Handle VmoFromFile(std::string path)
static zx_status_t ChannelWriteEtc(fml::RefPtr< Handle > channel, const tonic::DartByteData &data, std::vector< HandleDisposition * > handle_dispositions)
static zx_status_t ConnectToService(std::string path, fml::RefPtr< Handle > channel)
static Dart_Handle ChannelQueryAndReadEtc(fml::RefPtr< Handle > channel)
static Dart_Handle EventpairCreate(uint32_t options)
static Dart_Handle ChannelFromFile(std::string path)
static zx_status_t ChannelWrite(fml::RefPtr< Handle > channel, const tonic::DartByteData &data, std::vector< Handle * > handles)
static Dart_Handle VmoGetSize(fml::RefPtr< Handle > vmo)
static Dart_Handle ChannelQueryAndRead(fml::RefPtr< Handle > channel)
static zx_status_t VmoSetSize(fml::RefPtr< Handle > vmo, uint64_t size)
static Dart_Handle SocketRead(fml::RefPtr< Handle > socket, size_t size)
DART_EXPORT Dart_Handle Dart_HandleFromPersistent(Dart_PersistentHandle object)
DART_EXPORT Dart_Handle Dart_GetNonNullableType(Dart_Handle library, Dart_Handle class_name, intptr_t number_of_type_arguments, Dart_Handle *type_arguments)
DART_EXPORT Dart_FinalizableHandle Dart_NewFinalizableHandle(Dart_Handle object, void *peer, intptr_t external_allocation_size, Dart_HandleFinalizer callback)
DART_EXPORT Dart_Handle Dart_NewExternalTypedData(Dart_TypedData_Type type, void *data, intptr_t length)
DART_EXPORT Dart_Handle Dart_NewListOfTypeFilled(Dart_Handle element_type, Dart_Handle fill_object, intptr_t length)
struct _Dart_Handle * Dart_Handle
DART_EXPORT Dart_Handle Dart_TypedDataReleaseData(Dart_Handle object)
DART_EXPORT Dart_Handle Dart_EmptyString(void)
DART_EXPORT Dart_Handle Dart_LookupLibrary(Dart_Handle url)
DART_EXPORT Dart_Handle Dart_TypedDataAcquireData(Dart_Handle object, Dart_TypedData_Type *type, void **data, intptr_t *len)
DART_EXPORT DART_WARN_UNUSED_RESULT Dart_Handle Dart_New(Dart_Handle type, Dart_Handle constructor_name, int number_of_arguments, Dart_Handle *arguments)
@ Dart_TypedData_kByteData
DART_EXPORT Dart_Handle Dart_NewTypedData(Dart_TypedData_Type type, intptr_t length)
DART_EXPORT Dart_Handle Dart_StringToCString(Dart_Handle str, const char **cstr)
DART_EXPORT DART_WARN_UNUSED_RESULT Dart_Handle Dart_GetField(Dart_Handle container, Dart_Handle name)
DART_EXPORT bool Dart_IsError(Dart_Handle handle)
DART_EXPORT Dart_Handle Dart_ToString(Dart_Handle object)
DART_EXPORT Dart_Handle Dart_ListSetAt(Dart_Handle list, intptr_t index, Dart_Handle value)
DART_EXPORT Dart_Handle Dart_IntegerToUint64(Dart_Handle integer, uint64_t *value)
#define DART_NATIVE_CALLBACK_STATIC(CLASS, METHOD)
G_BEGIN_DECLS G_MODULE_EXPORT FlValue * args
#define FML_DCHECK(condition)
const char *const class_name
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
it will be possible to load the file into Perfetto s trace viewer disable asset Prevents usage of any non test fonts unless they were explicitly Loaded via prefetched default font Indicates whether the embedding started a prefetch of the default font manager before creating the engine run In non interactive keep the shell running after the Dart script has completed enable serial On low power devices with low core running concurrent GC tasks on threads can cause them to contend with the UI thread which could potentially lead to jank This option turns off all concurrent GC activities domain network JSON encoded network policy per domain This overrides the DisallowInsecureConnections switch Embedder can specify whether to allow or disallow insecure connections at a domain level old gen heap size
Dart_Handle ToDart(const T &object)
bool CheckAndHandleError(Dart_Handle handle)
IMPLEMENT_WRAPPERTYPEINFO(zircon, Handle)
SizedRegion(void *r, size_t s)
#define FOR_EACH_STATIC_BINDING(V)
#define DART_REGISTER_NATIVE_STATIC_(CLASS, METHOD)
std::shared_ptr< const fml::Mapping > data