using System; using System.Runtime; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; namespace XFESystem.Kernel; /// /// 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. /// 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 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; } } /// Produces truly nested active catch/rethrow funclets, then verifies a fresh exception can be handled. 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"); } /// /// Run in the actual guest before enabling ephemeron-based browser ownership. /// Weak handles are explicitly freed: this runtime does not run finalizers. /// public static class NativeWeakLifetimeRegression { public static bool WeakTargetCleared { get; private set; } public static bool LiveValuePreserved { get; private set; } public static bool CycleCleared { get; private set; } public static bool FixedPointPreserved { get; private set; } public static bool HandlesDisposed { get; private set; } public static bool NativeHandleCountChecked { get; private set; } public static ulong DependentHandlesBefore { get; private set; } public static ulong DependentHandlesAfter { get; private set; } public static int Failure { get; private set; } public static int ChecksPassed { get; private set; } public static bool Run(Func? dependentHandleCount = null) { WeakTargetCleared = LiveValuePreserved = CycleCleared = FixedPointPreserved = false; Failure = ChecksPassed = 0; HandlesDisposed = true; NativeHandleCountChecked = dependentHandleCount is not null; DependentHandlesBefore = dependentHandleCount?.Invoke() ?? 0; var weak = CreateWeakTarget(); CollectTwice(); WeakTargetCleared = weak.Target is null; weak.Free(); Record(WeakTargetCleared, 1); var live = CreateLivePair(); CollectTwice(); var livePair = live.Handle.TargetAndDependent; LiveValuePreserved = ReferenceEquals(livePair.Target, live.Key) && livePair.Dependent is Wrapper value && value.Value == 0x584645 && ReferenceEquals(value.Key, live.Key) && ReferenceEquals(live.WeakValue.Target, value); live.WeakValue.Free(); Record(LiveValuePreserved, 2); live.Handle.Dispose(); live.Handle.Dispose(); HandlesDisposed &= !live.Handle.IsAllocated; GC.KeepAlive(live); var cycle = CreateUnreachableCycle(); CollectTwice(); CycleCleared = cycle.WeakNode.Target is null && cycle.WeakWrapper.Target is null && cycle.NodeToWrapper.TargetAndDependent == (null, null) && cycle.WrapperToNode.TargetAndDependent == (null, null); cycle.WeakNode.Free(); cycle.WeakWrapper.Free(); cycle.NodeToWrapper.Dispose(); cycle.WrapperToNode.Dispose(); cycle.NodeToWrapper.Dispose(); cycle.WrapperToNode.Dispose(); HandlesDisposed &= !cycle.NodeToWrapper.IsAllocated && !cycle.WrapperToNode.IsAllocated; Record(CycleCleared, 3); GC.KeepAlive(cycle); var chain = CreateReverseOrderChain(); CollectTwice(); var firstPair = chain.First.TargetAndDependent; var secondPair = chain.Second.TargetAndDependent; var thirdPair = chain.Third.TargetAndDependent; FixedPointPreserved = ReferenceEquals(firstPair.Target, chain.Key) && firstPair.Dependent is not null && ReferenceEquals(firstPair.Dependent, secondPair.Target) && secondPair.Dependent is not null && ReferenceEquals(secondPair.Dependent, thirdPair.Target) && thirdPair.Dependent is byte[] payload && payload[0] == 0x58 && payload[^1] == 0x45; chain.First.Dispose(); chain.Second.Dispose(); chain.Third.Dispose(); chain.First.Dispose(); chain.Second.Dispose(); chain.Third.Dispose(); HandlesDisposed &= !chain.First.IsAllocated && !chain.Second.IsAllocated && !chain.Third.IsAllocated; Record(FixedPointPreserved, 4); GC.KeepAlive(chain); DependentHandlesAfter = dependentHandleCount?.Invoke() ?? 0; HandlesDisposed &= !NativeHandleCountChecked || DependentHandlesAfter == DependentHandlesBefore; Record(HandlesDisposed, 5); return Failure == 0 && ChecksPassed == 5; } private static void Record(bool passed, int failure) { if (passed) ChecksPassed++; else if (Failure == 0) Failure = failure; } [MethodImpl(MethodImplOptions.NoInlining)] private static GCHandle CreateWeakTarget() => GCHandle.Alloc(new byte[128], GCHandleType.Weak); [MethodImpl(MethodImplOptions.NoInlining)] private static LivePair CreateLivePair() { object node = new(); var wrapper = new Wrapper { Key = node, Value = 0x584645 }; return new LivePair { Key = node, Handle = new DependentHandle(node, wrapper), WeakValue = GCHandle.Alloc(wrapper, GCHandleType.Weak) }; } [MethodImpl(MethodImplOptions.NoInlining)] private static DeadCycle CreateUnreachableCycle() { object node = new(); var wrapper = new Wrapper { Key = node, Value = 7 }; var nodeToWrapper = new DependentHandle(node, wrapper); var wrapperToNode = new DependentHandle(wrapper, node); return new DeadCycle { NodeToWrapper = nodeToWrapper, WrapperToNode = wrapperToNode, WeakNode = GCHandle.Alloc(node, GCHandleType.Weak), WeakWrapper = GCHandle.Alloc(wrapper, GCHandleType.Weak) }; } [MethodImpl(MethodImplOptions.NoInlining)] private static LiveChain CreateReverseOrderChain() { object first = new(), second = new(), third = new(); byte[] payload = new byte[4096]; payload[0] = 0x58; payload[^1] = 0x45; // The handle store encounters the last link first. Preserving the // payload requires repeated primary->secondary marking to converge. var thirdHandle = new DependentHandle(third, payload); var secondHandle = new DependentHandle(second, third); var firstHandle = new DependentHandle(first, second); return new LiveChain { Key = first, First = firstHandle, Second = secondHandle, Third = thirdHandle }; } [MethodImpl(MethodImplOptions.NoInlining)] private static void CollectTwice() { GC.Collect(); GC.Collect(); } private sealed class Wrapper { public object Key = null!; public int Value; } private sealed class LivePair { public object Key = null!; public DependentHandle Handle; public GCHandle WeakValue; } private sealed class DeadCycle { public DependentHandle NodeToWrapper, WrapperToNode; public GCHandle WeakNode, WeakWrapper; } private sealed class LiveChain { public object Key = null!; public DependentHandle First, Second, Third; } } /// Exercises the actual exported range validator and registered guest metadata. public static class NativeFrozenRangeRegression { public static int ChecksPassed { get; private set; } public static int Failure { get; private set; } public static bool RegisteredRangesValid { get; private set; } public static bool Run(Func validate, Func registeredRangesValid) { ChecksPassed = Failure = 0; const ulong start = 0xFFFF800010000000, reserved = 4 * 1024 * 1024; Check(validate(start, reserved, start + 128, start + 65536) == 1, 1); Check(validate(start, reserved, start, start) == 1, 2); Check(validate(start, reserved, start - 8, start + 65536) == 0, 3); Check(validate(start, reserved, start + 65536, start + 128) == 0, 4); Check(validate(start, reserved, start + 128, start + reserved + 4096) == 0, 5); Check(validate(ulong.MaxValue - 7, 64, 8, 16) == 0, 6); Check(validate(0, reserved, 0, 65536) == 0, 7); RegisteredRangesValid = registeredRangesValid() == 1; Check(RegisteredRangesValid, 8); return Failure == 0 && ChecksPassed == 8; } private static void Check(bool passed, int failure) { if (passed) ChecksPassed++; else if (Failure == 0) Failure = failure; } }