Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Breaking Change Request] [vm/ffi] Disallow empty structs #44622

Closed
dcharkes opened this issue Jan 8, 2021 · 11 comments
Closed

[Breaking Change Request] [vm/ffi] Disallow empty structs #44622

dcharkes opened this issue Jan 8, 2021 · 11 comments
Assignees
Labels
area-vm Use area-vm for VM related issues, including code coverage, and the AOT and JIT backends. breaking-change-request This tracks requests for feedback on breaking changes library-ffi P1 A high priority bug; for example, a single project is unusable or has many test failures

Comments

@dcharkes
Copy link
Contributor

dcharkes commented Jan 8, 2021

Update 2021-01-22: We will deprecate empty structs as of Dart 2.12 and remove support for them in 2.13.

Summary

Disallow defining subclasses of Struct without fields.

class EmptyStruct extends Struct {}

Introduce the Opaque native type to represent opaque C types.

/// [Opaque]'s subtypes represent opaque types in C.
///
/// [Opaque]'s subtypes are not constructible in the Dart code and serve purely
/// as markers in type signatures.
abstract class Opaque extends NativeType {}

Motivation

  1. Simpler error messages. Currently we have error messages on using empty structs in nested structs and trampolines. Having these error messages on the empty structs themselves would be easier.
  2. Prevent people shooting themselves in the foot by calling allocate<Utf8>() and allocating 0 bytes of native memory ([vm/ffi] Deprecate empty structs #43974, Making Utf8/Utf16 a subclass of Struct is misleading dart-archive/ffi#34).

Impact

Definitions need to be migrated from extends Struct to extends Opaque.

Use sites can no longer use .ref to pass around 'references' with types such as Utf8. Instead, Pointer<Utf8> needs to be passed around.

Migration of package:ffi

Proposed changes:

Migration of users of package:ffi

Example migrations:

Discussion

We could change the NativeType hierarchy to the following and reduce the amount of custom checks in the analyzer and common frontend (for sizeOf, Pointer.elementAt and Allocator.call).

  • NativeType
    • Opaque
      • Void
      • NativeFunction
      • (user defined types)
    • Sized (only accept these in sizeOf, Pointer.elementAt (would become an extension method) and Allocator.call)
      • NativeInteger
        • ...
      • NativeDouble
      • Struct
        • (user defined types)
@dcharkes dcharkes added area-vm Use area-vm for VM related issues, including code coverage, and the AOT and JIT backends. breaking-change-request This tracks requests for feedback on breaking changes library-ffi labels Jan 8, 2021
@franklinyow
Copy link
Contributor

@dcharkes Please make an announcement to https://groups.google.com/a/dartlang.org/g/announce

cc @Hixie @matanlurey @vsmenon for review and approval.

@Hixie
Copy link
Contributor

Hixie commented Jan 11, 2021

I have no opinion on this and thus no objection.

@vsmenon
Copy link
Member

vsmenon commented Jan 12, 2021

lgtm

@matanlurey
Copy link
Contributor

I don't work in this area, no comment.

@dcharkes
Copy link
Contributor Author

@simolus3
Copy link
Contributor

Is there a rough ETA for these changes? Considering that the apis haven't landed yet and that Dart 2.12 was announced for "early 2021", I'm a bit worried about how little time we might have to migrate.

Is there any way to get the Opaque interface into the next beta?

@franklinyow
Copy link
Contributor

Approved

dart-bot pushed a commit that referenced this issue Jan 13, 2021
Issue: #44622
Issue: #43974

TEST=samples/ffi/sqlite/lib/src/bindings/types.dart
TEST=tests/ffi/vmspecific_static_checks_test.dart

Change-Id: Ib9e72df6a07b1bc2b72a7db66f945652814baf51
Cq-Include-Trybots: luci.dart.try:vm-precomp-ffi-qemu-linux-release-arm-try,analyzer-analysis-server-linux-try,analyzer-linux-release-try,analyzer-nnbd-linux-release-try,front-end-linux-release-x64-try,front-end-nnbd-linux-release-x64-try,benchmark-linux-try,dart-sdk-linux-try,pkg-linux-release-try,vm-ffi-android-release-arm-try,vm-ffi-android-release-arm64-try,vm-kernel-nnbd-win-debug-x64-try,vm-kernel-win-debug-x64-try
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/178984
Commit-Queue: Daco Harkes <dacoharkes@google.com>
Reviewed-by: Clement Skau <cskau@google.com>
@dcharkes
Copy link
Contributor Author

Is there a rough ETA for these changes?

I've landed the new API in master.

Deprecating the old API will take some time, because this has to roll into Flutter first.

dart-bot pushed a commit that referenced this issue Jan 15, 2021
This CL migrates empty `Struct`s to `Opaque` native types.
It stops using `.ref` on `Pointer`s to these types.
And stops using `.ref` on `Pointer<Utf8>` which will be migrated in
`package:ffi`.

Issue: #44622
Issue: #43974

Change-Id: I3aa256af7d4ceaa8ee37b1b2ada71f870f43617b
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/179181
Reviewed-by: Vyacheslav Egorov <vegorov@google.com>
Commit-Queue: Daco Harkes <dacoharkes@google.com>
dart-bot pushed a commit that referenced this issue Jan 15, 2021
This CL adds fields to `Struct`s which should not be empty or removes
the structs.

Issue: #44622
Issue: #43974

TEST=tests/ffi/vmspecific_static_checks_test.dart

Change-Id: I4a684bd96e3ed16a3fd0dd17019aeabf64ef074a
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/179182
Commit-Queue: Daco Harkes <dacoharkes@google.com>
Reviewed-by: Vyacheslav Egorov <vegorov@google.com>
dart-bot pushed a commit that referenced this issue Jan 20, 2021
This can only be landed when `Allocator` and `Opaque` have rolled into
Flutter/engine, that into Flutter/flutter, and that into g3.
flutter/flutter/commit/a706cd211240f27be3b61f06d70f958c7a4156fe

Deletes all the copies of `_CallocAllocator` and uses the one from
`package:ffi` instead.

Issue: #44622
Issue: #43974
Issue: #44621
Issue: #38721

Change-Id: I50b3b4c31a2b839b35e3e057bd54f463b90bc55e
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/179540
Reviewed-by: Aske Simon Christensen <askesc@google.com>
dart-bot pushed a commit that referenced this issue Jan 22, 2021
Issue: #44621
Issue: #44622

Change-Id: I9689db63a5b47946b501fac9df63cc86811d6e41
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/179764
Reviewed-by: Vyacheslav Egorov <vegorov@google.com>
@dcharkes dcharkes added the P1 A high priority bug; for example, a single project is unusable or has many test failures label Jan 22, 2021
@dcharkes dcharkes added this to the January Beta Release (2.12) milestone Jan 22, 2021
@dcharkes
Copy link
Contributor Author

Is there a rough ETA for these changes? Considering that the apis haven't landed yet and that Dart 2.12 was announced for "early 2021", I'm a bit worried about how little time we might have to migrate.

Is there any way to get the Opaque interface into the next beta?

@simolus3 We will deprecate support for empty structs and add Opaque in Dart 2.12, and remove support for empty structs in 2.13.

@simolus3
Copy link
Contributor

That's relieving to hear, thank you!

@dcharkes
Copy link
Contributor Author

We have landed deprecation hints for this in c3b30df.

Turning these hints into errors is tracked in #43974. Closing this issue.

dcharkes added a commit to dart-archive/ffi that referenced this issue Feb 1, 2021
Changes `Utf8` and `Utf16` to extend `Opaque` instead of `Struct`.
This means `.ref` is no longer available and `Pointer<Utf(..)>` should be used.
See [breaking change #44622](dart-lang/sdk#44622) for more info.

Removes `allocate` and `free`.
Instead, introduces `calloc` which implements the new `Allocator` interface.
See [breaking change #44621](dart-lang/sdk#44621) for more info.

This pre-release requires Dart `2.12.0-265.0.dev` or greater.
dart-bot pushed a commit that referenced this issue Feb 10, 2021
This can only be landed when `Allocator`, `Opaque`, and `AllocatorAlloc`
have rolled into Flutter/engine, that into Flutter/flutter, and into g3.

Deletes all the copies of `_CallocAllocator` and uses the one from
`package:ffi` instead.

Bug: #44622
Bug: #43974
Bug: #44621
Bug: #38721

Change-Id: I486034b379b5a63cad4aefd503ccd0f2ce5dd45e
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/180188
Commit-Queue: Daco Harkes <dacoharkes@google.com>
Reviewed-by: Aske Simon Christensen <askesc@google.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-vm Use area-vm for VM related issues, including code coverage, and the AOT and JIT backends. breaking-change-request This tracks requests for feedback on breaking changes library-ffi P1 A high priority bug; for example, a single project is unusable or has many test failures
Projects
None yet
Development

No branches or pull requests

6 participants