From 59a29682dc8f6956a548f34f2ad854c25d9c381e Mon Sep 17 00:00:00 2001 From: Daco Harkes Date: Mon, 17 Jun 2024 18:16:06 +0000 Subject: [PATCH 1/3] Setup `NativeAssetsApi` during isolate group creation --- runtime/BUILD.gn | 1 + runtime/dart_isolate.cc | 29 +++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/runtime/BUILD.gn b/runtime/BUILD.gn index 1ed968a4d62d4..80e5c372a4062 100644 --- a/runtime/BUILD.gn +++ b/runtime/BUILD.gn @@ -107,6 +107,7 @@ source_set("runtime") { ":test_font", "$dart_src/runtime:dart_api", "$dart_src/runtime/bin:dart_io_api", + "$dart_src/runtime/bin:native_assets_api", "//flutter/assets", "//flutter/common", "//flutter/flow", diff --git a/runtime/dart_isolate.cc b/runtime/dart_isolate.cc index 9d063bedbe35a..d37c9e14f8848 100644 --- a/runtime/dart_isolate.cc +++ b/runtime/dart_isolate.cc @@ -25,6 +25,7 @@ #include "fml/message_loop_task_queues.h" #include "fml/task_source.h" #include "fml/time/time_point.h" +#include "third_party/dart/runtime/include/bin/native_assets_api.h" #include "third_party/dart/runtime/include/dart_api.h" #include "third_party/dart/runtime/include/dart_tools_api.h" #include "third_party/tonic/converter/dart_converter.h" @@ -641,6 +642,32 @@ bool DartIsolate::UpdateThreadPoolNames() const { return true; } +static void* NativeAssetsDlopenRelative(const char* path, char** error) { + auto* isolate_group_data = + static_cast*>( + Dart_CurrentIsolateGroupData()); + const std::string& script_uri = (*isolate_group_data)->GetAdvisoryScriptURI(); + return dart::bin::NativeAssets::DlopenRelative(path, script_uri.data(), + error); +} + +// This code can't live as DartFFI::InitForIsolateGroup in +// lib/ffi/native_assets.cc with target "//flutter/lib/ffi:ffi", because it +// would have a cyclic import with "//flutter/runtime:runtime" by using +// DartIsolateGroupData. Because of the function pointers, it also cannot +// capture the script_uri in a lambda. +static void InitDartFFIForIsolateGroup() { + NativeAssetsApi native_assets; + memset(&native_assets, 0, sizeof(native_assets)); + native_assets.dlopen_absolute = &dart::bin::NativeAssets::DlopenAbsolute; + native_assets.dlopen_relative = &NativeAssetsDlopenRelative; + native_assets.dlopen_system = &dart::bin::NativeAssets::DlopenSystem; + native_assets.dlopen_executable = &dart::bin::NativeAssets::DlopenExecutable; + native_assets.dlopen_process = &dart::bin::NativeAssets::DlopenProcess; + native_assets.dlsym = &dart::bin::NativeAssets::Dlsym; + Dart_InitializeNativeAssetsResolver(&native_assets); +}; + bool DartIsolate::LoadLibraries() { TRACE_EVENT0("flutter", "DartIsolate::LoadLibraries"); if (phase_ != Phase::Initialized) { @@ -652,6 +679,8 @@ bool DartIsolate::LoadLibraries() { DartIO::InitForIsolate(may_insecurely_connect_to_all_domains_, domain_network_policy_); + InitDartFFIForIsolateGroup(); + DartUI::InitForIsolate(GetIsolateGroupData().GetSettings()); const bool is_service_isolate = Dart_IsServiceIsolate(isolate()); From c3b13decc53eed89e3e3c4ab612c37d5b676cec1 Mon Sep 17 00:00:00 2001 From: Daco Harkes Date: Mon, 17 Jun 2024 18:28:27 +0000 Subject: [PATCH 2/3] Move init to create isolate group --- runtime/dart_isolate.cc | 56 ++++++++++++++++++++--------------------- 1 file changed, 28 insertions(+), 28 deletions(-) diff --git a/runtime/dart_isolate.cc b/runtime/dart_isolate.cc index d37c9e14f8848..d5258558556ea 100644 --- a/runtime/dart_isolate.cc +++ b/runtime/dart_isolate.cc @@ -642,32 +642,6 @@ bool DartIsolate::UpdateThreadPoolNames() const { return true; } -static void* NativeAssetsDlopenRelative(const char* path, char** error) { - auto* isolate_group_data = - static_cast*>( - Dart_CurrentIsolateGroupData()); - const std::string& script_uri = (*isolate_group_data)->GetAdvisoryScriptURI(); - return dart::bin::NativeAssets::DlopenRelative(path, script_uri.data(), - error); -} - -// This code can't live as DartFFI::InitForIsolateGroup in -// lib/ffi/native_assets.cc with target "//flutter/lib/ffi:ffi", because it -// would have a cyclic import with "//flutter/runtime:runtime" by using -// DartIsolateGroupData. Because of the function pointers, it also cannot -// capture the script_uri in a lambda. -static void InitDartFFIForIsolateGroup() { - NativeAssetsApi native_assets; - memset(&native_assets, 0, sizeof(native_assets)); - native_assets.dlopen_absolute = &dart::bin::NativeAssets::DlopenAbsolute; - native_assets.dlopen_relative = &NativeAssetsDlopenRelative; - native_assets.dlopen_system = &dart::bin::NativeAssets::DlopenSystem; - native_assets.dlopen_executable = &dart::bin::NativeAssets::DlopenExecutable; - native_assets.dlopen_process = &dart::bin::NativeAssets::DlopenProcess; - native_assets.dlsym = &dart::bin::NativeAssets::Dlsym; - Dart_InitializeNativeAssetsResolver(&native_assets); -}; - bool DartIsolate::LoadLibraries() { TRACE_EVENT0("flutter", "DartIsolate::LoadLibraries"); if (phase_ != Phase::Initialized) { @@ -679,8 +653,6 @@ bool DartIsolate::LoadLibraries() { DartIO::InitForIsolate(may_insecurely_connect_to_all_domains_, domain_network_policy_); - InitDartFFIForIsolateGroup(); - DartUI::InitForIsolate(GetIsolateGroupData().GetSettings()); const bool is_service_isolate = Dart_IsServiceIsolate(isolate()); @@ -1194,6 +1166,32 @@ bool DartIsolate::DartIsolateInitializeCallback(void** child_callback_data, return true; } +static void* NativeAssetsDlopenRelative(const char* path, char** error) { + auto* isolate_group_data = + static_cast*>( + Dart_CurrentIsolateGroupData()); + const std::string& script_uri = (*isolate_group_data)->GetAdvisoryScriptURI(); + return dart::bin::NativeAssets::DlopenRelative(path, script_uri.data(), + error); +} + +// This code can't live as DartFFI::InitForIsolateGroup in +// lib/ffi/native_assets.cc with target "//flutter/lib/ffi:ffi", because it +// would have a cyclic import with "//flutter/runtime:runtime" by using +// DartIsolateGroupData. Because of the function pointers, it also cannot +// capture the script_uri in a lambda. +static void InitDartFFIForIsolateGroup() { + NativeAssetsApi native_assets; + memset(&native_assets, 0, sizeof(native_assets)); + native_assets.dlopen_absolute = &dart::bin::NativeAssets::DlopenAbsolute; + native_assets.dlopen_relative = &NativeAssetsDlopenRelative; + native_assets.dlopen_system = &dart::bin::NativeAssets::DlopenSystem; + native_assets.dlopen_executable = &dart::bin::NativeAssets::DlopenExecutable; + native_assets.dlopen_process = &dart::bin::NativeAssets::DlopenProcess; + native_assets.dlsym = &dart::bin::NativeAssets::Dlsym; + Dart_InitializeNativeAssetsResolver(&native_assets); +}; + Dart_Isolate DartIsolate::CreateDartIsolateGroup( std::unique_ptr> isolate_group_data, std::unique_ptr> isolate_data, @@ -1221,6 +1219,8 @@ Dart_Isolate DartIsolate::CreateDartIsolateGroup( // NOLINTEND(clang-analyzer-cplusplus.NewDeleteLeaks) success = InitializeIsolate(embedder_isolate, isolate, error); + + InitDartFFIForIsolateGroup(); } if (!success) { Dart_ShutdownIsolate(); From 6ce7d47bbcd5de4bebb8101038a28605a1300229 Mon Sep 17 00:00:00 2001 From: Daco Harkes Date: Mon, 17 Jun 2024 20:17:59 +0000 Subject: [PATCH 3/3] address comments --- runtime/dart_isolate.cc | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/runtime/dart_isolate.cc b/runtime/dart_isolate.cc index d5258558556ea..3dc8403506f7f 100644 --- a/runtime/dart_isolate.cc +++ b/runtime/dart_isolate.cc @@ -1175,11 +1175,6 @@ static void* NativeAssetsDlopenRelative(const char* path, char** error) { error); } -// This code can't live as DartFFI::InitForIsolateGroup in -// lib/ffi/native_assets.cc with target "//flutter/lib/ffi:ffi", because it -// would have a cyclic import with "//flutter/runtime:runtime" by using -// DartIsolateGroupData. Because of the function pointers, it also cannot -// capture the script_uri in a lambda. static void InitDartFFIForIsolateGroup() { NativeAssetsApi native_assets; memset(&native_assets, 0, sizeof(native_assets)); @@ -1218,9 +1213,9 @@ Dart_Isolate DartIsolate::CreateDartIsolateGroup( isolate_data.release(); // NOLINTEND(clang-analyzer-cplusplus.NewDeleteLeaks) - success = InitializeIsolate(embedder_isolate, isolate, error); - InitDartFFIForIsolateGroup(); + + success = InitializeIsolate(embedder_isolate, isolate, error); } if (!success) { Dart_ShutdownIsolate();