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

Destructuring record that contains Finalizable throws "Attempt to execute code removed by Dart AOT compiler (TFA)" #54414

Closed
blaugold opened this issue Dec 19, 2023 · 6 comments
Assignees
Labels
area-vm Use area-vm for VM related issues, including code coverage, and the AOT and JIT backends. library-ffi vm-tfa

Comments

@blaugold
Copy link
Contributor

This tracker is for issues related to:

  • Dart VM

Executing the following program (see this repository for a full repro) causes "Attempt to execute code removed by Dart AOT compiler (TFA)" to be thrown:

import 'package:cbl/cbl.dart';
import 'package:cbl_dart/cbl_dart.dart';

void main() async {
  await CouchbaseLiteDart.init(edition: Edition.enterprise);

  final db = Database.openSync('db');

  final replicatorConfiguration = ReplicatorConfiguration(
    target: UrlEndpoint(Uri.parse('ws://localhost:4984/db')),
  )..addCollection(db.defaultCollection);

  final replicator = await Replicator.createSync(replicatorConfiguration);

  await replicator.close();
  await db.close();
}
Unhandled exception:
Attempt to execute code removed by Dart AOT compiler (TFA)
#0      FfiReplicator.create (package:cbl/src/replication/ffi_replicator.dart)
#1      SyncReplicator.create (package:cbl/src/replication/replicator.dart:278)
#2      Replicator.createSync (package:cbl/src/replication/replicator.dart:147)
#3      main (file:///Users/terwesten/dev/repo_cbl_code_removed_tfa/main.dart:14)
<asynchronous suspension>
If providing this information as part of reporting a bug, please review the information
below to ensure it only contains things you're comfortable posting publicly.

#### General info

- Dart 3.3.0-219.0.dev (dev) (Mon Dec 11 20:03:47 2023 -0800) on "macos_arm64"
- on macos / Version 14.2 (Build 23C64)
- locale is en-US

#### Project info

- sdk constraint: '^3.2.0'
- dependencies: cbl, cbl_dart
- dev_dependencies: 

#### Process info

| Memory |  CPU | Elapsed time | Command line                                                                    |
| -----: | ---: | -----------: | ------------------------------------------------------------------------------- |
|  71 MB | 0.0% |        09:38 | dart devtools --machine --allow-embedding --port 9199                           |
| 588 MB | 0.0% |        09:38 | dart language-server --protocol=lsp --client-id=VS-Code --client-version=3.78.2 |
| 456 MB | 0.0% |        08:16 | dart language-server --protocol=lsp --client-id=VS-Code --client-version=3.78.2 |
| 150 MB | 0.0% |        09:38 | flutter_tools.snapshot daemon  
@blaugold
Copy link
Contributor Author

Actually, here is a minimal reproducible example:

import 'dart:ffi';

void main() {
  final (a, _) = b();
  print(a);
}

class A implements Finalizable {}

(A, bool) b() => (A(), true);
Unhandled exception:
Attempt to execute code removed by Dart AOT compiler (TFA)
#0      main (file:///Users/terwesten/dev/repo_cbl_code_removed_tfa/simple.dart)
#1      _delayEntrypointInvocation.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:297)
#2      _RawReceivePort._handleMessage (dart:isolate-patch/isolate_patch.dart:184)

@blaugold blaugold changed the title Attempt to execute code removed by Dart AOT compiler (TFA) Destructuring record that contains Finalizable throws "Attempt to execute code removed by Dart AOT compiler (TFA)" Dec 20, 2023
blaugold added a commit to cbl-dart/cbl-dart that referenced this issue Dec 20, 2023
blaugold added a commit to cbl-dart/cbl-dart that referenced this issue Dec 20, 2023
@blaugold
Copy link
Contributor Author

/cc @dcharkes

@dcharkes dcharkes added area-vm Use area-vm for VM related issues, including code coverage, and the AOT and JIT backends. library-ffi vm-tfa labels Dec 21, 2023
@dcharkes
Copy link
Contributor

dcharkes commented Dec 21, 2023

I suspect we have another corner case with the finalizable transform, some previous corner cases:

Let me investigate what the compiler makes of this.

static method main() → void {
  final hoisted has-declared-initializer self::A a;
  {
    final synthesized(self::A, core::bool) #0#0 = self::b();
    a = block {
      final synthesized self::A :expressionValueWrappedFinalizable = #0#0.$1{self::A};
      _in::reachabilityFence(a);
    } =>:expressionValueWrappedFinalizable;
  }
  core::print(a);
  _in::reachabilityFence(a);
}

Gets turned into

static method main() → void {
  final hoisted has-declared-initializer self::A a;
  {
    final synthesized(self::A, core::bool) #0#0 = [@vm.inferred-type.metadata=dart.core::_Record] self::b();
    a = block {
      final synthesized self::A :expressionValueWrappedFinalizable = #0#0.$1{self::A};
      throw "Attempt to execute code removed by Dart AOT compiler (TFA)";
    } =>:expressionValueWrappedFinalizable;
  }
  core::print(a);
  _in::reachabilityFence(a);
}

Basically, we have a fence on the variable before it get's projected out from the record.

final hoisted has-declared-initializer self::A a; This should be treated in the same way as a late variable as we did in https://dart-review.googlesource.com/c/sdk/+/287460:

static method main() → void {
  self::A? :a:finalizableValue;
  final hoisted has-declared-initializer self::A a;
  {
    final synthesized(self::A, core::bool) #0#0 = [@vm.inferred-type.metadata=dart.core::_Record] self::b();
    :a:finalizableValue = a = block {
      final synthesized self::A :expressionValueWrappedFinalizable = #0#0.$1{self::A};
      _in::reachabilityFence(:a:finalizableValue);
    } =>:expressionValueWrappedFinalizable;
  }
  core::print(a);
  _in::reachabilityFence(:a:finalizableValue);
}

@dcharkes dcharkes self-assigned this Dec 21, 2023
@dcharkes
Copy link
Contributor

Fix: https://dart-review.googlesource.com/c/sdk/+/343180

Thanks for reporting @blaugold !

@a-siva
Copy link
Contributor

a-siva commented Dec 21, 2023

@dcharkes Can this be closed ?

@dcharkes
Copy link
Contributor

I'd like someone from the VM team to review the fix and land it first.

nielsenko added a commit to realm/realm-dart that referenced this issue Apr 24, 2024
This is to avoid a few Dart compiler bugs related to Finalizable objects:
- dart-lang/sdk#54414
- dart-lang/sdk#52596
- dart-lang/sdk#51538
nielsenko added a commit to realm/realm-dart that referenced this issue Apr 24, 2024
This is to avoid a few Dart compiler bugs related to Finalizable objects:
- dart-lang/sdk#54414
- dart-lang/sdk#52596
- dart-lang/sdk#51538
nielsenko added a commit to realm/realm-dart that referenced this issue Apr 24, 2024
* Bump required sdk version to ^3.3.0

This is to avoid a few Dart compiler bugs related to Finalizable objects:
- dart-lang/sdk#54414
- dart-lang/sdk#52596
- dart-lang/sdk#51538

* Update CHANGELOG
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. library-ffi vm-tfa
Projects
None yet
Development

No branches or pull requests

3 participants