From 05ff8850c8c3d3049c1f429672750f8904d4b808 Mon Sep 17 00:00:00 2001 From: Copilot <198982749+Copilot@users.noreply.github.com> Date: Wed, 2 Jul 2025 21:50:55 +0000 Subject: Fix crash when using wrong type for inout parameter with WGSL target (#7588) * Initial plan * Fix crash in WGSL L-value cast lowering with type mismatches Co-authored-by: csyonghe <2652293+csyonghe@users.noreply.github.com> * Apply formatting to fix Co-authored-by: csyonghe <2652293+csyonghe@users.noreply.github.com> * Update test to verify successful compilation instead of error checking 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> Co-authored-by: Yong He --- source/slang/slang-ir-lower-l-value-cast.cpp | 13 +++++++++++-- tests/wgsl/inout-type-mismatch.slang | 29 ++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 2 deletions(-) create mode 100644 tests/wgsl/inout-type-mismatch.slang diff --git a/source/slang/slang-ir-lower-l-value-cast.cpp b/source/slang/slang-ir-lower-l-value-cast.cpp index 3d3c15132..058589862 100644 --- a/source/slang/slang-ir-lower-l-value-cast.cpp +++ b/source/slang/slang-ir-lower-l-value-cast.cpp @@ -175,8 +175,17 @@ struct LValueCastLoweringContext IRBuilder builder(m_module); - IRType* toValueType = as(toType)->getValueType(); - IRType* fromValueType = as(fromType)->getValueType(); + auto toPtrType = as(toType); + auto fromPtrType = as(fromType); + + if (!toPtrType || !fromPtrType) + { + // If either type is not a pointer type, we cannot process this L-value cast + return; + } + + IRType* toValueType = toPtrType->getValueType(); + IRType* fromValueType = fromPtrType->getValueType(); for (auto useSite : useSites) { diff --git a/tests/wgsl/inout-type-mismatch.slang b/tests/wgsl/inout-type-mismatch.slang new file mode 100644 index 000000000..939727ba8 --- /dev/null +++ b/tests/wgsl/inout-type-mismatch.slang @@ -0,0 +1,29 @@ +//TEST:SIMPLE(filecheck=CHECK): -target wgsl -entry main -stage compute + +// Test for issue #7362: Crash when using wrong type for inout parameter with WGSL target +// This test verifies that the shader compiles successfully without crashing. +// The fix changed IRPtrType to IRPtrTypeBase in slang-ir-lower-l-value-cast.cpp +// to handle implicit l-value casts properly. + +void B(inout uint param) +{ + param = param + 10; +} + +void A(inout int param) +{ + // This should work with implicit l-value cast from int to uint + // Previously this would cause a segfault due to null pointer dereference + B(param); +} + +[numthreads(1, 1, 1)] +void main(uint3 DTid : SV_DispatchThreadID) +{ + int param = 5; + A(param); +} + +// The test passes if compilation succeeds and generates valid WGSL +// CHECK: @compute +// CHECK: fn main \ No newline at end of file -- cgit v1.2.3