Skip to content

Commit a44a524

Browse files
simolus3Commit Queue
authored and
Commit Queue
committed
[vm/ffi]: Add Native.addressOf
This adds the `Native.addressOf` function, which takes a constant tear- off of a method annotated with `@Native` as a parameter and returns a pointer to the underlying C function. The CFE will resolve these calls in two steps: First, the existing transformer for `@Native` methods adds a pragma describing the fully- resolved native annotation (with the asset id inferred from the library if not given explicitly). Then, the FFI use sites transformer rewrites calls to `Native.addressOf` by searching for this pragma on the passed function and passing the `Native` constants to `Native._addressOf`. The latter method is implemented in the VM, which can re-use existing parts already used for `@Native` calls. An alternative implementation strategy would have been to forward `addressOf` calls to `Native.ffi_resolver_function` directly without any special consideration in the VM. However, the chosen approach makes it easier to support static linking in the future, as this requires unresolved symbols in the generated assembly. Closes #50552 TEST=pkg/vm/testcases/transformations/ffi/ffinative.dart TEST=tests/ffi/native_assets/asset_*_test.dart TEST=tests/ffi/vmspecific_static_checks_ffinative_test.dart TEST=pkg/analyzer/test/src/diagnostics/ffi_native_test.dart Cq-Include-Trybots: luci.dart.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-kernel-precomp-linux-release-x64-try CoreLibraryReviewExempt: VM & dart2wasm only feature Change-Id: Ic8e3a390146dffd44c95578f975a4472db79a0ee Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/333920 Reviewed-by: Martin Kustermann <[email protected]> Commit-Queue: Daco Harkes <[email protected]> Reviewed-by: Daco Harkes <[email protected]> Reviewed-by: Brian Wilkerson <[email protected]>
1 parent 9a4ca01 commit a44a524

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+1198
-247
lines changed

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4854,6 +4854,16 @@ const MessageCode messageFfiAbiSpecificIntegerMappingInvalid = const MessageCode
48544854
problemMessage:
48554855
r"""Classes extending 'AbiSpecificInteger' must have exactly one 'AbiSpecificIntegerMapping' annotation specifying the mapping from ABI to a NativeType integer with a fixed size.""");
48564856

4857+
// DO NOT EDIT. THIS FILE IS GENERATED. SEE TOP OF FILE.
4858+
const Code<Null> codeFfiAddressOfMustBeNative = messageFfiAddressOfMustBeNative;
4859+
4860+
// DO NOT EDIT. THIS FILE IS GENERATED. SEE TOP OF FILE.
4861+
const MessageCode messageFfiAddressOfMustBeNative = const MessageCode(
4862+
"FfiAddressOfMustBeNative",
4863+
analyzerCodes: <String>["ARGUMENT_MUST_BE_NATIVE"],
4864+
problemMessage:
4865+
r"""Argument to 'Native.addressOf' must be annotated with @Native.""");
4866+
48574867
// DO NOT EDIT. THIS FILE IS GENERATED. SEE TOP OF FILE.
48584868
const Template<Message Function(String string, String name)>
48594869
templateFfiCompoundImplementsFinalizable =
@@ -4894,6 +4904,16 @@ const MessageCode messageFfiCreateOfStructOrUnion = const MessageCode(
48944904
problemMessage:
48954905
r"""Subclasses of 'Struct' and 'Union' are backed by native memory, and can't be instantiated by a generative constructor. Try allocating it via allocation, or load from a 'Pointer'.""");
48964906

4907+
// DO NOT EDIT. THIS FILE IS GENERATED. SEE TOP OF FILE.
4908+
const Code<Null> codeFfiDefaultAssetDuplicate = messageFfiDefaultAssetDuplicate;
4909+
4910+
// DO NOT EDIT. THIS FILE IS GENERATED. SEE TOP OF FILE.
4911+
const MessageCode messageFfiDefaultAssetDuplicate = const MessageCode(
4912+
"FfiDefaultAssetDuplicate",
4913+
analyzerCodes: <String>["FFI_NATIVE_INVALID_DUPLICATE_DEFAULT_ASSET"],
4914+
problemMessage:
4915+
r"""There may be at most one @DefaultAsset annotation on a library.""");
4916+
48974917
// DO NOT EDIT. THIS FILE IS GENERATED. SEE TOP OF FILE.
48984918
const Template<
48994919
Message Function(
@@ -5134,6 +5154,17 @@ const MessageCode messageFfiLeafCallMustNotTakeHandle = const MessageCode(
51345154
"FfiLeafCallMustNotTakeHandle",
51355155
problemMessage: r"""FFI leaf call must not have Handle argument types.""");
51365156

5157+
// DO NOT EDIT. THIS FILE IS GENERATED. SEE TOP OF FILE.
5158+
const Code<Null> codeFfiNativeDuplicateAnnotations =
5159+
messageFfiNativeDuplicateAnnotations;
5160+
5161+
// DO NOT EDIT. THIS FILE IS GENERATED. SEE TOP OF FILE.
5162+
const MessageCode messageFfiNativeDuplicateAnnotations = const MessageCode(
5163+
"FfiNativeDuplicateAnnotations",
5164+
analyzerCodes: <String>["FFI_NATIVE_INVALID_MULTIPLE_ANNOTATIONS"],
5165+
problemMessage:
5166+
r"""Native functions must not have more than @Native annotation.""");
5167+
51375168
// DO NOT EDIT. THIS FILE IS GENERATED. SEE TOP OF FILE.
51385169
const Code<Null> codeFfiNativeMustBeExternal = messageFfiNativeMustBeExternal;
51395170

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1722,6 +1722,8 @@ FfiCode.ANNOTATION_ON_POINTER_FIELD:
17221722
status: needsFix
17231723
FfiCode.ARGUMENT_MUST_BE_A_CONSTANT:
17241724
status: noFix
1725+
FfiCode.ARGUMENT_MUST_BE_NATIVE:
1726+
status: needsEvaluation
17251727
FfiCode.COMPOUND_IMPLEMENTS_FINALIZABLE:
17261728
status: noFix
17271729
FfiCode.CREATION_OF_STRUCT_OR_UNION:
@@ -1747,6 +1749,10 @@ FfiCode.FFI_NATIVE_UNEXPECTED_NUMBER_OF_PARAMETERS:
17471749
FfiCode.FFI_NATIVE_UNEXPECTED_NUMBER_OF_PARAMETERS_WITH_RECEIVER:
17481750
status: noFix
17491751
since: ~2.15
1752+
FfiCode.FFI_NATIVE_INVALID_DUPLICATE_DEFAULT_ASSET:
1753+
status: needsEvaluation
1754+
FfiCode.FFI_NATIVE_INVALID_MULTIPLE_ANNOTATIONS:
1755+
status: needsEvaluation
17501756
FfiCode.FIELD_IN_STRUCT_WITH_INITIALIZER:
17511757
status: needsFix
17521758
notes: |-

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,13 @@ class FfiCode extends AnalyzerErrorCode {
8383
hasPublishedDocs: true,
8484
);
8585

86+
/// No parameters
87+
static const FfiCode ARGUMENT_MUST_BE_NATIVE = FfiCode(
88+
'ARGUMENT_MUST_BE_NATIVE',
89+
"Argument to 'Native.addressOf' must be annotated with @Native",
90+
correctionMessage: "Try passing a static function annotated with '@Native'",
91+
);
92+
8693
/// Parameters:
8794
/// 0: the name of the struct or union class
8895
static const FfiCode COMPOUND_IMPLEMENTS_FINALIZABLE = FfiCode(
@@ -130,6 +137,20 @@ class FfiCode extends AnalyzerErrorCode {
130137
hasPublishedDocs: true,
131138
);
132139

140+
/// No parameters
141+
static const FfiCode FFI_NATIVE_INVALID_DUPLICATE_DEFAULT_ASSET = FfiCode(
142+
'FFI_NATIVE_INVALID_DUPLICATE_DEFAULT_ASSET',
143+
"There may be at most one @DefaultAsset annotation on a library.",
144+
correctionMessage: "Try removing the extra annotation.",
145+
);
146+
147+
/// No parameters
148+
static const FfiCode FFI_NATIVE_INVALID_MULTIPLE_ANNOTATIONS = FfiCode(
149+
'FFI_NATIVE_INVALID_MULTIPLE_ANNOTATIONS',
150+
"Native functions must have exactly one `@Native` annotation.",
151+
correctionMessage: "Try removing the extra annotation.",
152+
);
153+
133154
/// No parameters.
134155
static const FfiCode FFI_NATIVE_MUST_BE_EXTERNAL = FfiCode(
135156
'FFI_NATIVE_MUST_BE_EXTERNAL',

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -580,11 +580,14 @@ const List<ErrorCode> errorCodeValues = [
580580
FfiCode.ABI_SPECIFIC_INTEGER_MAPPING_UNSUPPORTED,
581581
FfiCode.ANNOTATION_ON_POINTER_FIELD,
582582
FfiCode.ARGUMENT_MUST_BE_A_CONSTANT,
583+
FfiCode.ARGUMENT_MUST_BE_NATIVE,
583584
FfiCode.COMPOUND_IMPLEMENTS_FINALIZABLE,
584585
FfiCode.CREATION_OF_STRUCT_OR_UNION,
585586
FfiCode.EMPTY_STRUCT,
586587
FfiCode.EXTRA_ANNOTATION_ON_STRUCT_FIELD,
587588
FfiCode.EXTRA_SIZE_ANNOTATION_CARRAY,
589+
FfiCode.FFI_NATIVE_INVALID_DUPLICATE_DEFAULT_ASSET,
590+
FfiCode.FFI_NATIVE_INVALID_MULTIPLE_ANNOTATIONS,
588591
FfiCode.FFI_NATIVE_MUST_BE_EXTERNAL,
589592
FfiCode
590593
.FFI_NATIVE_ONLY_CLASSES_EXTENDING_NATIVEFIELDWRAPPERCLASS1_CAN_BE_POINTER,

0 commit comments

Comments
 (0)