Welcome to mirror list, hosted at ThFree Co, Russian Federation.

Overflow.cs « Span « tests « System.Memory « src - github.com/mono/corefx.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 228ee75bca6bdedb3e41a45fb971e8e9d06f9eb7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using Xunit;
using System.Runtime.InteropServices;
using System.Runtime.CompilerServices;

namespace System.SpanTests
{
    public static partial class SpanTests
    {
        [Fact]
        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);
                }
            }
        }

        private const long ThreeGiB = 3L * 1024L * 1024L * 1024L;
        private const long TwoGiB = 2L * 1024L * 1024L * 1024L;
        private const long OneGiB = 1L * 1024L * 1024L * 1024L;

        private static readonly int GuidThreeGiBLimit = (int)(ThreeGiB / Unsafe.SizeOf<Guid>());  // sizeof(Guid) requires unsafe keyword and I don't want to mark the entire class unsafe.
        private static readonly int GuidTwoGiBLimit = (int)(TwoGiB / Unsafe.SizeOf<Guid>());
        private static readonly int GuidOneGiBLimit = (int)(OneGiB / Unsafe.SizeOf<Guid>());
    }
}