Skip to content

Commit c0180d6

Browse files
EgorBoCopilot
andauthored
JIT: Fix several range check correctness bugs (#134466)
Fixes a few range check / bounds check correctness bugs (one commit per fix): - #134455: `RangeOps::ShiftRight` computed an unsound upper limit for `x >>> [0..N]` when `x` may be negative. - #134452: range check propagated integral ranges through casts to/from floating point types (rounding/saturation). Floating-point VNs now get an unknown range. - #134451: `DoesVarDefOverflow` skipped checking a local's def when assertions bounded it within a range with a dependent limit, while `Widen` later resolves that limit assuming no overflow (wrapping induction variable). - ARM64: `genLeaInstruction` dropped the sign/zero extension of a contained `CAST`/`BFIZ` index (e.g. write-barrier store address for `span[(int)longValue] = obj`), now emits `add xd, xn, wm, UXTW/SXTW #scale`. Regression tests are added for the first three. SPMI (benchmarks.run, aspnet2, libraries.pmi): no diffs for the first two, +123 bytes for the overflow fix; ARM64 diffs are size-neutral (`LSL` -> `UXTW`). --------- Co-authored-by: Copilot <[email protected]>
1 parent a774edc commit c0180d6

6 files changed

Lines changed: 147 additions & 17 deletions

File tree

‎src/coreclr/jit/codegenarmarch.cpp‎

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4278,31 +4278,33 @@ void CodeGen::genLeaInstruction(GenTreeAddrMode* lea)
42784278
else
42794279
{
42804280
#ifdef TARGET_ARM64
4281-
42824281
if (index->isContained())
42834282
{
4283+
// Only BFIZ/CAST nodes should be present for contained index on ARM64.
4284+
GenTreeCast* cast;
42844285
if (index->OperIs(GT_BFIZ))
42854286
{
4286-
// Handle LEA with "contained" BFIZ
42874287
assert(scale == 0);
42884288
scale = (DWORD)index->gtGetOp2()->AsIntConCommon()->IconValue();
4289-
index = index->gtGetOp1()->gtGetOp1();
4290-
}
4291-
else if (index->OperIs(GT_CAST))
4292-
{
4293-
index = index->AsCast()->gtGetOp1();
4289+
cast = index->gtGetOp1()->AsCast();
42944290
}
42954291
else
42964292
{
4297-
// Only BFIZ/CAST nodes should be present for for contained index on ARM64.
4298-
// If there are more, we need to handle them here.
4299-
unreached();
4293+
cast = index->AsCast();
43004294
}
4295+
4296+
// The 32-bit index has to be sign/zero-extended as part of the add.
4297+
assert(genActualTypeIsInt(cast->CastOp()) && (scale <= 4));
4298+
emit->emitIns_R_R_R_I(INS_add, size, lea->GetRegNum(), memBase->GetRegNum(),
4299+
cast->CastOp()->GetRegNum(), scale,
4300+
cast->IsUnsigned() ? INS_OPTS_UXTW : INS_OPTS_SXTW);
43014301
}
4302+
else
43024303
#endif
4303-
4304-
// Then compute target reg from [base + index*scale]
4305-
genScaledAdd(size, lea->GetRegNum(), memBase->GetRegNum(), index->GetRegNum(), scale);
4304+
{
4305+
// Then compute target reg from [base + index*scale]
4306+
genScaledAdd(size, lea->GetRegNum(), memBase->GetRegNum(), index->GetRegNum(), scale);
4307+
}
43064308
}
43074309
}
43084310
else if (lea->HasBase())

‎src/coreclr/jit/rangecheck.cpp‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -599,6 +599,12 @@ Range RangeCheck::GetRangeFromAssertionsWorker(
599599
#endif
600600
}
601601

602+
if (varTypeIsFloating(vnType))
603+
{
604+
// Integer ranges don't describe floating point values (e.g. int->float casts may round).
605+
return Limit(Limit::keUnknown);
606+
}
607+
602608
//
603609
// First, let's see if we can tighten the range based on VN information.
604610
//
@@ -2144,9 +2150,11 @@ bool RangeCheck::DoesVarDefOverflow(BasicBlock* block, GenTreeLclVarCommon* lcl,
21442150

21452151
// But only if the range from the assertion is more strict than the global
21462152
// range computed; otherwise we might still have used the def's value to
2147-
// tighten the range of the global range.
2153+
// tighten the range of the global range. A dependent limit doesn't qualify:
2154+
// Widen resolves it later assuming the defs don't overflow.
21482155
Range merged = RangeOps::Merge(range, assertionRange, false);
2149-
if (merged.LowerLimit().Equals(range.LowerLimit()) && merged.UpperLimit().Equals(range.UpperLimit()))
2156+
if (!range.LowerLimit().IsDependent() && !range.UpperLimit().IsDependent() &&
2157+
merged.LowerLimit().Equals(range.LowerLimit()) && merged.UpperLimit().Equals(range.UpperLimit()))
21502158
{
21512159
return false;
21522160
}

‎src/coreclr/jit/rangecheck.h‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -442,9 +442,13 @@ struct RangeOps
442442
// For RSZ by N >= 1, result is in [0, UINT_MAX >> N] regardless of r1's signedness.
443443
// When r1 isn't proven non-negative, the bound above is unsound (negative r1 reinterprets
444444
// as large unsigned), so override with the type-based bound.
445-
if (logical && (r2.LowerLimit().GetConstant() >= 1) &&
446-
!(r1.LowerLimit().IsConstant() && (r1.LowerLimit().GetConstant() >= 0)))
445+
if (logical && !(r1.LowerLimit().IsConstant() && (r1.LowerLimit().GetConstant() >= 0)))
447446
{
447+
if (r2.LowerLimit().GetConstant() == 0)
448+
{
449+
// A shift by 0 may preserve a negative r1, so nothing is known.
450+
return Limit(Limit::keUnknown);
451+
}
448452
result.lLimit = Limit(Limit::keConstant, 0);
449453
result.uLimit = Limit(Limit::keConstant, (int)(UINT32_MAX >> r2.LowerLimit().GetConstant()));
450454
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System;
5+
using System.Runtime.CompilerServices;
6+
using Xunit;
7+
8+
public class Runtime_134451
9+
{
10+
[Fact]
11+
public static void TestEntryPoint()
12+
{
13+
Assert.Throws<IndexOutOfRangeException>(() => Test(new int[101], true));
14+
}
15+
16+
// "x" wraps around, so "j += x" is not monotonic and "j" becomes negative.
17+
[MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.AggressiveOptimization)]
18+
private static int Test(int[] a, bool flag)
19+
{
20+
int j = 0, x = int.MaxValue - 10, sum = 0;
21+
if (a.Length != 101)
22+
return 0;
23+
while (j < 101)
24+
{
25+
if (flag)
26+
sum += a[j];
27+
x++;
28+
if (x >= int.MinValue + 1 && x <= 10)
29+
j += x;
30+
}
31+
return sum;
32+
}
33+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System;
5+
using System.Runtime.CompilerServices;
6+
using Xunit;
7+
8+
public class Runtime_134452
9+
{
10+
[Fact]
11+
[SkipOnMono("CoreCLR JIT regression test")]
12+
public static void TestEntryPoint()
13+
{
14+
Assert.Equal(1, IntToFloatRounding(16777219));
15+
Assert.Equal(1, DoubleToUIntSaturation(-5));
16+
Assert.Throws<IndexOutOfRangeException>(() => IndexThroughFloat(new byte[16777220], 16777219));
17+
}
18+
19+
[MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.AggressiveOptimization)]
20+
private static int IntToFloatRounding(int x)
21+
{
22+
if (x >= 0 && x <= 16777219)
23+
{
24+
// (float)16777219 rounds to 16777220
25+
int y = (int)(float)x;
26+
if (y > 16777219)
27+
return 1;
28+
}
29+
return 0;
30+
}
31+
32+
[MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.AggressiveOptimization)]
33+
private static int DoubleToUIntSaturation(int x)
34+
{
35+
if (x < 0)
36+
{
37+
// Saturates to 0
38+
uint u = (uint)(double)x;
39+
if (u == 0)
40+
return 1;
41+
}
42+
return 0;
43+
}
44+
45+
[MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.AggressiveOptimization)]
46+
private static int IndexThroughFloat(byte[] arr, int x)
47+
{
48+
if (arr.Length == 16777220 && x >= 0 && x <= 16777219)
49+
return arr[(int)(float)x];
50+
return -1;
51+
}
52+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System;
5+
using System.Runtime.CompilerServices;
6+
using Xunit;
7+
8+
public class Runtime_134455
9+
{
10+
[Fact]
11+
public static void TestEntryPoint()
12+
{
13+
Assert.Throws<IndexOutOfRangeException>(() => Test(-1, 1));
14+
Assert.Equal(0, Test(-1, 0));
15+
Assert.Equal(0, Test(50, 0));
16+
}
17+
18+
// "x >>> (y & 3)" is a large positive value for negative "x".
19+
[MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.AggressiveOptimization)]
20+
private static int Test(int x, int y)
21+
{
22+
int[] a = new int[51];
23+
if (x <= 50)
24+
{
25+
int v = x >>> (y & 3);
26+
if (v >= 0)
27+
return a[v];
28+
}
29+
return 0;
30+
}
31+
}

0 commit comments

Comments
 (0)