Skip to content

Commit 50131be

Browse files
EgorBoCopilot
andauthored
JIT: Reject native-int bounds checks in range check cloning (#135725)
`BoundsCheckInfo::Initialize` only checked the index type on the non-constant path. A `TYP_LONG` constant index (e.g. `arr[(nint)0]`, whose bounds check also has a `CAST(long <- ARR_LENGTH)` length) passed `IsIntCnsFitsInI32()` and was accepted, while `optRangeCheckCloning_DoClone` builds the guards as `TYP_INT`, producing mixed int/long relops (asserts in Checked, invalid IR in Release). Fix: reject bounds checks whose index or length isn't `TYP_INT` up front. No asm diffs in libraries.pmi. Fixes #134459 No [diffs](https://dev.azure.com/dnceng-public/public/_build/results?buildId=1634108&view=ms.vss-build-web.run-extensions-tab). Co-authored-by: Copilot <[email protected]> Copilot-Session: ef747772-3030-470e-b839-f9a4fc116717
1 parent d24a264 commit 50131be

2 files changed

Lines changed: 31 additions & 0 deletions

File tree

‎src/coreclr/jit/rangecheckcloning.cpp‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,12 @@ bool BoundsCheckInfo::Initialize(const Compiler* comp, Statement* statement, int
6868
return false;
6969
}
7070

71+
// The guards are built as TYP_INT, so native-int (e.g. nint index) bounds checks aren't supported.
72+
if ((genActualType(BndChk()->GetIndex()) != TYP_INT) || (genActualType(BndChk()->GetArrayLength()) != TYP_INT))
73+
{
74+
return false;
75+
}
76+
7177
if (BndChk()->GetIndex()->IsIntCnsFitsInI32())
7278
{
7379
// Index being a constant means we have index=0 and cns offset
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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.Runtime.CompilerServices;
5+
using Xunit;
6+
7+
public class Runtime_134459
8+
{
9+
[Fact]
10+
public static void TestEntryPoint()
11+
{
12+
int[] arr = new int[8];
13+
Test(arr);
14+
Assert.Equal(new int[] { 1, 2, 3, 4, 0, 0, 0, 0 }, arr);
15+
}
16+
17+
[MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.AggressiveOptimization)]
18+
private static void Test(int[] arr)
19+
{
20+
arr[(nint)0] = 1;
21+
arr[(nint)1] = 2;
22+
arr[(nint)2] = 3;
23+
arr[(nint)3] = 4;
24+
}
25+
}

0 commit comments

Comments
 (0)