From 8aae76014437cb32fb34f7d12240e36333f4ffd8 Mon Sep 17 00:00:00 2001
From: Roland Scheidegger <roland.scheidegger@broadcom.com>
Date: Tue, 15 Oct 2024 20:46:05 +0200
Subject: [PATCH] llvmpipe: don't assert on exceeding if_stack size

Rather than assert (and otherwise write past the array size), guard against
this (and miscompile the shader), to make the code more robust.
This mimics the behavior of exceeding the cond_stack size (and other similar
stacks) - the if_stack is only used together with the cond_stack, the behavior
should be the same.

Reviewed-by: Konstantin Seurer <konstantin.seurer@gmail.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/33338>
---
 src/gallium/auxiliary/gallivm/lp_bld_nir_soa.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)

diff --git a/src/gallium/auxiliary/gallivm/lp_bld_nir_soa.c b/src/gallium/auxiliary/gallivm/lp_bld_nir_soa.c
index af87520845ba..4ab635f631a6 100644
--- a/src/gallium/auxiliary/gallivm/lp_bld_nir_soa.c
+++ b/src/gallium/auxiliary/gallivm/lp_bld_nir_soa.c
@@ -2079,7 +2079,11 @@ static void lp_build_skip_branch(struct lp_build_nir_context *bld_base, bool fla
 
    LLVMValueRef any_active = LLVMBuildICmp(builder, LLVMIntNE, bitmask, lp_build_const_int32(gallivm, 0), "any_active");
 
-   assert(bld_base->if_stack_size < LP_MAX_TGSI_NESTING);
+   if (bld_base->if_stack_size >= LP_MAX_TGSI_NESTING) {
+      bld_base->if_stack_size++;
+      return;
+   }
+
    lp_build_if(&bld_base->if_stack[bld_base->if_stack_size], gallivm, any_active);
    bld_base->if_stack_size++;
 }
@@ -2091,6 +2095,9 @@ static void lp_build_skip_branch_end(struct lp_build_nir_context *bld_base, bool
 
    assert(bld_base->if_stack_size);
    bld_base->if_stack_size--;
+   if (bld_base->if_stack_size >= LP_MAX_TGSI_NESTING)
+      return;
+
    lp_build_endif(&bld_base->if_stack[bld_base->if_stack_size]);
 }
 
-- 
GitLab

