Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions tests/Java.Interop-Tests/Java.Interop/FinalizerHelpers.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Originally from: https://github.com/mono/mono/blob/8266c5604b8c03882f2b06af27fdea46b142d6b9/mono/mini/TestHelpers.cs#L12

using System;
using System.Threading;

namespace Java.InteropTests
{
// False pinning cases are still possible. For example the thread can die
// and its stack reused by another thread. It also seems that a thread that
// does a GC can keep on the stack references to objects it encountered
// during the collection which are never released afterwards. This would
// be more likely to happen with the interpreter which reuses more stack.
static class FinalizerHelpers
{
private static IntPtr aptr;
Copy link
Contributor

Choose a reason for hiding this comment

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

@BrzVlad: Why is this member needed at all?

Copy link
Contributor

Choose a reason for hiding this comment

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

Answer: probably to prevent optimizing out the stackalloc: https://discord.com/channels/732297728826277939/732297837953679412/902261731471007765


private static unsafe void NoPinActionHelper (int depth, Action act)
{
// Avoid tail calls
int* values = stackalloc int [20];
aptr = new IntPtr (values);

if (depth <= 0) {
//
// When the action is called, this new thread might have not allocated
// anything yet in the nursery. This means that the address of the first
// object that would be allocated would be at the start of the tlab and
// implicitly the end of the previous tlab (address which can be in use
// when allocating on another thread, at checking if an object fits in
// this other tlab). We allocate a new dummy object to avoid this type
// of false pinning for most common cases.
//
new object ();
act ();
} else {
NoPinActionHelper (depth - 1, act);
}
}

public static void PerformNoPinAction (Action act)
{
Thread thr = new Thread (() => NoPinActionHelper (128, act));
thr.Start ();
thr.Join ();
}
}
}
28 changes: 9 additions & 19 deletions tests/Java.Interop-Tests/Java.Interop/JavaObjectTest.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Threading;

using Java.Interop;

Expand All @@ -18,13 +17,11 @@ public void JavaReferencedInstanceSurvivesCollection ()
using (var t = new JniType ("java/lang/Object")) {
var oldHandle = IntPtr.Zero;
var array = new JavaObjectArray<JavaObject> (1);
var w = new Thread (() => {
FinalizerHelpers.PerformNoPinAction (() => {
var v = new JavaObject ();
oldHandle = v.PeerReference.Handle;
array [0] = v;
});
w.Start ();
w.Join ();
JniEnvironment.Runtime.ValueManager.CollectPeers ();
GC.WaitForPendingFinalizers ();
GC.WaitForPendingFinalizers ();
Expand Down Expand Up @@ -80,13 +77,11 @@ public void UnreferencedInstanceIsCollected ()
{
JniObjectReference oldHandle = new JniObjectReference ();
WeakReference r = null;
var t = new Thread (() => {
FinalizerHelpers.PerformNoPinAction (() => {
var v = new JavaObject ();
oldHandle = v.PeerReference.NewWeakGlobalRef ();
r = new WeakReference (v);
});
t.Start ();
t.Join ();
JniEnvironment.Runtime.ValueManager.CollectPeers ();
GC.WaitForPendingFinalizers ();
GC.WaitForPendingFinalizers ();
Expand All @@ -110,20 +105,17 @@ public void Dispose ()

#if !NO_GC_BRIDGE_SUPPORT
[Test]
// See: https://github.com/dotnet/runtime/issues/60638
[Category ("IgnoreInterpreter")]
public void Dispose_Finalized ()
{
var d = false;
var f = false;
var t = new Thread (() => {
var v = new JavaDisposedObject (() => d = true, () => f = true);
GC.KeepAlive (v);
FinalizerHelpers.PerformNoPinAction (() => {
FinalizerHelpers.PerformNoPinAction (() => {
var v = new JavaDisposedObject (() => d = true, () => f = true);
GC.KeepAlive (v);
});
JniEnvironment.Runtime.ValueManager.CollectPeers ();
Copy link
Contributor

Choose a reason for hiding this comment

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

This change somewhat confuses me: we need to call GC.Collect() from both a new thread and the main thread in order to get the test to pass? Instead of invoking GC.Collect() from the main thread twice?

This is very confusing to me.

Copy link
Member Author

Choose a reason for hiding this comment

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

From what I understood a single call to FinalizerHelpers.PerformNoPinAction() ensures one GC.

And so we need two GCs for the finalizer to always run in this case. @BrzVlad is that correct?

Details here: dotnet/runtime#60638 (comment)

Copy link
Member

Choose a reason for hiding this comment

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

The problem is that, if an object is alive then the GC will scan it and, by doing so, it will probably store it in the stack somewhere. In very unfortunate scenarios, at the second collection, the pinning step will find this reference existing on the stack and will pin the object (that might have now been dead otherwise). Because the JavaDisposedObject is alive during the first collection, we do this collection on a separate thread, so the second GC doesn't get to find this left over references on the stack.

});
t.Start ();
t.Join ();
JniEnvironment.Runtime.ValueManager.CollectPeers ();
GC.WaitForPendingFinalizers ();
JniEnvironment.Runtime.ValueManager.CollectPeers ();
GC.WaitForPendingFinalizers ();
Assert.IsFalse (d);
Expand Down Expand Up @@ -185,11 +177,9 @@ public void Ctor_Exceptions ()
public void CrossThreadSharingRequresRegistration ()
{
JavaObject o = null;
var t = new Thread (() => {
FinalizerHelpers.PerformNoPinAction (() => {
o = new JavaObject ();
});
t.Start ();
t.Join ();
o.ToString ();
o.Dispose ();
}
Expand Down