From d4fd282d4b3d2190f0350a2f1f92c188f0073017 Mon Sep 17 00:00:00 2001 From: Brian Sullivan Date: Wed, 19 Jun 2019 11:18:02 -0700 Subject: Fix Issue #25134 - AssertionProp incorrectly removes cast from uint Add additional check for the GT_UNSIGNED flag + Ran clang-format + Code review feedback, use IsUnsigned() --- .../JitBlue/GitHub_25134/GitHub_25134.cs | 90 ++++++++++++++++++++++ .../JitBlue/GitHub_25134/GitHub_25134.csproj | 33 ++++++++ 2 files changed, 123 insertions(+) create mode 100644 tests/src/JIT/Regression/JitBlue/GitHub_25134/GitHub_25134.cs create mode 100644 tests/src/JIT/Regression/JitBlue/GitHub_25134/GitHub_25134.csproj (limited to 'tests/src') diff --git a/tests/src/JIT/Regression/JitBlue/GitHub_25134/GitHub_25134.cs b/tests/src/JIT/Regression/JitBlue/GitHub_25134/GitHub_25134.cs new file mode 100644 index 0000000000..e68e2b82e8 --- /dev/null +++ b/tests/src/JIT/Regression/JitBlue/GitHub_25134/GitHub_25134.cs @@ -0,0 +1,90 @@ +// 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; + +class Program +{ + static bool s_caughtException; + static uint s_value = int.MaxValue + 1U; + static int s_result = 0; + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + static int CastToIntChecked(uint value) + { + return checked((int)value); + } + + // Testing a checked cast to Uint -- the inlining case + // + [MethodImpl(MethodImplOptions.NoInlining)] + static void Test1() + { + int result = CastToIntChecked(s_value); + s_result = result; + Console.WriteLine("Result is " + result); + } + + // Testing a checked cast to Uint -- the non-inlining case + // + [MethodImpl(MethodImplOptions.NoInlining)] + static void Test2() + { + uint copy = 0; + try + { + s_caughtException = false; + + copy = s_value; + + int result = checked((int)copy); + s_result = result; + Console.WriteLine("Result is " + result); + } + catch (System.OverflowException ex) + { + s_caughtException = true; + Console.WriteLine("CORRECT: " + ex); + copy = 0; + } + } + + static int Main() + { + bool failed = false; + + try + { + Test1(); + } + catch (System.OverflowException ex) + { + s_caughtException = true; + Console.WriteLine("CORRECT: " + ex); + } + + if (s_caughtException == false) + { + Console.WriteLine("FAILED - Test1"); + failed = true; + } + + Test2(); + if (s_caughtException == false) + { + Console.WriteLine("FAILED - Test2"); + failed = true; + } + + if (failed) + { + return 101; + } + else + { + return 100; + } + } +} diff --git a/tests/src/JIT/Regression/JitBlue/GitHub_25134/GitHub_25134.csproj b/tests/src/JIT/Regression/JitBlue/GitHub_25134/GitHub_25134.csproj new file mode 100644 index 0000000000..95052d9884 --- /dev/null +++ b/tests/src/JIT/Regression/JitBlue/GitHub_25134/GitHub_25134.csproj @@ -0,0 +1,33 @@ + + + + + Debug + AnyCPU + 2.0 + {95DFC527-4DC1-495E-97D7-E94EE1F7140D} + Exe + {786C830F-07A1-408B-BD7F-6EE04809D6DB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} + ..\..\ + + + + + + + False + + + + None + True + + + + + + + + + + -- cgit v1.2.3 From 56ea111329f8dd834795676178957d63d84ba167 Mon Sep 17 00:00:00 2001 From: Brian Sullivan Date: Thu, 20 Jun 2019 18:43:32 -0700 Subject: Added the complementary test cases of going from a negative int to an unsigned. --- .../JitBlue/GitHub_25134/GitHub_25134.cs | 85 ++++++++++++++++++++-- 1 file changed, 77 insertions(+), 8 deletions(-) (limited to 'tests/src') diff --git a/tests/src/JIT/Regression/JitBlue/GitHub_25134/GitHub_25134.cs b/tests/src/JIT/Regression/JitBlue/GitHub_25134/GitHub_25134.cs index e68e2b82e8..05b21d7a5b 100644 --- a/tests/src/JIT/Regression/JitBlue/GitHub_25134/GitHub_25134.cs +++ b/tests/src/JIT/Regression/JitBlue/GitHub_25134/GitHub_25134.cs @@ -8,26 +8,31 @@ using System.Runtime.CompilerServices; class Program { static bool s_caughtException; - static uint s_value = int.MaxValue + 1U; - static int s_result = 0; + + static uint s_uint_value = int.MaxValue + 1U; + static int s_int_result = 0; + + static int s_int_value = -1; + static uint s_uint_result = 0; [MethodImpl(MethodImplOptions.AggressiveInlining)] static int CastToIntChecked(uint value) { + // checked cast of uint to int return checked((int)value); } - // Testing a checked cast to Uint -- the inlining case + // Testing a checked cast of uint to int -- the inlining case // [MethodImpl(MethodImplOptions.NoInlining)] static void Test1() { - int result = CastToIntChecked(s_value); - s_result = result; + int result = CastToIntChecked(s_uint_value); + s_int_result = result; Console.WriteLine("Result is " + result); } - // Testing a checked cast to Uint -- the non-inlining case + // Testing a checked cast of uint to int -- the non-inlining case // [MethodImpl(MethodImplOptions.NoInlining)] static void Test2() @@ -37,10 +42,51 @@ class Program { s_caughtException = false; - copy = s_value; + copy = s_uint_value; int result = checked((int)copy); - s_result = result; + s_int_result = result; + Console.WriteLine("Result is " + result); + } + catch (System.OverflowException ex) + { + s_caughtException = true; + Console.WriteLine("CORRECT: " + ex); + copy = 0; + } + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + static uint CastToUIntChecked(int value) + { + // checked cast of int to uint + return checked((uint)value); + } + + // Testing a checked cast of int to uint -- the inlining case + // + [MethodImpl(MethodImplOptions.NoInlining)] + static void Test3() + { + uint result = CastToUIntChecked(s_int_value); + s_uint_result = result; + Console.WriteLine("Result is " + result); + } + + // Testing a checked cast of int to uint -- the non-inlining case + // + [MethodImpl(MethodImplOptions.NoInlining)] + static void Test4() + { + uint copy = 0; + try + { + s_caughtException = false; + + copy = s_uint_value; + + int result = checked((int)copy); + s_int_result = result; Console.WriteLine("Result is " + result); } catch (System.OverflowException ex) @@ -78,6 +124,29 @@ class Program failed = true; } + try + { + Test3(); + } + catch (System.OverflowException ex) + { + s_caughtException = true; + Console.WriteLine("CORRECT: " + ex); + } + + if (s_caughtException == false) + { + Console.WriteLine("FAILED - Test3"); + failed = true; + } + + Test4(); + if (s_caughtException == false) + { + Console.WriteLine("FAILED - Test4"); + failed = true; + } + if (failed) { return 101; -- cgit v1.2.3