[vm/ffi] Split out tests which use nulls

Issue: https://github.com/dart-lang/sdk/issues/40233
Change-Id: If4e79995f13ed2870f4663ceac5500ceacbe04df
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
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/132602
Commit-Queue: Daco Harkes <dacoharkes@google.com>
Reviewed-by: Martin Kustermann <kustermann@google.com>
This commit is contained in:
Daco Harkes 2020-01-20 16:01:08 +00:00 committed by commit-bot@chromium.org
parent e8c1391b26
commit ec66deec26
10 changed files with 262 additions and 110 deletions

View file

@ -39,7 +39,6 @@ void main() {
testVoid();
testPointerPointer();
testPointerPointerNull();
testPointerStoreNull();
testSizeOf();
testPointerChain(100);
testTypeTest();
@ -345,21 +344,6 @@ void testPointerPointerNull() {
free(pointerToPointer);
}
void testPointerStoreNull() {
int i = null;
Pointer<Int8> p = allocate();
Expect.throws(() => p.value = i);
free(p);
double d = null;
Pointer<Float> p2 = allocate();
Expect.throws(() => p2.value = d);
free(p2);
Pointer<Void> x = null;
Pointer<Pointer<Void>> p3 = allocate();
Expect.throws(() => p3.value = x);
free(p3);
}
void testSizeOf() {
Expect.equals(1, sizeOf<Int8>());
Expect.equals(2, sizeOf<Int16>());
@ -433,8 +417,6 @@ void testEquality() {
Pointer<Int16> p3 = p.cast();
Expect.equals(p, p3);
Expect.equals(p.hashCode, p3.hashCode);
Expect.notEquals(p, null);
Expect.notEquals(null, p);
Pointer<Int8> p4 = p.offsetBy(1337);
Expect.notEquals(p, p4);
}

View file

@ -10,9 +10,6 @@ import "package:ffi/ffi.dart";
main(List<String> arguments) {
for (int i = 0; i < 100; i++) {
testStoreLoad();
testNullReceivers();
testNullIndices();
testNullArguments();
testReifiedGeneric();
}
}
@ -55,46 +52,6 @@ testStoreLoad() {
free(p3);
}
/// With extension methods, the receiver position can be null.
testNullReceivers() {
Pointer<Int8> p = allocate();
Pointer<Int8> p4 = null;
Expect.throws(() => Expect.equals(10, p4.value));
Expect.throws(() => p4.value = 10);
Pointer<Pointer<Int8>> p5 = null;
Expect.throws(() => Expect.equals(10, p5.value));
Expect.throws(() => p5.value = p);
Pointer<Foo> p6 = null;
Expect.throws(() => Expect.equals(10, p6.ref));
free(p);
}
testNullIndices() {
Pointer<Int8> p = allocate();
Expect.throws(() => Expect.equals(10, p[null]));
Expect.throws(() => p[null] = 10);
Pointer<Pointer<Int8>> p5 = p.cast();
Expect.throws(() => Expect.equals(10, p5[null]));
Expect.throws(() => p5[null] = p);
Pointer<Foo> p6 = p.cast();
Expect.throws(() => Expect.equals(10, p6[null]));
free(p);
}
testNullArguments() {
Pointer<Int8> p = allocate();
Expect.throws(() => p.value = null);
free(p);
}
testReifiedGeneric() {
final p = allocate<Pointer<Int8>>();
Pointer<Pointer<NativeType>> p2 = p;

View file

@ -162,11 +162,6 @@ Pointer<Int64> store(Pointer<Int64> ptr) => ptr.elementAt(1)..value = 1337;
typedef NullPointersType = Pointer<Int64> Function(Pointer<Int64>);
Pointer<Int64> nullPointers(Pointer<Int64> ptr) => ptr.elementAt(1);
typedef ReturnNullType = Int32 Function();
int returnNull() {
return null;
}
typedef ReturnVoid = Void Function();
void returnVoid() {}
@ -233,7 +228,6 @@ final List<Test> testcases = [
Test("ManyArgs", Pointer.fromFunction<ManyArgsType>(manyArgs, 0.0)),
Test("Store", Pointer.fromFunction<StoreType>(store)),
Test("NullPointers", Pointer.fromFunction<NullPointersType>(nullPointers)),
Test("ReturnNull", Pointer.fromFunction<ReturnNullType>(returnNull, 42)),
Test("ReturnVoid", Pointer.fromFunction<ReturnVoid>(returnVoid)),
Test("ThrowExceptionDouble",
Pointer.fromFunction<ThrowExceptionDouble>(throwExceptionDouble, 42.0)),

View file

@ -35,14 +35,10 @@ void main() {
testNativeFunctionManyArguments4();
testNativeFunctionManyArguments5();
testNativeFunctionPointer();
testNullInt();
testNullDouble();
testNullManyArgs();
testNullPointers();
testFloatRounding();
testVoidReturn();
testNoArgs();
testException();
}
}
@ -399,22 +395,6 @@ void testNativeFunctionPointer() {
free(p2);
}
void testNullInt() {
BinaryOp sumPlus42 =
ffiTestFunctions.lookupFunction<NativeBinaryOp, BinaryOp>("SumPlus42");
Expect.throws(() => sumPlus42(43, null));
}
void testNullDouble() {
Expect.throws(() => times1_337Double(null));
}
void testNullManyArgs() {
Expect.throws(() => sumManyNumbers(1, 2.0, 3, 4.0, 5, 6.0, 7, 8.0, 9, 10.0,
11, 12.0, 13, 14.0, 15, 16.0, 17, 18.0, null, 20.0));
}
Int64PointerUnOp nullableInt64ElemAt1 = ffiTestFunctions
.lookupFunction<Int64PointerUnOp, Int64PointerUnOp>("NullableInt64ElemAt1");
@ -468,14 +448,3 @@ void testNoArgs() {
double result = inventFloatValue();
Expect.approxEquals(1337.0, result);
}
// Throw an exception from within the trampoline and collect a stacktrace
// include its frame.
void testException() {
try {
sumPlus42(null, null);
} catch (e, s) {
return;
}
throw "Didn't throw!";
}

222
tests/ffi/null_test.dart Normal file
View file

@ -0,0 +1,222 @@
// 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 dart:ffi with null values.
//
// Separated into a separate file to make NNBD testing easier.
//
// VMOptions=
// VMOptions=--deterministic --optimization-counter-threshold=10
// VMOptions=--use-slow-path
// VMOptions=--use-slow-path --stacktrace-every=100
// VMOptions=--write-protect-code --no-dual-map-code
// VMOptions=--write-protect-code --no-dual-map-code --use-slow-path
// VMOptions=--write-protect-code --no-dual-map-code --stacktrace-every=100
// SharedObjects=ffi_test_functions
import 'dart:ffi';
import "package:expect/expect.dart";
import "package:ffi/ffi.dart";
import 'dylib_utils.dart';
import 'ffi_test_helpers.dart';
void main() {
for (int i = 0; i < 100; i++) {
testPointerStoreNull();
testEquality();
testNullReceivers();
testNullIndices();
testNullArguments();
testNullInt();
testNullDouble();
testNullManyArgs();
testException();
testNullReturnCallback();
}
}
void testPointerStoreNull() {
int i = null;
Pointer<Int8> p = allocate();
Expect.throws(() => p.value = i);
free(p);
double d = null;
Pointer<Float> p2 = allocate();
Expect.throws(() => p2.value = d);
free(p2);
Pointer<Void> x = null;
Pointer<Pointer<Void>> p3 = allocate();
Expect.throws(() => p3.value = x);
free(p3);
}
void testEquality() {
Pointer<Int8> p = Pointer.fromAddress(12345678);
Expect.notEquals(p, null);
Expect.notEquals(null, p);
}
/// With extension methods, the receiver position can be null.
testNullReceivers() {
Pointer<Int8> p = allocate();
Pointer<Int8> p4 = null;
Expect.throws(() => Expect.equals(10, p4.value));
Expect.throws(() => p4.value = 10);
Pointer<Pointer<Int8>> p5 = null;
Expect.throws(() => Expect.equals(10, p5.value));
Expect.throws(() => p5.value = p);
Pointer<Foo> p6 = null;
Expect.throws(() => Expect.equals(10, p6.ref));
free(p);
}
testNullIndices() {
Pointer<Int8> p = allocate();
Expect.throws(() => Expect.equals(10, p[null]));
Expect.throws(() => p[null] = 10);
Pointer<Pointer<Int8>> p5 = p.cast();
Expect.throws(() => Expect.equals(10, p5[null]));
Expect.throws(() => p5[null] = p);
Pointer<Foo> p6 = p.cast();
Expect.throws(() => Expect.equals(10, p6[null]));
free(p);
}
testNullArguments() {
Pointer<Int8> p = allocate();
Expect.throws(() => p.value = null);
free(p);
}
class Foo extends Struct {
@Int8()
int a;
}
void testNullInt() {
Expect.throws(() => sumPlus42(43, null));
}
void testNullDouble() {
Expect.throws(() => times1_337Double(null));
}
void testNullManyArgs() {
Expect.throws(() => sumManyNumbers(1, 2.0, 3, 4.0, 5, 6.0, 7, 8.0, 9, 10.0,
11, 12.0, 13, 14.0, 15, 16.0, 17, 18.0, null, 20.0));
}
typedef NativeBinaryOp = Int32 Function(Int32, Int32);
typedef BinaryOp = int Function(int, int);
typedef NativeDoubleUnaryOp = Double Function(Double);
typedef DoubleUnaryOp = double Function(double);
DoubleUnaryOp times1_337Double = ffiTestFunctions
.lookupFunction<NativeDoubleUnaryOp, DoubleUnaryOp>("Times1_337Double");
typedef NativeVigesimalOp = Double Function(
IntPtr,
Float,
IntPtr,
Double,
IntPtr,
Float,
IntPtr,
Double,
IntPtr,
Float,
IntPtr,
Double,
IntPtr,
Float,
IntPtr,
Double,
IntPtr,
Float,
IntPtr,
Double);
typedef VigesimalOp = double Function(
int,
double,
int,
double,
int,
double,
int,
double,
int,
double,
int,
double,
int,
double,
int,
double,
int,
double,
int,
double);
VigesimalOp sumManyNumbers = ffiTestFunctions
.lookupFunction<NativeVigesimalOp, VigesimalOp>("SumManyNumbers");
// Throw an exception from within the trampoline and collect a stacktrace
// include its frame.
void testException() {
try {
sumPlus42(null, null);
} catch (e, s) {
return;
}
throw "Didn't throw!";
}
BinaryOp sumPlus42 =
ffiTestFunctions.lookupFunction<NativeBinaryOp, BinaryOp>("SumPlus42");
void testNullReturnCallback() {
final test =
Test("ReturnNull", Pointer.fromFunction<ReturnNullType>(returnNull, 42));
test.run();
}
typedef NativeCallbackTest = Int32 Function(Pointer);
typedef NativeCallbackTestFn = int Function(Pointer);
final DynamicLibrary testLibrary = dlopenPlatformSpecific("ffi_test_functions");
class Test {
final String name;
final Pointer callback;
final bool skip;
Test(this.name, this.callback, {bool skipIf: false}) : skip = skipIf {}
void run() {
if (skip) return;
final NativeCallbackTestFn tester = testLibrary
.lookupFunction<NativeCallbackTest, NativeCallbackTestFn>("Test$name");
final int testCode = tester(callback);
if (testCode != 0) {
Expect.fail("Test $name failed.");
}
}
}
typedef ReturnNullType = Int32 Function();
int returnNull() {
return null;
}

View file

@ -25,7 +25,6 @@ void main() {
void testOpen() {
DynamicLibrary l = dlopenPlatformSpecific("ffi_test_dynamic_library");
Expect.notEquals(null, l);
}
void testOpenError() {
@ -74,8 +73,6 @@ void testEquality() {
DynamicLibrary l2 = dlopenPlatformSpecific("ffi_test_dynamic_library");
Expect.equals(l, l2);
Expect.equals(l.hashCode, l2.hashCode);
Expect.notEquals(l, null);
Expect.notEquals(null, l);
DynamicLibrary l3 = dlopenPlatformSpecific("ffi_test_functions");
Expect.notEquals(l, l3);
}

View file

@ -87,12 +87,10 @@ final largePointer = ffiTestFunctions
// Forces boxing into ffi.Pointer and ffi.Mint.
void testBoxPointer() {
ffi.Pointer pointer = largePointer();
if (pointer != null) {
if (ffi.sizeOf<ffi.Pointer>() == 4) {
Expect.equals(0x82000000, pointer.address);
} else {
Expect.equals(0x8100000082000000, pointer.address);
}
if (ffi.sizeOf<ffi.Pointer>() == 4) {
Expect.equals(0x82000000, pointer.address);
} else {
Expect.equals(0x8100000082000000, pointer.address);
}
}

View file

@ -0,0 +1,35 @@
// 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 dart:ffi with null values.
//
// Separated into a separate file to make NNBD testing easier.
//
// SharedObjects=ffi_test_dynamic_library ffi_test_functions
import 'dart:ffi';
import 'package:expect/expect.dart';
import 'dylib_utils.dart';
void main() {
testOpen();
testEquality();
}
void testOpen() {
DynamicLibrary l = dlopenPlatformSpecific("ffi_test_dynamic_library");
Expect.notEquals(null, l);
}
typedef NativeDoubleUnOp = Double Function(Double);
typedef DoubleUnOp = double Function(double);
void testEquality() {
DynamicLibrary l = dlopenPlatformSpecific("ffi_test_dynamic_library");
Expect.notEquals(l, null);
Expect.notEquals(null, l);
}

View file

@ -240,10 +240,8 @@ class X {
double tearoff(double d) => d / 27.0;
}
DoubleUnOp fld = null;
void testFromFunctionTearOff() {
fld = X().tearoff;
DoubleUnOp fld = X().tearoff;
Pointer<NativeFunction<NativeDoubleUnOp>> p;
p = Pointer.fromFunction(fld); //# 75: compile-time error
}