Skip to content

Commit 70e4ff3

Browse files
committed
Revert "[vm/ffi] Unwrap typed data in FFI calls"
This reverts: https://dart-review.googlesource.com/c/sdk/+/338620 We'd like to support this use case with a different API. See the discussion in #54739. TEST=tests/ffi Bug: #44589 Bug: #54771 Change-Id: Ic22fbcab14d374bb9c81bba1f1bf6ae2dfc9e674 Cq-Include-Trybots: luci.dart.try:vm-aot-android-release-arm64c-try,vm-aot-android-release-arm_x64-try,vm-aot-linux-debug-x64-try,vm-aot-linux-debug-x64c-try,vm-aot-mac-release-arm64-try,vm-aot-mac-release-x64-try,vm-aot-obfuscate-linux-release-x64-try,vm-aot-optimization-level-linux-release-x64-try,vm-aot-win-debug-arm64-try,vm-aot-win-debug-x64c-try,vm-aot-win-release-x64-try,vm-appjit-linux-debug-x64-try,vm-asan-linux-release-x64-try,vm-checked-mac-release-arm64-try,vm-eager-optimization-linux-release-ia32-try,vm-eager-optimization-linux-release-x64-try,vm-ffi-android-debug-arm-try,vm-ffi-android-debug-arm64c-try,vm-ffi-qemu-linux-release-arm-try,vm-ffi-qemu-linux-release-riscv64-try,vm-fuchsia-release-x64-try,vm-kernel-linux-debug-x64-try,vm-kernel-precomp-linux-release-x64-try,vm-linux-debug-ia32-try,vm-linux-debug-x64-try,vm-linux-debug-x64c-try,vm-mac-debug-arm64-try,vm-mac-debug-x64-try,vm-msan-linux-release-x64-try,vm-reload-linux-debug-x64-try,vm-reload-rollback-linux-debug-x64-try,vm-ubsan-linux-release-x64-try,vm-win-debug-arm64-try,vm-win-debug-x64-try,vm-win-debug-x64c-try,vm-win-release-ia32-try Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/349340 Commit-Queue: Daco Harkes <[email protected]> Reviewed-by: Martin Kustermann <[email protected]>
1 parent 4dff1b3 commit 70e4ff3

File tree

23 files changed

+2
-4235
lines changed

23 files changed

+2
-4235
lines changed

pkg/_fe_analyzer_shared/lib/src/messages/codes_generated.dart

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -5005,29 +5005,6 @@ const MessageCode messageFfiAddressOfMustBeNative = const MessageCode(
50055005
problemMessage:
50065006
r"""Argument to 'Native.addressOf' must be annotated with @Native.""");
50075007

5008-
// DO NOT EDIT. THIS FILE IS GENERATED. SEE TOP OF FILE.
5009-
const Code<Null> codeFfiCallMustNotReturnTypedData =
5010-
messageFfiCallMustNotReturnTypedData;
5011-
5012-
// DO NOT EDIT. THIS FILE IS GENERATED. SEE TOP OF FILE.
5013-
const MessageCode messageFfiCallMustNotReturnTypedData = const MessageCode(
5014-
"FfiCallMustNotReturnTypedData",
5015-
analyzerCodes: <String>["CALL_MUST_NOT_RETURN_TYPED_DATA"],
5016-
problemMessage: r"""FFI calls can't return typed data.""",
5017-
correctionMessage: r"""Try changing using `Pointer`s.""");
5018-
5019-
// DO NOT EDIT. THIS FILE IS GENERATED. SEE TOP OF FILE.
5020-
const Code<Null> codeFfiCallbackMustNotUseTypedData =
5021-
messageFfiCallbackMustNotUseTypedData;
5022-
5023-
// DO NOT EDIT. THIS FILE IS GENERATED. SEE TOP OF FILE.
5024-
const MessageCode messageFfiCallbackMustNotUseTypedData = const MessageCode(
5025-
"FfiCallbackMustNotUseTypedData",
5026-
analyzerCodes: <String>["CALLBACK_MUST_NOT_USE_TYPED_DATA"],
5027-
problemMessage:
5028-
r"""FFI callbacks can't take typed data arguments or return value.""",
5029-
correctionMessage: r"""Try changing using `Pointer`s.""");
5030-
50315008
// DO NOT EDIT. THIS FILE IS GENERATED. SEE TOP OF FILE.
50325009
const Template<Message Function(String string, String name)>
50335010
templateFfiCompoundImplementsFinalizable =
@@ -5430,17 +5407,6 @@ Message _withArgumentsFfiNativeUnexpectedNumberOfParametersWithReceiver(
54305407
arguments: {'count': count, 'count2': count2});
54315408
}
54325409

5433-
// DO NOT EDIT. THIS FILE IS GENERATED. SEE TOP OF FILE.
5434-
const Code<Null> codeFfiNonLeafCallMustNotTakeTypedData =
5435-
messageFfiNonLeafCallMustNotTakeTypedData;
5436-
5437-
// DO NOT EDIT. THIS FILE IS GENERATED. SEE TOP OF FILE.
5438-
const MessageCode messageFfiNonLeafCallMustNotTakeTypedData = const MessageCode(
5439-
"FfiNonLeafCallMustNotTakeTypedData",
5440-
analyzerCodes: <String>["NON_LEAF_CALL_MUST_NOT_TAKE_TYPED_DATA"],
5441-
problemMessage: r"""FFI non-leaf calls can't take typed data arguments.""",
5442-
correctionMessage: r"""Try changing the call to a leaf call.""");
5443-
54445410
// DO NOT EDIT. THIS FILE IS GENERATED. SEE TOP OF FILE.
54455411
const Template<
54465412
Message Function(String name)> templateFfiNotStatic = const Template<

pkg/analysis_server/lib/src/services/correction/error_fix_status.yaml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1780,12 +1780,6 @@ FfiCode.LEAF_CALL_MUST_NOT_RETURN_HANDLE:
17801780
status: noFix
17811781
FfiCode.LEAF_CALL_MUST_NOT_TAKE_HANDLE:
17821782
status: noFix
1783-
FfiCode.CALLBACK_MUST_NOT_USE_TYPED_DATA:
1784-
status: noFix
1785-
FfiCode.CALL_MUST_NOT_RETURN_TYPED_DATA:
1786-
status: noFix
1787-
FfiCode.NON_LEAF_CALL_MUST_NOT_TAKE_TYPED_DATA:
1788-
status: noFix
17891783
FfiCode.MISMATCHED_ANNOTATION_ON_STRUCT_FIELD:
17901784
status: noFix
17911785
FfiCode.MISSING_ANNOTATION_ON_STRUCT_FIELD:

pkg/analyzer/lib/src/dart/error/ffi_code.g.dart

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -91,22 +91,6 @@ class FfiCode extends AnalyzerErrorCode {
9191
"Try passing a static function or field annotated with '@Native'",
9292
);
9393

94-
/// No parameters.
95-
static const FfiCode CALLBACK_MUST_NOT_USE_TYPED_DATA = FfiCode(
96-
'CALLBACK_MUST_NOT_USE_TYPED_DATA',
97-
"FFI callbacks can't take typed data arguments or return value.",
98-
correctionMessage: "Try changing using `Pointer`s.",
99-
hasPublishedDocs: true,
100-
);
101-
102-
/// No parameters.
103-
static const FfiCode CALL_MUST_NOT_RETURN_TYPED_DATA = FfiCode(
104-
'CALL_MUST_NOT_RETURN_TYPED_DATA',
105-
"FFI calls can't return typed data.",
106-
correctionMessage: "Try changing using `Pointer`s.",
107-
hasPublishedDocs: true,
108-
);
109-
11094
/// Parameters:
11195
/// 0: the name of the struct or union class
11296
static const FfiCode COMPOUND_IMPLEMENTS_FINALIZABLE = FfiCode(
@@ -391,14 +375,6 @@ class FfiCode extends AnalyzerErrorCode {
391375
hasPublishedDocs: true,
392376
);
393377

394-
/// No parameters.
395-
static const FfiCode NON_LEAF_CALL_MUST_NOT_TAKE_TYPED_DATA = FfiCode(
396-
'NON_LEAF_CALL_MUST_NOT_TAKE_TYPED_DATA',
397-
"FFI non-leaf calls can't take typed data arguments.",
398-
correctionMessage: "Try changing the call to a leaf call.",
399-
hasPublishedDocs: true,
400-
);
401-
402378
/// Parameters:
403379
/// 0: the type that should be a valid dart:ffi native type.
404380
static const FfiCode NON_NATIVE_FUNCTION_TYPE_ARGUMENT_TO_POINTER = FfiCode(

pkg/analyzer/lib/src/error/error_code_values.g.dart

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -585,8 +585,6 @@ const List<ErrorCode> errorCodeValues = [
585585
FfiCode.ANNOTATION_ON_POINTER_FIELD,
586586
FfiCode.ARGUMENT_MUST_BE_A_CONSTANT,
587587
FfiCode.ARGUMENT_MUST_BE_NATIVE,
588-
FfiCode.CALLBACK_MUST_NOT_USE_TYPED_DATA,
589-
FfiCode.CALL_MUST_NOT_RETURN_TYPED_DATA,
590588
FfiCode.COMPOUND_IMPLEMENTS_FINALIZABLE,
591589
FfiCode.CREATION_OF_STRUCT_OR_UNION,
592590
FfiCode.EMPTY_STRUCT,
@@ -617,7 +615,6 @@ const List<ErrorCode> errorCodeValues = [
617615
FfiCode.NATIVE_FIELD_MISSING_TYPE,
618616
FfiCode.NATIVE_FIELD_NOT_STATIC,
619617
FfiCode.NON_CONSTANT_TYPE_ARGUMENT,
620-
FfiCode.NON_LEAF_CALL_MUST_NOT_TAKE_TYPED_DATA,
621618
FfiCode.NON_NATIVE_FUNCTION_TYPE_ARGUMENT_TO_POINTER,
622619
FfiCode.NON_POSITIVE_ARRAY_DIMENSION,
623620
FfiCode.NON_SIZED_TYPE_ARGUMENT,

pkg/analyzer/lib/src/generated/ffi_verifier.dart

Lines changed: 0 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -587,14 +587,6 @@ class FfiVerifier extends RecursiveAstVisitor<void> {
587587
[nativeType, dartType, 'Native']);
588588
return;
589589
}
590-
591-
_validateFfiTypedDataUnwrapping(
592-
dartType,
593-
nativeType,
594-
errorNode,
595-
isLeaf: isLeaf,
596-
isCall: true,
597-
);
598590
}
599591

600592
bool _extendsNativeFieldWrapperClass1(InterfaceType? type) {
@@ -1027,13 +1019,6 @@ class FfiVerifier extends RecursiveAstVisitor<void> {
10271019
if (isLeaf) {
10281020
_validateFfiLeafCallUsesNoHandles(TPrime, node);
10291021
}
1030-
_validateFfiTypedDataUnwrapping(
1031-
F,
1032-
TPrime,
1033-
errorNode,
1034-
isLeaf: isLeaf,
1035-
isCall: true,
1036-
);
10371022
}
10381023
_validateIsLeafIsConst(node);
10391024
}
@@ -1193,46 +1178,6 @@ class FfiVerifier extends RecursiveAstVisitor<void> {
11931178
}
11941179
}
11951180

1196-
void _validateFfiTypedDataUnwrapping(
1197-
DartType dartType,
1198-
DartType nativeType,
1199-
AstNode errorNode, {
1200-
required bool isLeaf,
1201-
required bool isCall,
1202-
}) {
1203-
if (dartType is FunctionType && nativeType is FunctionType) {
1204-
if (dartType.returnType.isTypedData && nativeType.returnType.isPointer) {
1205-
if (!isCall) {
1206-
_errorReporter.reportErrorForNode(
1207-
FfiCode.CALLBACK_MUST_NOT_USE_TYPED_DATA, errorNode);
1208-
} else {
1209-
_errorReporter.reportErrorForNode(
1210-
FfiCode.CALL_MUST_NOT_RETURN_TYPED_DATA, errorNode);
1211-
}
1212-
}
1213-
int i = 0;
1214-
final nativeParamTypes = nativeType.normalParameterTypes.flattenVarArgs();
1215-
for (final dartParam in dartType.normalParameterTypes) {
1216-
if (i >= nativeParamTypes.length) {
1217-
// Cascading error as not the same amount of arguments.
1218-
// Already results in an error earlier.
1219-
return;
1220-
}
1221-
final nativeParam = nativeParamTypes[i];
1222-
i++;
1223-
if (dartParam.isTypedData && nativeParam.isPointer) {
1224-
if (!isCall) {
1225-
_errorReporter.reportErrorForNode(
1226-
FfiCode.CALLBACK_MUST_NOT_USE_TYPED_DATA, errorNode);
1227-
} else if (!isLeaf) {
1228-
_errorReporter.reportErrorForNode(
1229-
FfiCode.NON_LEAF_CALL_MUST_NOT_TAKE_TYPED_DATA, errorNode);
1230-
}
1231-
}
1232-
}
1233-
}
1234-
}
1235-
12361181
/// Validate that the fields declared by the given [node] meet the
12371182
/// requirements for fields within a struct or union class.
12381183
void _validateFieldsInCompound(FieldDeclaration node) {
@@ -1354,7 +1299,6 @@ class FfiVerifier extends RecursiveAstVisitor<void> {
13541299
FfiCode.ARGUMENT_MUST_BE_A_CONSTANT, e, ['exceptionalReturn']);
13551300
}
13561301
}
1357-
_validateFfiTypedDataUnwrapping(FT, T, f, isLeaf: false, isCall: false);
13581302
}
13591303

13601304
/// Ensure `isLeaf` is const as we need the value at compile time to know
@@ -1406,9 +1350,6 @@ class FfiVerifier extends RecursiveAstVisitor<void> {
14061350
if (isLeaf) {
14071351
_validateFfiLeafCallUsesNoHandles(S, typeArguments[0]);
14081352
}
1409-
final AstNode errorNode = typeArguments[1];
1410-
_validateFfiTypedDataUnwrapping(F, S, errorNode,
1411-
isLeaf: isLeaf, isCall: true);
14121353
}
14131354

14141355
/// Validate the invocation of `Native.addressOf`.
@@ -1551,13 +1492,6 @@ class FfiVerifier extends RecursiveAstVisitor<void> {
15511492
_errorReporter.reportErrorForNode(
15521493
FfiCode.MUST_BE_A_SUBTYPE, e, [eType, natRetType, name]);
15531494
}
1554-
_validateFfiTypedDataUnwrapping(
1555-
funcType,
1556-
typeArg,
1557-
e,
1558-
isLeaf: false,
1559-
isCall: false,
1560-
);
15611495
if (!_isConst(e)) {
15621496
_errorReporter.reportErrorForNode(
15631497
FfiCode.ARGUMENT_MUST_BE_A_CONSTANT, e, ['exceptionalReturn']);

pkg/analyzer/messages.yaml

Lines changed: 0 additions & 133 deletions
Original file line numberDiff line numberDiff line change
@@ -20032,139 +20032,6 @@ FfiCode:
2003220032
}
2003320033
}
2003420034
```
20035-
NON_LEAF_CALL_MUST_NOT_TAKE_TYPED_DATA:
20036-
problemMessage: "FFI non-leaf calls can't take typed data arguments."
20037-
correctionMessage: Try changing the call to a leaf call.
20038-
comment: No parameters.
20039-
hasPublishedDocs: true
20040-
documentation: |-
20041-
#### Description
20042-
20043-
The analyzer produces this diagnostic when the value of the `isLeaf`
20044-
argument of `Pointer.asFunction`, `DynamicLibrary.lookupFunction`, or
20045-
`@Native` is `false` and the Dart function signature contains a typed
20046-
data parameter.
20047-
20048-
Typed data unwrapping is only supported on arguments for leaf FFI calls.
20049-
20050-
For more information about FFI, see [C interop using dart:ffi][ffi].
20051-
20052-
#### Example
20053-
20054-
The following code produces this diagnostic because the dart function
20055-
signature contains a typed data, but the `isLeaf` argument is `false`:
20056-
20057-
```dart
20058-
import 'dart:ffi';
20059-
import 'dart:typed_data';
20060-
20061-
void f(Pointer<NativeFunction<Void Function(Pointer<Uint8>)>> p) {
20062-
p.asFunction<[!void Function(Uint8List)!]>();
20063-
}
20064-
```
20065-
20066-
#### Common fixes
20067-
20068-
If the function has at least one typed data parameter, then add
20069-
the `isLeaf` argument:
20070-
20071-
```dart
20072-
import 'dart:ffi';
20073-
import 'dart:typed_data';
20074-
20075-
void f(Pointer<NativeFunction<Void Function(Pointer<Uint8>)>> p) {
20076-
p.asFunction<void Function(Uint8List)>(isLeaf: true);
20077-
}
20078-
```
20079-
20080-
If the function also uses `Handle`s, then it must be non-leaf. In That
20081-
case use `Pointer`s instead of typed data.
20082-
CALL_MUST_NOT_RETURN_TYPED_DATA:
20083-
problemMessage: "FFI calls can't return typed data."
20084-
correctionMessage: Try changing using `Pointer`s.
20085-
comment: No parameters.
20086-
hasPublishedDocs: true
20087-
documentation: |-
20088-
#### Description
20089-
20090-
The analyzer produces this diagnostic when the return type of
20091-
`Pointer.asFunction`, `DynamicLibrary.lookupFunction`, or
20092-
`@Native` is a typed data.
20093-
20094-
Typed data unwrapping is only supported on arguments for leaf FFI calls.
20095-
20096-
For more information about FFI, see [C interop using dart:ffi][ffi].
20097-
20098-
#### Example
20099-
20100-
The following code produces this diagnostic because the dart function
20101-
signature contains a typed data, but the `isLeaf` argument is `false`:
20102-
20103-
```dart
20104-
import 'dart:ffi';
20105-
import 'dart:typed_data';
20106-
20107-
void f(Pointer<NativeFunction<Pointer<Uint8> Function()>> p) {
20108-
p.asFunction<[!Uint8List Function()!]>();
20109-
}
20110-
```
20111-
20112-
#### Common fixes
20113-
20114-
Use the `Pointer` type instead:
20115-
20116-
```dart
20117-
import 'dart:ffi';
20118-
20119-
void f(Pointer<NativeFunction<Pointer<Uint8> Function()>> p) {
20120-
p.asFunction<Pointer<Uint8> Function()>();
20121-
}
20122-
```
20123-
CALLBACK_MUST_NOT_USE_TYPED_DATA:
20124-
problemMessage: "FFI callbacks can't take typed data arguments or return value."
20125-
correctionMessage: Try changing using `Pointer`s.
20126-
comment: No parameters.
20127-
hasPublishedDocs: true
20128-
documentation: |-
20129-
#### Description
20130-
20131-
The analyzer produces this diagnostic when an invocation of
20132-
`Pointer.fromFunction`, one of`NativeCallable`'s constructors has a
20133-
typed data argument or return value."
20134-
20135-
Typed data unwrapping is only supported on arguments for leaf FFI calls.
20136-
20137-
For more information about FFI, see [C interop using dart:ffi][ffi].
20138-
20139-
#### Example
20140-
20141-
The following code produces this diagnostic because the parameter type
20142-
of `g` is a typed data.
20143-
20144-
```dart
20145-
import 'dart:ffi';
20146-
import 'dart:typed_data';
20147-
20148-
void f(Uint8List i) {}
20149-
20150-
void g() {
20151-
Pointer.fromFunction<Void Function(Pointer<Uint8>)>([!f!]);
20152-
}
20153-
```
20154-
20155-
#### Common fixes
20156-
20157-
Use the `Pointer` type instead:
20158-
20159-
```dart
20160-
import 'dart:ffi';
20161-
20162-
void f(Pointer<Uint8> i) {}
20163-
20164-
void g() {
20165-
Pointer.fromFunction<Void Function(Pointer<Uint8>)>(f);
20166-
}
20167-
```
2016820035
NON_NATIVE_FUNCTION_TYPE_ARGUMENT_TO_POINTER:
2016920036
problemMessage: "Can't invoke 'asFunction' because the function signature '{0}' for the pointer isn't a valid C function signature."
2017020037
correctionMessage: "Try changing the function argument in 'NativeFunction' to only use NativeTypes."

0 commit comments

Comments
 (0)