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

Implementing Finalizable causes unhandled exception in FrontendCompiler #49075

Closed
blaugold opened this issue May 21, 2022 · 5 comments
Closed
Assignees
Labels
area-front-end Use area-front-end for front end / CFE / kernel format related issues. P2 A bug or feature request we're likely to work on

Comments

@blaugold
Copy link
Contributor

When adding Finalizable to a specific class in my project, I get the following exception when running dart test:

unhandled exception:
Null check operator used on a null value
#0      BinaryPrinter._getVariableIndex (package:kernel/binary/ast_to_binary.dart:83:17)
#1      BinaryPrinter.visitVariableGet (package:kernel/binary/ast_to_binary.dart:1497:17)
#2      VariableGet.accept (package:kernel/ast.dart:4137:44)
#3      BinaryPrinter.writeNode (package:kernel/binary/ast_to_binary.dart:430:10)
#4      BinaryPrinter.writeNodeList (package:kernel/binary/ast_to_binary.dart:340:7)
#5      BinaryPrinter.visitArguments (package:kernel/binary/ast_to_binary.dart:1751:5)
#6      Arguments.accept (package:kernel/ast.dart:5119:38)
#7      BinaryPrinter.writeArgumentsNode (package:kernel/binary/ast_to_binary.dart:444:10)
#8      BinaryPrinter.visitStaticInvocation (package:kernel/binary/ast_to_binary.dart:1734:5)
#9      StaticInvocation.accept (package:kernel/ast.dart:6289:44)
#10     BinaryPrinter.writeNode (package:kernel/binary/ast_to_binary.dart:430:10)
#11     BinaryPrinter.visitExpressionStatement (package:kernel/binary/ast_to_binary.dart:2052:5)
#12     ExpressionStatement.accept (package:kernel/ast.dart:8829:43)
#13     BinaryPrinter.writeNode (package:kernel/binary/ast_to_binary.dart:430:10)
#14     BinaryPrinter.writeNodeList (package:kernel/binary/ast_to_binary.dart:340:7)
#15     BinaryPrinter.visitBlockExpression (package:kernel/binary/ast_to_binary.dart:2017:5)
#16     BlockExpression.accept (package:kernel/ast.dart:8461:44)
#17     BinaryPrinter.writeNode (package:kernel/binary/ast_to_binary.dart:430:10)
#18     BinaryPrinter.writeNodeList (package:kernel/binary/ast_to_binary.dart:340:7)
#19     BinaryPrinter.visitArguments (package:kernel/binary/ast_to_binary.dart:1751:5)
#20     Arguments.accept (package:kernel/ast.dart:5119:38)
#21     BinaryPrinter.writeArgumentsNode (package:kernel/binary/ast_to_binary.dart:444:10)
#22     BinaryPrinter.visitStaticInvocation (package:kernel/binary/ast_to_binary.dart:1734:5)
#23     StaticInvocation.accept (package:kernel/ast.dart:6289:44)
#24     BinaryPrinter.writeNode (package:kernel/binary/ast_to_binary.dart:430:10)
#25     BinaryPrinter.writeOptionalNode (package:kernel/binary/ast_to_binary.dart:522:7)
#26     BinaryPrinter.writeVariableDeclaration (package:kernel/binary/ast_to_binary.dart:2266:5)
#27     BinaryPrinter.visitLet (package:kernel/binary/ast_to_binary.dart:2006:5)
#28     Let.accept (package:kernel/ast.dart:8390:44)
#29     BinaryPrinter.writeNode (package:kernel/binary/ast_to_binary.dart:430:10)
#30     BinaryPrinter.visitYieldStatement (package:kernel/binary/ast_to_binary.dart:2246:5)
#31     YieldStatement.accept (package:kernel/ast.dart:10180:43)
#32     BinaryPrinter.writeNode (package:kernel/binary/ast_to_binary.dart:430:10)
#33     BinaryPrinter.writeNodeList (package:kernel/binary/ast_to_binary.dart:340:7)
#34     BinaryPrinter.visitBlock (package:kernel/binary/ast_to_binary.dart:2063:5)
#35     Block.accept (package:kernel/ast.dart:8885:43)
#36     BinaryPrinter.writeNode (package:kernel/binary/ast_to_binary.dart:430:10)
#37     BinaryPrinter.visitLabeledStatement (package:kernel/binary/ast_to_binary.dart:2096:5)
#38     LabeledStatement.accept (package:kernel/ast.dart:9095:43)
#39     BinaryPrinter.writeNode (package:kernel/binary/ast_to_binary.dart:430:10)
#40     BinaryPrinter.writeNodeList (package:kernel/binary/ast_to_binary.dart:340:7)
#41     BinaryPrinter.visitBlock (package:kernel/binary/ast_to_binary.dart:2063:5)
#42     Block.accept (package:kernel/ast.dart:8885:43)
#43     BinaryPrinter.writeNode (package:kernel/binary/ast_to_binary.dart:430:10)
#44     BinaryPrinter.visitTryCatch (package:kernel/binary/ast_to_binary.dart:2214:5)
#45     TryCatch.accept (package:kernel/ast.dart:9918:43)
#46     BinaryPrinter.writeNode (package:kernel/binary/ast_to_binary.dart:430:10)
#47     BinaryPrinter.writeOptionalNode (package:kernel/binary/ast_to_binary.dart:522:7)
#48     BinaryPrinter.visitFunctionNode (package:kernel/binary/ast_to_binary.dart:1481:5)
#49     FunctionNode.accept (package:kernel/ast.dart:3846:38)
#50     BinaryPrinter.writeFunctionNode (package:kernel/binary/ast_to_binary.dart:437:10)
#51     BinaryPrinter.visitFunctionDeclaration (package:kernel/binary/ast_to_binary.dart:2290:5)
#52     FunctionDeclaration.accept (package:kernel/ast.dart:10502:43)
#53     BinaryPrinter.writeNode (package:kernel/binary/ast_to_binary.dart:430:10)
#54     BinaryPrinter.writeNodeList (package:kernel/binary/ast_to_binary.dart:340:7)
#55     BinaryPrinter.visitBlock (package:kernel/binary/ast_to_binary.dart:2063:5)
#56     Block.accept (package:kernel/ast.dart:8885:43)
#57     BinaryPrinter.writeNode (package:kernel/binary/ast_to_binary.dart:430:10)
#58     BinaryPrinter.writeOptionalNode (package:kernel/binary/ast_to_binary.dart:522:7)
#59     BinaryPrinter.visitFunctionNode (package:kernel/binary/ast_to_binary.dart:1481:5)
#60     FunctionNode.accept (package:kernel/ast.dart:3846:38)
#61     BinaryPrinter.writeFunctionNode (package:kernel/binary/ast_to_binary.dart:437:10)
#62     BinaryPrinter.visitProcedure (package:kernel/binary/ast_to_binary.dart:1312:5)
#63     Procedure.accept (package:kernel/ast.dart:3151:40)
#64     BinaryPrinter.writeProcedureNode (package:kernel/binary/ast_to_binary.dart:458:10)
#65     BinaryPrinter.writeProcedureNodeList (package:kernel/binary/ast_to_binary.dart:349:7)
#66     BinaryPrinter.visitClass (package:kernel/binary/ast_to_binary.dart:1216:5)
#67     Class.accept (package:kernel/ast.dart:1408:38)
#68     BinaryPrinter.writeClassNode (package:kernel/binary/ast_to_binary.dart:472:10)
#69     BinaryPrinter.writeClassNodeList (package:kernel/binary/ast_to_binary.dart:367:7)
#70     BinaryPrinter.visitLibrary (package:kernel/binary/ast_to_binary.dart:1058:5)
#71     Library.accept (package:kernel/ast.dart:565:38)
#72     BinaryPrinter.writeLibraryNode (package:kernel/binary/ast_to_binary.dart:451:10)
#73     BinaryPrinter.writeLibraries (package:kernel/binary/ast_to_binary.dart:756:9)
#74     BinaryPrinter.writeComponentFile.<anonymous closure> (package:kernel/binary/ast_to_binary.dart:595:7)
#75     Timeline.timeSync (dart:developer/timeline.dart:157:22)
#76     BinaryPrinter.writeComponentFile (package:kernel/binary/ast_to_binary.dart:577:14)
#77     IncrementalSerializer.serialize (package:front_end/src/fasta/incremental_serializer.dart:308:13)
#78     IncrementalSerializer.writePackagesToSinkAndTrimComponent (package:front_end/src/fasta/incremental_serializer.dart:158:24)
#79     FrontendCompiler.writeDillFile (file:///opt/s/w/ir/cache/builder/sdk/pkg/frontend_server/lib/frontend_server.dart:728:29)
#80     FrontendCompiler.recompileDelta (file:///opt/s/w/ir/cache/builder/sdk/pkg/frontend_server/lib/frontend_server.dart:838:13)
<asynchronous suspension>

Dart version:

Dart SDK version: 2.17.1 (stable) (Tue May 17 17:58:21 2022 +0000) on "macos_arm64"

Steps to replicate:

git clone https://github.com/cbl-dart/cbl-dart.git --depth 1 -b refactor/finalizable
cd cbl-dart
dart pub global activate melos
melos bs --scope cbl_e2e_tests_standalone_dart
cd packages/cbl_e2e_tests_standalone_dart
dart test

When FfiReplicator is not implementing Finalizable the error goes away.

@lrhn lrhn added the area-front-end Use area-front-end for front end / CFE / kernel format related issues. label May 21, 2022
@johnniwinther johnniwinther added the P2 A bug or feature request we're likely to work on label May 23, 2022
@blaugold
Copy link
Contributor Author

Maybe @dcharkes has some insight on this since he implemented Finalizable.

@blaugold
Copy link
Contributor Author

#49122 looks very similar to this issue, but much easier to reproduce and not specific to Finalizable.

@voledyaev
Copy link

voledyaev commented May 31, 2022

I was able to reproduce the same issue in my project and found the root of problem, this could be any async operation with saving a finalizable instance to a variable
Consider this example with async factory method

Future<void> main(List<String> arguments) async {
  // Working without any problem
  await MyFinalizable.create();

  // You'll face BinaryPrinter._getVariableIndex crash if will try to save created instance to the variable
  final myFinalizable = await MyFinalizable.create();

  // Or you even can just try to use await for finalizable, same crash
  final mySecondFinalizable = await MySecondFinalizable();
}

class MyFinalizable implements Finalizable {
  MyFinalizable._();

  static Future<MyFinalizable> create() async {
    // Some initialization...
    await Future.delayed(Duration(seconds: 1));

    return MyFinalizable._();
  }
}

class MySecondFinalizable implements Finalizable {
  MySecondFinalizable();
}

Dart SDK version: 2.17.1 (stable) on "macos_arm64"

@dcharkes
Copy link
Contributor

@dcharkes dcharkes self-assigned this May 31, 2022
@dcharkes
Copy link
Contributor

Thanks @voledyaev for making the minimal repro, that was really useful! 🚀
Thanks for reporting @blaugold.

copybara-service bot pushed a commit that referenced this issue Jun 20, 2022
We were adding fences around the variable declaration expressions
including the variable that was not declared. This led to verification
errors and crashes when not running the verifier.
`Verification error: Variable 'VariableDeclarationImpl(final MyFinalizable myFinalizable)' used out of scope.`

TEST=pkg/vm/testcases/transformations/ffi/regress_49075.dart

Closes: #49075
Change-Id: I1f07f0343d29c3efb3c63c0aa0e3f20338b5c653
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/246520
Commit-Queue: Daco Harkes <[email protected]>
Reviewed-by: Jens Johansen <[email protected]>
copybara-service bot pushed a commit that referenced this issue Jun 20, 2022
This is a patch release that fixes:

- Improve analysis of enums and switch (issue [#49188]).
- Fix compiler crash when initializing Finalizable objects (issue [#49075]).

[#49188]: #49188
[#49075]: #49075

Change-Id: If2059474ce2acbadf7f3c6e407f0087d262e2842
copybara-service bot pushed a commit that referenced this issue Jun 20, 2022
This is a patch release that fixes:

- Improve analysis of enums and switch (issue [#49188]).
- Fix compiler crash when initializing Finalizable objects (issue [#49075]).

[#49188]: #49188
[#49075]: #49075

Change-Id: If2059474ce2acbadf7f3c6e407f0087d262e2842
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/249161
Reviewed-by: Michael Thomsen <[email protected]>
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. P2 A bug or feature request we're likely to work on
Projects
None yet
Development

No branches or pull requests

6 participants