Skip to content

Fix OOB in subscript indexing of col-major matrix in cbuffer#7866

Merged
tex3d merged 2 commits intomicrosoft:mainfrom
tex3d:fix-cb-col-matrix-subscript
Feb 4, 2026
Merged

Fix OOB in subscript indexing of col-major matrix in cbuffer#7866
tex3d merged 2 commits intomicrosoft:mainfrom
tex3d:fix-cb-col-matrix-subscript

Conversation

@tex3d
Copy link
Contributor

@tex3d tex3d commented Nov 1, 2025

A bug in HLOperationLower code that lowers the matrix subscript operation when the matrix is in a cbuffer could cause out-of-bounds alloca indexing.

The indexing creates an alloca to store a column-vector loaded from one cbuffer vector. This should have one element per row of the matrix, so it can then index along that dimension using the local array.

The bug was using the number of columns when sizing the alloca instead of the number of rows. This caused it to write to out-of-bounds index when the number of rows exceeded the number of columns (like for float3x2).

Fixes #7865.

Copy link
Collaborator

@bogner bogner left a comment

Choose a reason for hiding this comment

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

The fix is obviously good.

Not sure if its worth spending the time, but we might be able to reduce the test case a bit to make it clearer what exactly we're testing.

Copy link
Collaborator

@bob80905 bob80905 left a comment

Choose a reason for hiding this comment

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

LGTM, but echoing Justin, if it can be made more concise that would be great too.

@damyanp
Copy link
Member

damyanp commented Feb 3, 2026

#8131 might have a test case we can use for this :)

@damyanp
Copy link
Member

damyanp commented Feb 3, 2026

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@tex3d
Copy link
Contributor Author

tex3d commented Feb 3, 2026

#8131 might have a test case we can use for this :)

This is already as minimal (if not more) than that example. A more custom IR, rather than one originally generated from HLSL, could potentially be a bit smaller, but it'll be a bit more work to craft and rewrite the checks.

Since it's an IR test for dxilgen, it has to start with High Level (HL) IR and check the output immediately after the dxilgen pass, making sure each component is linked to the right operation, before later passes clean up intermediates with scalarize and so on. Unfortunately, the IR looks ugly initially after dxilgen due to extra packing/unpacking vectors when translating vector HL ops to scalar DXIL ops and keeping intermediate vectors for connecting code and later cleanup, so the checks are either going to be ugly, or much more minimal in what they verify.

It could simply check for the alloca [3 x float], since the bug would have caused it to be alloca [2 x float], but that doesn't check that the right components got hooked up to the right destinations.

tex3d added 2 commits February 3, 2026 16:34
A bug in HLOperationLower code that lowers the matrix subscript operation
when the matrix is in a cbuffer could cause out-of-bounds alloca indexing.

The indexing creates an alloca to store a column-vector loaded from one
cbuffer vector. This should have one element per row of the matrix, so it
can then index along that dimension using the local array.

The bug was using the number of columns when sizing the alloca instead of
the number of rows. This caused it to write to out-of-bounds index when the
number of rows exceeded the number of columns (like for float3x2).
Also added CHECK-NEXT, where possible.
@tex3d tex3d force-pushed the fix-cb-col-matrix-subscript branch from b6636d3 to 57502a7 Compare February 4, 2026 00:35
@tex3d tex3d merged commit f27e8a5 into microsoft:main Feb 4, 2026
12 checks passed
@github-project-automation github-project-automation bot moved this from New to Done in HLSL Roadmap Feb 4, 2026
@tex3d tex3d deleted the fix-cb-col-matrix-subscript branch February 4, 2026 18:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

OOB write to alloca for col-major cbuffer matrix subscript when rows > cols

5 participants