From cb9a4b5b6ef5b3750db1e81a6967a64e52bf9725 Mon Sep 17 00:00:00 2001 From: Ari Suonpaa Date: Mon, 13 Dec 2021 11:20:41 +0200 Subject: [PATCH] Fix signed integer overflow in shader operator tests 32bit integer reference value calculation had possible signed integer overflows which are undefined. Now the references are calculated using 64bit integers. VK-GL-CTS Issue: 3335 Affects: dEQP-GLES3.functional.shaders.operator.binary_operator.* Components: OpenGL ES Change-Id: I18e5eec98a7b93375c075a833bdf68534353c634 --- .../gles3/functional/es3fShaderOperatorTests.cpp | 47 ++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/modules/gles3/functional/es3fShaderOperatorTests.cpp b/modules/gles3/functional/es3fShaderOperatorTests.cpp index c71eea4..4bc1ebe 100644 --- a/modules/gles3/functional/es3fShaderOperatorTests.cpp +++ b/modules/gles3/functional/es3fShaderOperatorTests.cpp @@ -178,6 +178,53 @@ template inline T selection (bool cond, T a, T b) { return cond ? a template inline Vector subVecScalar (const Vector& v, deUint32 s) { return (v.asInt() - (int)s).asUint(); }; template inline Vector addVecScalar (const Vector& v, T s) { return v + s; }; + +// Specialize add, sub, and mul integer operations to use 64bit to avoid undefined signed integer overflows. +inline int add (int a, int b) { return static_cast(static_cast(a) + static_cast(b)); } +inline int sub (int a, int b) { return static_cast(static_cast(a) - static_cast(b)); } +inline int mul (int a, int b) { return static_cast(static_cast(a) * static_cast(b)); } + +inline deUint32 add (deUint32 a, deUint32 b) { return a + b; } +inline deUint32 sub (deUint32 a, deUint32 b) { return a - b; } +inline deUint32 mul (deUint32 a, deUint32 b) { return a * b; } + +#define DECLARE_IVEC_BINARY_FUNC(OP_NAME) \ +template inline Vector OP_NAME (const Vector& a, const Vector& b) \ +{ \ + Vector res; \ + for (int i = 0; i < Size; i++) \ + res.m_data[i] = OP_NAME(a.m_data[i], b.m_data[i]); \ + return res; \ +} + +#define DECLARE_IVEC_INT_BINARY_FUNC(OP_NAME) \ +template inline Vector OP_NAME##VecScalar (const Vector& v, int s) \ +{ \ + Vector ret; \ + for (int i = 0; i < Size; i++) \ + ret[i] = OP_NAME(v.m_data[i], s); \ + return ret; \ +}; + +#define DECLARE_INT_IVEC_BINARY_FUNC(OP_NAME) \ +template inline Vector OP_NAME##ScalarVec (int s, const Vector& v) \ +{ \ + Vector ret; \ + for (int i = 0; i < Size; i++) \ + ret[i] = OP_NAME(s, v.m_data[i]); \ + return ret; \ +}; + +DECLARE_IVEC_BINARY_FUNC(add) +DECLARE_IVEC_BINARY_FUNC(sub) +DECLARE_IVEC_BINARY_FUNC(mul) +DECLARE_IVEC_INT_BINARY_FUNC(add) +DECLARE_IVEC_INT_BINARY_FUNC(sub) +DECLARE_IVEC_INT_BINARY_FUNC(mul) +DECLARE_INT_IVEC_BINARY_FUNC(add) +DECLARE_INT_IVEC_BINARY_FUNC(sub) +DECLARE_INT_IVEC_BINARY_FUNC(mul) + template inline Vector subVecScalar (const Vector& v, T s) { return v - s; }; template inline Vector mulVecScalar (const Vector& v, T s) { return v * s; }; template inline Vector divVecScalar (const Vector& v, T s) { return v / s; }; -- 2.7.4