From 360e70f8935bf1c429b61713affe38114378a928 Mon Sep 17 00:00:00 2001 From: Andy Ayers Date: Thu, 10 Jan 2019 17:55:09 -0800 Subject: [PATCH] JIT: fix byte range used by RangeCheck (#21915) Range is -128 to 127, not -127 to 128. --- src/jit/rangecheck.cpp | 2 +- .../JitBlue/GitHub_21915/Github_21915.cs | 109 +++++++++++++++++++++ .../JitBlue/GitHub_21915/Github_21915.csproj | 34 +++++++ 3 files changed, 144 insertions(+), 1 deletion(-) create mode 100644 tests/src/JIT/Regression/JitBlue/GitHub_21915/Github_21915.cs create mode 100644 tests/src/JIT/Regression/JitBlue/GitHub_21915/Github_21915.csproj diff --git a/src/jit/rangecheck.cpp b/src/jit/rangecheck.cpp index 91ffb1a..b963744 100644 --- a/src/jit/rangecheck.cpp +++ b/src/jit/rangecheck.cpp @@ -1151,7 +1151,7 @@ Range RangeCheck::ComputeRange(BasicBlock* block, GenTree* expr, bool monotonic range = Range(Limit(Limit::keConstant, 0), Limit(Limit::keConstant, 255)); break; case TYP_BYTE: - range = Range(Limit(Limit::keConstant, -127), Limit(Limit::keConstant, 128)); + range = Range(Limit(Limit::keConstant, -128), Limit(Limit::keConstant, 127)); break; case TYP_USHORT: range = Range(Limit(Limit::keConstant, 0), Limit(Limit::keConstant, 65535)); diff --git a/tests/src/JIT/Regression/JitBlue/GitHub_21915/Github_21915.cs b/tests/src/JIT/Regression/JitBlue/GitHub_21915/Github_21915.cs new file mode 100644 index 0000000..d554f15 --- /dev/null +++ b/tests/src/JIT/Regression/JitBlue/GitHub_21915/Github_21915.cs @@ -0,0 +1,109 @@ +// 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 System; +using System.Runtime.CompilerServices; + +// Some tests for removing bounds checks based +// on byte and sbyte-based indices + +class GitHub_21915 +{ + private static ReadOnlySpan A => new byte[256] { + 0, 0, 0, 7, 8, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, + 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + }; + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static byte NeedsEscapingByte256(int value) => A[value]; + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static byte NeedsEscapingByte255(int value) => A.Slice(0, 255)[value]; + + [MethodImpl(MethodImplOptions.NoInlining)] + private static byte ByteRemoveBoundsCheck(ReadOnlySpan data, int i) + { + return NeedsEscapingByte256(data[i]); + } + + [MethodImpl(MethodImplOptions.NoInlining)] + private static byte ByteKeepBoundsCheck1(ReadOnlySpan data, int i) + { + return NeedsEscapingByte255(data[i]); + } + + [MethodImpl(MethodImplOptions.NoInlining)] + private static byte ByteKeepBoundsCheck2(ReadOnlySpan data, int i) + { + return NeedsEscapingByte256(data[i] + 1); + } + + [MethodImpl(MethodImplOptions.NoInlining)] + private static byte SByteRemoveBoundsCheck(ReadOnlySpan data, int i) + { + return NeedsEscapingByte256(data[i] + 128); + } + + [MethodImpl(MethodImplOptions.NoInlining)] + private static byte SByteKeepBoundsCheck1(ReadOnlySpan data, int i) + { + return NeedsEscapingByte255(data[i] + 128); + } + + [MethodImpl(MethodImplOptions.NoInlining)] + private static byte SByteKeepBoundsCheck2(ReadOnlySpan data, int i) + { + return NeedsEscapingByte256(data[i] + 127); + } + + public static int Main() + { + ReadOnlySpan bytes = new byte[] { 2, 3 }; + + byte brbc = ByteRemoveBoundsCheck(bytes, 1); + byte bkbc1 = ByteKeepBoundsCheck1(bytes, 1); + byte bkbc2 = ByteKeepBoundsCheck2(bytes, 0); + + Console.WriteLine($"byte cases: {brbc} {bkbc1} {bkbc2} (expected 7 7 7)"); + + ReadOnlySpan sbytes = new sbyte[] { -124, -125, -128 }; + + byte sbrbc = SByteRemoveBoundsCheck(sbytes, 1); + byte sbkbc1 = SByteKeepBoundsCheck1(sbytes, 1); + byte sbkbc2 = SByteKeepBoundsCheck2(sbytes, 0); + + Console.WriteLine($"sbyte cases: {sbrbc} {sbkbc1} {sbkbc2} (expected 7 7 7)"); + + bool ok = (brbc == 7) && (bkbc1 == 7) && (bkbc2 == 7) && (sbrbc == 7) && (sbkbc1 == 7) && (sbkbc2 == 7); + + try + { + // Check for buggy case in initial PR 21857 + // -128 + 127 should index with -1 and throw. + SByteKeepBoundsCheck2(sbytes, 2); + Console.WriteLine($"error: did not throw as expected"); + ok = false; + } + catch (IndexOutOfRangeException) + { + Console.WriteLine($"threw exception, as expected"); + } + + return ok ? 100 : -1; + } +} diff --git a/tests/src/JIT/Regression/JitBlue/GitHub_21915/Github_21915.csproj b/tests/src/JIT/Regression/JitBlue/GitHub_21915/Github_21915.csproj new file mode 100644 index 0000000..c46f1c3 --- /dev/null +++ b/tests/src/JIT/Regression/JitBlue/GitHub_21915/Github_21915.csproj @@ -0,0 +1,34 @@ + + + + + Debug + AnyCPU + 2.0 + {2649FAFE-07BF-4F93-8120-BA9A69285ABB} + Exe + {786C830F-07A1-408B-BD7F-6EE04809D6DB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} + ..\..\ + + + + + + latest + None + True + + + + False + + + + + + + + + + + \ No newline at end of file -- 2.7.4