Skip to content

Conversation

@kunalspathak
Copy link
Contributor

Add more tracking to catch reasons for getting #115712.

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
@dotnet-policy-service
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 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

{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

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.
{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

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.
{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

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.
{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

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
Contributor Author

@dotnet/arm64-contrib

@kunalspathak
Copy link
Contributor Author

@dotnet/jit-contrib @amanasifkhalid PTAL

{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
Contributor

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
Contributor 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
Contributor

@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,

{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
Contributor

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
Contributor 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
@github-actions github-actions bot locked and limited conversation to collaborators Jul 4, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants