Adding SAN option to CMakeLists.txt, to make it easier to use clang's address/undefined behavior sanitizer on codebase

Fixing overflow issue on extreme colors in color_distance().
Explicitly disabling unaligned addressing in miniz and transcoder when UBSAN is enabled
Disabling a needless optimization in transcoder causing ubsan issues (that could have easily been worked around in a different way, but it was an unnecessary optimization to begin with)
Rebuild wasm builds
This commit is contained in:
Richard Geldreich
2022-02-24 19:45:01 -05:00
parent 44e1bcd3e1
commit 7a2094b807
9 changed files with 47 additions and 15 deletions
+10 -3
View File
@@ -2,6 +2,7 @@ project(basisu)
cmake_minimum_required(VERSION 3.0)
option(STATIC "static linking" FALSE)
option(SAN "sanitize" FALSE)
# For MSVC builds default to SSE enabled, and determine if it's a 64-bit (-A x64) vs. 32-bit (-A Win32) build.
if (MSVC)
@@ -24,6 +25,7 @@ message("Initial CMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}")
message("Initial SSE=${SSE}")
message("Initial ZSTD=${ZSTD}")
message("Initial OPENCL=${OPENCL}")
message("Initial SAN=${SAN}")
if (NOT MSVC)
# With MSVC builds we use the Khronos lib/include files in the project's "OpenCL" directory, to completely avoid requiring fiddly to install vendor SDK's.
@@ -60,10 +62,15 @@ endif()
if (NOT MSVC)
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -g")
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -g")
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -g")
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE}")
set(CMAKE_C_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE}")
set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE}")
if (SAN)
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -fsanitize=address -fno-omit-frame-pointer -fsanitize=undefined")
set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -fsanitize=address -fno-omit-frame-pointer -fsanitize=undefined")
endif()
set(CMAKE_CXX_FLAGS -std=c++11)
set(GCC_COMPILE_FLAGS "-fvisibility=hidden -fPIC -fno-strict-aliasing -D_LARGEFILE64_SOURCE=1 -D_FILE_OFFSET_BITS=64 -Wall -Wextra -Wno-unused-local-typedefs -Wno-unused-value -Wno-unused-parameter -Wno-unused-variable")
+12 -1
View File
@@ -990,17 +990,28 @@ namespace basisu
int dg = e1.g - e2.g;
int db = e1.b - e2.b;
#if 0
int delta_l = dr * 27 + dg * 92 + db * 9;
int delta_cr = dr * 128 - delta_l;
int delta_cb = db * 128 - delta_l;
uint32_t id = ((uint32_t)(delta_l * delta_l) >> 7U) +
((((uint32_t)(delta_cr * delta_cr) >> 7U) * 26U) >> 7U) +
((((uint32_t)(delta_cb * delta_cb) >> 7U) * 3U) >> 7U);
#else
int64_t delta_l = dr * 27 + dg * 92 + db * 9;
int64_t delta_cr = dr * 128 - delta_l;
int64_t delta_cb = db * 128 - delta_l;
uint32_t id = ((uint32_t)((delta_l * delta_l) >> 7U)) +
((((uint32_t)((delta_cr * delta_cr) >> 7U)) * 26U) >> 7U) +
((((uint32_t)((delta_cb * delta_cb) >> 7U)) * 3U) >> 7U);
#endif
if (alpha)
{
int da = (e1.a - e2.a) << 7;
// This shouldn't overflow if da is 255 or -255: 29.99 bits after squaring.
id += ((uint32_t)(da * da) >> 7U);
}
+8
View File
@@ -76,6 +76,14 @@
#define MINIZ_USE_UNALIGNED_LOADS_AND_STORES 1
#endif
// Using unaligned loads and stores causes errors when using UBSan. Jam it off.
#if defined(__has_feature)
#if __has_feature(undefined_behavior_sanitizer)
#undef MINIZ_USE_UNALIGNED_LOADS_AND_STORES
#define MINIZ_USE_UNALIGNED_LOADS_AND_STORES 0
#endif
#endif
#if defined(_M_X64) || defined(_WIN64) || defined(__MINGW64__) || defined(_LP64) || defined(__LP64__) || defined(__ia64__) || defined(__x86_64__)
// Set MINIZ_HAS_64BIT_REGISTERS to 1 if operations on 64-bit integers are reasonably fast (and don't involve compiler generated calls to helper functions).
#define MINIZ_HAS_64BIT_REGISTERS 1
+8 -2
View File
@@ -285,7 +285,10 @@ namespace basisu
m_size = other.m_size;
if (BASISU_IS_BITWISE_COPYABLE(T))
memcpy(m_p, other.m_p, m_size * sizeof(T));
{
if ((m_p) && (other.m_p))
memcpy(m_p, other.m_p, m_size * sizeof(T));
}
else
{
T* pDst = m_p;
@@ -326,7 +329,10 @@ namespace basisu
}
if (BASISU_IS_BITWISE_COPYABLE(T))
memcpy(m_p, other.m_p, other.m_size * sizeof(T));
{
if ((m_p) && (other.m_p))
memcpy(m_p, other.m_p, other.m_size * sizeof(T));
}
else
{
T* pDst = m_p;
+8 -4
View File
@@ -37,6 +37,14 @@
#endif
#endif
// Using unaligned loads and stores causes errors when using UBSan. Jam it off.
#if defined(__has_feature)
#if __has_feature(undefined_behavior_sanitizer)
#undef BASISD_USE_UNALIGNED_WORD_READS
#define BASISD_USE_UNALIGNED_WORD_READS 0
#endif
#endif
#define BASISD_SUPPORTED_BASIS_VERSION (0x13)
#ifndef BASISD_SUPPORT_KTX2
@@ -12440,12 +12448,8 @@ namespace basist
bits = read_bits64(blk.m_bytes, bit_ofs, basisu::minimum<int>(64, 128 - (int)bit_ofs));
else
{
#ifdef __EMSCRIPTEN__
bits = blk.m_dwords[2];
bits |= (((uint64_t)blk.m_dwords[3]) << 32U);
#else
bits = blk.m_qwords[1];
#endif
if (bit_ofs >= 64U)
bits >>= (bit_ofs - 64U);
-4
View File
@@ -205,10 +205,6 @@ namespace basist
{
uint8_t m_bytes[16];
uint32_t m_dwords[4];
#ifndef __EMSCRIPTEN__
uint64_t m_qwords[2];
#endif
};
};
File diff suppressed because one or more lines are too long
Binary file not shown.
Binary file not shown.