From 6b7c34736afd45a45d10fc6e3a799dba25ce7be7 Mon Sep 17 00:00:00 2001 From: Hyunjun Ko Date: Tue, 16 Aug 2022 18:34:12 +0900 Subject: [PATCH] Fix undefined behaviour by bit-shifting with negative value. When getTextureFormatMantissaBitDepth returns 0 for the non-existent channels, it leads to 1u << (0u - 1u), that is undefined behaviour, especially leads to unexpected results on arm64. Affects: dEQP-VK.image.store.* dEQP-VK.image.load_store.* dEQP-VK.image.format_reinterpret.* Components: Vulkan VK-GL-CTS issue: 3888 Change-Id: I669eebb1f78c78dc89fb456e71f2ca0af3b66836 --- external/vulkancts/modules/vulkan/image/vktImageLoadStoreTests.cpp | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/external/vulkancts/modules/vulkan/image/vktImageLoadStoreTests.cpp b/external/vulkancts/modules/vulkan/image/vktImageLoadStoreTests.cpp index 45abcda..83289a1 100644 --- a/external/vulkancts/modules/vulkan/image/vktImageLoadStoreTests.cpp +++ b/external/vulkancts/modules/vulkan/image/vktImageLoadStoreTests.cpp @@ -51,6 +51,7 @@ #include "tcuTextureUtil.hpp" #include "tcuFloat.hpp" #include "tcuStringTemplate.hpp" +#include "tcuVectorUtil.hpp" #include #include @@ -179,8 +180,12 @@ bool comparePixelBuffers (tcu::TestLog& log, case tcu::TEXTURECHANNELCLASS_SIGNED_FIXED_POINT: { + const tcu::UVec4 bitDepth = tcu::getTextureFormatMantissaBitDepth(mapVkFormat(format)).cast() - 1u; + // To avoid bit-shifting with negative value, which is undefined behaviour. + const tcu::UVec4 fixedBitDepth = tcu::select(bitDepth, tcu::UVec4(0u, 0u, 0u, 0u), tcu::greaterThanEqual(bitDepth.cast(), tcu::IVec4(0, 0, 0, 0))); + // Allow error of minimum representable difference - const tcu::Vec4 threshold (1.0f / ((tcu::UVec4(1u) << (tcu::getTextureFormatMantissaBitDepth(mapVkFormat(format)).cast() - 1u)) - 1u).cast()); + const tcu::Vec4 threshold (1.0f / ((tcu::UVec4(1u) << fixedBitDepth) - 1u).cast()); ok = tcu::floatThresholdCompare(log, comparisonName.c_str(), comparisonDesc.c_str(), refLayer, resultLayer, threshold, tcu::COMPARE_LOG_RESULT); break; -- 2.7.4