XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet

XFESystem

【Rust】XFE系统,基于Rust和汇编

公开
关注 0 Fork 0 Star 0
UTF-8
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// Exercises the Microsoft NativeAOT runtime contracts that the bare-metal PAL
// must support. Compilation alone is never a guest runtime verification.
internal static class Program
{
    [DllImport("xfe_pal", EntryPoint = "xfe_runtime_probe_report")]
    private static extern void Report(int stage, long value);
    [DllImport("xfe_pal", EntryPoint = "xfe_runtime_native_cpu_count")]
    private static extern uint NativeCpuCount();
    [DllImport("xfe_pal", EntryPoint = "xfe_runtime_native_copy_busy")]
    private static extern uint NativeCopyBusy();
    [DllImport("xfe_pal", EntryPoint = "xfe_runtime_write")]
    private static extern unsafe void WriteNative(byte* text, ulong length);
    [DllImport("xfe_pal", EntryPoint = "xfe_runtime_parallel_copy32")]
    private static extern unsafe int Copy32(int* source, ulong sourceBytes, byte* target, ulong targetBytes,
        uint sourcePitch, uint targetPitch, uint width, uint rows, uint swap);

    private sealed class Root(int value) { internal int Value = value; }
    private sealed class DependentValue(object owner)
    {
        internal object Owner = owner;
        ~DependentValue() => Interlocked.Increment(ref finalized);
    }
    [ThreadStatic] private static int threadToken;
    private static readonly object Gate = new();
    private static int finalized, stopWorker, iterations, workerResult, zeroDivisor;
    private static bool workerReady;

    private static int Main()
    {
        var table = new ConditionalWeakTable<object, Root>();
        var key = new object();
        table.Add(key, new Root(42));
        byte[] bytes = new byte[32768]; bytes[^1] = 211;
        threadToken = 7;
        GC.Collect();
        if (table.GetValue(key, _ => new Root(-1)).Value != 42 || bytes[^1] != 211 || threadToken != 7)
            return 1;
        Report(1, GC.GetTotalMemory(false));
        int exceptionMask = 0;
        try
        {
            try { Throw(); }
            catch (InvalidOperationException) { exceptionMask |= 1; }
            try { _ = ReadNull(null); }
            catch (NullReferenceException) { exceptionMask |= 2; }
            try { _ = Divide(70, Volatile.Read(ref zeroDivisor)); }
            catch (DivideByZeroException) { exceptionMask |= 4; }
            try { _ = Divide(int.MinValue, Volatile.Read(ref zeroDivisor) - 1); }
            catch (OverflowException) { exceptionMask |= 8; }
            try { _ = DivideLong(long.MinValue, Volatile.Read(ref zeroDivisor) - 1); }
            catch (OverflowException) { exceptionMask |= 16; }
        }
        finally { Report(2, exceptionMask == 31 ? 1 : 0); }
        if (exceptionMask != 31) return 2;

        var cycleTable = new ConditionalWeakTable<object, DependentValue>();
        CreateUnrootedCycle(cycleTable, out WeakReference weakKey, out WeakReference weakValue);
        for (int i = 0; i < 4; i++) { GC.Collect(); GC.WaitForPendingFinalizers(); }
        if (weakKey.IsAlive || weakValue.IsAlive || Volatile.Read(ref finalized) != 1) return 3;
        GC.KeepAlive(cycleTable);
        Report(3, 1);

        var worker = new Thread(Worker);
        worker.Start();
        lock (Gate)
            while (!workerReady)
                if (!Monitor.Wait(Gate, 5000)) return 4;
        if (Volatile.Read(ref workerResult) != 0) return 4;
        Report(4, 1);
        for (int i = 0; i < 8; i++)
        {
            _ = new byte[64 * 1024];
            GC.Collect(2, GCCollectionMode.Forced, blocking: true, compacting: true);
            if (Volatile.Read(ref workerResult) != 0 || bytes[^1] != 211 || table.GetValue(key, _ => new Root(-1)).Value != 42)
                return 5;
            Thread.Yield();
        }
        if (Volatile.Read(ref iterations) < 8 || threadToken != 7) return 5;
        Report(5, 1);
        Volatile.Write(ref stopWorker, 1);
        if (!worker.Join(5000) || Volatile.Read(ref workerResult) != 1 || threadToken != 7) return 6;
        if (!ParallelCopyWithGc()) return 7;
        Report(6, 1);
        GC.KeepAlive(key);
        GC.KeepAlive(bytes);
        return 0;
    }

    private static unsafe bool ParallelCopyWithGc()
    {
        const int width = 3840, rows = 2160, stride = width * 4 + 7;
        var source = new int[width * rows];
        var target = new byte[stride * rows + 64];
        for (int i = 0; i < source.Length; i++) source[i] = unchecked((int)(0xf0000000u | ((uint)i * 0x12731u)));
        Array.Fill(target, (byte)0xa7);
        fixed (int* input = source)
        fixed (byte* output = target)
        {
            if (Copy32(input, 1, output + 3, (ulong)target.Length - 3, width * 4, stride, width, rows, 0) != -1 ||
                Copy32(input, (ulong)source.Length * 4, output + 3, 1, width * 4, stride, width, rows, 0) != -1 ||
                Copy32(input, (ulong)source.Length * 4, (byte*)input, (ulong)source.Length * 4, width * 4, width * 4, width, rows, 0) != -1 ||
                Copy32(input, (ulong)source.Length * 4, output + 3, (ulong)target.Length - 3, width * 4, stride, width, rows, 2) != -1)
                return false;
        }
        if (target.Any(value => value != 0xa7)) return false;
        int stop = 0, collectionsWhilePinned = 0;
        using var started = new ManualResetEventSlim();
        var collector = new Thread(() =>
        {
            started.Set();
            while (Volatile.Read(ref stop) == 0)
            {
                if (NativeCopyBusy() == 1)
                {
                    GC.Collect(2, GCCollectionMode.Forced, blocking: true, compacting: true);
                    Interlocked.Increment(ref collectionsWhilePinned);
                }
                Thread.Yield();
            }
        }) { IsBackground = true };
        collector.Start();
        if (!started.Wait(10000)) return false;
        bool passed = true;
        for (uint round = 0; round < 16; round++)
        {
            fixed (int* input = source)
            fixed (byte* output = target)
            {
                if (Copy32(input, (ulong)source.Length * 4, output + 3, (ulong)target.Length - 3,
                    width * 4, stride, width, rows, round & 1) != 1) passed = false;
            }
            uint swap = round & 1;
            for (int row = 0; row < rows; row++)
            {
                // Inspect every output byte, padding and both outer guards.
                for (int x = 0; x < width; x++)
                {
                    uint rgb = (uint)source[row * width + x];
                    uint expected = swap == 0 ? rgb & 0xffffff : ((rgb & 255) << 16) | (rgb & 0xff00) | ((rgb >> 16) & 255);
                    int offset = 3 + row * stride + x * 4;
                    for (int b = 0; b < 4; b++) if (target[offset + b] != (byte)(expected >> (b * 8))) passed = false;
                }
                for (int pad = width * 4; pad < stride; pad++) if (target[3 + row * stride + pad] != 0xa7) passed = false;
            }
        }
        Volatile.Write(ref stop, 1);
        if (!collector.Join(10000)) return false;
        for (int i = 0; i < 3; i++) if (target[i] != 0xa7) passed = false;
        for (int i = 3 + stride * rows; i < target.Length; i++) if (target[i] != 0xa7) passed = false;
        uint cpus = NativeCpuCount();
        if (cpus > 1 && collectionsWhilePinned == 0) passed = false;
        byte[] report = System.Text.Encoding.UTF8.GetBytes("XFE_NATIVE_PARALLEL_PIN_PROOF|Online|" + cpus +
            "|Copies|16|RejectedExtents|4|CollectionsWhilePinned|" + collectionsWhilePinned +
            "|Passed|" + (passed ? 1 : 0) + "|ManagedSmp|0\n");
        fixed (byte* text = report) WriteNative(text, (ulong)report.Length);
        GC.KeepAlive(source); GC.KeepAlive(target);
        return passed;
    }

    [MethodImpl(MethodImplOptions.NoInlining)]
    private static void Throw() => throw new InvalidOperationException("NativeAOT EH probe");
    [MethodImpl(MethodImplOptions.NoInlining)]
    private static int ReadNull(Root? value) => value!.Value;
    [MethodImpl(MethodImplOptions.NoInlining)]
    private static int Divide(int numerator, int denominator) => numerator / denominator;
    [MethodImpl(MethodImplOptions.NoInlining)]
    private static long DivideLong(long numerator, long denominator) => numerator / denominator;
    [MethodImpl(MethodImplOptions.NoInlining)]
    private static void CreateUnrootedCycle(ConditionalWeakTable<object, DependentValue> table, out WeakReference keyReference, out WeakReference valueReference)
    {
        var key = new object(); var value = new DependentValue(key);
        table.Add(key, value); keyReference = new WeakReference(key); valueReference = new WeakReference(value);
    }
    [MethodImpl(MethodImplOptions.NoInlining)]
    private static void Worker()
    {
        try
        {
            threadToken = 91;
            var root = new Root(91);
            var retained = new byte[65536]; retained[0] = 17; retained[^1] = 193;
            lock (Gate) { workerReady = true; Monitor.PulseAll(Gate); }
            while (Volatile.Read(ref stopWorker) == 0)
            {
                var allocation = new byte[4096]; allocation[^1] = 113;
                if (root.Value != 91 || retained[0] != 17 || retained[^1] != 193 || allocation[^1] != 113 || threadToken != 91)
                { Volatile.Write(ref workerResult, -1); return; }
                Interlocked.Increment(ref iterations);
                Thread.SpinWait(64);
            }
            try { Throw(); }
            catch (InvalidOperationException) { Volatile.Write(ref workerResult, 1); }
            GC.KeepAlive(root); GC.KeepAlive(retained);
        }
        catch (Exception) { Volatile.Write(ref workerResult, -2); }
        finally { if (threadToken != 91) Volatile.Write(ref workerResult, -3); }
    }
}