Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion src/coreclr/jit/rangecheck.h
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,19 @@ struct RangeOps

static Range Add(const Range& r1, const Range& r2, bool unsignedAdd = false)
{
if (unsignedAdd)
{
bool r1StraddlesZero = r1.IsConstantRange() && (r1.LowerLimit().GetConstant() < 0) &&
(r1.UpperLimit().GetConstant() >= 0);
bool r2StraddlesZero = r2.IsConstantRange() && (r2.LowerLimit().GetConstant() < 0) &&
(r2.UpperLimit().GetConstant() >= 0);
if (r1StraddlesZero || r2StraddlesZero)
{
// Signed intervals that straddle zero are not monotonic when interpreted as unsigned.
return Limit(Limit::keUnknown);
}
}

return ApplyRangeOp(r1, r2, [unsignedAdd](const Limit& a, const Limit& b) {
// For Add we support:
// keConstant + keConstant => keConstant
Expand All @@ -340,7 +353,10 @@ struct RangeOps
}

static_assert(CheckedOps::Unsigned == true);
if (!CheckedOps::AddOverflows(a.GetConstant(), b.GetConstant(), unsignedAdd))
// For unsigned adds, require both unsigned and signed endpoint sums to not overflow.
Comment thread
EgorBo marked this conversation as resolved.
bool requestedAddOverflows = CheckedOps::AddOverflows(a.GetConstant(), b.GetConstant(), unsignedAdd);
bool signedEndpointOverflows = unsignedAdd && CheckedOps::AddOverflows(a.GetConstant(), b.GetConstant(), CheckedOps::Signed);
if (!requestedAddOverflows && !signedEndpointOverflows)
{
if (a.IsConstant() && b.IsConstant())
{
Expand Down
40 changes: 40 additions & 0 deletions src/tests/JIT/Regression/JitBlue/Runtime_130431/Runtime_130431.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

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

public class Runtime_130431
{
[Fact]
public static void TestEntryPoint()
{
Assert.Throws<OverflowException>(() => Add(1, uint.MaxValue));
Assert.Throws<OverflowException>(() => AddWithSignStraddlingRange(uint.MaxValue, 1));
}

[MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.AggressiveOptimization)]
private static uint Add(uint x, uint y)
{
uint result = checked(y + unchecked((byte)x));

if (x == 0)
{
return result + 1;
}

return result;
}

[MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.AggressiveOptimization)]
private static uint AddWithSignStraddlingRange(uint x, uint y)
{
if (((int)x > 0) || (y > 1))
{
return 0;
}

return checked(x + y);
}
}
1 change: 1 addition & 0 deletions src/tests/JIT/Regression/Regression_ro_2.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@
<Compile Include="JitBlue\Runtime_130216\Runtime_130216.cs" />
<Compile Include="JitBlue\Runtime_130248\Runtime_130248.cs" />
<Compile Include="JitBlue\Runtime_130286\Runtime_130286.cs" />
<Compile Include="JitBlue\Runtime_130431\Runtime_130431.cs" />
<Compile Include="JitBlue\Runtime_130425\Runtime_130425.cs" />
Comment thread
EgorBo marked this conversation as resolved.
<Compile Include="JitBlue\Runtime_10337\Runtime_10337.cs" />
<Compile Include="JitBlue\Runtime_125160\Runtime_125160.cs" />
Expand Down
Loading