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

Fix Subclassed WeakReference to be GCed #3347

Merged
merged 4 commits into from
Jun 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
#include "GCTypes.h"

extern int __object_array_id;
extern int __weak_ref_id;
extern int __weak_ref_ids_min;
extern int __weak_ref_ids_max;
extern int __weak_ref_field_offset;
extern int __array_ids_min;
extern int __array_ids_max;
Expand Down Expand Up @@ -86,7 +87,8 @@ static inline size_t Object_Size(Object *object) {
}

static inline bool Object_IsWeakReference(Object *object) {
return object->rtti->rt.id == __weak_ref_id;
int32_t id = object->rtti->rt.id;
return __weak_ref_ids_min <= id && id <= __weak_ref_ids_max;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[note] range is closed set 👍

def loop(node: Class): Unit = {
out += node
val start = id
id += 1
val directSubclasses =
node.subclasses.filter(_.parent == Some(node)).toArray
directSubclasses.sortBy(_.name.show).foreach { subcls => loop(subcls) }
val end = id - 1
ids(node) = start
ranges(node) = start to end

}

static inline bool Object_IsReferantOfWeakReference(Object *object,
Expand Down
16 changes: 11 additions & 5 deletions tools/src/main/scala/scala/scalanative/codegen/Generate.scala
Original file line number Diff line number Diff line change
Expand Up @@ -500,10 +500,10 @@ object Generate {
Val.Int(value)
)

val (weakRefId, modifiedFieldOffset) = linked.infos
val (weakRefIdsMin, weakRefIdsMax, modifiedFieldOffset) = linked.infos
.get(Global.Top("java.lang.ref.WeakReference"))
.collect { case cls: Class if cls.allocated => cls }
.fold((-1, -1)) { weakRef =>
.fold((-1, -1, -1)) { weakRef =>
// if WeakReferences are being compiled and therefore supported
val gcModifiedFieldIndexes: Seq[Int] =
meta.layout(weakRef).entries.zipWithIndex.collect {
Expand All @@ -517,9 +517,14 @@ object Generate {
"Exactly one field should have the \"_gc_modified_\" modifier in java.lang.ref.WeakReference"
)

(meta.ids(weakRef), gcModifiedFieldIndexes.head)
(
meta.ranges(weakRef).start,
meta.ranges(weakRef).end,
gcModifiedFieldIndexes.head
)
}
addToBuf(weakRefIdName, weakRefId)
addToBuf(weakRefIdsMaxName, weakRefIdsMax)
addToBuf(weakRefIdsMinName, weakRefIdsMin)
addToBuf(weakRefFieldOffsetName, modifiedFieldOffset)
}

Expand Down Expand Up @@ -621,7 +626,8 @@ object Generate {
val moduleArrayName = extern("__modules")
val moduleArraySizeName = extern("__modules_size")
val objectArrayIdName = extern("__object_array_id")
val weakRefIdName = extern("__weak_ref_id")
val weakRefIdsMaxName = extern("__weak_ref_ids_max")
val weakRefIdsMinName = extern("__weak_ref_ids_min")
val weakRefFieldOffsetName = extern("__weak_ref_field_offset")
val registryOffsetName = extern("__weak_ref_registry_module_offset")
val registryFieldOffsetName = extern("__weak_ref_registry_field_offset")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import org.scalanative.testsuite.utils.Platform
class WeakReferenceTest {

case class A()
class SubclassedWeakRef[A](a: A, referenceQueue: ReferenceQueue[A])
extends WeakReference[A](a, referenceQueue)

def gcAssumption(): Unit = {
assumeTrue(
Expand All @@ -36,6 +38,16 @@ class WeakReferenceTest {
weakRef
}

@noinline def allocSubclassedWeakRef(
referenceQueue: ReferenceQueue[A]
): SubclassedWeakRef[A] = {
var a = A()
val weakRef = new SubclassedWeakRef(a, referenceQueue)
assertEquals("get() should return object reference", weakRef.get(), A())
a = null
weakRef
}

@deprecated @nooptimize @Test def addsToReferenceQueueAfterGC(): Unit = {
assumeFalse(
"In the CI Scala 3 sometimes SN fails to clean weak references in some of Windows build configurations",
Expand Down Expand Up @@ -72,22 +84,33 @@ class WeakReferenceTest {
val refQueue = new ReferenceQueue[A]()
val weakRef1 = allocWeakRef(refQueue)
val weakRef2 = allocWeakRef(refQueue)
val weakRefList = List(weakRef1, weakRef2)
val weakRef3 = allocSubclassedWeakRef(refQueue)
val weakRefList = List(weakRef1, weakRef2, weakRef3)

GC.collect()
def newDeadline() = System.currentTimeMillis() + 60 * 1000
assertEventuallyIsCollected("weakRef1", weakRef1, deadline = newDeadline())
assertEventuallyIsCollected("weakRef2", weakRef2, deadline = newDeadline())
assertEventuallyIsCollected("weakRef3", weakRef3, deadline = newDeadline())

assertEquals("weakRef1", null, weakRef1.get())
assertEquals("weakRef2", null, weakRef2.get())
assertEquals("weakRef3", null, weakRef3.get())
val a = refQueue.poll()
assertNotNull("a was null", a)
val b = refQueue.poll()
assertNotNull("b was null", b)
val c = refQueue.poll()
assertNotNull("c was null", c)
assertTrue("!contains a", weakRefList.contains(a))
assertTrue("!contains b", weakRefList.contains(b))
assertNotEquals(a, b)
assertTrue("!contains c", weakRefList.contains(c))
def allDistinct(list: List[_]): Unit = list match {
case head :: next =>
next.foreach(assertNotEquals(_, head)); allDistinct(next)
case Nil => ()
}
allDistinct(List(a, b, c))
assertEquals("pool not null", null, refQueue.poll())
}

Expand Down