Files
sdk/tests/ffi/native_assets/asset_executable_test.dart
Shikhar Soni 93ca44b048 [ffi] Add DynamicLibrary.openFromAssetId
Adds a new API `DynamicLibrary.openFromAssetId` to `dart:ffi` that
allows opening dynamically loaded libraries using their native asset IDs
instead of physical file paths.

Closes https://github.com/dart-lang/sdk/issues/63295

TEST=./tools/test.py -n vm-aot-linux-debug-x64 'ffi/native_assets/asset_*'
Cq-Include-Trybots: dart/try:vm-aot-linux-debug-arm64-try,vm-aot-linux-debug-x64-try,vm-aot-linux-debug-x64c-try,vm-aot-mac-debug-arm64-try,vm-aot-mac-debug-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,vm-asan-linux-release-arm64-try,vm-asan-linux-release-x64-try,vm-asan-mac-release-arm64-try,vm-asan-win-release-x64-try,vm-dyn-linux-debug-x64-try,vm-dyn-mac-debug-arm64-try,vm-ffi-dyn-mac-debug-simarm64_arm64-try,vm-msan-linux-release-arm64-try,vm-msan-linux-release-x64-try,vm-tsan-linux-release-arm64-try,vm-tsan-linux-release-x64-try,vm-tsan-mac-release-arm64-try,vm-ubsan-linux-release-arm64-try,vm-ubsan-linux-release-x64-try,vm-ubsan-mac-release-arm64-try,vm-ubsan-win-release-x64-try
R=vegorov@google.com
Change-Id: Ic617cb0906d1688d2d080dae7d1e08ee58b4c8d6

CoreLibraryReviewExempt: VM-only
Change-Id: Ic617cb0906d1688d2d080dae7d1e08ee58b4c8d6
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/507180
Auto-Submit: Shikhar Soni <shikharsoni@google.com>
Reviewed-by: Daco Harkes <dacoharkes@google.com>
Commit-Queue: Daco Harkes <dacoharkes@google.com>
2026-06-10 09:49:30 -07:00

101 lines
2.9 KiB
Dart

// Copyright (c) 2023, 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.
// This test compiles itself with gen_kernel and invokes the compiled kernel
// file with `Process.run(dart, <...>)` and `Isolate.spawn` and
// `Isolate.spawnUri`.
//
// This tests test including a native asset mapping that looks up its symbols
// in the executable.
// OtherResources=asset_executable_test.dart
// OtherResources=helpers.dart
// ignore_for_file: deprecated_member_use
import 'dart:async';
import 'dart:ffi';
import 'dart:io';
import 'dart:isolate';
import 'package:expect/expect.dart';
import 'helpers.dart';
const runTestsArg = 'run-tests';
main(List<String> args, Object? message) async {
return await selfInvokingTest(
doOnOuterInvocation: selfInvokes,
doOnProcessInvocation: () async {
await runTests(args[1]);
await testIsolateSpawn(() => runTests(args[1]));
await testIsolateSpawnUri(spawnUri: Platform.script, arguments: args);
},
doOnSpawnUriInvocation: () async {
await runTests(args[1]);
await testIsolateSpawn(() => runTests(args[1]));
},
)(args, message);
}
Future<void> selfInvokes() async {
final selfSourceUri = Platform.script.resolve('asset_executable_test.dart');
final nativeAssetsYaml = createNativeAssetYaml(
asset: selfSourceUri.toString(),
assetMapping: ['executable'],
);
await invokeSelf(
selfSourceUri: selfSourceUri,
runtime: Runtime.jit,
arguments: [runTestsArg, selfSourceUri.toString()],
nativeAssetsYaml: nativeAssetsYaml,
protobufAwareTreeshaking: true,
);
await invokeSelf(
selfSourceUri: selfSourceUri,
runtime: Runtime.aot,
arguments: [runTestsArg, selfSourceUri.toString()],
nativeAssetsYaml: nativeAssetsYaml,
);
}
Future<void> runTests(String assetId) async {
await testExecutable();
await testOpenExecutableAsset(assetId);
testNonExistingFunction();
testOpenFromAssetIdNotFound();
}
typedef _PostInteger = Bool Function(Int64 port, Int64 message);
@Native<_PostInteger>()
external bool Dart_PostInteger(int port, int message);
Future<void> testExecutable() async {
await _testWith(Dart_PostInteger);
final viaAddressOf = Native.addressOf<NativeFunction<_PostInteger>>(
Dart_PostInteger,
);
await _testWith(viaAddressOf.asFunction());
}
Future<void> _testWith(bool Function(int, int) postInteger) async {
const int message = 1337 * 42;
final completer = Completer();
final receivePort = ReceivePort()
..listen((receivedMessage) => completer.complete(receivedMessage));
final bool success = postInteger(receivePort.sendPort.nativePort, message);
Expect.isTrue(success);
final postedMessage = await completer.future;
Expect.equals(message, postedMessage);
receivePort.close();
}