mirror of
https://github.com/dart-lang/sdk
synced 2024-11-02 12:24:24 +00:00
c1467ab5d3
This prevents them from being called dynamically. Moreover, it prevents asFunction from being called on a non-NativeFunction type argument, simplifying the amount of manual checks. Note that this CL had to change the CFE and analzyer, and their tests (including mock_sdk) as well. This can potentially be a breaking change, as the extension methods are only visible when `dart:ffi` is imported, while methods on objects are always visible. Issue: https://github.com/dart-lang/sdk/issues/35903 Change-Id: I1e291f154228d5d9a34b21a022088bf493f6557d Cq-Include-Trybots: luci.dart.try:vm-ffi-android-debug-arm-try,vm-ffi-android-debug-arm64-try,app-kernel-linux-debug-x64-try,vm-kernel-linux-debug-ia32-try,vm-kernel-win-debug-x64-try,vm-kernel-win-debug-ia32-try,vm-kernel-precomp-linux-debug-x64-try,vm-dartkb-linux-release-x64-abi-try,vm-kernel-precomp-android-release-arm64-try,vm-kernel-asan-linux-release-x64-try,vm-kernel-linux-release-simarm-try,vm-kernel-linux-release-simarm64-try,vm-kernel-precomp-android-release-arm_x64-try,vm-kernel-precomp-obfuscate-linux-release-x64-try,dart-sdk-linux-try,analyzer-analysis-server-linux-try,analyzer-linux-release-try,front-end-linux-release-x64-try,vm-kernel-precomp-win-release-x64-try,vm-kernel-mac-debug-x64-try,analyzer-nnbd-linux-release-try,dart2js-nnbd-linux-x64-chrome-try,ddc-nnbd-linux-release-chrome-try,front-end-nnbd-linux-release-x64-try,vm-kernel-nnbd-linux-debug-x64-try,vm-kernel-nnbd-linux-release-x64-try Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/135463 Commit-Queue: Daco Harkes <dacoharkes@google.com> Reviewed-by: Martin Kustermann <kustermann@google.com>
83 lines
2.6 KiB
Dart
83 lines
2.6 KiB
Dart
// Copyright (c) 2019, 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.
|
|
//
|
|
// Dart test program for testing error handling with dart:ffi functions.
|
|
//
|
|
// SharedObjects=ffi_test_functions
|
|
|
|
import 'dart:ffi' as ffi;
|
|
import 'dylib_utils.dart';
|
|
import "package:expect/expect.dart";
|
|
|
|
main() {
|
|
testWrongArity();
|
|
testWrongTypes();
|
|
testDynamicAsFunction();
|
|
testDynamicLookupFunction();
|
|
}
|
|
|
|
ffi.DynamicLibrary ffiTestFunctions =
|
|
dlopenPlatformSpecific("ffi_test_functions");
|
|
|
|
typedef NativeBinaryOp = ffi.Int32 Function(ffi.Int32, ffi.Int32);
|
|
typedef BinaryOp = int Function(int, int);
|
|
|
|
typedef NativeUnaryOp = ffi.Int64 Function(ffi.Pointer<ffi.Int64>);
|
|
typedef UnaryOp = int Function(ffi.Pointer<ffi.Int64>);
|
|
|
|
void testWrongArity() {
|
|
{
|
|
dynamic sumPlus42 =
|
|
ffiTestFunctions.lookupFunction<NativeBinaryOp, BinaryOp>("SumPlus42");
|
|
Expect.throwsNoSuchMethodError(() => sumPlus42(10));
|
|
Expect.throwsNoSuchMethodError(() => sumPlus42(10, 11, 12));
|
|
Expect.throwsNoSuchMethodError(() => sumPlus42(10, 11, 12, y: 13));
|
|
}
|
|
|
|
{
|
|
Function sumPlus42 =
|
|
ffiTestFunctions.lookupFunction<NativeBinaryOp, BinaryOp>("SumPlus42");
|
|
Expect.throwsNoSuchMethodError(() => sumPlus42(10));
|
|
Expect.throwsNoSuchMethodError(() => sumPlus42(10, 11, 12));
|
|
Expect.throwsNoSuchMethodError(() => sumPlus42(10, 11, 12, y: 13));
|
|
}
|
|
}
|
|
|
|
void testWrongTypes() {
|
|
{
|
|
dynamic sumPlus42 =
|
|
ffiTestFunctions.lookupFunction<NativeBinaryOp, BinaryOp>("SumPlus42");
|
|
Expect.throwsTypeError(() => sumPlus42("abc", "def"));
|
|
}
|
|
|
|
{
|
|
Function sumPlus42 =
|
|
ffiTestFunctions.lookupFunction<NativeBinaryOp, BinaryOp>("SumPlus42");
|
|
Expect.throwsTypeError(() => sumPlus42("abc", "def"));
|
|
}
|
|
|
|
{
|
|
dynamic pointerOp = ffiTestFunctions
|
|
.lookupFunction<NativeUnaryOp, UnaryOp>("Assign1337Index1");
|
|
Expect.throwsTypeError(() => pointerOp(0));
|
|
}
|
|
}
|
|
|
|
// Test that invoking 'Pointer.asFunction' with a dynamic receiver type throws
|
|
// an exception.
|
|
void testDynamicAsFunction() {
|
|
dynamic x = ffi.nullptr.cast<ffi.NativeFunction<ffi.Void Function()>>();
|
|
Expect.throwsNoSuchMethodError(() {
|
|
x.asFunction<void Function()>();
|
|
});
|
|
}
|
|
|
|
// Test that invoking 'DynamicLibrary.lookupFunction' with a dynamic receiver
|
|
// type throws an exception.
|
|
void testDynamicLookupFunction() {
|
|
dynamic lib = ffiTestFunctions;
|
|
Expect.throwsNoSuchMethodError(() {
|
|
lib.lookupFunction<ffi.Void Function(), void Function()>("_");
|
|
});
|
|
}
|