dart-sdk/tests/ffi/vmspecific_leaf_call_test.dart
Clement Skau 4d5055805f [VM/FFI] Adds FFI leaf calls.
This CL adds FFI leaf calls by adding `lookupFunction(.., isLeaf)`
and `_asFunctionInternal(.., isLeaf)`, which generate FFI leaf calls.
These calls skip a lot of the usual frame building and generated <->
native transition overhead.

`benchmark/FfiCall/` shows a 1.1x - 4.3x speed-up between the regular
FFI calls and their leaf call counterparts (JIT, x64, release).

TEST=Adds `tests/ffi{,_2}/vmspecific_leaf_call_test.dart`. Tested FFI tests.

Closes: https://github.com/dart-lang/sdk/issues/36707
Cq-Include-Trybots: luci.dart.try:vm-precomp-ffi-qemu-linux-release-arm-try,vm-ffi-android-release-arm64-try,vm-ffi-android-release-arm-try,vm-ffi-android-product-arm64-try,vm-ffi-android-product-arm-try,vm-ffi-android-debug-arm64-try,vm-ffi-android-debug-arm-try,vm-kernel-linux-debug-ia32-try,vm-kernel-win-debug-ia32-try,vm-kernel-win-debug-x64-try,vm-kernel-win-release-x64-try,vm-kernel-mac-debug-x64-try,vm-kernel-precomp-nnbd-mac-release-simarm64-try,vm-kernel-precomp-android-release-arm64-try,vm-kernel-precomp-asan-linux-release-x64-try,vm-kernel-precomp-linux-release-simarm_x64-try,vm-kernel-precomp-obfuscate-linux-release-x64-try,vm-kernel-precomp-ubsan-linux-release-x64-try,vm-kernel-precomp-tsan-linux-release-x64-try,vm-kernel-precomp-win-release-x64-try,vm-precomp-ffi-qemu-linux-release-arm-try,vm-kernel-reload-rollback-linux-debug-x64-try,vm-kernel-reload-linux-debug-x64-try
Bug: https://github.com/dart-lang/sdk/issues/36707
Change-Id: Id8824f36b0006bf09951207bd004356fe6e9f46e
Cq-Do-Not-Cancel-Tryjobs: true
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/179768
Commit-Queue: Clement Skau <cskau@google.com>
Reviewed-by: Daco Harkes <dacoharkes@google.com>
Reviewed-by: Martin Kustermann <kustermann@google.com>
2021-05-21 11:12:02 +00:00

62 lines
2.2 KiB
Dart

// Copyright (c) 2021, 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.
//
// SharedObjects=ffi_test_functions
import 'dart:ffi';
import 'dart:io';
import 'package:expect/expect.dart';
import 'dylib_utils.dart';
import 'ffi_test_helpers.dart';
import 'callback_tests_utils.dart';
DynamicLibrary ffiTestFunctions = dlopenPlatformSpecific("ffi_test_functions");
testLeafCall() {
// Note: This test currently fails on Windows AOT: https://dartbug.com/40579
// Regular calls should transition generated -> native.
final isThreadInGenerated = ffiTestFunctions.lookupFunction<
Int8 Function(), int Function()>("IsThreadInGenerated");
Expect.equals(0, isThreadInGenerated());
// Leaf calls should remain in generated state.
final isThreadInGeneratedLeaf = ffiTestFunctions.lookupFunction<
Int8 Function(), int Function()>("IsThreadInGenerated", isLeaf: true);
Expect.equals(1, isThreadInGeneratedLeaf());
}
testLeafCallApi() {
// Note: This will only crash as expected in debug build mode. In other modes
// it's effectively skip.
final f = ffiTestFunctions.lookupFunction<
Void Function(), void Function()>("TestLeafCallApi", isLeaf: true);
// Calling Dart_.. API is unsafe from leaf calls since we explicitly haven't
// made the generated -> native transition.
f();
}
void nop() {}
testCallbackLeaf() {
// This should crash with "expected: T->IsAtSafepoint()", since it's unsafe to
// do callbacks from leaf calls (otherwise they wouldn't be leaf calls).
// Note: This will only crash as expected in debug build mode. In other modes
// it's effectively skip.
CallbackTest("CallbackLeaf", Pointer.fromFunction<Void Function()>(nop),
isLeaf:true).run();
}
main() {
testLeafCall(); //# 01: ok
// These tests terminate the process after successful completion, so we have
// to run them separately.
//
// Since they use signal handlers they only run on Linux.
if (Platform.isLinux && !const bool.fromEnvironment("dart.vm.product")) {
testLeafCallApi(); //# 02: ok
testCallbackLeaf(); //# 03: ok
}
}