diff options
| author | Copilot <198982749+Copilot@users.noreply.github.com> | 2025-07-17 20:58:02 -0700 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-07-18 03:58:02 +0000 |
| commit | 85edfb178cd243134f4bb3d35ad71f154d76c81c (patch) | |
| tree | 68f5340c8796f6c2d5ff343cabf3d96c7d387ac5 /tests | |
| parent | 447d73f8c2245d061b0e84890fb994a77816a736 (diff) | |
Add bounds checking for out-of-bounds array access with constant indices (#7814)
* Initial plan
* Implement out-of-bounds array access checking
Co-authored-by: csyonghe <2652293+csyonghe@users.noreply.github.com>
* Add tests and format code for array bounds checking
Co-authored-by: csyonghe <2652293+csyonghe@users.noreply.github.com>
* Move checkForOutOfBoundAccess to separate file and refactor using InstPassBase
Co-authored-by: csyonghe <2652293+csyonghe@users.noreply.github.com>
* Optimize out-of-bounds checker to use single IR traversal
Co-authored-by: csyonghe <2652293+csyonghe@users.noreply.github.com>
* Fix DiagnosticSink forward declaration from struct to class
Co-authored-by: csyonghe <2652293+csyonghe@users.noreply.github.com>
* Update 0-array-1 test to use runtime indices to avoid bounds checking diagnostic
Co-authored-by: csyonghe <2652293+csyonghe@users.noreply.github.com>
* Use SV_DispatchThreadID for truly runtime array access in 0-array-1 test
Co-authored-by: csyonghe <2652293+csyonghe@users.noreply.github.com>
---------
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: csyonghe <2652293+csyonghe@users.noreply.github.com>
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/diagnostics/array-out-of-bounds-2.slang | 29 | ||||
| -rw-r--r-- | tests/diagnostics/array-out-of-bounds.slang | 28 | ||||
| -rw-r--r-- | tests/language-feature/0-array-1.slang | 12 |
3 files changed, 65 insertions, 4 deletions
diff --git a/tests/diagnostics/array-out-of-bounds-2.slang b/tests/diagnostics/array-out-of-bounds-2.slang new file mode 100644 index 000000000..978f61d9f --- /dev/null +++ b/tests/diagnostics/array-out-of-bounds-2.slang @@ -0,0 +1,29 @@ +// array-out-of-bounds-2.slang + +// Test the specific scenario from the issue (array of size 3, accessing index 3) + +//TEST:SIMPLE(filecheck=CHECK): -target spirv -entry computeMain -stage compute + +struct SH3_t +{ + float coeffs[4]; +}; + +RWStructuredBuffer<float> outputBuffer; + +[numthreads(1, 1, 1)] +void computeMain(uint3 dispatchThreadID : SV_DispatchThreadID) +{ + SH3_t a[3]; + SH3_t b; + + for (int i = 0; i < 3; ++i) + { + // This should be fine + outputBuffer[i] = a[i].coeffs[0]; + } + + // This reproduces the issue - accessing index 3 in array of size 3 + //CHECK: error 30029: array index '3' is out of bounds for array of size '3'. + outputBuffer[3] = a[3].coeffs[0]; +}
\ No newline at end of file diff --git a/tests/diagnostics/array-out-of-bounds.slang b/tests/diagnostics/array-out-of-bounds.slang new file mode 100644 index 000000000..81c243031 --- /dev/null +++ b/tests/diagnostics/array-out-of-bounds.slang @@ -0,0 +1,28 @@ +// array-out-of-bounds.slang + +// Test that out-of-bounds array access with constant indices generates an error + +//TEST:SIMPLE(filecheck=CHECK): -target spirv -entry computeMain -stage compute + +RWStructuredBuffer<int> outputBuffer; + +[numthreads(1, 1, 1)] +void computeMain(uint3 dispatchThreadID : SV_DispatchThreadID) +{ + int a[3]; + a[0] = 10; + a[1] = 20; + a[2] = 30; + + // Valid access - should be fine + outputBuffer[0] = a[0]; + outputBuffer[1] = a[2]; + + // Invalid access - index 3 is out of bounds for array of size 3 + //CHECK: error 30029: array index '3' is out of bounds for array of size '3'. + outputBuffer[2] = a[3]; + + // Invalid access - negative index + //CHECK: error 30029: array index '-1' is out of bounds for array of size '3'. + outputBuffer[3] = a[-1]; +}
\ No newline at end of file diff --git a/tests/language-feature/0-array-1.slang b/tests/language-feature/0-array-1.slang index 327f71444..b02be1af0 100644 --- a/tests/language-feature/0-array-1.slang +++ b/tests/language-feature/0-array-1.slang @@ -10,7 +10,7 @@ uniform MyData* myData; uniform int * output; [numthreads(1, 1, 1)] -void computeMain() +void computeMain(uint3 dispatchThreadID : SV_DispatchThreadID) { // These are all ill-formed, but we want to still ensure our backend // can handle them gracefully without crashing. @@ -18,9 +18,13 @@ void computeMain() // by a `if` statement that checks the size before accessing. // The condition would then evaluate to false and causing all the accessing // code to be optimized out. - InterlockedAdd(myData.a[0][0][0], 1); - myData.a[0][0][0] += 1; - output[0] = myData.a[0][0][0]; + + // Use runtime values to access the 0-sized array to avoid triggering + // the new out-of-bounds diagnostic for constant indices + uint runtimeIndex = dispatchThreadID.x; + InterlockedAdd(myData.a[runtimeIndex][runtimeIndex][runtimeIndex], 1); + myData.a[runtimeIndex][runtimeIndex][runtimeIndex] += 1; + output[0] = myData.a[runtimeIndex][runtimeIndex][runtimeIndex]; } //SPV: OpEntryPoint |
