#if PLUGIN_VALIDATION
namespace XFEEDToolBox.Plugin;
internal static unsafe class BoundarySelfTests
{
[UnmanagedCallersOnly(EntryPoint = "XFEInteractionSelfTest")]
internal static uint InteractionTest()
{ try { return InteractionRuntime.SelfTest(); } catch (Exception) { return 0; } }
[UnmanagedCallersOnly]
private static ulong Integers(ulong a, ulong b, ulong c, ulong d, ulong e, ulong f, ulong g, ulong h, ulong i, ulong j, ulong k, ulong l)
=> a + 2*b + 3*c + 4*d + 5*e + 6*f + 7*g + 8*h + 9*i + 10*j + 11*k + 12*l;
[UnmanagedCallersOnly]
private static float Floats(float a, float b, float c, float d, float e) => a + 2*b + 3*c + 4*d + 5*e;
[UnmanagedCallersOnly]
private static double Doubles(double a, double b, double c, double d, double e) => a + 2*b + 3*c + 4*d + 5*e;
[UnmanagedCallersOnly]
private static double Mixed(nuint a, float b, double c, nuint d, double e) => a + 2*b + 3*c + 4*d + 5*e;
[UnmanagedCallersOnly(EntryPoint = "XFEPlatformSelfTest")]
internal static uint Run() => RunCore(10);
[UnmanagedCallersOnly(EntryPoint = "XFEPlatformSelfTestPart")]
internal static uint RunPart(uint part) => RunCore(part);
private static uint RunCore(uint part)
{
uint mask = 0; nuint memory = 0, fault = 0;
try
{
var integers = (nuint)(delegate* unmanaged<ulong,ulong,ulong,ulong,ulong,ulong,ulong,ulong,ulong,ulong,ulong,ulong,ulong>)&Integers;
if (Call(integers, 1,2,3,4,5,6,7,8,9,10,11,12) == 650) mask |= 1;
if (part == 1) return mask;
var floats = (nuint)(delegate* unmanaged<float,float,float,float,float,float>)&Floats;
if (Math.Abs(CallFloat(floats, F32(1.25f), F32(2.5f), F32(3.75f), F32(4.5f), F32(5.25f)) - 61.75f) < .00001f) mask |= 2;
if (part == 2) return mask;
var doubles = (nuint)(delegate* unmanaged<double,double,double,double,double,double>)&Doubles;
if (Math.Abs(CallDouble(doubles, F64(1.25), F64(2.5), F64(3.75), F64(4.5), F64(5.25)) - 61.75) < .00001) mask |= 4;
if (part == 3) return mask;
var mixed = (nuint)(delegate* unmanaged<nuint,float,double,nuint,double,double>)&Mixed;
if (Math.Abs(CallDouble(mixed, 3, F32(2.5f), F64(4.25), 7, F64(3.75)) - 67.5) < .00001) mask |= 8;
if (part == 4) return mask;
memory = Allocate(4096);
if (memory == 0) return mask;
Write(memory, 0x123456789abcdef0UL);
if (Read<ulong>(memory) == 0x123456789abcdef0UL) mask |= 16;
if (part == 5) return mask;
Protect(memory, 4096, 1, out var previous);
try { Read<ulong>(memory); } catch (NativeAccessException) { mask |= 32; }
if (part == 6) return mask;
try { Write(memory, 0UL); } catch (NativeAccessException) { mask |= 64; }
if (part == 7) return mask;
Protect(memory, 4096, previous, out _);
// xor rax,rax; mov rax,[rax]; ret: SEH must contain the native fault.
fault = Allocate(4096);
WriteBytes(fault, new byte[] {0x48,0x31,0xc0,0x48,0x8b,0x00,0xc3});
Protect(fault, 4096, 0x20, out _); FlushCode(fault, 7);
try { Call(fault); } catch (NativeAccessException) { mask |= 128; }
if (part == 8) return mask;
// A fault must not poison the native bridge or reverse P/Invoke state.
if (Call(integers, 1,2,3,4,5,6,7,8,9,10,11,12) == 650) mask |= 256;
Write(memory, 2);
if (AtomicDecrement(memory) == 1 && Read<int>(memory) == 1) mask |= 512;
Protect(memory, 4096, 1, out previous);
try { AtomicDecrement(memory); } catch (NativeAccessException) { mask |= 1024; }
Protect(memory, 4096, previous, out _);
}
catch (Exception) { }
finally { Free(fault); Free(memory); }
return mask;
}
}
#endif
#if PLUGIN_VALIDATION
namespace XFEEDToolBox.Plugin;
internal static unsafe class BoundarySelfTests
{
[UnmanagedCallersOnly(EntryPoint = "XFEInteractionSelfTest")]
internal static uint InteractionTest()
{ try { return InteractionRuntime.SelfTest(); } catch (Exception) { return 0; } }
[UnmanagedCallersOnly]
private static ulong Integers(ulong a, ulong b, ulong c, ulong d, ulong e, ulong f, ulong g, ulong h, ulong i, ulong j, ulong k, ulong l)
=> a + 2*b + 3*c + 4*d + 5*e + 6*f + 7*g + 8*h + 9*i + 10*j + 11*k + 12*l;
[UnmanagedCallersOnly]
private static float Floats(float a, float b, float c, float d, float e) => a + 2*b + 3*c + 4*d + 5*e;
[UnmanagedCallersOnly]
private static double Doubles(double a, double b, double c, double d, double e) => a + 2*b + 3*c + 4*d + 5*e;
[UnmanagedCallersOnly]
private static double Mixed(nuint a, float b, double c, nuint d, double e) => a + 2*b + 3*c + 4*d + 5*e;
[UnmanagedCallersOnly(EntryPoint = "XFEPlatformSelfTest")]
internal static uint Run() => RunCore(10);
[UnmanagedCallersOnly(EntryPoint = "XFEPlatformSelfTestPart")]
internal static uint RunPart(uint part) => RunCore(part);
private static uint RunCore(uint part)
{
uint mask = 0; nuint memory = 0, fault = 0;
try
{
var integers = (nuint)(delegate* unmanaged<ulong,ulong,ulong,ulong,ulong,ulong,ulong,ulong,ulong,ulong,ulong,ulong,ulong>)&Integers;
if (Call(integers, 1,2,3,4,5,6,7,8,9,10,11,12) == 650) mask |= 1;
if (part == 1) return mask;
var floats = (nuint)(delegate* unmanaged<float,float,float,float,float,float>)&Floats;
if (Math.Abs(CallFloat(floats, F32(1.25f), F32(2.5f), F32(3.75f), F32(4.5f), F32(5.25f)) - 61.75f) < .00001f) mask |= 2;
if (part == 2) return mask;
var doubles = (nuint)(delegate* unmanaged<double,double,double,double,double,double>)&Doubles;
if (Math.Abs(CallDouble(doubles, F64(1.25), F64(2.5), F64(3.75), F64(4.5), F64(5.25)) - 61.75) < .00001) mask |= 4;
if (part == 3) return mask;
var mixed = (nuint)(delegate* unmanaged<nuint,float,double,nuint,double,double>)&Mixed;
if (Math.Abs(CallDouble(mixed, 3, F32(2.5f), F64(4.25), 7, F64(3.75)) - 67.5) < .00001) mask |= 8;
if (part == 4) return mask;
memory = Allocate(4096);
if (memory == 0) return mask;
Write(memory, 0x123456789abcdef0UL);
if (Read<ulong>(memory) == 0x123456789abcdef0UL) mask |= 16;
if (part == 5) return mask;
Protect(memory, 4096, 1, out var previous);
try { Read<ulong>(memory); } catch (NativeAccessException) { mask |= 32; }
if (part == 6) return mask;
try { Write(memory, 0UL); } catch (NativeAccessException) { mask |= 64; }
if (part == 7) return mask;
Protect(memory, 4096, previous, out _);
// xor rax,rax; mov rax,[rax]; ret: SEH must contain the native fault.
fault = Allocate(4096);
WriteBytes(fault, new byte[] {0x48,0x31,0xc0,0x48,0x8b,0x00,0xc3});
Protect(fault, 4096, 0x20, out _); FlushCode(fault, 7);
try { Call(fault); } catch (NativeAccessException) { mask |= 128; }
if (part == 8) return mask;
// A fault must not poison the native bridge or reverse P/Invoke state.
if (Call(integers, 1,2,3,4,5,6,7,8,9,10,11,12) == 650) mask |= 256;
Write(memory, 2);
if (AtomicDecrement(memory) == 1 && Read<int>(memory) == 1) mask |= 512;
Protect(memory, 4096, 1, out previous);
try { AtomicDecrement(memory); } catch (NativeAccessException) { mask |= 1024; }
Protect(memory, 4096, previous, out _);
}
catch (Exception) { }
finally { Free(fault); Free(memory); }
return mask;
}
}
#endif