From 85edfb178cd243134f4bb3d35ad71f154d76c81c Mon Sep 17 00:00:00 2001 From: Copilot <198982749+Copilot@users.noreply.github.com> Date: Thu, 17 Jul 2025 20:58:02 -0700 Subject: 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> --- tests/language-feature/0-array-1.slang | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) (limited to 'tests/language-feature') 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 -- cgit v1.2.3