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
10 changes: 6 additions & 4 deletions src/coreclr/jit/loopcloning.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1520,10 +1520,12 @@ bool Compiler::optDeriveLoopCloningConditions(FlowGraphNaturalLoop* loop, LoopCl
iterInfo->LimitOffset);
}

// arr.Length is non-negative, but arr.Length + offset can be < 0 when
// offset < 0 and the array is short. Guard the fast clone against
// out-of-bounds access on the low side.
if (iterInfo->LimitOffset < 0)
// arr.Length is non-negative, but arr.Length + offset can be < 0 when a negative offset exceeds the array
// length or a positive offset overflows. Array lengths are at most CORINFO_Array_MaxLength, so small positive
// offsets cannot overflow.
const bool positiveOffsetCanOverflow =
iterInfo->LimitOffset > (INT32_MAX - static_cast<int>(CORINFO_Array_MaxLength));
if ((iterInfo->LimitOffset < 0) || positiveOffsetCanOverflow)
{
LC_Ident arrLenIdent =
LC_Ident::CreateArrAccess(LC_Array(LC_Array::Jagged, limitArrIndex, LC_Array::ArrLen),
Expand Down
76 changes: 76 additions & 0 deletions src/tests/JIT/opt/Cloning/OffsetLimit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,50 @@ static int DecGtArrayLengthMinusK(int[] a, int initVal)
return sum;
}

[MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.AggressiveOptimization)]
static int DecGtArrayLengthPlus1(int[] a, int[] limit)
{
int sum = 0;
for (int i = a.Length - 1; i > limit.Length + 1; i--)
{
sum += a[i];
}
return sum;
}

[MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.AggressiveOptimization)]
static int DecGtArrayLengthPlusMaxValue(int[] a, int[] limit)
{
int sum = 0;
for (int i = a.Length - 1; i > limit.Length + int.MaxValue; i--)
{
sum += a[i];
}
return sum;
}

[MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.AggressiveOptimization)]
static int DecGeArrayLengthPlusMaxValue(int[] a, int[] limit)
{
int sum = 0;
for (int i = a.Length - 1; i >= limit.Length + int.MaxValue; i--)
{
sum += a[i];
}
return sum;
}

[MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.AggressiveOptimization)]
static int DecNeArrayLengthPlusMaxValue(int[] a, int[] limit)
{
int sum = 0;
for (int i = a.Length - 1; i != limit.Length + int.MaxValue; i--)
{
sum += a[i];
}
return sum;
}

[MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.AggressiveOptimization)]
static int IncLeSpanLengthMinusK(Span<int> a)
{
Expand Down Expand Up @@ -210,6 +254,38 @@ public static void DecGtArrayLengthMinusKShortArrayTest(int initVal, int len)
Assert.Throws<IndexOutOfRangeException>(() => DecGtArrayLengthMinusK(a, initVal));
}

[Fact]
public static void DecGtArrayLengthPlus1Test()
{
int[] a = MakeArray(10);
int[] limit = MakeArray(2);
int got = DecGtArrayLengthPlus1(a, limit);
int want = ExpectedDecGt(a.Length - 1, limit.Length + 1, 1, a);
Assert.Equal(want, got);
}

[ActiveIssue(
"https://github.com/dotnet/runtime/issues/134014",
typeof(TestLibrary.PlatformDetection),
nameof(TestLibrary.PlatformDetection.IsMonoLLVMAOT))]
[Theory]
[InlineData(nameof(DecGtArrayLengthPlusMaxValue))]
[InlineData(nameof(DecGeArrayLengthPlusMaxValue))]
[InlineData(nameof(DecNeArrayLengthPlusMaxValue))]
public static void DecArrayLengthPlusMaxValueOverflowTest(string methodName)
{
int[] a = MakeArray(4);
int[] limit = MakeArray(4);

Assert.Throws<IndexOutOfRangeException>(() => methodName switch
{
nameof(DecGtArrayLengthPlusMaxValue) => DecGtArrayLengthPlusMaxValue(a, limit),
nameof(DecGeArrayLengthPlusMaxValue) => DecGeArrayLengthPlusMaxValue(a, limit),
nameof(DecNeArrayLengthPlusMaxValue) => DecNeArrayLengthPlusMaxValue(a, limit),
_ => throw new ArgumentOutOfRangeException(nameof(methodName)),
});
}

[Theory]
[InlineData(5)]
[InlineData(50)]
Expand Down
1 change: 1 addition & 0 deletions src/tests/JIT/opt/Cloning/OffsetLimit.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
</PropertyGroup>
<ItemGroup>
<Compile Include="$(MSBuildProjectName).cs" />
<ProjectReference Include="$(TestLibraryProjectPath)" />
</ItemGroup>
</Project>
Loading