From 96df31a9fa53e3d897a2b7c4eef021f37f421c91 Mon Sep 17 00:00:00 2001 From: Ellie Hermaszewska Date: Mon, 13 Oct 2025 23:14:45 +0900 Subject: Fix segfault on arrays of structs containing parameter blocks (#8555) Closes https://github.com/shader-slang/slang/issues/8154 However there is further design work to do on implementing the "NonAddressableType" suggestion --- .../array-of-struct-with-parameterblock.slang | 44 +++++++++++++++++++++ .../array-of-struct-with-unbounded.slang | 20 ++++++++++ tests/diagnostics/array-of-unbounded-arrays.slang | 13 ++++++ .../array-parameterblock-array-in-struct.slang | 22 +++++++++++ .../array-parameterblock-deeply-nested.slang | 20 ++++++++++ .../diagnostics/array-parameterblock-direct.slang | 11 ++++++ .../diagnostics/array-parameterblock-nested.slang | 26 ++++++++++++ .../diagnostics/array-parameterblock-struct.slang | 21 ++++++++++ tests/diagnostics/array-parameterblock.slang | 10 ----- .../diagnostics/nested-struct-with-unbounded.slang | 31 +++++++++++++++ .../structured-buffer-parameterblock.slang | 43 ++++++++++++++++++++ .../structuredbuffer-with-unbounded.slang | 20 ++++++++++ .../unbounded-array-nonaddressable.slang | 46 ++++++++++++++++++++++ 13 files changed, 317 insertions(+), 10 deletions(-) create mode 100644 tests/diagnostics/array-of-struct-with-parameterblock.slang create mode 100644 tests/diagnostics/array-of-struct-with-unbounded.slang create mode 100644 tests/diagnostics/array-of-unbounded-arrays.slang create mode 100644 tests/diagnostics/array-parameterblock-array-in-struct.slang create mode 100644 tests/diagnostics/array-parameterblock-deeply-nested.slang create mode 100644 tests/diagnostics/array-parameterblock-direct.slang create mode 100644 tests/diagnostics/array-parameterblock-nested.slang create mode 100644 tests/diagnostics/array-parameterblock-struct.slang delete mode 100644 tests/diagnostics/array-parameterblock.slang create mode 100644 tests/diagnostics/nested-struct-with-unbounded.slang create mode 100644 tests/diagnostics/structured-buffer-parameterblock.slang create mode 100644 tests/diagnostics/structuredbuffer-with-unbounded.slang create mode 100644 tests/diagnostics/unbounded-array-nonaddressable.slang (limited to 'tests') diff --git a/tests/diagnostics/array-of-struct-with-parameterblock.slang b/tests/diagnostics/array-of-struct-with-parameterblock.slang new file mode 100644 index 000000000..07ca3a50f --- /dev/null +++ b/tests/diagnostics/array-of-struct-with-parameterblock.slang @@ -0,0 +1,44 @@ +// Test that arrays of structs containing ParameterBlocks are properly diagnosed +// with error 30027 instead of causing a compiler crash + +//TEST:SIMPLE(filecheck=CHECK): -target spirv + +// A struct that contains a ParameterBlock member +struct MyStruct +{ + ParameterBlock pb; + int data; +} + +interface IMyInterface +{ + int getValue(); +} + +[shader("compute")] +[numthreads(1, 1, 1)] +void computeMain() +{ + // CHECK: ([[# @LINE+1]]): error 30027: + MyStruct arr[2]; + + // Also test with dynamic arrays + // CHECK: ([[# @LINE+1]]): error 30027: + MyStruct dynamicArr[]; +} + +// Test nested structs as well +struct OuterStruct +{ + MyStruct inner; + float value; +} + +[shader("compute")] +[numthreads(1, 1, 1)] +void computeMain2() +{ + // Nested struct containing ParameterBlock should also trigger the error + // CHECK: ([[# @LINE+1]]): error 30027: + OuterStruct nestedArr[3]; +} diff --git a/tests/diagnostics/array-of-struct-with-unbounded.slang b/tests/diagnostics/array-of-struct-with-unbounded.slang new file mode 100644 index 000000000..f3deda494 --- /dev/null +++ b/tests/diagnostics/array-of-struct-with-unbounded.slang @@ -0,0 +1,20 @@ +// Test that arrays of structs containing unbounded arrays are not allowed + +//TEST:SIMPLE(filecheck=CHECK): -target spirv -allow-glsl -stage compute -entry computeMain + +// Struct containing unbounded array (must be last member) +struct StructWithUnbounded +{ + float value; + int data[]; // Unbounded array must be last member +} + +// Array of struct containing unbounded array - should error (struct is NonAddressable) +//CHECK: ([[# @LINE+1]]): error 30027 +StructWithUnbounded myArray[2]; + +[numthreads(1, 1, 1)] +void computeMain() +{ + // Empty - we're just testing the declarations +} \ No newline at end of file diff --git a/tests/diagnostics/array-of-unbounded-arrays.slang b/tests/diagnostics/array-of-unbounded-arrays.slang new file mode 100644 index 000000000..0a5c42ce0 --- /dev/null +++ b/tests/diagnostics/array-of-unbounded-arrays.slang @@ -0,0 +1,13 @@ +// Test that arrays of unbounded arrays are not allowed + +//TEST:SIMPLE(filecheck=CHECK): -target spirv -allow-glsl -stage compute -entry computeMain + +// Array of unbounded arrays - should error (unbounded arrays are NonAddressable) +//CHECK: ([[# @LINE+1]]): error 30027 +int arrayOfUnbounded[3][]; + +[numthreads(1, 1, 1)] +void computeMain() +{ + // Empty - we're just testing the declaration +} \ No newline at end of file diff --git a/tests/diagnostics/array-parameterblock-array-in-struct.slang b/tests/diagnostics/array-parameterblock-array-in-struct.slang new file mode 100644 index 000000000..832c90925 --- /dev/null +++ b/tests/diagnostics/array-parameterblock-array-in-struct.slang @@ -0,0 +1,22 @@ +// Test that structs containing arrays of ParameterBlocks are also NonAddressable +// and therefore cannot be used in arrays + +//TEST:SIMPLE(filecheck=CHECK): -target spirv -allow-glsl -stage compute -entry computeMain +//TEST_DISABLED:SIMPLE(filecheck=CHECK): -target glsl -allow-glsl -stage compute -entry computeMain + +// Test case: struct containing array of ParameterBlock +struct StructWithParameterBlockArray +{ + ParameterBlock blocks[2]; // Array of ParameterBlocks + int value; +} + +// This should trigger error 30027 - arrays of NonAddressable types are not allowed +//CHECK: ([[# @LINE+1]]): error 30027 +StructWithParameterBlockArray myArray[3]; // Array of struct containing ParameterBlock array + +[numthreads(1, 1, 1)] +void computeMain() +{ + // Empty - we're just testing the declaration +} \ No newline at end of file diff --git a/tests/diagnostics/array-parameterblock-deeply-nested.slang b/tests/diagnostics/array-parameterblock-deeply-nested.slang new file mode 100644 index 000000000..86dcf913b --- /dev/null +++ b/tests/diagnostics/array-parameterblock-deeply-nested.slang @@ -0,0 +1,20 @@ +//DIAGNOSTIC_TEST:SIMPLE(filecheck=CHECK): + +// Test: Array of deeply nested struct containing ParameterBlock - should error + +struct DeeplyNested +{ + struct Inner + { + ParameterBlock deepPB; + } + Inner inner; +} + +[numthreads(4, 1, 1)] +void computeMain(uint3 dispatchThreadID : SV_DispatchThreadID) +{ +} + +//CHECK: ([[# @LINE+1]]): error 30027 +uniform DeeplyNested arrayOfDeeplyNested[4]; \ No newline at end of file diff --git a/tests/diagnostics/array-parameterblock-direct.slang b/tests/diagnostics/array-parameterblock-direct.slang new file mode 100644 index 000000000..b94aa4f60 --- /dev/null +++ b/tests/diagnostics/array-parameterblock-direct.slang @@ -0,0 +1,11 @@ +//DIAGNOSTIC_TEST:SIMPLE(filecheck=CHECK): + +// Test: Direct array of ParameterBlock - should error + +[numthreads(4, 1, 1)] +void computeMain(uint3 dispatchThreadID : SV_DispatchThreadID) +{ +} + +//CHECK: ([[# @LINE+1]]): error 30027 +uniform ParameterBlock qs[2]; \ No newline at end of file diff --git a/tests/diagnostics/array-parameterblock-nested.slang b/tests/diagnostics/array-parameterblock-nested.slang new file mode 100644 index 000000000..f2c30ea88 --- /dev/null +++ b/tests/diagnostics/array-parameterblock-nested.slang @@ -0,0 +1,26 @@ +//DIAGNOSTIC_TEST:SIMPLE(filecheck=CHECK): + +// Test: Array of nested struct containing ParameterBlock - should error + +struct Inner +{ + ParameterBlock pb; +} + +struct NestedStruct +{ + Inner nested; + float value; +} + +RWStructuredBuffer outputBuffer; + +[numthreads(4, 1, 1)] +void computeMain(uint3 dispatchThreadID : SV_DispatchThreadID) +{ + // Force usage of the array to prevent optimization + outputBuffer[dispatchThreadID.x] = arrayOfNestedStruct[dispatchThreadID.x % 2].nested.pb; +} + +//CHECK: ([[# @LINE+1]]): error 30027 +uniform NestedStruct arrayOfNestedStruct[2]; \ No newline at end of file diff --git a/tests/diagnostics/array-parameterblock-struct.slang b/tests/diagnostics/array-parameterblock-struct.slang new file mode 100644 index 000000000..5723ce709 --- /dev/null +++ b/tests/diagnostics/array-parameterblock-struct.slang @@ -0,0 +1,21 @@ +//DIAGNOSTIC_TEST:SIMPLE(filecheck=CHECK): + +// Test: Array of struct containing ParameterBlock - should error + +struct StructWithParameterBlock +{ + ParameterBlock pb; + int other; +} + +RWStructuredBuffer outputBuffer; + +[numthreads(4, 1, 1)] +void computeMain(uint3 dispatchThreadID : SV_DispatchThreadID) +{ + // Force usage of the array to prevent optimization + outputBuffer[dispatchThreadID.x] = arrayOfStructWithPB[dispatchThreadID.x % 3].pb; +} + +//CHECK: ([[# @LINE+1]]): error 30027 +uniform StructWithParameterBlock arrayOfStructWithPB[3]; \ No newline at end of file diff --git a/tests/diagnostics/array-parameterblock.slang b/tests/diagnostics/array-parameterblock.slang deleted file mode 100644 index 566694e37..000000000 --- a/tests/diagnostics/array-parameterblock.slang +++ /dev/null @@ -1,10 +0,0 @@ -//DIAGNOSTIC_TEST:SIMPLE(filecheck=CHECK): - -[numthreads(4, 1, 1)] -void computeMain(uint3 dispatchThreadID : SV_DispatchThreadID) -{ -} - -//CHECK: ([[# @LINE+1]]): error 30027 -uniform ParameterBlock qs[2]; - diff --git a/tests/diagnostics/nested-struct-with-unbounded.slang b/tests/diagnostics/nested-struct-with-unbounded.slang new file mode 100644 index 000000000..4271ad9ab --- /dev/null +++ b/tests/diagnostics/nested-struct-with-unbounded.slang @@ -0,0 +1,31 @@ +// Test that arrays and StructuredBuffers of nested structs with unbounded arrays are not allowed + +//TEST:SIMPLE(filecheck=CHECK): -target spirv -allow-glsl -stage compute -entry computeMain + +// Struct containing unbounded array (must be last member) +struct StructWithUnbounded +{ + float value; + int data[]; // Unbounded array must be last member +} + +// Nested case +struct NestedUnbounded +{ + int id; + StructWithUnbounded nested; // Struct with unbounded array member +} + +// Array of nested struct - should error +//CHECK: ([[# @LINE+1]]): error 30027 +NestedUnbounded nestedArray[5]; + +// StructuredBuffer of nested struct - should error +//CHECK: ([[# @LINE+1]]): error 30028 +StructuredBuffer nestedBuffer; + +[numthreads(1, 1, 1)] +void computeMain() +{ + // Empty - we're just testing the declarations +} \ No newline at end of file diff --git a/tests/diagnostics/structured-buffer-parameterblock.slang b/tests/diagnostics/structured-buffer-parameterblock.slang new file mode 100644 index 000000000..1fd2b52c8 --- /dev/null +++ b/tests/diagnostics/structured-buffer-parameterblock.slang @@ -0,0 +1,43 @@ +// Test that StructuredBuffer of ParameterBlock (NonAddressable type) is not allowed + +//TEST:SIMPLE(filecheck=CHECK): -target spirv -allow-glsl -stage compute -entry computeMain + +// Direct ParameterBlock in StructuredBuffer - should error +//CHECK: ([[# @LINE+1]]): error 30028 +StructuredBuffer> myBuffer1; + +// Struct containing ParameterBlock in StructuredBuffer - should error +struct StructWithParameterBlock +{ + ParameterBlock block; + int value; +} + +//CHECK: ([[# @LINE+1]]): error 30028 +StructuredBuffer myBuffer2; + +// Nested struct containing ParameterBlock - should error +struct NestedStruct +{ + StructWithParameterBlock nested; + float data; +} + +//CHECK: ([[# @LINE+1]]): error 30028 +StructuredBuffer myBuffer3; + +// Struct containing array of ParameterBlock - should error +struct StructWithParameterBlockArray +{ + ParameterBlock blocks[2]; + int value; +} + +//CHECK: ([[# @LINE+1]]): error 30028 +StructuredBuffer myBuffer4; + +[numthreads(1, 1, 1)] +void computeMain() +{ + // Empty - we're just testing the declarations +} \ No newline at end of file diff --git a/tests/diagnostics/structuredbuffer-with-unbounded.slang b/tests/diagnostics/structuredbuffer-with-unbounded.slang new file mode 100644 index 000000000..7c2a3bd47 --- /dev/null +++ b/tests/diagnostics/structuredbuffer-with-unbounded.slang @@ -0,0 +1,20 @@ +// Test that StructuredBuffer of struct with unbounded array is not allowed + +//TEST:SIMPLE(filecheck=CHECK): -target spirv -allow-glsl -stage compute -entry computeMain + +// Struct containing unbounded array (must be last member) +struct StructWithUnbounded +{ + float value; + int data[]; // Unbounded array must be last member +} + +// StructuredBuffer of struct with unbounded array - should error +//CHECK: ([[# @LINE+1]]): error 30028 +StructuredBuffer myBuffer; + +[numthreads(1, 1, 1)] +void computeMain() +{ + // Empty - we're just testing the declarations +} \ No newline at end of file diff --git a/tests/diagnostics/unbounded-array-nonaddressable.slang b/tests/diagnostics/unbounded-array-nonaddressable.slang new file mode 100644 index 000000000..19a870594 --- /dev/null +++ b/tests/diagnostics/unbounded-array-nonaddressable.slang @@ -0,0 +1,46 @@ +// Test that unbounded arrays are NonAddressable and cannot be used in arrays + +//TEST:SIMPLE(filecheck=CHECK): -target spirv -allow-glsl -stage compute -entry computeMain + +// Unbounded array itself is fine +int unboundedArray[]; + +// Array of unbounded arrays - should error (unbounded arrays are NonAddressable) +//CHECK: ([[# @LINE+1]]): error 30027 +int arrayOfUnbounded[3][]; + +// Struct containing unbounded array (must be last member) +struct StructWithUnbounded +{ + float value; + int data[]; // Unbounded array must be last member +} + +// Array of struct containing unbounded array - should error (struct is NonAddressable) +//CHECK: ([[# @LINE+1]]): error 30027 +StructWithUnbounded myArray[2]; + +// StructuredBuffer of struct with unbounded array - should error +//CHECK: ([[# @LINE+1]]): error 30028 +StructuredBuffer myBuffer; + +// Nested case +struct NestedUnbounded +{ + int id; + StructWithUnbounded nested; // Struct with unbounded array member +} + +// Array of nested struct - should error +//CHECK: ([[# @LINE+1]]): error 30027 +NestedUnbounded nestedArray[5]; + +// StructuredBuffer of nested struct - should error +//CHECK: ([[# @LINE+1]]): error 30028 +StructuredBuffer nestedBuffer; + +[numthreads(1, 1, 1)] +void computeMain() +{ + // Empty - we're just testing the declarations +} \ No newline at end of file -- cgit v1.2.3