[vm] Native asset relative path resolution with symlinks

This CL moves native assets resolution to the embedder.

The Dart VM looks up the asset path (for example
`['relative', 'foo.so']`) with the asset id. The embedder defines
callbacks for asset loading, and returns a handle to the dylib.
Then the VM calls the embedder again with `dlsym` to lookup the symbol.

The Dart VM & kernel compiler are responsible for the asset id to
asset path mapping. The kernel compiler compiles it into the snapshot
and the VM looks it up in the snapshot.

Relative paths are resolved relative to the isolate script uri (kernel
snapshot, jit snapshot, aot snapshot, or `dart compile exe` result).
The embedder is responsible for remembering the script uri it set when
spawning the isolate group.

This CL does not add `dlclose` or `dladdr` embedder callbacks yet.
Bug: https://github.com/dart-lang/sdk/issues/55521
Bug: https://github.com/dart-lang/sdk/issues/55966

TEST=pkg/dartdev/test/native_assets/build_test.dart
TEST=tests/ffi/native_assets/asset_relative_test.dart

Bug: https://github.com/dart-lang/sdk/issues/55410
Bug: https://github.com/dart-lang/sdk/issues/55523
Bug: https://github.com/dart-lang/sdk/issues/55207
Change-Id: I75ec4a368c5fb3d2f76b03771e796ff56bcac941
Cq-Include-Trybots: dart/try:vm-aot-linux-debug-x64-try,vm-aot-linux-debug-x64c-try,vm-aot-mac-release-arm64-try,vm-aot-mac-release-x64-try,vm-aot-obfuscate-linux-release-x64-try,vm-aot-optimization-level-linux-release-x64-try,vm-aot-win-debug-arm64-try,vm-aot-win-debug-x64-try,vm-aot-win-debug-x64c-try,pkg-linux-debug-try,pkg-linux-release-arm64-try,pkg-mac-release-try,pkg-mac-release-arm64-try,pkg-win-release-try,pkg-win-release-arm64-try,vm-aot-asan-linux-release-x64-try,vm-asan-linux-release-x64-try,vm-aot-msan-linux-release-x64-try,vm-msan-linux-release-x64-try
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/361881
Reviewed-by: Martin Kustermann <kustermann@google.com>
Commit-Queue: Daco Harkes <dacoharkes@google.com>
This commit is contained in:
Daco Harkes 2024-06-12 16:45:19 +00:00 committed by Commit Queue
parent 4b2906cdc1
commit 9588927faf
14 changed files with 587 additions and 53 deletions

View file

@ -75,15 +75,25 @@ void main(List<String> args) async {
final relativeExeUri = Uri.file('./bin/dart_app/dart_app.exe');
final absoluteExeUri = dartAppUri.resolveUri(relativeExeUri);
expect(await File.fromUri(absoluteExeUri).exists(), true);
for (final exeUri in [absoluteExeUri, relativeExeUri]) {
final result = await runProcess(
executable: exeUri,
arguments: [],
workingDirectory: dartAppUri,
logger: logger,
);
expectDartAppStdout(result.stdout);
}
await _withTempDir((tempUri) async {
// The link needs to have the same extension as the executable on
// Windows to be able to be executable.
final link = Link.fromUri(tempUri.resolve('my_link.exe'));
await link.create(absoluteExeUri.toFilePath());
for (final exeUri in [
absoluteExeUri,
relativeExeUri,
link.uri,
]) {
final result = await runProcess(
executable: exeUri,
arguments: [],
workingDirectory: dartAppUri,
logger: logger,
);
expectDartAppStdout(result.stdout);
}
});
});
});
}
@ -177,3 +187,16 @@ void main(List<String> args) {
});
});
}
Future<void> _withTempDir(Future<void> Function(Uri tempUri) fun) async {
final tempDir = await Directory.systemTemp.createTemp('link_dir');
final tempDirResolved = Directory(await tempDir.resolveSymbolicLinks());
try {
await fun(tempDirResolved.uri);
} finally {
if (!Platform.environment.containsKey(keepTempKey) ||
Platform.environment[keepTempKey]!.isEmpty) {
await tempDirResolved.delete(recursive: true);
}
}
}

View file

@ -16,6 +16,7 @@ import("builtin_sources.gni")
import("cli_sources.gni")
import("io_impl_sources.gni")
import("io_sources.gni")
import("native_assets_impl_sources.gni")
config("libdart_builtin_config") {
if (is_win) {
@ -108,6 +109,35 @@ build_libdart_builtin("libdart_builtin_product_host_targeting_host") {
extra_configs = [ "..:dart_product_config" ]
}
template("build_native_assets_api") {
extra_configs = []
if (defined(invoker.extra_configs)) {
extra_configs += invoker.extra_configs
}
source_set(target_name) {
configs += [ "..:dart_config" ] + extra_configs
deps = []
include_dirs = [ ".." ]
sources = native_assets_impl_sources
}
}
build_native_assets_api("native_assets_api") {
extra_configs = [
"..:dart_maybe_product_config",
"..:dart_os_config",
"..:dart_arch_config",
]
}
build_native_assets_api("native_assets_api_product") {
extra_configs = [
"..:dart_product_config",
"..:dart_os_config",
"..:dart_arch_config",
]
}
static_library("crashpad") {
configs += [
"..:dart_arch_config",
@ -845,6 +875,11 @@ dart_executable("dart") {
if (!exclude_kernel_service) {
extra_deps += [ ":dart_kernel_platform_cc" ]
}
if (dart_runtime_mode == "release") {
extra_deps += [ ":native_assets_api_product" ]
} else {
extra_deps += [ ":native_assets_api" ]
}
}
dart_executable("dart_precompiled_runtime") {
@ -871,9 +906,15 @@ dart_executable("dart_precompiled_runtime") {
]
if (dart_runtime_mode == "release") {
extra_deps += [ ":elf_loader_product" ]
extra_deps += [
":elf_loader_product",
":native_assets_api_product",
]
} else {
extra_deps += [ ":elf_loader" ]
extra_deps += [
":elf_loader",
":native_assets_api",
]
}
if (dart_runtime_mode == "release") {
@ -903,7 +944,10 @@ dart_executable("dart_precompiled_runtime_product") {
"snapshot_empty.cc",
]
extra_deps += [ ":elf_loader_product" ]
extra_deps += [
":elf_loader_product",
":native_assets_api_product",
]
}
# This flag is set in runtime/runtime_args.gni

View file

@ -8,16 +8,12 @@
#include "bin/directory.h"
#include "bin/file.h"
#include "bin/io_buffer.h"
#include "bin/namespace.h"
#include "bin/platform.h"
#include "bin/typed_data_utils.h"
#include "bin/utils.h"
#include "include/dart_api.h"
#include "include/dart_native_api.h"
#include "include/dart_tools_api.h"
#include "platform/assert.h"
#include "platform/globals.h"
#include "platform/memory_sanitizer.h"
#include "platform/utils.h"
// Return the error from the containing function if handle is in error handle.

View file

@ -18,7 +18,6 @@
#include "bin/dartutils.h"
#include "bin/dfe.h"
#include "bin/error_exit.h"
#include "bin/eventhandler.h"
#include "bin/exe_utils.h"
#include "bin/file.h"
#include "bin/gzip.h"
@ -28,18 +27,15 @@
#include "bin/platform.h"
#include "bin/process.h"
#include "bin/snapshot_utils.h"
#include "bin/thread.h"
#include "bin/utils.h"
#include "bin/vmservice_impl.h"
#include "include/bin/dart_io_api.h"
#include "include/bin/native_assets_api.h"
#include "include/dart_api.h"
#include "include/dart_embedder_api.h"
#include "include/dart_tools_api.h"
#include "platform/globals.h"
#include "platform/growable_array.h"
#include "platform/hashmap.h"
#include "platform/syslog.h"
#include "platform/text_buffer.h"
#include "platform/utils.h"
extern "C" {
@ -251,6 +247,13 @@ failed:
return false;
}
static void* NativeAssetsDlopenRelative(const char* path, char** error) {
auto isolate_group_data =
reinterpret_cast<IsolateGroupData*>(Dart_CurrentIsolateGroupData());
const char* script_uri = isolate_group_data->script_url;
return NativeAssets::DlopenRelative(path, script_uri, error);
}
static Dart_Isolate IsolateSetupHelper(Dart_Isolate isolate,
bool is_main_isolate,
const char* script_uri,
@ -385,6 +388,18 @@ static Dart_Isolate IsolateSetupHelper(Dart_Isolate isolate,
}
#endif // !defined(DART_PRECOMPILED_RUNTIME)
#if !defined(DART_PRECOMPILER)
NativeAssetsApi native_assets;
memset(&native_assets, 0, sizeof(native_assets));
native_assets.dlopen_absolute = &NativeAssets::DlopenAbsolute;
native_assets.dlopen_relative = &NativeAssetsDlopenRelative;
native_assets.dlopen_system = &NativeAssets::DlopenSystem;
native_assets.dlopen_executable = &NativeAssets::DlopenExecutable;
native_assets.dlopen_process = &NativeAssets::DlopenProcess;
native_assets.dlsym = &NativeAssets::Dlsym;
Dart_InitializeNativeAssetsResolver(&native_assets);
#endif // !defined(DART_PRECOMPILER)
// Make the isolate runnable so that it is ready to handle messages.
Dart_ExitScope();
Dart_ExitIsolate();

View file

@ -0,0 +1,230 @@
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
#include "include/bin/native_assets_api.h"
#include "platform/globals.h"
#if defined(DART_HOST_OS_WINDOWS)
#include <Psapi.h>
#include <Windows.h>
#include <combaseapi.h>
#include <stdio.h>
#include <tchar.h>
#endif
#if defined(DART_HOST_OS_LINUX) || defined(DART_HOST_OS_MACOS) || \
defined(DART_HOST_OS_ANDROID) || defined(DART_HOST_OS_FUCHSIA)
#include <dlfcn.h>
#endif
#include "bin/file.h"
#include "platform/uri.h"
namespace dart {
namespace bin {
#define SET_ERROR_MSG(error_msg, format, ...) \
intptr_t len = snprintf(nullptr, 0, format, __VA_ARGS__); \
char* msg = reinterpret_cast<char*>(malloc(len + 1)); \
snprintf(msg, len + 1, format, __VA_ARGS__); \
*error_msg = msg
#if defined(DART_TARGET_OS_WINDOWS)
// Replaces back slashes with forward slashes in place.
static void ReplaceBackSlashes(char* cstr) {
const intptr_t length = strlen(cstr);
for (int i = 0; i < length; i++) {
cstr[i] = cstr[i] == '\\' ? '/' : cstr[i];
}
}
#endif
const char* file_schema = "file://";
const int file_schema_length = 7;
// Get a file uri with only forward slashes from the script path.
// Returned string must be freed by caller.
static char* CleanScriptUri(const char* script_uri) {
const char* path = script_uri;
#if defined(DART_TARGET_OS_WINDOWS)
// Isolate.spawnUri sets a `source` including the file schema.
// And on Windows we get an extra forward slash in that case.
const char* file_schema_slash = "file:///";
const int file_schema_slash_length = 8;
if (strlen(script_uri) > file_schema_slash_length &&
strncmp(script_uri, file_schema_slash, file_schema_slash_length) == 0) {
path = (script_uri + file_schema_slash_length);
}
#else
// Isolate.spawnUri sets a `source` including the file schema.
if (strlen(script_uri) > file_schema_length &&
strncmp(script_uri, file_schema, file_schema_length) == 0) {
path = (script_uri + file_schema_length);
}
#endif
// Resolve symlinks.
char canon_path[PATH_MAX];
File::GetCanonicalPath(nullptr, path, canon_path, PATH_MAX);
// Replace backward slashes with forward slashes.
const intptr_t len = strlen(canon_path);
char* path_copy =
reinterpret_cast<char*>(malloc(file_schema_length + len + 1));
snprintf(path_copy, len + 1, "%s%s", file_schema, canon_path);
#if defined(DART_TARGET_OS_WINDOWS)
ReplaceBackSlashes(path_copy);
#endif
return path_copy;
}
// If an error occurs populates |error| (if provided) with an error message
// (caller must free this message when it is no longer needed).
static void* LoadDynamicLibrary(const char* library_file,
char** error = nullptr) {
char* utils_error = nullptr;
void* handle = Utils::LoadDynamicLibrary(library_file, &utils_error);
if (utils_error != nullptr) {
if (error != nullptr) {
SET_ERROR_MSG(error, "Failed to load dynamic library '%s': %s",
library_file != nullptr ? library_file : "<process>",
utils_error);
}
free(utils_error);
}
return handle;
}
#if defined(DART_HOST_OS_WINDOWS)
// On windows, nullptr signals trying a lookup in all loaded modules.
const nullptr_t kWindowsDynamicLibraryProcessPtr = nullptr;
#endif
static void WrapError(const char* path, char** error) {
if (*error != nullptr) {
char* inner_error = *error;
SET_ERROR_MSG(error, "Failed to load dynamic library '%s': %s", path,
inner_error);
free(inner_error);
}
}
void* NativeAssets::DlopenAbsolute(const char* path, char** error) {
// If we'd want to be strict, it should not take into account include paths.
void* handle = LoadDynamicLibrary(path, error);
WrapError(path, error);
return handle;
}
void* NativeAssets::DlopenRelative(const char* path,
const char* script_uri,
char** error) {
void* handle = nullptr;
char* platform_script_cstr = CleanScriptUri(script_uri);
const intptr_t len = strlen(path);
char* path_copy = reinterpret_cast<char*>(malloc(len + 1));
snprintf(path_copy, len + 1, "%s", path);
#if defined(DART_TARGET_OS_WINDOWS)
ReplaceBackSlashes(path_copy);
#endif
auto target_uri = ResolveUri(path_copy, platform_script_cstr);
if (!target_uri) {
SET_ERROR_MSG(error, "Failed to resolve '%s' relative to '%s'.", path_copy,
platform_script_cstr);
} else {
const char* target_path = target_uri.get() + file_schema_length;
handle = LoadDynamicLibrary(target_path, error);
}
free(path_copy);
free(platform_script_cstr);
WrapError(path, error);
return handle;
}
void* NativeAssets::DlopenSystem(const char* path, char** error) {
// Should take into account LD_PATH etc.
void* handle = LoadDynamicLibrary(path, error);
WrapError(path, error);
return handle;
}
void* NativeAssets::DlopenProcess(char** error) {
#if defined(DART_HOST_OS_LINUX) || defined(DART_HOST_OS_MACOS) || \
defined(DART_HOST_OS_ANDROID) || defined(DART_HOST_OS_FUCHSIA)
return RTLD_DEFAULT;
#else
return kWindowsDynamicLibraryProcessPtr;
#endif
}
void* NativeAssets::DlopenExecutable(char** error) {
return LoadDynamicLibrary(nullptr, error);
}
#if defined(DART_HOST_OS_WINDOWS)
void* co_task_mem_allocated = nullptr;
// If an error occurs populates |error| with an error message
// (caller must free this message when it is no longer needed).
void* LookupSymbolInProcess(const char* symbol, char** error) {
// Force loading ole32.dll.
if (co_task_mem_allocated == nullptr) {
co_task_mem_allocated = CoTaskMemAlloc(sizeof(intptr_t));
CoTaskMemFree(co_task_mem_allocated);
}
HANDLE current_process =
OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE,
GetCurrentProcessId());
if (current_process == nullptr) {
SET_ERROR_MSG(error, "Failed to open current process.");
return nullptr;
}
HMODULE modules[1024];
DWORD cb_needed;
if (EnumProcessModules(current_process, modules, sizeof(modules),
&cb_needed) != 0) {
for (intptr_t i = 0; i < (cb_needed / sizeof(HMODULE)); i++) {
if (auto result =
reinterpret_cast<void*>(GetProcAddress(modules[i], symbol))) {
CloseHandle(current_process);
return result;
}
}
}
CloseHandle(current_process);
SET_ERROR_MSG(
error, "None of the loaded modules contained the requested symbol '%s'.",
symbol);
return nullptr;
}
#endif
// If an error occurs populates |error| with an error message
// (caller must free this message when it is no longer needed).
static void* ResolveSymbol(void* handle, const char* symbol, char** error) {
#if defined(DART_HOST_OS_WINDOWS)
if (handle == kWindowsDynamicLibraryProcessPtr) {
return LookupSymbolInProcess(symbol, error);
}
#endif
return Utils::ResolveSymbolInDynamicLibrary(handle, symbol, error);
}
void* NativeAssets::Dlsym(void* handle, const char* symbol, char** error) {
void* const result = ResolveSymbol(handle, symbol, error);
if (*error != nullptr) {
char* inner_error = *error;
SET_ERROR_MSG(error, "Failed to lookup symbol '%s': %s", symbol,
inner_error);
free(inner_error);
}
return result;
}
} // namespace bin
} // namespace dart

View file

@ -0,0 +1,8 @@
# Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file
# for details. All rights reserved. Use of this source code is governed by a
# BSD-style license that can be found in the LICENSE file.
native_assets_impl_sources = [
"../include/bin/dart_io_api.h",
"native_assets_api_impl.cc",
]

View file

@ -0,0 +1,26 @@
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
#ifndef RUNTIME_INCLUDE_BIN_NATIVE_ASSETS_API_H_
#define RUNTIME_INCLUDE_BIN_NATIVE_ASSETS_API_H_
namespace dart {
namespace bin {
class NativeAssets {
public:
static void* DlopenAbsolute(const char* path, char** error);
static void* DlopenRelative(const char* path,
const char* script_uri,
char** error);
static void* DlopenSystem(const char* path, char** error);
static void* DlopenProcess(char** error);
static void* DlopenExecutable(char** error);
static void* Dlsym(void* handle, const char* symbol, char** error);
};
} // namespace bin
} // namespace dart
#endif // RUNTIME_INCLUDE_BIN_NATIVE_ASSETS_API_H_

View file

@ -3338,6 +3338,77 @@ DART_EXPORT Dart_Handle Dart_GetNativeSymbol(Dart_Handle library,
DART_EXPORT Dart_Handle
Dart_SetFfiNativeResolver(Dart_Handle library, Dart_FfiNativeResolver resolver);
/**
* Callback provided by the embedder that is used by the VM to resolve asset
* paths.
* If no callback is provided, using `@Native`s with `native_asset.yaml`s will
* fail.
*
* The VM is responsible for looking up the asset path with the asset id in the
* kernel mapping.
* The embedder is responsible for providing the asset mapping during kernel
* compilation and using the asset path to return a library handle in this
* function.
*
* \param path The string in the asset path as passed in native_assets.yaml
* during kernel compilation.
*
* \param error Returns NULL if creation is successful, an error message
* otherwise. The caller is responsible for calling free() on the error
* message.
*
* \return The library handle. If |error| is not-null, the return value is
* undefined.
*/
typedef void* (*Dart_NativeAssetsDlopenCallback)(const char* path,
char** error);
typedef void* (*Dart_NativeAssetsDlopenCallbackNoPath)(char** error);
/**
* Callback provided by the embedder that is used by the VM to lookup symbols
* in native code assets.
* If no callback is provided, using `@Native`s with `native_asset.yaml`s will
* fail.
*
* \param handle The library handle returned from a
* `Dart_NativeAssetsDlopenCallback` or
* `Dart_NativeAssetsDlopenCallbackNoPath`.
*
* \param symbol The symbol to look up. Is a string.
*
* \param error Returns NULL if creation is successful, an error message
* otherwise. The caller is responsible for calling free() on the error
* message.
*
* \return The symbol address. If |error| is not-null, the return value is
* undefined.
*/
typedef void* (*Dart_NativeAssetsDlsymCallback)(void* handle,
const char* symbol,
char** error);
typedef struct {
Dart_NativeAssetsDlopenCallback dlopen_absolute;
Dart_NativeAssetsDlopenCallback dlopen_relative;
Dart_NativeAssetsDlopenCallback dlopen_system;
Dart_NativeAssetsDlopenCallbackNoPath dlopen_process;
Dart_NativeAssetsDlopenCallbackNoPath dlopen_executable;
Dart_NativeAssetsDlsymCallback dlsym;
} NativeAssetsApi;
/**
* Initializes native asset resolution for the current isolate group.
*
* The caller is responsible for ensuring this is called right after isolate
* group creation, and before running any dart code (or spawning isolates).
*
* @param native_assets_api The callbacks used by native assets resolution.
* The VM does not take ownership of the parameter,
* it can be freed immediately after the call.
*/
DART_EXPORT void Dart_InitializeNativeAssetsResolver(
NativeAssetsApi* native_assets_api);
/*
* =====================
* Scripts and Libraries

View file

@ -377,26 +377,15 @@ static char* AvailableAssetsToCString(Thread* const thread) {
return buffer.buffer();
}
// If an error occurs populates |error| with an error message
// (caller must free this message when it is no longer needed).
//
// The |asset_location| is formatted as follows:
// ['<path_type>', '<path (optional)>']
// The |asset_location| is conform to: pkg/vm/lib/native_assets/validator.dart
static void* FfiResolveAsset(Thread* const thread,
const Array& asset_location,
const String& symbol,
char** error) {
// Fall back to old implementation temporarily to ease the roll into flutter.
// TODO(https://dartbug.com/55523): Remove fallback and throw errors that
// native assets API is not initialized.
static void* FfiResolveAssetFallback(Thread* const thread,
const String& asset_type,
const String& path,
const String& symbol,
char** error) {
Zone* const zone = thread->zone();
const auto& asset_type =
String::Cast(Object::Handle(zone, asset_location.At(0)));
String& path = String::Handle(zone);
if (asset_type.Equals(Symbols::absolute()) ||
asset_type.Equals(Symbols::relative()) ||
asset_type.Equals(Symbols::system())) {
path = String::RawCast(asset_location.At(1));
}
void* handle = nullptr;
if (asset_type.Equals(Symbols::absolute())) {
handle = LoadDynamicLibrary(path.ToCString(), error);
@ -414,9 +403,11 @@ static void* FfiResolveAsset(Thread* const thread,
ResolveUri(path_cstr, platform_script_uri.ToCString());
free(path_cstr);
if (!target_uri) {
*error = OS::SCreate(/*use malloc*/ nullptr,
"Failed to resolve '%s' relative to '%s'.",
path.ToCString(), platform_script_uri.ToCString());
*error = OS::SCreate(
/*use malloc*/ nullptr,
"Failed to resolve '%s' relative to "
"'%s'.",
path.ToCString(), platform_script_uri.ToCString());
} else {
const char* target_path = target_uri.get() + file_schema_length;
handle = LoadDynamicLibrary(target_path, error);
@ -457,6 +448,76 @@ static void* FfiResolveAsset(Thread* const thread,
return nullptr;
}
// If an error occurs populates |error| with an error message
// (caller must free this message when it is no longer needed).
//
// The |asset_location| is formatted as follows:
// ['<path_type>', '<path (optional)>']
// The |asset_location| is conform to: pkg/vm/lib/native_assets/validator.dart
static void* FfiResolveAsset(Thread* const thread,
const Array& asset_location,
const String& symbol,
char** error) {
Zone* const zone = thread->zone();
const auto& asset_type =
String::Cast(Object::Handle(zone, asset_location.At(0)));
String& path = String::Handle(zone);
const char* path_cstr = nullptr;
if (asset_type.Equals(Symbols::absolute()) ||
asset_type.Equals(Symbols::relative()) ||
asset_type.Equals(Symbols::system())) {
path = String::RawCast(asset_location.At(1));
path_cstr = path.ToCString();
}
NativeAssetsApi* native_assets_api =
thread->isolate_group()->native_assets_api();
void* handle;
if (asset_type.Equals(Symbols::absolute())) {
if (native_assets_api->dlopen_absolute == nullptr) {
return FfiResolveAssetFallback(thread, asset_type, path, symbol, error);
}
NoActiveIsolateScope no_active_isolate_scope;
handle = native_assets_api->dlopen_absolute(path_cstr, error);
} else if (asset_type.Equals(Symbols::relative())) {
if (native_assets_api->dlopen_relative == nullptr) {
return FfiResolveAssetFallback(thread, asset_type, path, symbol, error);
}
NoActiveIsolateScope no_active_isolate_scope;
handle = native_assets_api->dlopen_relative(path_cstr, error);
} else if (asset_type.Equals(Symbols::system())) {
if (native_assets_api->dlopen_system == nullptr) {
return FfiResolveAssetFallback(thread, asset_type, path, symbol, error);
}
NoActiveIsolateScope no_active_isolate_scope;
handle = native_assets_api->dlopen_system(path_cstr, error);
} else if (asset_type.Equals(Symbols::executable())) {
if (native_assets_api->dlopen_executable == nullptr) {
return FfiResolveAssetFallback(thread, asset_type, path, symbol, error);
}
NoActiveIsolateScope no_active_isolate_scope;
handle = native_assets_api->dlopen_executable(error);
} else {
RELEASE_ASSERT(asset_type.Equals(Symbols::process()));
if (native_assets_api->dlopen_process == nullptr) {
return FfiResolveAssetFallback(thread, asset_type, path, symbol, error);
}
NoActiveIsolateScope no_active_isolate_scope;
handle = native_assets_api->dlopen_process(error);
}
if (*error != nullptr) {
return nullptr;
}
if (native_assets_api->dlsym == nullptr) {
return FfiResolveAssetFallback(thread, asset_type, path, symbol, error);
}
void* const result =
native_assets_api->dlsym(handle, symbol.ToCString(), error);
return result;
}
// Frees |error|.
static void ThrowFfiResolveError(const String& symbol,
const String& asset,

View file

@ -66,6 +66,7 @@ main() {
"Dart_ClosureFunction",
"Dart_CompileAll",
"Dart_CompileToKernel",
"Dart_CopyUTF8EncodingOfString",
"Dart_CreateAppAOTSnapshotAsAssemblies",
"Dart_CreateAppAOTSnapshotAsAssembly",
"Dart_CreateAppAOTSnapshotAsElf",
@ -150,6 +151,7 @@ main() {
"Dart_HasStickyError",
"Dart_IdentityEquals",
"Dart_Initialize",
"Dart_InitializeNativeAssetsResolver",
"Dart_InstanceGetType",
"Dart_IntegerFitsIntoInt64",
"Dart_IntegerFitsIntoUint64",
@ -323,13 +325,12 @@ main() {
"Dart_StopProfiling",
"Dart_StringGetProperties",
"Dart_StringLength",
"Dart_StringUTF8Length",
"Dart_StringStorageSize",
"Dart_StringToCString",
"Dart_StringToLatin1",
"Dart_StringToUTF16",
"Dart_StringToUTF8",
"Dart_CopyUTF8EncodingOfString",
"Dart_StringUTF8Length",
"Dart_ThreadDisableProfiling",
"Dart_ThreadEnableProfiling",
"Dart_ThrowException",

View file

@ -5942,6 +5942,14 @@ Dart_SetFfiNativeResolver(Dart_Handle library,
return Api::Success();
}
DART_EXPORT
void Dart_InitializeNativeAssetsResolver(NativeAssetsApi* native_assets_api) {
Thread* T = Thread::Current();
IsolateGroup* isolate_group = T->isolate_group();
CHECK_ISOLATE_GROUP(isolate_group);
isolate_group->SetNativeAssetsCallbacks(native_assets_api);
}
// --- Peer support ---
DART_EXPORT Dart_Handle Dart_GetPeer(Dart_Handle object, void** peer) {

View file

@ -742,6 +742,11 @@ class IsolateGroup : public IntrusiveDListEntry<IsolateGroup> {
Isolate* EnterTemporaryIsolate();
static void ExitTemporaryIsolate();
void SetNativeAssetsCallbacks(NativeAssetsApi* native_assets_api) {
native_assets_api_ = *native_assets_api;
}
NativeAssetsApi* native_assets_api() { return &native_assets_api_; }
private:
friend class Dart; // For `object_store_ = ` in Dart::Init
friend class Heap;
@ -883,6 +888,8 @@ class IsolateGroup : public IntrusiveDListEntry<IsolateGroup> {
intptr_t max_active_mutators_ = 0;
NOT_IN_PRODUCT(GroupDebugger* debugger_ = nullptr);
NativeAssetsApi native_assets_api_;
};
// When an isolate sends-and-exits this class represent things that it passed

View file

@ -27,7 +27,7 @@ import 'helpers.dart';
const runTestsArg = 'run-tests';
main(List<String> args, Object? message) async {
void main(List<String> args, Object? message) async {
return await selfInvokingTest(
doOnOuterInvocation: selfInvokes,
doOnProcessInvocation: () async {
@ -50,12 +50,14 @@ Future<void> selfInvokes() async {
kernelCombine: KernelCombine.concatenation,
relativePath: RelativePath.same,
arguments: [runTestsArg],
useSymlink: true,
);
await invokeSelf(
selfSourceUri: selfSourceUri,
runtime: Runtime.jit,
relativePath: RelativePath.down,
arguments: [runTestsArg],
useSymlink: true,
);
await invokeSelf(
selfSourceUri: selfSourceUri,
@ -66,6 +68,7 @@ Future<void> selfInvokes() async {
: AotCompile.elf,
relativePath: RelativePath.up,
arguments: [runTestsArg],
useSymlink: true,
);
}
@ -83,6 +86,7 @@ Future<void> invokeSelf({
KernelCombine kernelCombine = KernelCombine.source,
AotCompile aotCompile = AotCompile.elf,
RelativePath relativePath = RelativePath.same,
bool useSymlink = false,
}) async {
await withTempDir((Uri tempUri) async {
final nestedUri = tempUri.resolve('nested/');
@ -121,6 +125,7 @@ Future<void> invokeSelf({
kernelCombine: kernelCombine,
aotCompile: aotCompile,
runArguments: arguments,
useSymlink: useSymlink,
);
print([
selfSourceUri.toFilePath(),

View file

@ -337,6 +337,7 @@ Future<void> compileAndRun({
required KernelCombine kernelCombine,
AotCompile aotCompile = AotCompile.elf,
required List<String> runArguments,
bool useSymlink = false,
}) async {
final nativeAssetsUri = tempUri.resolve('native_assets.yaml');
await File(nativeAssetsUri.toFilePath()).writeAsString(nativeAssetsYaml);
@ -360,8 +361,21 @@ Future<void> compileAndRun({
outputUri: snapshotUri,
aotCompile: aotCompile,
);
await runDartAotRuntime(
aotSnapshotUri: snapshotUri, arguments: runArguments);
if (useSymlink) {
await withTempDir(prefix: 'link_dir', (tempDir) async {
final link = Link.fromUri(tempDir.resolve('my_link'));
await link.create(snapshotUri.toFilePath());
await runDartAotRuntime(
aotSnapshotUri: link.uri,
arguments: runArguments,
);
});
} else {
await runDartAotRuntime(
aotSnapshotUri: snapshotUri,
arguments: runArguments,
);
}
case Runtime.appjit:
final outJitUri = tempUri.resolve('out.jit');
await runDart(
@ -372,12 +386,37 @@ Future<void> compileAndRun({
scriptUri: outDillUri,
arguments: runArguments,
);
await runDart(
scriptUri: outJitUri,
arguments: runArguments,
);
if (useSymlink) {
await withTempDir(prefix: 'link_dir', (tempDir) async {
final link = Link.fromUri(tempDir.resolve('my_link'));
await link.create(outDillUri.toFilePath());
await runDart(
scriptUri: link.uri,
arguments: runArguments,
);
});
} else {
await runDart(
scriptUri: outJitUri,
arguments: runArguments,
);
}
case Runtime.jit:
await runDart(scriptUri: outDillUri, arguments: runArguments);
if (useSymlink) {
await withTempDir(prefix: 'link_dir', (tempDir) async {
final link = Link.fromUri(tempDir.resolve('my_link'));
await link.create(outDillUri.toFilePath());
await runDart(
scriptUri: link.uri,
arguments: runArguments,
);
});
} else {
await runDart(
scriptUri: outDillUri,
arguments: runArguments,
);
}
}
}