nir/lower_blend: Don't dereference null
authorAlyssa Rosenzweig <alyssa@rosenzweig.io>
Mon, 27 Feb 2023 00:15:48 +0000 (19:15 -0500)
committerMarge Bot <emma+marge@anholt.net>
Mon, 27 Feb 2023 15:47:33 +0000 (15:47 +0000)
If a dual source blend colour is never written, src1 will be null and it will be
invalid to dereference it. src1 is dereferenced both for the f2fN instruction
but also if a dual blend factor is used... even if the latter isn't strictly
valid, segfaulting in the NIR pass seems a lot meaner than blending with zero.

The referenced commit hosed Asahi, causing anything that used blending to crash.
Panfrost is unaffected since it always supplies a dual colour due to our crude
construction of blend shaders.

Fixes: 83130165437 ("nir/lower_blend: Consume dual stores")
Signed-off-by: Alyssa Rosenzweig <alyssa@rosenzweig.io>
Reviewed-by: Faith Ekstrand <faith.ekstrand@collabora.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/21544>

src/compiler/nir/nir_lower_blend.c

index d80241a..9a5ade4 100644 (file)
@@ -357,6 +357,12 @@ nir_blend(
    unsigned rt,
    nir_ssa_def *src, nir_ssa_def *src1, nir_ssa_def *dst)
 {
+   /* Don't crash if src1 isn't written. It doesn't matter what dual colour we
+    * blend with in that case, as long as we don't dereference NULL.
+    */
+   if (!src1)
+      src1 = nir_imm_zero(b, 4, src->bit_size);
+
    /* Grab the blend constant ahead of time */
    nir_ssa_def *bconst;
    if (options->scalar_blend_const) {