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

Kernel compiler crash #47673

Closed
dnfield opened this issue Nov 9, 2021 · 11 comments
Closed

Kernel compiler crash #47673

dnfield opened this issue Nov 9, 2021 · 11 comments
Assignees
Labels
area-front-end Use area-front-end for front end / CFE / kernel format related issues. library-ffi type-bug Incorrect behavior (everything from a crash to more subtle misbehavior)

Comments

@dnfield
Copy link
Contributor

dnfield commented Nov 9, 2021

ffigen 4.1.1

LLVM 13

Tried to run it on wuffs:
https://raw.githubusercontent.com/google/wuffs/34e2786dd2e766a1800d7c55e71f024421585dd9/release/c/wuffs-v0.3.c

ffigen:
  output: 'generated_bindings.dart'
  headers:
    entry-points:
      - 'wuffs-v0.3.c'
  llvm-path:
    - /Users/dnfield/Downloads/llvm

Running dart run ffigen does generate some SEVERE red warnings, but they all seem to be about Nullability issues in the stdlib.h from my Xcode install, e.g.

[SEVERE] : Header hello_library/wufs-v0.3.c: Total errors/warnings: 180.
[SEVERE] :     /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/stdlib.h:134:25: warning: pointer is missing a nullability type specifier (_Nonnull, _Nullable, or _Null_unspecified) [Nullability Issue]
[SEVERE] :     /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/stdlib.h:135:22: warning: pointer is missing a nullability type specifier (_Nonnull, _Nullable, or _Null_unspecified) [Nullability Issue]

When I try to dart hello.dart on a program that loads and uses the NativeLibrary generated from that, I get a kernel compiler crash:

Crash when compiling null,
at character offset null:
type 'InvalidType' is not a subtype of type 'InterfaceType' in type cast
#0      FfiTransformer.arraySingleElementType (package:vm/transformations/ffi/common.dart:803:50)
dart-lang/ffigen#1      _FfiDefinitionTransformer._checkFieldAnnotations (package:vm/transformations/ffi/definitions.dart:404:39)
dart-lang/ffigen#2      _FfiDefinitionTransformer.visitClass (package:vm/transformations/ffi/definitions.dart:265:25)
dart-lang/ffigen#3      Class.accept (package:kernel/ast.dart:1417:38)
dart-lang/ffigen#4      Transformer.transform (package:kernel/visitor.dart:1537:21)
dart-lang/ffigen#5      Transformer.transformList (package:kernel/visitor.dart:1572:18)
dart-lang/ffigen#6      Library.transformChildren (package:kernel/ast.dart:580:7)
dart-lang/ffigen#7      Transformer.defaultTreeNode (package:kernel/visitor.dart:1595:10)
dart-lang/ffigen#8      TreeVisitor.visitLibrary (package:kernel/visitor.dart:407:35)
dart-lang/ffigen#9      FfiTransformer.visitLibrary (package:vm/transformations/ffi/common.dart:504:26)
dart-lang/ffigen#10     List.forEach (dart:core-patch/growable_array.dart:410:8)
dart-lang/ffigen#11     transformLibraries (package:vm/transformations/ffi/definitions.dart:91:13)
dart-lang/ffigen#12     VmTarget.performModularTransformationsOnLibraries (package:vm/target/vm.dart:171:31)
dart-lang/ffigen#13     KernelTarget.runBuildTransformations (package:front_end/src/fasta/kernel/kernel_target.dart:1415:19)
dart-lang/ffigen#14     KernelTarget.buildComponent.<anonymous closure> (package:front_end/src/fasta/kernel/kernel_target.dart:465:7)
<asynchronous suspension>
dart-lang/ffigen#15     withCrashReporting (package:front_end/src/fasta/crash.dart:122:12)
<asynchronous suspension>
dart-lang/ffigen#16     generateKernelInternal.<anonymous closure> (package:front_end/src/kernel_generator_impl.dart:162:19)
<asynchronous suspension>
dart-lang/ffigen#17     withCrashReporting (package:front_end/src/fasta/crash.dart:122:12)
<asynchronous suspension>
dart-lang/ffigen#18     generateKernel.<anonymous closure> (package:front_end/src/kernel_generator_impl.dart:50:12)
<asynchronous suspension>
dart-lang/ffigen#19     generateKernel (package:front_end/src/kernel_generator_impl.dart:49:10)
<asynchronous suspension>
dart-lang/ffigen#20     kernelForModule (package:front_end/src/api_prototype/kernel_generator.dart:97:11)
<asynchronous suspension>
dart-lang/ffigen#21     SingleShotCompilerWrapper.compileInternal (file:///opt/s/w/ir/cache/builder/sdk/pkg/vm/bin/kernel_service.dart:398:11)
<asynchronous suspension>
dart-lang/ffigen#22     Compiler.compile.<anonymous closure> (file:///opt/s/w/ir/cache/builder/sdk/pkg/vm/bin/kernel_service.dart:216:45)
<asynchronous suspension>
dart-lang/ffigen#23     _processLoadRequest (file:///opt/s/w/ir/cache/builder/sdk/pkg/vm/bin/kernel_service.dart:886:37)
<asynchronous suspension>


#0      FfiTransformer.arraySingleElementType (package:vm/transformations/ffi/common.dart:803:50)
dart-lang/ffigen#1      _FfiDefinitionTransformer._checkFieldAnnotations (package:vm/transformations/ffi/definitions.dart:404:39)
dart-lang/ffigen#2      _FfiDefinitionTransformer.visitClass (package:vm/transformations/ffi/definitions.dart:265:25)
dart-lang/ffigen#3      Class.accept (package:kernel/ast.dart:1417:38)
dart-lang/ffigen#4      Transformer.transform (package:kernel/visitor.dart:1537:21)
dart-lang/ffigen#5      Transformer.transformList (package:kernel/visitor.dart:1572:18)
dart-lang/ffigen#6      Library.transformChildren (package:kernel/ast.dart:580:7)
dart-lang/ffigen#7      Transformer.defaultTreeNode (package:kernel/visitor.dart:1595:10)
dart-lang/ffigen#8      TreeVisitor.visitLibrary (package:kernel/visitor.dart:407:35)
dart-lang/ffigen#9      FfiTransformer.visitLibrary (package:vm/transformations/ffi/common.dart:504:26)
dart-lang/ffigen#10     List.forEach (dart:core-patch/growable_array.dart:410:8)
dart-lang/ffigen#11     transformLibraries (package:vm/transformations/ffi/definitions.dart:91:13)
dart-lang/ffigen#12     VmTarget.performModularTransformationsOnLibraries (package:vm/target/vm.dart:171:31)
dart-lang/ffigen#13     KernelTarget.runBuildTransformations (package:front_end/src/fasta/kernel/kernel_target.dart:1415:19)
dart-lang/ffigen#14     KernelTarget.buildComponent.<anonymous closure> (package:front_end/src/fasta/kernel/kernel_target.dart:465:7)
<asynchronous suspension>
dart-lang/ffigen#15     withCrashReporting (package:front_end/src/fasta/crash.dart:122:12)
<asynchronous suspension>
dart-lang/ffigen#16     generateKernelInternal.<anonymous closure> (package:front_end/src/kernel_generator_impl.dart:162:19)
<asynchronous suspension>
dart-lang/ffigen#17     withCrashReporting (package:front_end/src/fasta/crash.dart:122:12)
<asynchronous suspension>
dart-lang/ffigen#18     generateKernel.<anonymous closure> (package:front_end/src/kernel_generator_impl.dart:50:12)
<asynchronous suspension>
dart-lang/ffigen#19     generateKernel (package:front_end/src/kernel_generator_impl.dart:49:10)
<asynchronous suspension>
dart-lang/ffigen#20     kernelForModule (package:front_end/src/api_prototype/kernel_generator.dart:97:11)
<asynchronous suspension>
dart-lang/ffigen#21     SingleShotCompilerWrapper.compileInternal (file:///opt/s/w/ir/cache/builder/sdk/pkg/vm/bin/kernel_service.dart:398:11)
<asynchronous suspension>
dart-lang/ffigen#22     Compiler.compile.<anonymous closure> (file:///opt/s/w/ir/cache/builder/sdk/pkg/vm/bin/kernel_service.dart:216:45)
<asynchronous suspension>
dart-lang/ffigen#23     _processLoadRequest (file:///opt/s/w/ir/cache/builder/sdk/pkg/vm/bin/kernel_service.dart:886:37)
<asynchronous suspension>

Dart SDK version: 2.15.0-268.0.dev (dev) (Fri Oct 29 15:59:24 2021 -0700) on "macos_x64"

/cc @mraleph

@mannprerak2
Copy link

Regarding the nullability warnings, adding this should remove those.

compiler-opts:
  - '-Wno-nullability-completeness'

@mraleph
Copy link
Member

mraleph commented Nov 10, 2021

@cskau-g Clement, could you take a look at the crash in the FFI related transformer? You are the most familiar with it after Daco.

@ghost ghost self-assigned this Nov 10, 2021
@ghost
Copy link

ghost commented Nov 11, 2021

I can reproduce this issue locally with:

import 'dart:ffi';
typedef T = Int64;
class A extends Struct {
  @Array.multi([16])
  external Array<T> b;
}

Where Array<T> parses as Array<InvalidType>.

But with a bit more fiddling I ended up with:

Error: Can't create typedef from non-function type.
typedef T = Int64;

Which led me to this:

Version note: Before 2.13, typedefs were restricted to function types. Using the new typedefs requires a language version of at least 2.13.

And sure enough, once I updated my pubspec all issues went away:

environment:
  sdk: '>=2.13.0 <3.0.0'

@dnfield you should be able to work around this issue the same way.

The bottom line here is that underlying issue is that we don't fail on the typedef error soon enough to avoid transforms getting invalid input.
I will start digging into this.

@ghost ghost transferred this issue from dart-archive/ffigen Nov 11, 2021
@ghost
Copy link

ghost commented Nov 11, 2021

Bringing in Chloe who recently introduced the change that I believe ultimately triggered this.
To quote Chloe, we need to figure out if this is "CFE breaking the expectations or the transformation that doesn't handle the CFE output correctly."

@ghost ghost added the type-bug Incorrect behavior (everything from a crash to more subtle misbehavior) label Nov 11, 2021
@chloestefantsova
Copy link
Contributor

I agree with @cskau-g's analysis: the program shouldn't work as written in 2.12, but we should report a compile-time error instead of crashing.

@chloestefantsova
Copy link
Contributor

I work on the fix here: https://dart-review.googlesource.com/c/sdk/+/220008

@dnfield
Copy link
Contributor Author

dnfield commented Nov 11, 2021

Why is ffigen generating code incompatible with the version from my pubspec? What if I intend to support clients down to 2.12?

@mannprerak2
Copy link

One can select an older version to do this. The supported SDK mentioned on https://pub.dev/packages/ffigen/versions should be the same as ghe minimum SDK one is planning to support.

@dnfield
Copy link
Contributor Author

dnfield commented Nov 11, 2021

Ahh ok, that makes sense. Thanks.

@dnfield
Copy link
Contributor Author

dnfield commented Nov 11, 2021

It would be very helpful if whatever error gets shown somehow helps me understand this as the user.

Alternatively, it would be helpful if dart run ffigen warned me that my pubspec has a lower SDK constraint than the supported minimum.

@lrhn lrhn added the area-front-end Use area-front-end for front end / CFE / kernel format related issues. label Nov 15, 2021
@chloestefantsova chloestefantsova assigned chloestefantsova, dcharkes and ghost and unassigned chloestefantsova and ghost Nov 18, 2021
@dcharkes
Copy link
Contributor

Thanks for reporting.

The FFI transform should not crash.

We could opt to not run the FFI transform at all if there are any CFE errors. However, that will hide all FFI errors until all non-FFI errors are resolved. So maybe it's better to just deal with this in the FFI transform.

The FFI transform also crashes in the latest language version with an unknown type.

class A extends Struct {
  @Array.multi([16])
  external Array<Unknown> b;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-front-end Use area-front-end for front end / CFE / kernel format related issues. library-ffi type-bug Incorrect behavior (everything from a crash to more subtle misbehavior)
Projects
None yet
Development

No branches or pull requests

6 participants