diff --git a/.github/workflows/sanitizers.yml b/.github/workflows/sanitizers.yml new file mode 100644 index 000000000..c5ed6139d --- /dev/null +++ b/.github/workflows/sanitizers.yml @@ -0,0 +1,114 @@ +name: Sanitizers +on: + workflow_dispatch: + pull_request: + push: + branches: [master] +concurrency: + group: ${{ github.workflow }}-${{ github.job }}-${{ github.ref }} + cancel-in-progress: true +defaults: + run: + shell: bash -e -l {0} +jobs: + build: + runs-on: ${{ matrix.os }} + name: sanitizer / ${{ matrix.sys.compiler }} ${{ matrix.sys.version }} / ${{ matrix.config.name }} / ${{ matrix.sys.name }} + strategy: + fail-fast: false + matrix: + os: [ubuntu-24.04] + sys: + - {compiler: clang, version: '21', name: asan, sanitizer: address} + - {compiler: clang, version: '21', name: msan, sanitizer: memory} + - {compiler: clang, version: '21', name: lsan, sanitizer: leak} + - {compiler: clang, version: '21', name: ubsan, sanitizer: undefined} + config: + - {name: Debug} + + steps: + + - name: Install LLVM and Clang + if: matrix.sys.compiler == 'clang' + run: | + wget https://apt.llvm.org/llvm.sh + chmod +x llvm.sh + sudo ./llvm.sh ${{matrix.sys.version}} + sudo apt-get install -y clang-tools-${{matrix.sys.version}} + sudo update-alternatives --install /usr/bin/clang++ clang++ /usr/bin/clang++-${{matrix.sys.version}} 200 + sudo update-alternatives --install /usr/bin/clang clang /usr/bin/clang-${{matrix.sys.version}} 200 + sudo update-alternatives --install /usr/bin/clang-scan-deps clang-scan-deps /usr/bin/clang-scan-deps-${{matrix.sys.version}} 200 + sudo update-alternatives --set clang /usr/bin/clang-${{matrix.sys.version}} + sudo update-alternatives --set clang++ /usr/bin/clang++-${{matrix.sys.version}} + sudo update-alternatives --set clang-scan-deps /usr/bin/clang-scan-deps-${{matrix.sys.version}} + + - name: Checkout code + uses: actions/checkout@v6 + + - name: Set conda environment + uses: mamba-org/setup-micromamba@main + with: + environment-name: myenv + environment-file: environment-dev.yml + init-shell: bash + cache-downloads: true + + - name: Configure using CMake + run: | + export CC=clang + export CXX=clang++ + cmake -G Ninja \ + -Bbuild \ + -DCMAKE_BUILD_TYPE=${{matrix.config.name}} \ + -DCMAKE_INSTALL_PREFIX=$CONDA_PREFIX \ + -DBUILD_TESTS=ON \ + -DUSE_SANITIZER=${{ matrix.sys.sanitizer }} + + - name: Build tests + working-directory: build + run: cmake --build . --config ${{matrix.config.name}} --target test_xtensor_lib --parallel 8 + + - name: Run tests + working-directory: build + run: | + SAN=${{ matrix.sys.sanitizer }} + case "$SAN" in + address) + export ASAN_OPTIONS=log_path=asan_log_:alloc_dealloc_mismatch=0:halt_on_error=0:handle_abort=0 + export ASAN_SAVE_DUMPS=AsanDump.dmp + ;; + memory) + export MSAN_OPTIONS=log_path=msan_log_:halt_on_error=0:suppressions=${GITHUB_WORKSPACE}/test/msan_suppressions.txt + ;; + leak) + export LSAN_OPTIONS=log_path=lsan_log_:halt_on_error=0 + ;; + undefined) + export UBSAN_OPTIONS=log_path=ubsan_log_:halt_on_error=0:print_stacktrace=1 + ;; + esac + ctest -R ^xtest$ --output-on-failure + + - name: Upload sanitizer log + if: always() + uses: actions/upload-artifact@v6 + with: + name: sanitizer-log-${{ matrix.sys.sanitizer }}-${{ matrix.sys.compiler }}-${{ matrix.sys.version }}-${{ matrix.config.name }}-${{ runner.os }} + path: '**/*san_log_*' + if-no-files-found: ignore + + - name: Upload sanitizer dump + if: always() + uses: actions/upload-artifact@v6 + with: + name: sanitizer-dump-${{ matrix.sys.sanitizer }}-${{ matrix.sys.compiler }}-${{ matrix.sys.version }}-${{ matrix.config.name }}-${{ runner.os }} + path: '**/AsanDump.dmp' + if-no-files-found: ignore + + - name: Return errors if sanitizer log content is not empty + if: always() + run: | + if [ -n "$(find build/test -name '*san_log_*' -type f -size +0 2>/dev/null)" ]; then + echo "Sanitizer detected errors. See the log for details." + exit 1 + fi diff --git a/cmake/sanitizers.cmake b/cmake/sanitizers.cmake new file mode 100644 index 000000000..11c7a1075 --- /dev/null +++ b/cmake/sanitizers.cmake @@ -0,0 +1,50 @@ +set(AVALAIBLE_SANITIZERS "address;leak;memory;thread;undefined") +OPTION(USE_SANITIZER "Enable sanitizer(s). Options are: ${AVALAIBLE_SANITIZERS}. Case insensitive; multiple options delimited by comma or space possible." "") +string(TOLOWER "${USE_SANITIZER}" USE_SANITIZER) + +if((CMAKE_BUILD_TYPE IN_LIST "Debug;RelWithDebInfo") AND USE_SANITIZER) + message(FATAL_ERROR "❌ Sanitizer only supported in Debug and RelWithDebInfo build types.") +endif() + +if(USE_SANITIZER) + if(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC") + set(CMAKE_MSVC_DEBUG_INFORMATION_FORMAT "$,$>,$<$:EditAndContinue>,$<$:ProgramDatabase>>") + + if(USE_SANITIZER MATCHES "address") + list(APPEND SANITIZER_COMPILE_OPTIONS /fsanitize=address /D_DISABLE_VECTOR_ANNOTATION /D_DISABLE_STRING_ANNOTATION) + else() + message(FATAL_ERROR "❌ Sanitizer not supported by MSVC: ${USE_SANITIZER}. It only supports 'address'.") + endif() + elseif(CMAKE_CXX_COMPILER_ID STREQUAL "Clang" AND CMAKE_CXX_COMPILER_FRONTEND_VARIANT STREQUAL "MSVC") + if(USE_SANITIZER MATCHES "address") + list(APPEND SANITIZER_COMPILE_OPTIONS /fsanitize=address /D_DISABLE_VECTOR_ANNOTATION /D_DISABLE_STRING_ANNOTATION) + list(APPEND SANITIZER_LINK_LIBRARIES clang_rt.asan_dynamic-x86_64 clang_rt.asan_dynamic_runtime_thunk-x86_64) + else() + message(FATAL_ERROR "❌ Sanitizer not supported by Clang-MSVC: ${USE_SANITIZER}. It only supports 'address'.") + endif() + elseif(CMAKE_CXX_COMPILER_ID STREQUAL "Clang" OR CMAKE_CXX_COMPILER_ID STREQUAL "AppleClang" OR CMAKE_CXX_COMPILER_ID STREQUAL "GNU") + foreach(sanitizer ${USE_SANITIZER}) + if(NOT ${sanitizer} IN_LIST AVALAIBLE_SANITIZERS) + message(FATAL_ERROR "❌ Sanitizer not supported: ${sanitizer}. It should be one of: ${AVALAIBLE_SANITIZERS}.") + endif() + list(APPEND SANITIZER_COMPILE_OPTIONS -fsanitize=${sanitizer}) + list(APPEND SANITIZER_LINK_OPTIONS -fsanitize=${sanitizer}) + if (${sanitizer} MATCHES "undefined") + list(APPEND SANITIZER_COMPILE_OPTIONS -fno-sanitize=signed-integer-overflow) + endif() + if (${sanitizer} MATCHES "memory") + list(APPEND SANITIZER_LINK_LIBRARIES -fsanitize-memory-track-origins -fPIE -pie) + list(APPEND SANITIZER_LINK_OPTIONS -fsanitize-memory-track-origins -fPIE -pie) + endif() + endforeach() + list(APPEND SANITIZER_COMPILE_OPTIONS -fno-omit-frame-pointer) + else() + message(FATAL_ERROR "❌ Sanitizer: Unsupported compiler: ${CMAKE_CXX_COMPILER_ID}") + endif() + + list(REMOVE_DUPLICATES SANITIZER_COMPILE_OPTIONS) + list(REMOVE_DUPLICATES SANITIZER_LINK_OPTIONS) + list(REMOVE_DUPLICATES SANITIZER_LINK_LIBRARIES) + + message(STATUS "🔍 Using sanitizer: ${USE_SANITIZER}") +endif() diff --git a/include/xtensor/core/xiterator.hpp b/include/xtensor/core/xiterator.hpp index 448f6093d..ca0817108 100644 --- a/include/xtensor/core/xiterator.hpp +++ b/include/xtensor/core/xiterator.hpp @@ -483,6 +483,14 @@ namespace xt template inline auto xstepper::operator*() const -> reference { + if constexpr (std::is_pointer::value) + { + if (m_it == nullptr) + { + static std::remove_reference_t sentinel{}; + return sentinel; + } + } return *m_it; } diff --git a/include/xtensor/core/xstrides.hpp b/include/xtensor/core/xstrides.hpp index d413fcd26..998ffcd6b 100644 --- a/include/xtensor/core/xstrides.hpp +++ b/include/xtensor/core/xstrides.hpp @@ -171,6 +171,10 @@ namespace xt It strided_data_end(const C& c, It begin, layout_type l, size_type offset) { using difference_type = typename std::iterator_traits::difference_type; + if (c.size() == 0 || std::find(c.shape().cbegin(), c.shape().cend(), size_type(0)) != c.shape().cend()) + { + return begin; + } if (c.dimension() == 0) { ++begin; diff --git a/include/xtensor/misc/xfft.hpp b/include/xtensor/misc/xfft.hpp index 954b55a9b..caf37145e 100644 --- a/include/xtensor/misc/xfft.hpp +++ b/include/xtensor/misc/xfft.hpp @@ -61,7 +61,7 @@ namespace xt auto odd = radix2(xt::view(ev, xt::range(1, _, 2))); #endif - auto range = xt::arange(N / 2); + auto range = xt::arange(static_cast(N) / 2); auto exp = xt::exp(static_cast(-2i) * pi * range / N); auto t = exp * odd; auto first_half = even + t; @@ -82,8 +82,8 @@ namespace xt // Find a power-of-2 convolution length m such that m >= n * 2 + 1 const std::size_t n = data.size(); - size_t m = std::ceil(std::log2(n * 2 + 1)); - m = std::pow(2, m); + size_t m = static_cast(std::ceil(std::log2(n * 2 + 1))); + m = static_cast(std::pow(2, m)); // Trignometric table auto exp_table = xt::xtensor, 1>::from_shape({n}); @@ -128,6 +128,10 @@ namespace xt inline auto fft(E&& e, std::ptrdiff_t axis = -1) { using value_type = typename std::decay::type::value_type; + if (e.dimension() == 0) + { + XTENSOR_THROW(std::runtime_error, "Cannot take the FFT of a scalar expression"); + } if constexpr (xtl::is_complex::type::value_type>::value) { using precision = typename value_type::value_type; @@ -159,10 +163,14 @@ namespace xt template inline auto ifft(E&& e, std::ptrdiff_t axis = -1) { + if (e.dimension() == 0) + { + XTENSOR_THROW(std::runtime_error, "Cannot take the iFFT of a scalar expression"); + } if constexpr (xtl::is_complex::type::value_type>::value) { // check the length of the data on that axis - const std::size_t n = e.shape(axis); + const std::size_t n = e.shape(xt::normalize_axis(e.dimension(), axis)); if (n == 0) { XTENSOR_THROW(std::runtime_error, "Cannot take the iFFT along an empty dimention"); diff --git a/include/xtensor/views/index_mapper.hpp b/include/xtensor/views/index_mapper.hpp index 574bcb476..330a71baa 100644 --- a/include/xtensor/views/index_mapper.hpp +++ b/include/xtensor/views/index_mapper.hpp @@ -193,7 +193,7 @@ namespace xt * @throws Assertion failure if `i != 0` for integral slices. * @throws Assertion failure if `i >= slice.size()` for non-integral slices. */ - template + template size_t map_ith_index(const view_type& view, const Index i) const; /** @@ -490,16 +490,16 @@ namespace xt { if constexpr (ACCESS == access_t::SAFE) { - return container.at(map_ith_index(view, indices[Is])...); + return container.at(map_ith_index(view, indices[Is])...); } else { - return container(map_ith_index(view, indices[Is])...); + return container(map_ith_index(view, indices[Is])...); } } template - template + template auto index_mapper>::map_ith_index(const view_type& view, const Index i) const -> size_t @@ -515,14 +515,51 @@ namespace xt if constexpr (std::is_integral_v) { - assert(i == 0); + if constexpr (ACCESS == access_t::SAFE) + { + if (i != 0) + { + XTENSOR_THROW(std::out_of_range, "Index out of range in index_mapper access"); + } + } + else + { + assert(i == 0); + } return size_t(slice); } + else if constexpr (xt::detail::is_xall_slice>::value) + { + return size_t(i); + } else { using slice_size_type = typename current_slice::size_type; - assert(i < slice.size()); - return size_t(slice(static_cast(i))); + const auto slice_index = static_cast(i); + + if constexpr (ACCESS == access_t::SAFE) + { + if constexpr (std::is_signed_v) + { + if (slice_index < 0 || slice_index >= slice.size()) + { + XTENSOR_THROW(std::out_of_range, "Index out of range in index_mapper access"); + } + } + else if (slice_index >= slice.size()) + { + XTENSOR_THROW(std::out_of_range, "Index out of range in index_mapper access"); + } + } + else + { + if constexpr (std::is_signed_v) + { + assert(slice_index >= 0); + } + assert(slice_index < slice.size()); + } + return size_t(slice(slice_index)); } } else diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 8341230ee..677aacc23 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -231,6 +231,15 @@ endforeach() file(GLOB XTENSOR_PREPROCESS_FILES files/cppy_source/*.cppy) +# Sanitizer support +include(${CMAKE_SOURCE_DIR}/cmake/sanitizers.cmake) + +if(USE_SANITIZER MATCHES "memory" AND CMAKE_CXX_COMPILER_ID MATCHES "Clang") + # doctest's String union triggers MSan false positives during + # static-init reporter registration. Fixed in main.cpp with + # __attribute__((no_sanitize("memory"))) on doctest functions. +endif() + # This target should only be run when the test source files have been changed. add_custom_target( preprocess_cppy @@ -258,6 +267,8 @@ foreach(filename IN LISTS COMMON_BASE XTENSOR_TESTS) endif() target_include_directories(${targetname} PRIVATE ${XTENSOR_INCLUDE_DIR}) target_link_libraries(${targetname} PRIVATE xtensor doctest::doctest ${CMAKE_THREAD_LIBS_INIT}) + target_compile_options(${targetname} PRIVATE $<$:${SANITIZER_COMPILE_OPTIONS}>) + target_link_options(${targetname} PRIVATE $<$:${SANITIZER_LINK_OPTIONS}>) add_custom_target( x${targetname} COMMAND ${targetname} @@ -282,11 +293,27 @@ if(XTENSOR_USE_OPENMP) target_compile_definitions(test_xtensor_lib PRIVATE XTENSOR_USE_OPENMP) endif() +target_compile_options(test_xtensor_lib PRIVATE $<$:${SANITIZER_COMPILE_OPTIONS}>) +target_link_options(test_xtensor_lib PRIVATE $<$:${SANITIZER_LINK_OPTIONS}>) + target_include_directories(test_xtensor_lib PRIVATE ${XTENSOR_INCLUDE_DIR}) target_link_libraries(test_xtensor_lib PRIVATE xtensor doctest::doctest ${CMAKE_THREAD_LIBS_INIT}) -add_custom_target(xtest COMMAND test_xtensor_lib DEPENDS test_xtensor_lib) +set(XTENSOR_TEST_ENV) +if(USE_SANITIZER MATCHES "memory") + set(XTENSOR_MSAN_SUPPRESSIONS_FILE "${CMAKE_CURRENT_SOURCE_DIR}/msan_suppressions.txt") + set(XTENSOR_TEST_ENV "MSAN_OPTIONS=suppressions=${XTENSOR_MSAN_SUPPRESSIONS_FILE}") +endif() + +add_custom_target( + xtest + COMMAND ${CMAKE_COMMAND} -E env ${XTENSOR_TEST_ENV} $ + DEPENDS test_xtensor_lib +) add_test(NAME xtest COMMAND test_xtensor_lib) +if(XTENSOR_TEST_ENV) + set_tests_properties(xtest PROPERTIES ENVIRONMENT "${XTENSOR_TEST_ENV}") +endif() # Some files will be compiled twice, however compiling common files in a static # library and linking test_xtensor_lib with it removes half of the tests at diff --git a/test/main.cpp b/test/main.cpp index f16c661eb..2d5f46d33 100644 --- a/test/main.cpp +++ b/test/main.cpp @@ -1,5 +1,23 @@ +#ifdef __clang__ +#if __has_feature(memory_sanitizer) +// Suppress MSan false positives in doctest's String union comparison. +// doctest::String uses a union of stack/heap storage; the padding +// between union members is flagged as uninitialized during strcmp +// inside reporter registration at static-init time. +// no_sanitize("memory") suppresses the check while still allowing +// shadow memory propagation for stores. +#pragma clang attribute push(__attribute__((no_sanitize("memory"))), apply_to = function) +#endif +#endif + #define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN #if defined(XTENSOR_DISABLE_EXCEPTIONS) #define DOCTEST_CONFIG_NO_EXCEPTIONS_BUT_WITH_ALL_ASSERTS #endif #include "doctest/doctest.h" + +#ifdef __clang__ +#if __has_feature(memory_sanitizer) +#pragma clang attribute pop +#endif +#endif diff --git a/test/msan_suppressions.txt b/test/msan_suppressions.txt new file mode 100644 index 000000000..73be572ea --- /dev/null +++ b/test/msan_suppressions.txt @@ -0,0 +1,5 @@ +# MSan false positive: doctest reporter registration during static init +# doctest::String has internal padding that MSan flags as uninitialized +# when std::map compares keys during insert. +fun:*doctest::detail::registerReporterImpl* +src:*doctest/doctest.h diff --git a/test/test_xadapt.cpp b/test/test_xadapt.cpp index 5876ff926..ef5451b91 100644 --- a/test/test_xadapt.cpp +++ b/test/test_xadapt.cpp @@ -132,6 +132,8 @@ namespace xt a1(1, 0) = static_cast(i); EXPECT_EQ(i, data[i * size + st]); } + + delete[] data; } TEST(xarray_adaptor, pointer_acquire_ownership) @@ -300,6 +302,8 @@ namespace xt a1(1, 0) = static_cast(i); EXPECT_EQ(i, data[i * size + st]); } + + delete[] data; } TEST(xtensor_adaptor, pointer_const_no_ownership) diff --git a/test/test_xblockwise_reducer.cpp b/test/test_xblockwise_reducer.cpp index 9c625335e..d888ec396 100644 --- a/test/test_xblockwise_reducer.cpp +++ b/test/test_xblockwise_reducer.cpp @@ -111,14 +111,24 @@ namespace xt dynamic_shape chunk_shape({5, 4, 2}); xarray input_exp(shape); - // just iota is a bit boring since it will - // lead to an uniform variance - std::iota(input_exp.begin(), input_exp.end(), -5); - for (std::size_t i = 0; i < input_exp.size(); ++i) + if (std::is_same::value) { - if (i % 2) + for (std::size_t i = 0; i < input_exp.size(); ++i) { - input_exp.flat(i) += 10; + input_exp.flat(i) = (i % 2 == 0) ? 1 : -1; + } + } + else + { + // just iota is a bit boring since it will + // lead to an uniform variance + std::iota(input_exp.begin(), input_exp.end(), -5); + for (std::size_t i = 0; i < input_exp.size(); ++i) + { + if (i % 2) + { + input_exp.flat(i) += 10; + } } } diff --git a/test/test_xbuffer_adaptor.cpp b/test/test_xbuffer_adaptor.cpp index d6ad32b21..5b0a5fcb6 100644 --- a/test/test_xbuffer_adaptor.cpp +++ b/test/test_xbuffer_adaptor.cpp @@ -201,6 +201,8 @@ namespace xt size_t size2 = 50; XT_EXPECT_THROW(adapt.resize(size2), std::runtime_error); EXPECT_EQ(adapt.size(), size1); + + delete[] data1; } TEST(xbuffer_adaptor, no_owner_iterating) diff --git a/test/test_xfft.cpp b/test/test_xfft.cpp index 7665cb592..a1b050ce4 100644 --- a/test/test_xfft.cpp +++ b/test/test_xfft.cpp @@ -27,6 +27,15 @@ namespace xt REQUIRE(A == doctest::Approx(std::abs(res(k))).epsilon(.0001)); } + TEST(xfft, scalar_input_throws) + { + auto scalar = xt::xarray::from_shape({}); + scalar() = 1.0f; + + XT_EXPECT_THROW(xt::fft::fft(scalar), std::runtime_error); + XT_EXPECT_THROW(xt::fft::ifft(scalar), std::runtime_error); + } + TEST(xfft, convolve_power_2) { xt::xarray x = {1.0, 1.0, 1.0, 5.0}; diff --git a/test/test_xmath_result_type.cpp b/test/test_xmath_result_type.cpp index 4ded1b7e9..333d0fb07 100644 --- a/test/test_xmath_result_type.cpp +++ b/test/test_xmath_result_type.cpp @@ -115,7 +115,7 @@ namespace xt TEST(xmath, uchar_result_type) { shape_type shape = {3, 2}; - xarray auchar(shape); + auto auchar = xt::zeros(shape); CHECK_RESULT_TYPE(auchar + auchar, int); CHECK_RESULT_TYPE(2 * auchar, int); @@ -131,7 +131,7 @@ namespace xt TEST(xmath, short_result_type) { shape_type shape = {3, 2}; - xarray ashort(shape); + auto ashort = xt::zeros(shape); CHECK_RESULT_TYPE(ashort + ashort, int); CHECK_RESULT_TYPE(2 * ashort, int); @@ -147,7 +147,7 @@ namespace xt TEST(xmath, ushort_result_type) { shape_type shape = {3, 2}; - xarray aushort(shape); + auto aushort = xt::zeros(shape); CHECK_RESULT_TYPE(aushort + aushort, int); CHECK_RESULT_TYPE(2u * aushort, unsigned int); @@ -163,7 +163,7 @@ namespace xt TEST(xmath, int_result_type) { shape_type shape = {3, 2}; - xarray aint(shape); + auto aint = xt::zeros(shape); CHECK_RESULT_TYPE(aint + aint, int); CHECK_RESULT_TYPE(2 * aint, int); @@ -179,7 +179,7 @@ namespace xt TEST(xmath, uint_result_type) { shape_type shape = {3, 2}; - xarray auint(shape); + auto auint = xt::zeros(shape); CHECK_RESULT_TYPE(auint + auint, unsigned int); CHECK_RESULT_TYPE(2u * auint, unsigned int); @@ -195,7 +195,7 @@ namespace xt TEST(xmath, long_result_type) { shape_type shape = {3, 2}; - xarray along(shape); + auto along = xt::zeros(shape); CHECK_RESULT_TYPE(along + along, signed long long); CHECK_RESULT_TYPE(2 * along, signed long long); @@ -211,7 +211,7 @@ namespace xt TEST(xmath, ulong_result_type) { shape_type shape = {3, 2}; - xarray aulong(shape); + auto aulong = xt::zeros(shape); CHECK_RESULT_TYPE(aulong + aulong, unsigned long long); CHECK_RESULT_TYPE(2ul * aulong, unsigned long long); @@ -227,7 +227,7 @@ namespace xt TEST(xmath, float_result_type) { shape_type shape = {3, 2}; - xarray afloat(shape); + auto afloat = xt::zeros(shape); CHECK_RESULT_TYPE(afloat + afloat, float); CHECK_RESULT_TYPE(2.0f * afloat, float); @@ -243,7 +243,7 @@ namespace xt TEST(xmath, double_result_type) { shape_type shape = {3, 2}; - xarray adouble(shape); + auto adouble = xt::zeros(shape); CHECK_RESULT_TYPE(adouble + adouble, double); CHECK_RESULT_TYPE(2.0 * adouble, double);