public static void IndexOverflow()
{
//
// Although Span constrains indexes to 0..2Gb, it does not similarly constrain index * sizeof(T).
// Make sure that internal offset calculcations handle the >2Gb case properly.
//
unsafe
{
byte* pMemory;
try
{
pMemory = (byte*)Marshal.AllocHGlobal((IntPtr)ThreeGiB);
}
catch (Exception)
{
return; // It's not implausible to believe that a 3gb allocation will fail - if so, skip this test to avoid unnecessary test flakiness.
}
try
{
Span<Guid> span = new Span<Guid>(pMemory, GuidThreeGiBLimit);
int bigIndex = checked(GuidTwoGiBLimit + 1);
uint byteOffset = checked((uint)bigIndex * (uint)sizeof(Guid));
Assert.True(byteOffset > (uint)int.MaxValue); // Make sure byteOffset actually overflows 2Gb, or this test is pointless.
ref Guid expected = ref Unsafe.AsRef<Guid>(((byte*)pMemory) + byteOffset);
Assert.True(Unsafe.AreSame<Guid>(ref expected, ref span.GetItem(bigIndex)));
Span<Guid> slice = span.Slice(bigIndex);
Assert.True(Unsafe.AreSame<Guid>(ref expected, ref slice.DangerousGetPinnableReference()));
slice = span.Slice(bigIndex, 1);
Assert.True(Unsafe.AreSame<Guid>(ref expected, ref slice.DangerousGetPinnableReference()));
}
finally
{
Marshal.FreeHGlobal((IntPtr)pMemory);
}
}
}