Skip to content

Commit 6ef57b8

Browse files
committed
[vm/ffi] Support varargs
This CL introduces `VarArgs` to `NativeFunction` signatures. The `VarArgs` type takes a single type argument. This type argument is a subtype of `NativeType` if there is a single variadic argument, and a record with native types if there are multiple variadic arguments. For example: `NativeFunction<Void Function(Pointer<Char>, VarArgs<(Int32,Int32)>)>` for calling refering to a `printf` binding with two `int32_t` arguments passed as variadic arguments. The logic of the native calling conventions are detailed in https://dart-review.googlesource.com/c/sdk/+/278342. Here we explain how this influences the FFI pipeline. First, now that `VarArgs` is part of signatures, we have to unwrap that when with the C types in the CFE transform and checking (analyzer is in a separate CL), and also in the marshaller when looking up the C type of arguments. Second, we have to deal with `BothNativeLocations`. On windows x64, floating point arguments must be passed both in FPU _and_ CPU registers. For FFI calls, we solve this in the argument moves by just copying to both locations. For FFI callbacks, we just take the FPU register location (which avoids an extra bitcast). Third, on System-V, we have to pass an upper bound of the number of XMM registers used in AL. This means we instead RAX, we use R13 for the target address. For variadic calls, we always pass 8 in AL as the valid upper bound. We could consider passing the actual number of XMM registers used. We keep using RAX as default register for the function address on non- variadic calls, because changing to R13 (the first free) register creates more spilling in leaf calls. R13 is callee-saved while RAX is not, so using R13 instead of RAX causes us to have to spill the value from RAX on leaf calls. Fourth, on both x64 and RISC-V, we pass floats in integer locations. `EmitNativeMove` has been modified to deal with this, so that we do not have to insert more `BitCastInstr`s. The tests are generated by a test generator: `tests/ffi/generator/`. The formatter doesn't support records yet, so the tests are not properly formatted. Bug: #50798 TEST=tests/ffi/*_varargs_* Closes: #38578 Closes: #49460 Closes: #50858 Change-Id: I6a6296fe972527f8a54ac75a630131769e3cc540 Cq-Include-Trybots: luci.dart.try:vm-kernel-reload-rollback-linux-debug-x64-try,vm-kernel-reload-linux-debug-x64-try,vm-kernel-linux-debug-ia32-try,vm-kernel-nnbd-linux-debug-ia32-try,vm-kernel-win-debug-ia32-try,vm-kernel-linux-debug-x64-try,vm-kernel-mac-debug-x64-try,vm-kernel-win-debug-x64-try,vm-kernel-nnbd-win-release-ia32-try,vm-kernel-nnbd-win-debug-x64-try,vm-ffi-android-debug-arm-try,vm-ffi-android-debug-arm64c-try,vm-kernel-precomp-android-release-arm64c-try,vm-kernel-precomp-android-release-arm_x64-try,vm-precomp-ffi-qemu-linux-release-arm-try,vm-precomp-ffi-qemu-linux-release-riscv64-try,vm-kernel-asan-linux-release-x64-try,vm-kernel-precomp-asan-linux-release-x64-try,vm-kernel-msan-linux-release-x64-try,vm-kernel-precomp-msan-linux-release-x64-try,app-kernel-linux-debug-x64-try,vm-kernel-mac-release-arm64-try,vm-kernel-nnbd-mac-debug-arm64-try,vm-kernel-nnbd-mac-debug-x64-try Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/276921 Reviewed-by: Devon Carew <[email protected]> Reviewed-by: Ryan Macnak <[email protected]>
1 parent 7e60e54 commit 6ef57b8

32 files changed

+5437
-661
lines changed

pkg/front_end/testcases/incremental/no_outline_change_50_ffi.yaml.world.1.expect

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ additionalExports = (ffi::nullptr,
123123
ffi::UnsignedLongLong,
124124
ffi::UnsignedShort,
125125
ffi::Unsized,
126+
ffi::VarArgs,
126127
ffi::Void,
127128
ffi::WChar)
128129

@@ -218,6 +219,7 @@ additionalExports = (ffi::nullptr,
218219
ffi::UnsignedLongLong,
219220
ffi::UnsignedShort,
220221
ffi::Unsized,
222+
ffi::VarArgs,
221223
ffi::Void,
222224
ffi::WChar)
223225

pkg/front_end/testcases/incremental/no_outline_change_50_ffi.yaml.world.2.expect

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ additionalExports = (ffi::nullptr,
123123
ffi::UnsignedLongLong,
124124
ffi::UnsignedShort,
125125
ffi::Unsized,
126+
ffi::VarArgs,
126127
ffi::Void,
127128
ffi::WChar)
128129

@@ -218,6 +219,7 @@ additionalExports = (ffi::nullptr,
218219
ffi::UnsignedLongLong,
219220
ffi::UnsignedShort,
220221
ffi::Unsized,
222+
ffi::VarArgs,
221223
ffi::Void,
222224
ffi::WChar)
223225

pkg/vm/lib/transformations/ffi/common.dart

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@ class FfiTransformer extends Transformer {
185185
final Class unionClass;
186186
final Class abiSpecificIntegerClass;
187187
final Class abiSpecificIntegerMappingClass;
188+
final Class varArgsClass;
188189
final Class ffiNativeClass;
189190
final Class nativeFieldWrapperClass1Class;
190191
final Class ffiStructLayoutClass;
@@ -341,6 +342,7 @@ class FfiTransformer extends Transformer {
341342
index.getClass('dart:ffi', 'AbiSpecificInteger'),
342343
abiSpecificIntegerMappingClass =
343344
index.getClass('dart:ffi', 'AbiSpecificIntegerMapping'),
345+
varArgsClass = index.getClass('dart:ffi', 'VarArgs'),
344346
ffiNativeClass = index.getClass('dart:ffi', 'FfiNative'),
345347
nativeFieldWrapperClass1Class =
346348
index.getClass('dart:nativewrappers', 'NativeFieldWrapperClass1'),
@@ -614,12 +616,40 @@ class FfiTransformer extends Transformer {
614616
final DartType? returnType = convertNativeTypeToDartType(fun.returnType,
615617
allowCompounds: true, allowHandle: true);
616618
if (returnType == null) return null;
617-
final List<DartType> argumentTypes = fun.positionalParameters
618-
.map((t) =>
619-
convertNativeTypeToDartType(t,
620-
allowCompounds: true, allowHandle: true) ??
621-
dummyDartType)
622-
.toList();
619+
final argumentTypes = <DartType>[];
620+
bool seenVarArgs = false;
621+
for (final paramDartType in fun.positionalParameters) {
622+
if (seenVarArgs) {
623+
// VarArgs is not last.
624+
return null;
625+
}
626+
if (paramDartType is InterfaceType &&
627+
paramDartType.classNode == varArgsClass) {
628+
seenVarArgs = true;
629+
final typeArgument = paramDartType.typeArguments.single;
630+
if (typeArgument is RecordType) {
631+
if (typeArgument.named.isNotEmpty) {
632+
// Named record fields are not supported.
633+
return null;
634+
}
635+
for (final paramDartType in typeArgument.positional) {
636+
argumentTypes.add(
637+
convertNativeTypeToDartType(paramDartType,
638+
allowCompounds: true, allowHandle: true) ??
639+
dummyDartType,
640+
);
641+
}
642+
} else {
643+
return null;
644+
}
645+
} else {
646+
argumentTypes.add(
647+
convertNativeTypeToDartType(paramDartType,
648+
allowCompounds: true, allowHandle: true) ??
649+
dummyDartType,
650+
);
651+
}
652+
}
623653
if (argumentTypes.contains(dummyDartType)) return null;
624654
return FunctionType(argumentTypes, returnType, Nullability.legacy);
625655
}

pkg/vm/lib/transformations/ffi/use_sites.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -755,6 +755,7 @@ mixin _FfiUseSiteTransformer on FfiTransformer {
755755
klass == structClass ||
756756
klass == unionClass ||
757757
klass == abiSpecificIntegerClass ||
758+
klass == varArgsClass ||
758759
classNativeTypes[klass] != null) {
759760
return null;
760761
}

runtime/bin/ffi_test/ffi_test_functions.cc

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
// therefore not allowed to use `dart_api.h`. (The flutter/flutter integration
88
// tests will run dart tests using this library only.)
99

10+
#include <stdarg.h>
1011
#include <stddef.h>
1112
#include <stdlib.h>
1213
#include <sys/types.h>
@@ -1175,4 +1176,28 @@ DART_EXPORT int64_t WCharMaxValue() {
11751176
return WCHAR_MAX;
11761177
}
11771178

1179+
struct VarArgs {
1180+
int32_t a;
1181+
};
1182+
1183+
DART_EXPORT int64_t VariadicStructVarArgs(VarArgs a0, ...) {
1184+
va_list var_args;
1185+
va_start(var_args, a0);
1186+
VarArgs a1 = va_arg(var_args, VarArgs);
1187+
va_end(var_args);
1188+
1189+
std::cout << "VariadicStructVarArgs"
1190+
<< "(" << a0.a << ", " << a1.a << ")"
1191+
<< "\n";
1192+
1193+
int64_t result = 0;
1194+
1195+
result += a0.a;
1196+
result += a1.a;
1197+
1198+
std::cout << "result = " << result << "\n";
1199+
1200+
return result;
1201+
}
1202+
11781203
} // namespace dart

0 commit comments

Comments
 (0)