[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:
committed by
Commit Queue
parent
4b2906cdc1
commit
9588927faf
@@ -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_
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user