using System;
using System.Runtime.CompilerServices;
namespace XFESystem.Kernel;
/// <summary>
/// Compile explicitly into the native kernel and call from a diagnostic entry point.
/// Host CoreCLR success is not evidence that the Cosmos collector passed this regression.
/// No static or child-to-parent chain retains every frame's independent closure.
/// </summary>
public static class NativeGcRegression
{
public const int RequestedDepth = 320;
public static int FramesVerified { get; private set; }
public static int CollectionsRequested { get; private set; }
public static int FailureDepth { get; private set; }
public static bool Run()
{
FramesVerified = 0;
CollectionsRequested = 0;
FailureDepth = -1;
return Recurse(RequestedDepth) && FramesVerified == RequestedDepth + 1 && CollectionsRequested == 2;
}
[MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.NoOptimization)]
private static bool Recurse(int depth)
{
var independent = new Payload
{
Value = depth * 17 + 31,
Text = new string((char)('A' + depth % 26), 17),
Parent = new Parent { Salt = depth * 29 + 47 }
};
// Each frame's closure and payload must survive on that frame's roots.
// Parents are reached transitively through payload fields, never through lower recursion frames.
Func<Payload> readBack = () => independent;
bool descendants;
if (depth == 0)
{
ForceCollection();
descendants = true;
}
else descendants = Recurse(depth - 1);
var actual = readBack();
bool valid = actual.Value == depth * 17 + 31 && actual.Parent.Salt == depth * 29 + 47 &&
actual.Text.Length == 17 && actual.Text[0] == (char)('A' + depth % 26) && actual.Text[16] == actual.Text[0];
if (!valid && FailureDepth < 0) FailureDepth = depth;
if (valid) FramesVerified++;
GC.KeepAlive(readBack);
return descendants && valid;
}
[MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.NoOptimization)]
private static void ForceCollection()
{
GC.Collect();
CollectionsRequested++;
GC.Collect();
CollectionsRequested++;
}
private sealed class Payload
{
public int Value;
public string Text = null!;
public Parent Parent = null!;
}
private sealed class Parent { public int Salt; }
}
/// <summary>Produces truly nested active catch/rethrow funclets, then verifies a fresh exception can be handled.</summary>
public static class NativeEhRegression
{
// The boot thread has a 1 MiB stack; every active native rethrow also retains dispatch
// context. Exceed the former 64-entry defect without testing accidental stack exhaustion.
public const int RequestedDepth = 80;
public static int FramesCaught { get; private set; }
public static bool OriginalExceptionPreserved { get; private set; }
public static int PostCatchFrames { get; private set; }
public static bool Run()
{
FramesCaught = 0; PostCatchFrames = 0; OriginalExceptionPreserved = false;
var original = new InvalidOperationException("XFESystem deep EH regression");
try { Recurse(RequestedDepth, original); }
catch (InvalidOperationException caught) { OriginalExceptionPreserved = ReferenceEquals(original, caught); }
for (var index = 0; index < 4; ++index)
try { FreshThrow(); } catch (ArgumentException) { ++PostCatchFrames; }
return FramesCaught == RequestedDepth + 1 && OriginalExceptionPreserved && PostCatchFrames == 4;
}
[MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.NoOptimization)]
private static void Recurse(int depth, InvalidOperationException original)
{
try { if (depth == 0) throw original; Recurse(depth - 1, original); }
catch (InvalidOperationException) { ++FramesCaught; throw; }
}
[MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.NoOptimization)]
private static void FreshThrow() => throw new ArgumentException("XFESystem recovered EH regression");
}
using System;
using System.Runtime.CompilerServices;
namespace XFESystem.Kernel;
/// <summary>
/// Compile explicitly into the native kernel and call from a diagnostic entry point.
/// Host CoreCLR success is not evidence that the Cosmos collector passed this regression.
/// No static or child-to-parent chain retains every frame's independent closure.
/// </summary>
public static class NativeGcRegression
{
public const int RequestedDepth = 320;
public static int FramesVerified { get; private set; }
public static int CollectionsRequested { get; private set; }
public static int FailureDepth { get; private set; }
public static bool Run()
{
FramesVerified = 0;
CollectionsRequested = 0;
FailureDepth = -1;
return Recurse(RequestedDepth) && FramesVerified == RequestedDepth + 1 && CollectionsRequested == 2;
}
[MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.NoOptimization)]
private static bool Recurse(int depth)
{
var independent = new Payload
{
Value = depth * 17 + 31,
Text = new string((char)('A' + depth % 26), 17),
Parent = new Parent { Salt = depth * 29 + 47 }
};
// Each frame's closure and payload must survive on that frame's roots.
// Parents are reached transitively through payload fields, never through lower recursion frames.
Func<Payload> readBack = () => independent;
bool descendants;
if (depth == 0)
{
ForceCollection();
descendants = true;
}
else descendants = Recurse(depth - 1);
var actual = readBack();
bool valid = actual.Value == depth * 17 + 31 && actual.Parent.Salt == depth * 29 + 47 &&
actual.Text.Length == 17 && actual.Text[0] == (char)('A' + depth % 26) && actual.Text[16] == actual.Text[0];
if (!valid && FailureDepth < 0) FailureDepth = depth;
if (valid) FramesVerified++;
GC.KeepAlive(readBack);
return descendants && valid;
}
[MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.NoOptimization)]
private static void ForceCollection()
{
GC.Collect();
CollectionsRequested++;
GC.Collect();
CollectionsRequested++;
}
private sealed class Payload
{
public int Value;
public string Text = null!;
public Parent Parent = null!;
}
private sealed class Parent { public int Salt; }
}
/// <summary>Produces truly nested active catch/rethrow funclets, then verifies a fresh exception can be handled.</summary>
public static class NativeEhRegression
{
// The boot thread has a 1 MiB stack; every active native rethrow also retains dispatch
// context. Exceed the former 64-entry defect without testing accidental stack exhaustion.
public const int RequestedDepth = 80;
public static int FramesCaught { get; private set; }
public static bool OriginalExceptionPreserved { get; private set; }
public static int PostCatchFrames { get; private set; }
public static bool Run()
{
FramesCaught = 0; PostCatchFrames = 0; OriginalExceptionPreserved = false;
var original = new InvalidOperationException("XFESystem deep EH regression");
try { Recurse(RequestedDepth, original); }
catch (InvalidOperationException caught) { OriginalExceptionPreserved = ReferenceEquals(original, caught); }
for (var index = 0; index < 4; ++index)
try { FreshThrow(); } catch (ArgumentException) { ++PostCatchFrames; }
return FramesCaught == RequestedDepth + 1 && OriginalExceptionPreserved && PostCatchFrames == 4;
}
[MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.NoOptimization)]
private static void Recurse(int depth, InvalidOperationException original)
{
try { if (depth == 0) throw original; Recurse(depth - 1, original); }
catch (InvalidOperationException) { ++FramesCaught; throw; }
}
[MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.NoOptimization)]
private static void FreshThrow() => throw new ArgumentException("XFESystem recovered EH regression");
}