Skip to content

Sve/Scatter test: Add Debug.Assert for array length #116158

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 3, 2025

Conversation

kunalspathak
Copy link
Member

Add more tracking to catch reasons for getting #115712.

@Copilot Copilot AI review requested due to automatic review settings May 30, 2025 20:26
@kunalspathak kunalspathak changed the title Add Debug.Assert for array length Sve/Scatter test: Add Debug.Assert for array length May 30, 2025
Copy link
Contributor

Tagging subscribers to this area: @dotnet/area-system-runtime-intrinsics
See info in area-owners.md if you want to be subscribed.

Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

Adds runtime assertions in test templates to detect out-of-range vector indices before array access, helping diagnose the root cause of issue #115712.

  • Inserts Debug.Assert checks when computing base pointers to validate index bounds.
  • Updates scatter, gather, and first-fault gather test templates with similar guard logic.

Reviewed Changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 4 comments.

File Description
src/tests/JIT/HardwareIntrinsics/Arm/Shared/SveScatterVectorBases.template Added bounds check for outArray indexing
src/tests/JIT/HardwareIntrinsics/Arm/Shared/SveGatherVectorVectorBases.template Added bounds check for baseArray indexing
src/tests/JIT/HardwareIntrinsics/Arm/Shared/SveGatherVectorFirstFaultingVectorBases.template Added bounds checks in both gather loops

@@ -120,6 +120,10 @@ namespace JIT.HardwareIntrinsics.Arm._Sve
{Op2BaseType} baseAddrToValidate = (({Op2BaseType})outArrayPtr + (sizeof({Op2BaseType}) * inAddress[i]));

// Make sure we got the correct base pointers.
if ((int)inAddress[i] >= (int)outArray.Length)
Copy link
Preview

Copilot AI May 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Casting inAddress[i] to int before comparing to outArray.Length could overflow or miss negative values. Consider comparing using the original index type, e.g., if (inAddress[i] >= outArray.Length), to ensure a correct bounds check.

Suggested change
if ((int)inAddress[i] >= (int)outArray.Length)
if (inAddress[i] >= outArray.Length)

Copilot uses AI. Check for mistakes.

@@ -124,6 +124,10 @@ namespace JIT.HardwareIntrinsics.Arm
{Op2BaseType} baseAddrToValidate = (({Op2BaseType})baseArrayPtr + (sizeof({RetBaseType}) * inArray2[i]));

// Make sure we got the correct base pointers.
if ((int)inArray2[i] >= (int)baseArray.Length)
Copy link
Preview

Copilot AI May 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The cast to int may overflow or ignore negative values. Use the original index type for comparison: if (inArray2[i] >= baseArray.Length) to reliably guard against out-of-range accesses.

Suggested change
if ((int)inArray2[i] >= (int)baseArray.Length)
if (inArray2[i] >= baseArray.Length)

Copilot uses AI. Check for mistakes.

@@ -135,6 +135,10 @@ namespace JIT.HardwareIntrinsics.Arm
{Op2BaseType} baseAddrToValidate = (({Op2BaseType})baseArrayPtr + (sizeof({RetBaseType}) * inArray2[i]));

// Make sure we got the correct base pointers.
if ((int)inArray2[i] >= (int)baseArray.Length)
Copy link
Preview

Copilot AI May 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Avoid casting to int for inArray2[i]; this can introduce overflow or sign issues. Compare the original index type directly against baseArray.Length for accurate validation.

Copilot uses AI. Check for mistakes.

@@ -157,6 +161,10 @@ namespace JIT.HardwareIntrinsics.Arm
{Op2BaseType} baseAddrToValidate = (({Op2BaseType})baseArrayPtr + (sizeof({RetBaseType}) * inArray2Ffr[i]));

// Make sure we got the correct base pointers.
if ((int)inArray2Ffr[i] >= (int)baseArray.Length)
Copy link
Preview

Copilot AI May 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the first-fault loop, casting inArray2Ffr[i] to int may mis-handle large or negative values. Use the original type (e.g., if (inArray2Ffr[i] >= baseArray.Length)) for correct bounds checking.

Suggested change
if ((int)inArray2Ffr[i] >= (int)baseArray.Length)
if (inArray2Ffr[i] >= baseArray.Length)

Copilot uses AI. Check for mistakes.

@kunalspathak
Copy link
Member Author

@dotnet/arm64-contrib

@kunalspathak
Copy link
Member Author

@dotnet/jit-contrib @amanasifkhalid PTAL

@@ -135,6 +135,10 @@ namespace JIT.HardwareIntrinsics.Arm
{Op2BaseType} baseAddrToValidate = (({Op2BaseType})baseArrayPtr + (sizeof({RetBaseType}) * inArray2[i]));

// Make sure we got the correct base pointers.
if ((int)inArray2[i] >= (int)baseArray.Length)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Why not pass the condition directly to Debug.Assert?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I started with Console.WriteLine and converted to Debug.Assert. Fixed it.

Copy link
Member

@amanasifkhalid amanasifkhalid left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, just a few nits,

@@ -193,7 +197,7 @@ namespace JIT.HardwareIntrinsics.Arm._Sve
{
{Op2BaseType} baseAddrToValidate = (({Op2BaseType})_dataTable.outArrayPtr + (sizeof({Op2BaseType}) * _addressArr[i]));

// Make sure we got the correct base pointers.
// Make sure we got the correct base pointers.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// Make sure we got the correct base pointers.
// Make sure we got the correct base pointers.

@kunalspathak
Copy link
Member Author

/ba-g failures are unrelated

@kunalspathak kunalspathak merged commit a3f5189 into dotnet:main Jun 3, 2025
71 of 73 checks passed
@kunalspathak kunalspathak deleted the issue_115712 branch June 3, 2025 21:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants