Skip to content

Commit ec66dee

Browse files
dcharkescommit-bot@chromium.org
authored andcommitted
[vm/ffi] Split out tests which use nulls
Issue: #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 <[email protected]> Reviewed-by: Martin Kustermann <[email protected]>
1 parent e8c1391 commit ec66dee

10 files changed

+262
-110
lines changed

tests/ffi/data_test.dart

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ void main() {
3939
testVoid();
4040
testPointerPointer();
4141
testPointerPointerNull();
42-
testPointerStoreNull();
4342
testSizeOf();
4443
testPointerChain(100);
4544
testTypeTest();
@@ -345,21 +344,6 @@ void testPointerPointerNull() {
345344
free(pointerToPointer);
346345
}
347346

348-
void testPointerStoreNull() {
349-
int i = null;
350-
Pointer<Int8> p = allocate();
351-
Expect.throws(() => p.value = i);
352-
free(p);
353-
double d = null;
354-
Pointer<Float> p2 = allocate();
355-
Expect.throws(() => p2.value = d);
356-
free(p2);
357-
Pointer<Void> x = null;
358-
Pointer<Pointer<Void>> p3 = allocate();
359-
Expect.throws(() => p3.value = x);
360-
free(p3);
361-
}
362-
363347
void testSizeOf() {
364348
Expect.equals(1, sizeOf<Int8>());
365349
Expect.equals(2, sizeOf<Int16>());
@@ -433,8 +417,6 @@ void testEquality() {
433417
Pointer<Int16> p3 = p.cast();
434418
Expect.equals(p, p3);
435419
Expect.equals(p.hashCode, p3.hashCode);
436-
Expect.notEquals(p, null);
437-
Expect.notEquals(null, p);
438420
Pointer<Int8> p4 = p.offsetBy(1337);
439421
Expect.notEquals(p, p4);
440422
}

tests/ffi/extension_methods_test.dart

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@ import "package:ffi/ffi.dart";
1010
main(List<String> arguments) {
1111
for (int i = 0; i < 100; i++) {
1212
testStoreLoad();
13-
testNullReceivers();
14-
testNullIndices();
15-
testNullArguments();
1613
testReifiedGeneric();
1714
}
1815
}
@@ -55,46 +52,6 @@ testStoreLoad() {
5552
free(p3);
5653
}
5754

58-
/// With extension methods, the receiver position can be null.
59-
testNullReceivers() {
60-
Pointer<Int8> p = allocate();
61-
62-
Pointer<Int8> p4 = null;
63-
Expect.throws(() => Expect.equals(10, p4.value));
64-
Expect.throws(() => p4.value = 10);
65-
66-
Pointer<Pointer<Int8>> p5 = null;
67-
Expect.throws(() => Expect.equals(10, p5.value));
68-
Expect.throws(() => p5.value = p);
69-
70-
Pointer<Foo> p6 = null;
71-
Expect.throws(() => Expect.equals(10, p6.ref));
72-
73-
free(p);
74-
}
75-
76-
testNullIndices() {
77-
Pointer<Int8> p = allocate();
78-
79-
Expect.throws(() => Expect.equals(10, p[null]));
80-
Expect.throws(() => p[null] = 10);
81-
82-
Pointer<Pointer<Int8>> p5 = p.cast();
83-
Expect.throws(() => Expect.equals(10, p5[null]));
84-
Expect.throws(() => p5[null] = p);
85-
86-
Pointer<Foo> p6 = p.cast();
87-
Expect.throws(() => Expect.equals(10, p6[null]));
88-
89-
free(p);
90-
}
91-
92-
testNullArguments() {
93-
Pointer<Int8> p = allocate();
94-
Expect.throws(() => p.value = null);
95-
free(p);
96-
}
97-
9855
testReifiedGeneric() {
9956
final p = allocate<Pointer<Int8>>();
10057
Pointer<Pointer<NativeType>> p2 = p;

tests/ffi/function_callbacks_test.dart

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -162,11 +162,6 @@ Pointer<Int64> store(Pointer<Int64> ptr) => ptr.elementAt(1)..value = 1337;
162162
typedef NullPointersType = Pointer<Int64> Function(Pointer<Int64>);
163163
Pointer<Int64> nullPointers(Pointer<Int64> ptr) => ptr.elementAt(1);
164164

165-
typedef ReturnNullType = Int32 Function();
166-
int returnNull() {
167-
return null;
168-
}
169-
170165
typedef ReturnVoid = Void Function();
171166
void returnVoid() {}
172167

@@ -233,7 +228,6 @@ final List<Test> testcases = [
233228
Test("ManyArgs", Pointer.fromFunction<ManyArgsType>(manyArgs, 0.0)),
234229
Test("Store", Pointer.fromFunction<StoreType>(store)),
235230
Test("NullPointers", Pointer.fromFunction<NullPointersType>(nullPointers)),
236-
Test("ReturnNull", Pointer.fromFunction<ReturnNullType>(returnNull, 42)),
237231
Test("ReturnVoid", Pointer.fromFunction<ReturnVoid>(returnVoid)),
238232
Test("ThrowExceptionDouble",
239233
Pointer.fromFunction<ThrowExceptionDouble>(throwExceptionDouble, 42.0)),

tests/ffi/function_test.dart

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,10 @@ void main() {
3535
testNativeFunctionManyArguments4();
3636
testNativeFunctionManyArguments5();
3737
testNativeFunctionPointer();
38-
testNullInt();
39-
testNullDouble();
40-
testNullManyArgs();
4138
testNullPointers();
4239
testFloatRounding();
4340
testVoidReturn();
4441
testNoArgs();
45-
testException();
4642
}
4743
}
4844

@@ -399,22 +395,6 @@ void testNativeFunctionPointer() {
399395
free(p2);
400396
}
401397

402-
void testNullInt() {
403-
BinaryOp sumPlus42 =
404-
ffiTestFunctions.lookupFunction<NativeBinaryOp, BinaryOp>("SumPlus42");
405-
406-
Expect.throws(() => sumPlus42(43, null));
407-
}
408-
409-
void testNullDouble() {
410-
Expect.throws(() => times1_337Double(null));
411-
}
412-
413-
void testNullManyArgs() {
414-
Expect.throws(() => sumManyNumbers(1, 2.0, 3, 4.0, 5, 6.0, 7, 8.0, 9, 10.0,
415-
11, 12.0, 13, 14.0, 15, 16.0, 17, 18.0, null, 20.0));
416-
}
417-
418398
Int64PointerUnOp nullableInt64ElemAt1 = ffiTestFunctions
419399
.lookupFunction<Int64PointerUnOp, Int64PointerUnOp>("NullableInt64ElemAt1");
420400

@@ -468,14 +448,3 @@ void testNoArgs() {
468448
double result = inventFloatValue();
469449
Expect.approxEquals(1337.0, result);
470450
}
471-
472-
// Throw an exception from within the trampoline and collect a stacktrace
473-
// include its frame.
474-
void testException() {
475-
try {
476-
sumPlus42(null, null);
477-
} catch (e, s) {
478-
return;
479-
}
480-
throw "Didn't throw!";
481-
}

tests/ffi/null_test.dart

Lines changed: 222 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,222 @@
1+
// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
//
5+
// Dart test program for testing dart:ffi with null values.
6+
//
7+
// Separated into a separate file to make NNBD testing easier.
8+
//
9+
// VMOptions=
10+
// VMOptions=--deterministic --optimization-counter-threshold=10
11+
// VMOptions=--use-slow-path
12+
// VMOptions=--use-slow-path --stacktrace-every=100
13+
// VMOptions=--write-protect-code --no-dual-map-code
14+
// VMOptions=--write-protect-code --no-dual-map-code --use-slow-path
15+
// VMOptions=--write-protect-code --no-dual-map-code --stacktrace-every=100
16+
// SharedObjects=ffi_test_functions
17+
18+
import 'dart:ffi';
19+
20+
import "package:expect/expect.dart";
21+
import "package:ffi/ffi.dart";
22+
23+
import 'dylib_utils.dart';
24+
import 'ffi_test_helpers.dart';
25+
26+
void main() {
27+
for (int i = 0; i < 100; i++) {
28+
testPointerStoreNull();
29+
testEquality();
30+
testNullReceivers();
31+
testNullIndices();
32+
testNullArguments();
33+
testNullInt();
34+
testNullDouble();
35+
testNullManyArgs();
36+
testException();
37+
testNullReturnCallback();
38+
}
39+
}
40+
41+
void testPointerStoreNull() {
42+
int i = null;
43+
Pointer<Int8> p = allocate();
44+
Expect.throws(() => p.value = i);
45+
free(p);
46+
double d = null;
47+
Pointer<Float> p2 = allocate();
48+
Expect.throws(() => p2.value = d);
49+
free(p2);
50+
Pointer<Void> x = null;
51+
Pointer<Pointer<Void>> p3 = allocate();
52+
Expect.throws(() => p3.value = x);
53+
free(p3);
54+
}
55+
56+
void testEquality() {
57+
Pointer<Int8> p = Pointer.fromAddress(12345678);
58+
Expect.notEquals(p, null);
59+
Expect.notEquals(null, p);
60+
}
61+
62+
/// With extension methods, the receiver position can be null.
63+
testNullReceivers() {
64+
Pointer<Int8> p = allocate();
65+
66+
Pointer<Int8> p4 = null;
67+
Expect.throws(() => Expect.equals(10, p4.value));
68+
Expect.throws(() => p4.value = 10);
69+
70+
Pointer<Pointer<Int8>> p5 = null;
71+
Expect.throws(() => Expect.equals(10, p5.value));
72+
Expect.throws(() => p5.value = p);
73+
74+
Pointer<Foo> p6 = null;
75+
Expect.throws(() => Expect.equals(10, p6.ref));
76+
77+
free(p);
78+
}
79+
80+
testNullIndices() {
81+
Pointer<Int8> p = allocate();
82+
83+
Expect.throws(() => Expect.equals(10, p[null]));
84+
Expect.throws(() => p[null] = 10);
85+
86+
Pointer<Pointer<Int8>> p5 = p.cast();
87+
Expect.throws(() => Expect.equals(10, p5[null]));
88+
Expect.throws(() => p5[null] = p);
89+
90+
Pointer<Foo> p6 = p.cast();
91+
Expect.throws(() => Expect.equals(10, p6[null]));
92+
93+
free(p);
94+
}
95+
96+
testNullArguments() {
97+
Pointer<Int8> p = allocate();
98+
Expect.throws(() => p.value = null);
99+
free(p);
100+
}
101+
102+
class Foo extends Struct {
103+
@Int8()
104+
int a;
105+
}
106+
107+
void testNullInt() {
108+
Expect.throws(() => sumPlus42(43, null));
109+
}
110+
111+
void testNullDouble() {
112+
Expect.throws(() => times1_337Double(null));
113+
}
114+
115+
void testNullManyArgs() {
116+
Expect.throws(() => sumManyNumbers(1, 2.0, 3, 4.0, 5, 6.0, 7, 8.0, 9, 10.0,
117+
11, 12.0, 13, 14.0, 15, 16.0, 17, 18.0, null, 20.0));
118+
}
119+
120+
typedef NativeBinaryOp = Int32 Function(Int32, Int32);
121+
typedef BinaryOp = int Function(int, int);
122+
123+
typedef NativeDoubleUnaryOp = Double Function(Double);
124+
typedef DoubleUnaryOp = double Function(double);
125+
126+
DoubleUnaryOp times1_337Double = ffiTestFunctions
127+
.lookupFunction<NativeDoubleUnaryOp, DoubleUnaryOp>("Times1_337Double");
128+
129+
typedef NativeVigesimalOp = Double Function(
130+
IntPtr,
131+
Float,
132+
IntPtr,
133+
Double,
134+
IntPtr,
135+
Float,
136+
IntPtr,
137+
Double,
138+
IntPtr,
139+
Float,
140+
IntPtr,
141+
Double,
142+
IntPtr,
143+
Float,
144+
IntPtr,
145+
Double,
146+
IntPtr,
147+
Float,
148+
IntPtr,
149+
Double);
150+
typedef VigesimalOp = double Function(
151+
int,
152+
double,
153+
int,
154+
double,
155+
int,
156+
double,
157+
int,
158+
double,
159+
int,
160+
double,
161+
int,
162+
double,
163+
int,
164+
double,
165+
int,
166+
double,
167+
int,
168+
double,
169+
int,
170+
double);
171+
172+
VigesimalOp sumManyNumbers = ffiTestFunctions
173+
.lookupFunction<NativeVigesimalOp, VigesimalOp>("SumManyNumbers");
174+
175+
// Throw an exception from within the trampoline and collect a stacktrace
176+
// include its frame.
177+
void testException() {
178+
try {
179+
sumPlus42(null, null);
180+
} catch (e, s) {
181+
return;
182+
}
183+
throw "Didn't throw!";
184+
}
185+
186+
BinaryOp sumPlus42 =
187+
ffiTestFunctions.lookupFunction<NativeBinaryOp, BinaryOp>("SumPlus42");
188+
189+
void testNullReturnCallback() {
190+
final test =
191+
Test("ReturnNull", Pointer.fromFunction<ReturnNullType>(returnNull, 42));
192+
test.run();
193+
}
194+
195+
typedef NativeCallbackTest = Int32 Function(Pointer);
196+
typedef NativeCallbackTestFn = int Function(Pointer);
197+
198+
final DynamicLibrary testLibrary = dlopenPlatformSpecific("ffi_test_functions");
199+
200+
class Test {
201+
final String name;
202+
final Pointer callback;
203+
final bool skip;
204+
205+
Test(this.name, this.callback, {bool skipIf: false}) : skip = skipIf {}
206+
207+
void run() {
208+
if (skip) return;
209+
210+
final NativeCallbackTestFn tester = testLibrary
211+
.lookupFunction<NativeCallbackTest, NativeCallbackTestFn>("Test$name");
212+
final int testCode = tester(callback);
213+
if (testCode != 0) {
214+
Expect.fail("Test $name failed.");
215+
}
216+
}
217+
}
218+
219+
typedef ReturnNullType = Int32 Function();
220+
int returnNull() {
221+
return null;
222+
}

0 commit comments

Comments
 (0)