nir/lower_samplers: Clamp out-of-bounds access to array of samplers
authorDanylo Piliaiev <danylo.piliaiev@globallogic.com>
Fri, 21 Aug 2020 14:21:14 +0000 (17:21 +0300)
committerMarge Bot <eric+marge@anholt.net>
Tue, 22 Sep 2020 09:06:52 +0000 (09:06 +0000)
commitf2b17dec1208423061309e0e03ba32b2c5566ace
tree534322401b8e2bde58201d7f9312bbdfeeb5b7cf
parent0ba82f78a57d352c1042678962e8a386b411322f
nir/lower_samplers: Clamp out-of-bounds access to array of samplers

Section 5.11 (Out-of-Bounds Accesses) of the GLSL 4.60 spec says:

"In the subsections described above for array, vector, matrix and
 structure accesses, any out-of-bounds access produced undefined
 behavior.... Out-of-bounds reads return undefined values, which
 include values from other variables of the active program or zero."

Robustness extensions suggest to return zero on out-of-bounds
accesses, however it's not applicable to the arrays of samplers,
so just clamp the index.

Otherwise instr->sampler_index or instr->texture_index would be out
of bounds, and they are used as an index to arrays of driver state.

E.g. this fixes such dereference:
 if (options->lower_tex_packing[tex->sampler_index] !=
in nir_lower_tex.c

CC: <mesa-stable@lists.freedesktop.org>
Signed-off-by: Danylo Piliaiev <danylo.piliaiev@globallogic.com>
Reviewed-by: Eric Anholt <eric@anholt.net>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/6428>
src/compiler/nir/nir_lower_samplers.c