summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorJoseph Tremoulet <jotrem@microsoft.com>2016-09-13 13:01:15 -0400
committerJoseph Tremoulet <jotrem@microsoft.com>2016-09-15 11:40:52 -0400
commitf6b070c69585761996778fa5228b07d0be26be57 (patch)
tree3625b6323258a504b6aa31fb9a140e364e597944 /tests
parenta4bdd3b33762d07be30f207eb66be934ccd2ffb4 (diff)
downloadcoreclr-f6b070c69585761996778fa5228b07d0be26be57.tar.gz
coreclr-f6b070c69585761996778fa5228b07d0be26be57.tar.bz2
coreclr-f6b070c69585761996778fa5228b07d0be26be57.zip
Harden test against JIT optimizations
Declare static field `finalizerCompletedOnce` volatile -- this test has a side-effect-free busy-loop which checks that static variable for a change it expects a finalizer to make to it; this static field must be volatile to ensure the jit doesn't hoist the load from the loop. Call GC.KeepAlive on the objects constructed in the various allocation loops in this test, to make sure that the entire allocation isn't optimized away.
Diffstat (limited to 'tests')
-rw-r--r--tests/src/GC/Scenarios/FinalizeTimeout/FinalizeTimeout.cs7
1 files changed, 6 insertions, 1 deletions
diff --git a/tests/src/GC/Scenarios/FinalizeTimeout/FinalizeTimeout.cs b/tests/src/GC/Scenarios/FinalizeTimeout/FinalizeTimeout.cs
index 0fda8172e4..60a6861734 100644
--- a/tests/src/GC/Scenarios/FinalizeTimeout/FinalizeTimeout.cs
+++ b/tests/src/GC/Scenarios/FinalizeTimeout/FinalizeTimeout.cs
@@ -17,6 +17,7 @@ public class FinalizeTimeout
do
{
finalizableObject = new BlockingFinalizerOnShutdown();
+ GC.KeepAlive(finalizableObject);
} while (!BlockingFinalizerOnShutdown.finalizerCompletedOnce);
// Start a bunch of threads that allocate continuously, to increase the chance that when Main returns, one of the
@@ -42,12 +43,15 @@ public class FinalizeTimeout
{
byte[] b;
while (true)
+ {
b = new byte[1024];
+ GC.KeepAlive(b);
+ }
}
private class BlockingFinalizerOnShutdown
{
- public static bool finalizerCompletedOnce = false;
+ public volatile static bool finalizerCompletedOnce = false;
public bool isLastObject = false;
~BlockingFinalizerOnShutdown()
@@ -68,6 +72,7 @@ public class FinalizeTimeout
do
{
o = new object();
+ GC.KeepAlive(o);
} while ((++i & 0xff) != 0 || (elapsed = DateTime.Now - start) < timeout);
Console.WriteLine("Finalizer end");