Skip to content

Commit a228bf5

Browse files
dcharkescommit-bot@chromium.org
authored andcommitted
[vm/ffi] Test alignment of small stack arguments
Test exercises alignment on stack, will currently fail on iOS arm64. In addition, the Dart functions used as callbacks in tests now print their arguments for debugging purposes. Issue: #39637 Splitting test off large CL (https://dart-review.googlesource.com/c/sdk/+/129081) to be able to land separately. Change-Id: Iba3c63338f5d91d6e3819e54c166bbfade48d53f 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/+/131074 Reviewed-by: Teagan Strickland <[email protected]> Commit-Queue: Daco Harkes <[email protected]>
1 parent d08f070 commit a228bf5

File tree

4 files changed

+67
-5
lines changed

4 files changed

+67
-5
lines changed

runtime/bin/ffi_test/ffi_test_functions.cc

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,28 @@ DART_EXPORT intptr_t SumManyInts(intptr_t a,
186186
return retval;
187187
}
188188

189+
// Sums many ints.
190+
// Used for testing calling conventions. With small integers on stack slots we
191+
// test stack alignment.
192+
DART_EXPORT int16_t SumManySmallInts(int8_t a,
193+
int16_t b,
194+
int8_t c,
195+
int16_t d,
196+
int8_t e,
197+
int16_t f,
198+
int8_t g,
199+
int16_t h,
200+
int8_t i,
201+
int16_t j) {
202+
std::cout << "SumManySmallInts(" << static_cast<int>(a) << ", " << b << ", "
203+
<< static_cast<int>(c) << ", " << d << ", " << static_cast<int>(e)
204+
<< ", " << f << ", " << static_cast<int>(g) << ", " << h << ", "
205+
<< static_cast<int>(i) << ", " << j << ")\n";
206+
int16_t retval = a + b + c + d + e + f + g + h + i + j;
207+
std::cout << "returning " << retval << "\n";
208+
return retval;
209+
}
210+
189211
// Sums an odd number of ints.
190212
// Used for testing calling conventions. With so many arguments, and an odd
191213
// number of arguments, we are testing stack alignment on various architectures.

samples/ffi/sample_ffi_functions.dart

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ typedef NativeDoubleUnaryOp = Double Function(Double);
2020
typedef NativeFloatUnaryOp = Float Function(Float);
2121
typedef NativeDecenaryOp = IntPtr Function(IntPtr, IntPtr, IntPtr, IntPtr,
2222
IntPtr, IntPtr, IntPtr, IntPtr, IntPtr, IntPtr);
23+
typedef NativeDecenaryOp2 = Int16 Function(
24+
Int8, Int16, Int8, Int16, Int8, Int16, Int8, Int16, Int8, Int16);
2325
typedef NativeDoubleDecenaryOp = Double Function(Double, Double, Double, Double,
2426
Double, Double, Double, Double, Double, Double);
2527
typedef NativeVigesimalOp = Double Function(
@@ -149,6 +151,15 @@ main() {
149151
print(result.runtimeType);
150152
}
151153

154+
{
155+
// Function with many arguments: arguments get passed in registers and stack.
156+
DecenaryOp sumManyInts = ffiTestFunctions
157+
.lookupFunction<NativeDecenaryOp2, DecenaryOp>("SumManySmallInts");
158+
var result = sumManyInts(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
159+
print(result);
160+
print(result.runtimeType);
161+
}
162+
152163
{
153164
// Function with many double arguments.
154165
DoubleDecenaryOp sumManyDoubles = ffiTestFunctions.lookupFunction<

tests/ffi/function_callbacks_test.dart

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,31 +45,48 @@ class Test {
4545
}
4646

4747
typedef SimpleAdditionType = Int32 Function(Int32, Int32);
48-
int simpleAddition(int x, int y) => x + y;
48+
int simpleAddition(int x, int y) {
49+
print("simpleAddition($x, $y)");
50+
return x + y;
51+
}
4952

5053
typedef IntComputationType = Int64 Function(Int8, Int16, Int32, Int64);
51-
int intComputation(int a, int b, int c, int d) => d - c + b - a;
54+
int intComputation(int a, int b, int c, int d) {
55+
print("intComputation($a, $b, $c, $d)");
56+
return d - c + b - a;
57+
}
5258

5359
typedef UintComputationType = Uint64 Function(Uint8, Uint16, Uint32, Uint64);
54-
int uintComputation(int a, int b, int c, int d) => d - c + b - a;
60+
int uintComputation(int a, int b, int c, int d) {
61+
print("uintComputation($a, $b, $c, $d)");
62+
return d - c + b - a;
63+
}
5564

5665
typedef SimpleMultiplyType = Double Function(Double);
57-
double simpleMultiply(double x) => x * 1.337;
66+
double simpleMultiply(double x) {
67+
print("simpleMultiply($x)");
68+
return x * 1.337;
69+
}
5870

5971
typedef SimpleMultiplyFloatType = Float Function(Float);
60-
double simpleMultiplyFloat(double x) => x * 1.337;
72+
double simpleMultiplyFloat(double x) {
73+
print("simpleMultiplyFloat($x)");
74+
return x * 1.337;
75+
}
6176

6277
typedef ManyIntsType = IntPtr Function(IntPtr, IntPtr, IntPtr, IntPtr, IntPtr,
6378
IntPtr, IntPtr, IntPtr, IntPtr, IntPtr);
6479
int manyInts(
6580
int a, int b, int c, int d, int e, int f, int g, int h, int i, int j) {
81+
print("manyInts($a, $b, $c, $d, $e, $f, $g, $h, $i, $j");
6682
return a + b + c + d + e + f + g + h + i + j;
6783
}
6884

6985
typedef ManyDoublesType = Double Function(Double, Double, Double, Double,
7086
Double, Double, Double, Double, Double, Double);
7187
double manyDoubles(double a, double b, double c, double d, double e, double f,
7288
double g, double h, double i, double j) {
89+
print("manyDoubles($a, $b, $c, $d, $e, $f, $g, $h, $i, $j");
7390
return a + b + c + d + e + f + g + h + i + j;
7491
}
7592

@@ -115,6 +132,8 @@ double manyArgs(
115132
double _18,
116133
int _19,
117134
double _20) {
135+
print("manyArgs( $_1, $_2, $_3, $_4, $_5, $_6, $_7, $_8, $_9, $_10," +
136+
"$_11, $_12, $_13, $_14, $_15, $_16, $_17, $_18, $_19, $_20)");
118137
return _1 +
119138
_2 +
120139
_3 +

tests/ffi/function_test.dart

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ void main() {
3333
testNativeFunctionManyArguments2();
3434
testNativeFunctionManyArguments3();
3535
testNativeFunctionManyArguments4();
36+
testNativeFunctionManyArguments5();
3637
testNativeFunctionPointer();
3738
testNullInt();
3839
testNullDouble();
@@ -229,6 +230,8 @@ void testNativeFunctionFloats() {
229230

230231
typedef NativeDecenaryOp = IntPtr Function(IntPtr, IntPtr, IntPtr, IntPtr,
231232
IntPtr, IntPtr, IntPtr, IntPtr, IntPtr, IntPtr);
233+
typedef NativeDecenaryOp2 = Int16 Function(
234+
Int8, Int16, Int8, Int16, Int8, Int16, Int8, Int16, Int8, Int16);
232235
typedef DecenaryOp = int Function(
233236
int, int, int, int, int, int, int, int, int, int);
234237

@@ -239,6 +242,13 @@ void testNativeFunctionManyArguments1() {
239242
Expect.equals(55, sumManyInts(1, 2, 3, 4, 5, 6, 7, 8, 9, 10));
240243
}
241244

245+
DecenaryOp sumManySmallInts = ffiTestFunctions
246+
.lookupFunction<NativeDecenaryOp2, DecenaryOp>("SumManySmallInts");
247+
248+
void testNativeFunctionManyArguments5() {
249+
Expect.equals(55, sumManySmallInts(1, 2, 3, 4, 5, 6, 7, 8, 9, 10));
250+
}
251+
242252
typedef NativeUndenaryOp = IntPtr Function(IntPtr, IntPtr, IntPtr, IntPtr,
243253
IntPtr, IntPtr, IntPtr, IntPtr, IntPtr, IntPtr, IntPtr);
244254
typedef UndenaryOp = int Function(

0 commit comments

Comments
 (0)