summaryrefslogtreecommitdiff
path: root/tests/src/JIT/Regression/JitBlue/DevDiv_1206929/DevDiv_1206929.cs
blob: d3633602721888c6dad0ccb4369ac439675e7cf0 (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
using System;
using System.Runtime.CompilerServices;

namespace ReadMemBytes
{
    class Program
    {
        static int Pass = 100;
        static int Fail = -1;

        [MethodImpl(MethodImplOptions.NoInlining)]
        static unsafe int TestMemBytesNotReadPastTheLimit(byte *p, int byteLength)
        {
            int count = 0;
            for (int i= 0; i< byteLength; ++i)
            {
                // RyuJIT lowering has an optimization to recognize the condition in
                // "If" stmnt and generate "test byte ptr [p+i], 0" instruction. Due
                // to a bug that has been fixed it will end up generating "test dword ptr [p+i], 0"
                // that will lead to reading 4 bytes instead of a single byte.
                if ((p[i] & 0xffffff00) != 0)
                {
                    ++count;
                }
            }

            return count;
        }
        static unsafe int Main(string[] args)
        {
            byte* buffer = stackalloc byte[4];
            buffer[0] = 0;
            buffer[1] = buffer[2] = buffer[3] = 0xff;

            int result = TestMemBytesNotReadPastTheLimit(buffer, 1);
            if (result != 0)
            {
                Console.WriteLine("Failed: Read past the end of buffer");
                return Fail;
            }
            else
            {
                Console.WriteLine("Pass");
            }

            return Pass;

        }
    }
}