Commit graph

9 commits

Author SHA1 Message Date
Daco Harkes aa6abbf464 [cfe/ffi] Transform FfiNatives in a single pass
Before this CL, `FfiNative`s were first transformed to `asFunction`
calls, which were then immediately transformed to `_asFunctionInternal`
calls.

This caused the the static checks to be done in two steps, the second
step happening after the first transform. It is cleaner to first do all
checks.

This refactoring enables implementing `_asFunctionInternal` variants
for `FfiNative`s that don't use a `Pointer` for the address.

Besides the transform change, this CL
- moves shared logic over to pkg/vm/lib/transformations/ffi/common.dart,
- splits up the ffi-native tests in to positive and negative tests, and
- adds negative tests for mismatches between Dart and native types.

These new tests do _not yet_ pass on the analyzer. This is tracked in:
https://github.com/dart-lang/sdk/issues/49412

TEST=tests/ffi/ffi_native_test.dart
TEST=tests/ffi/vmspecific_static_checks_ffinative_test.dart

Closes: https://github.com/dart-lang/sdk/issues/49413
Change-Id: I5baded43eab7ff1dc1ffb16550b2a638e4b7a34e
Cq-Include-Trybots: luci.dart.try:analyzer-linux-release-try,analyzer-mac-release-try,vm-ffi-android-debug-arm-try,vm-ffi-android-debug-arm64c-try
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/250843
Reviewed-by: Martin Kustermann <kustermann@google.com>
Commit-Queue: Daco Harkes <dacoharkes@google.com>
2022-07-08 12:36:09 +00:00
Clement Skau 2c0ba56ea1 [vm/ffi] Add error for FFI annotation with opt. args.
Optional arguments (positional and named) in @FfiNative
annotations are not meaningful, and should result in a
compile-time error.

TEST=tests/ffi/ffi_native_test.dart
Bug: https://github.com/dart-lang/sdk/issues/47169
Change-Id: I8896e6a43f9399b537e6ee7c7a0e2857a370203f
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/232622
Reviewed-by: Daco Harkes <dacoharkes@google.com>
Reviewed-by: Chloe Stefantsova <cstefantsova@google.com>
Commit-Queue: Clement Skau <cskau@google.com>
2022-03-18 20:58:51 +00:00
Clement Skau 998d5f5a9b [vm/ffi] Adds FFI transform FfiNative instance methods.
- Allows FfiNative annotation to be attached to non-static
  methods of classes.
- Transforms non-static instance methods to static, adding
  wrappers with the receiver as an extra, implicit first paramters.
- Transform all parameters and arguments to Pointer if the object
  being passed supports it (i.e. extends NativeFieldWrapperClass1).
- Adds compile time errors for cases where the FfiNative annotation
  doesn't align with the annotated function. Taking into account
  implicit receivers and converted Pointers.
- Adds complimentary Analyzer checks for the above errors as well.
- Adds tests for the transforms, compile time errors and analyzer
  changes.

TEST=Adds new tests for instance methods, analyzer changes.

Change-Id: Idf54430acf2728a650008333b149b254941290ad
Cq-Do-Not-Cancel-Tryjobs: true
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/213773
Commit-Queue: Clement Skau <cskau@google.com>
Reviewed-by: Martin Kustermann <kustermann@google.com>
Reviewed-by: Daco Harkes <dacoharkes@google.com>
2021-10-12 10:58:44 +00:00
Daco Harkes 7487e5ea8b [test/ffi] Fix errors in tests
A cleanup round of our FFI tests.

Change-Id: I3883b9d14785c759c7129c072c9588bfa5a3fba0
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/214802
Reviewed-by: Clement Skau <cskau@google.com>
Commit-Queue: Daco Harkes <dacoharkes@google.com>
2021-09-28 13:17:01 +00:00
Clement Skau cd7c9922eb [vm/ffi] Adds FFI transform for Handle -> Pointer.
This will automatically desugar FfiNative calls passing
NativeFieldWrapperClass1 handles to use Pointer when
the underlying native function expects a pointer.
E.g.:

```
class ClassWithNativeField extends NativeFieldWrapperClass1 {}

@FfiNative<IntPtr Function(Pointer<Void>)>('PassAsPointer')
external int passAsPointer(ClassWithNativeField obj);

passAsPointer(ClassWithNativeField());
// Becomes roughly:
// #t0 = PointerClassWithNativeField();
// passAsPointer(Pointer.fromAddress(getNativeField(#t0)));
// reachabilityFence(#t0);
```

TEST=Adds new tests in tests/ffi/ffi_native_test.dart.
Change-Id: I4460f9249803054f12be5d5ff34dbdf7c96567fd
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/213260
Commit-Queue: Clement Skau <cskau@google.com>
Reviewed-by: Daco Harkes <dacoharkes@google.com>
2021-09-15 12:47:35 +00:00
Clement Skau bf2691c2fe [VM] Moves FfiNative fields to function parent.
Previously the synthetic field that holds the FfiNative
function pointer was injected into the current library.
This change makes sure we instead add the field to the
relevant parent - Class or Library.

TEST=Added regression test for name collision.

Bug: https://github.com/dart-lang/sdk/issues/43889
Change-Id: Ifbf2d70de00e4748c179fe7d626c495675c2b338
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/208502
Reviewed-by: Martin Kustermann <kustermann@google.com>
Commit-Queue: Clement Skau <cskau@google.com>
2021-08-09 09:13:38 +00:00
Clement Skau 2f0b352e98 [VM] Adds compile-time error for non-static FfiNative
This makes it a compile-time error to add @FfiNative annotation
to any non-static function, such as an instance method.

TEST=tests/ffi/ffi_native_test.dart,pkg/analyzer/test/src/diagnostics/ffi_native_test.dart

Bug: https://github.com/dart-lang/sdk/issues/43889
Change-Id: Ib9ec61345bb47e735ed635c5ceea15ab643f65a6
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/207306
Commit-Queue: Clement Skau <cskau@google.com>
Reviewed-by: Tess Strickland <sstrickl@google.com>
2021-07-20 11:54:30 +00:00
Clement Skau 06a35b8c2e [VM] Adds test for FfiNative leaf calls.
- Tests that non-leaf calls transition generated -> native,
  and that leaf calls don't.
- Tests that leaf calls with Handle argument or return type
  results in a compile-time error.
- Adds note explaining why there is no ffi_2 version.

TEST=tests/ffi/ffi_native_test.dart

Cq-Include-Trybots: luci.dart.try:analyzer-linux-release-try,analyzer-nnbd-win-release-try,analyzer-analysis-server-linux-try,analyzer-win-release-try,analyzer-nnbd-mac-release-try,analyzer-nnbd-linux-release-try,analyzer-mac-release-try,flutter-analyze-try
Change-Id: I9af60e21f5122f2479c909a52249809850824692
Cq-Do-Not-Cancel-Tryjobs: true
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/206788
Commit-Queue: Clement Skau <cskau@google.com>
Reviewed-by: Martin Kustermann <kustermann@google.com>
2021-07-15 16:14:45 +00:00
Clement Skau 460e00a6a6 [VM] Adds leaf call option to FfiNative.
This change essentially exposes `asFunction`'s `isLeaf` in
`@FfiNative`, allowing us to declare FFI Natives as leaf calls.

TEST=Adds tests/ffi/ffi_native_test.dart

Bug: https://github.com/dart-lang/sdk/issues/43889
Change-Id: I2a396fae2ab28d21df282f3afb35fa401485ed52
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/206375
Reviewed-by: Daco Harkes <dacoharkes@google.com>
Commit-Queue: Clement Skau <cskau@google.com>
2021-07-14 09:13:51 +00:00