summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDanylo Piliaiev <danylo.piliaiev@globallogic.com>2020-08-17 18:13:24 +0300
committerEric Engestrom <eric@engestrom.ch>2020-09-02 21:50:43 +0200
commit1d9aa0e99a5d8b5e9d4a762fceaa20ab67401358 (patch)
treefb165d20a723d8ed85225c1bc251b93e6f59e5a0
parent6273688e65a80564228ea36f36bc5c5b44b3cb5b (diff)
ir_constant: Return zero on out-of-bounds vector accesses
Several optimization paths, including constant folding, can lead to accessing an ir_constant vector with an out of bounds index. Return 0 since GL_ARB_robustness and GL_KHR_robustness encourage us to do so. Fixes piglit tests: spec@glsl-1.20@execution@vector-out-of-bounds-access@fs-vec4-out-of-bounds-2 spec@glsl-1.20@execution@vector-out-of-bounds-access@fs-vec4-out-of-bounds-4 spec@glsl-1.20@execution@vector-out-of-bounds-access@fs-vec4-out-of-bounds-5 Closes: https://gitlab.freedesktop.org/mesa/mesa/-/issues/2604 CC: <mesa-stable@lists.freedesktop.org> Signed-off-by: Danylo Piliaiev <danylo.piliaiev@globallogic.com> Reviewed-by: Eric Anholt <eric@anholt.net> Reviewed-by: Marcin Ĺšlusarz <marcin.slusarz@intel.com> Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/6363> (cherry picked from commit e93979ba599355c42df01a89073362b970489a3a)
-rw-r--r--.pick_status.json2
-rw-r--r--src/compiler/glsl/ir.cpp14
2 files changed, 15 insertions, 1 deletions
diff --git a/.pick_status.json b/.pick_status.json
index c108e806880..53a0109298d 100644
--- a/.pick_status.json
+++ b/.pick_status.json
@@ -3937,7 +3937,7 @@
"description": "ir_constant: Return zero on out-of-bounds vector accesses",
"nominated": true,
"nomination_type": 0,
- "resolution": 0,
+ "resolution": 1,
"master_sha": null,
"because_sha": null
},
diff --git a/src/compiler/glsl/ir.cpp b/src/compiler/glsl/ir.cpp
index 922dcd3c819..12faf8e41fc 100644
--- a/src/compiler/glsl/ir.cpp
+++ b/src/compiler/glsl/ir.cpp
@@ -799,6 +799,20 @@ ir_constant::ir_constant(const ir_constant *c, unsigned i)
this->const_elements = NULL;
this->type = c->type->get_base_type();
+ /* 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.
+ *
+ * GL_KHR_robustness and GL_ARB_robustness encourage us to return zero.
+ */
+ if (i >= c->type->vector_elements) {
+ this->value = { { 0 } };
+ return;
+ }
+
switch (this->type->base_type) {
case GLSL_TYPE_UINT: this->value.u[0] = c->value.u[i]; break;
case GLSL_TYPE_INT: this->value.i[0] = c->value.i[i]; break;