Skip to content

[vm] Optimizer can keep object longer alive due to CSE/Store-to-Load of WeakReference.target #55518

@mkustermann

Description

@mkustermann

The following example (from cl/363581):

Future testWeakReferenceWeakness() async {
  late final WeakReference<Foo> weakReference;
  {
    Foo? foo = Foo();
    weakReference = WeakReference<Foo>(foo);
    Expect.equals(weakReference.target, foo);
    foo = null;
  }
  asyncStart();
  while (weakReference.target != null) {
    produceGarbage();
    await Future.delayed(const Duration(milliseconds: 10));
  }
  Expect.isNull(weakReference.target);
  asyncEnd();
}

is allocating an object that becomes immediately unreachable. We then produce garbage until the object was collected and the weak reference to it null'ed out.

This works in JIT mode but in AOT it seems that the optimizer is going to optimize it (incorrectly IMHO) away by forwarding the allocated object to the load from the WeakReference.

This is proof that the optimizer can prevent collection of unreachable objects.

/cc @alexmarkov @mraleph opinions on this behavior?

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-vmUse area-vm for VM related issues, including code coverage, and the AOT and JIT backends.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions