Skip to content

Commit

Permalink
Adding SAN option to CMakeLists.txt, to make it easier to use clang's…
Browse files Browse the repository at this point in the history
… 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
  • Loading branch information
richgel999 committed Feb 25, 2022
1 parent 44e1bcd commit 7a2094b
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 15 deletions.
13 changes: 10 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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.
Expand Down Expand Up @@ -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")
Expand Down
13 changes: 12 additions & 1 deletion encoder/basisu_enc.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
8 changes: 8 additions & 0 deletions encoder/basisu_miniz.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 8 additions & 2 deletions transcoder/basisu_containers.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
12 changes: 8 additions & 4 deletions transcoder/basisu_transcoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 0 additions & 4 deletions transcoder/basisu_transcoder_uastc.h
Original file line number Diff line number Diff line change
Expand Up @@ -205,10 +205,6 @@ namespace basist
{
uint8_t m_bytes[16];
uint32_t m_dwords[4];

#ifndef __EMSCRIPTEN__
uint64_t m_qwords[2];
#endif
};
};

Expand Down
2 changes: 1 addition & 1 deletion webgl/encoder/build/basis_encoder.js

Large diffs are not rendered by default.

Binary file modified webgl/encoder/build/basis_encoder.wasm
Binary file not shown.
Binary file modified webgl/transcoder/build/basis_transcoder.wasm
Binary file not shown.

0 comments on commit 7a2094b

Please sign in to comment.