[test/ffi] Fix errors in tests

A cleanup round of our FFI tests.

Change-Id: I3883b9d14785c759c7129c072c9588bfa5a3fba0
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/214802
Reviewed-by: Clement Skau <cskau@google.com>
Commit-Queue: Daco Harkes <dacoharkes@google.com>
This commit is contained in:
Daco Harkes 2021-09-28 13:17:01 +00:00 committed by commit-bot@chromium.org
parent e922f3016b
commit 7487e5ea8b
14 changed files with 1122 additions and 883 deletions

View file

@ -6,25 +6,23 @@
// with type arguments isn't supported in that version of Dart.
import 'dart:ffi';
import 'dart:nativewrappers';
import 'package:expect/expect.dart';
// Error: FFI leaf call must not have Handle return type.
@FfiNative<Handle Function()>("foo", isLeaf: true) //# 01: compile-time error
external Object foo(); //# 01: compile-time error
@FfiNative<Handle Function()>("foo", isLeaf: true) //# 01: compile-time error
external Object foo(); //# 01: compile-time error
// Error: FFI leaf call must not have Handle argument types.
@FfiNative<Void Function(Handle)>("bar", isLeaf: true) //# 02: compile-time error
external void bar(Object); //# 02: compile-time error
@FfiNative<Void Function(Handle)>("bar", //# 02: compile-time error
isLeaf: true) //# 02: compile-time error
external void bar(Object); //# 02: compile-time error
class Classy {
@FfiNative<IntPtr Function(IntPtr)>('ReturnIntPtr')
external static int returnIntPtrStatic(int x);
// Error: FfiNative annotations can only be used on static functions.
@FfiNative<IntPtr Function(IntPtr)>('ReturnIntPtr') //# 03: compile-time error
external int returnIntPtrMethod(int x); //# 03: compile-time error
@FfiNative<IntPtr Function(IntPtr)>('ReturnIntPtr') //# 03: compile-time error
external int returnIntPtrMethod(int x); //# 03: compile-time error
}
// Regression test: Ensure same-name FfiNative functions don't collide in the
@ -33,9 +31,10 @@ class A {
@FfiNative<Void Function()>('nop')
external static void foo();
}
class B {
@FfiNative<Void Function()>('nop')
external static void foo();
}
void main() { /* Intentionally empty: Compile-time error tests. */ }
void main() {/* Intentionally empty: Compile-time error tests. */}

View file

@ -72,7 +72,8 @@ final cPassStructRecursive = ffiTestFunctions.lookupFunction<
Struct20BytesHomogeneousInt32 Function(int recursionCounter,
Struct20BytesHomogeneousInt32, Pointer)>("PassStructRecursive");
Struct8BytesNestedInt typedDataBackedStruct = Struct8BytesNestedInt();
Struct8BytesNestedInt typedDataBackedStruct =
Pointer<Struct8BytesNestedInt>.fromAddress(0).ref;
bool typedDataBackedStructSet = false;
void _receiveStructByValue(Struct8BytesNestedInt struct) {
typedDataBackedStruct = struct;

View file

@ -268,7 +268,7 @@ final ${dartType} ${variableName} = ${variableName}Pointer.ref;
if (structsAsPointers) {
return "Pointer<${dartType}> ${variableName}Pointer = nullptr;\n";
} else {
return "${dartType} ${variableName} = ${dartType}();\n";
return "${dartType} ${variableName} = Pointer<${dartType}>.fromAddress(0).ref;\n";
}
}

View file

@ -9,8 +9,6 @@
import "dart:ffi";
import "package:expect/expect.dart";
import "dylib_utils.dart";
typedef VigesimalOp = double Function(

View file

@ -17,9 +17,6 @@ import 'dart:ffi';
import 'dylib_utils.dart';
import "package:ffi/ffi.dart";
import "package:expect/expect.dart";
void main() {
for (int i = 0; i < 100; ++i) {
testLookupFunctionPointerNativeType();

View file

@ -4,7 +4,6 @@
import 'dart:ffi';
import 'dart:io';
import 'dart:typed_data';
import 'package:expect/expect.dart';

View file

@ -9,29 +9,30 @@ import 'dart:io';
import 'package:expect/expect.dart';
import 'dylib_utils.dart';
import 'ffi_test_helpers.dart';
import 'callback_tests_utils.dart';
import 'dylib_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");
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);
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);
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();
@ -45,7 +46,8 @@ testCallbackLeaf() {
// 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();
isLeaf: true)
.run();
}
main() {

View file

@ -74,7 +74,8 @@ final cPassStructRecursive = ffiTestFunctions.lookupFunction<
Struct20BytesHomogeneousInt32 Function(int recursionCounter,
Struct20BytesHomogeneousInt32, Pointer)>("PassStructRecursive");
Struct8BytesNestedInt typedDataBackedStruct = Struct8BytesNestedInt();
Struct8BytesNestedInt typedDataBackedStruct =
Pointer<Struct8BytesNestedInt>.fromAddress(0).ref;
bool typedDataBackedStructSet = false;
void _receiveStructByValue(Struct8BytesNestedInt struct) {
typedDataBackedStruct = struct;

View file

@ -11,8 +11,6 @@
import "dart:ffi";
import "package:expect/expect.dart";
import "dylib_utils.dart";
typedef VigesimalOp = double Function(

View file

@ -19,9 +19,6 @@ import 'dart:ffi';
import 'dylib_utils.dart';
import "package:ffi/ffi.dart";
import "package:expect/expect.dart";
void main() {
for (int i = 0; i < 100; ++i) {
testLookupFunctionPointerNativeType();

View file

@ -6,7 +6,6 @@
import 'dart:ffi';
import 'dart:io';
import 'dart:typed_data';
import 'package:expect/expect.dart';

View file

@ -11,28 +11,29 @@ import 'dart:io';
import 'package:expect/expect.dart';
import 'dylib_utils.dart';
import 'ffi_test_helpers.dart';
import 'callback_tests_utils.dart';
import 'dylib_utils.dart';
DynamicLibrary ffiTestFunctions = dlopenPlatformSpecific("ffi_test_functions");
testLeafCall() {
// Regular calls should transition generated -> native.
final isThreadInGenerated = ffiTestFunctions.lookupFunction<
Int8 Function(), int Function()>("IsThreadInGenerated");
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);
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);
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();
@ -46,7 +47,8 @@ testCallbackLeaf() {
// 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();
isLeaf: true)
.run();
}
main() {