Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 110 additions & 0 deletions .github/workflows/sanitizers.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
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: 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
;;
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
50 changes: 50 additions & 0 deletions cmake/sanitizers.cmake
Original file line number Diff line number Diff line change
@@ -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 "$<IF:$<AND:$<C_COMPILER_ID:MSVC>,$<CXX_COMPILER_ID:MSVC>>,$<$<CONFIG:Debug,RelWithDebInfo>:EditAndContinue>,$<$<CONFIG:Debug,RelWithDebInfo>: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()
8 changes: 8 additions & 0 deletions include/xtensor/core/xiterator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,14 @@ namespace xt
template <class C>
inline auto xstepper<C>::operator*() const -> reference
{
if constexpr (std::is_pointer<subiterator_type>::value)
{
if (m_it == nullptr)
{
static std::remove_reference_t<reference> sentinel{};
return sentinel;
}
}
return *m_it;
}

Expand Down
4 changes: 4 additions & 0 deletions include/xtensor/core/xstrides.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<It>::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;
Expand Down
16 changes: 12 additions & 4 deletions include/xtensor/misc/xfft.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ namespace xt
auto odd = radix2(xt::view(ev, xt::range(1, _, 2)));
#endif

auto range = xt::arange<double>(N / 2);
auto range = xt::arange<double>(static_cast<double>(N) / 2);
auto exp = xt::exp(static_cast<value_type>(-2i) * pi * range / N);
auto t = exp * odd;
auto first_half = even + t;
Expand All @@ -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<size_t>(std::ceil(std::log2(n * 2 + 1)));
m = static_cast<size_t>(std::pow(2, m));

// Trignometric table
auto exp_table = xt::xtensor<std::complex<precision>, 1>::from_shape({n});
Expand Down Expand Up @@ -128,6 +128,10 @@ namespace xt
inline auto fft(E&& e, std::ptrdiff_t axis = -1)
{
using value_type = typename std::decay<E>::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<typename std::decay<E>::type::value_type>::value)
{
using precision = typename value_type::value_type;
Expand Down Expand Up @@ -159,10 +163,14 @@ namespace xt
template <class E>
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<typename std::decay<E>::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");
Expand Down
51 changes: 44 additions & 7 deletions include/xtensor/views/index_mapper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <size_t I, std::integral Index>
template <size_t I, access_t ACCESS, std::integral Index>
size_t map_ith_index(const view_type& view, const Index i) const;

/**
Expand Down Expand Up @@ -490,16 +490,16 @@ namespace xt
{
if constexpr (ACCESS == access_t::SAFE)
{
return container.at(map_ith_index<Is>(view, indices[Is])...);
return container.at(map_ith_index<Is, ACCESS>(view, indices[Is])...);
}
else
{
return container(map_ith_index<Is>(view, indices[Is])...);
return container(map_ith_index<Is, ACCESS>(view, indices[Is])...);
}
}

template <class UnderlyingContainer, class... Slices>
template <size_t I, std::integral Index>
template <size_t I, access_t ACCESS, std::integral Index>
auto
index_mapper<xt::xview<UnderlyingContainer, Slices...>>::map_ith_index(const view_type& view, const Index i) const
-> size_t
Expand All @@ -515,14 +515,51 @@ namespace xt

if constexpr (std::is_integral_v<current_slice>)
{
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<std::decay_t<current_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<slice_size_type>(i)));
const auto slice_index = static_cast<slice_size_type>(i);

if constexpr (ACCESS == access_t::SAFE)
{
if constexpr (std::is_signed_v<slice_size_type>)
{
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<slice_size_type>)
{
assert(slice_index >= 0);
}
assert(slice_index < slice.size());
}
return size_t(slice(slice_index));
}
}
else
Expand Down
27 changes: 26 additions & 1 deletion test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,9 @@ endforeach()

file(GLOB XTENSOR_PREPROCESS_FILES files/cppy_source/*.cppy)

# Sanitizer support
include(${CMAKE_SOURCE_DIR}/cmake/sanitizers.cmake)

# This target should only be run when the test source files have been changed.
add_custom_target(
preprocess_cppy
Expand Down Expand Up @@ -258,6 +261,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 $<$<BOOL:USE_SANITIZER>:${SANITIZER_COMPILE_OPTIONS}>)
target_link_options(${targetname} PRIVATE $<$<BOOL:USE_SANITIZER>:${SANITIZER_LINK_OPTIONS}>)
add_custom_target(
x${targetname}
COMMAND ${targetname}
Expand All @@ -282,10 +287,30 @@ if(XTENSOR_USE_OPENMP)
target_compile_definitions(test_xtensor_lib PRIVATE XTENSOR_USE_OPENMP)
endif()

target_compile_options(test_xtensor_lib PRIVATE $<$<BOOL:USE_SANITIZER>:${SANITIZER_COMPILE_OPTIONS}>)
target_link_options(test_xtensor_lib PRIVATE $<$<BOOL:USE_SANITIZER>:${SANITIZER_LINK_OPTIONS}>)

# doctest's String union + MSan libc interceptors (strlen/strcmp) cause
# false-positive use-of-uninitialized-value reports during reporter
# registration and exception formatting. These cannot be suppressed at the
# attribute level because MSan's libc interceptors check memory regardless
# of calling-function attributes. The combined convenience binary therefore
# opts out of MSan instrumentation so the xtest target can complete.
# Individual per-test executables (test_xarray, test_xview, etc.) remain
# fully instrumented and are the correct targets for MSan CI validation.
if(USE_SANITIZER MATCHES "memory")
target_compile_options(test_xtensor_lib PRIVATE -fno-sanitize=memory)
target_link_options(test_xtensor_lib PRIVATE -fno-sanitize=memory)
endif()

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)
add_custom_target(
xtest
COMMAND $<TARGET_FILE:test_xtensor_lib>
DEPENDS test_xtensor_lib
)
add_test(NAME xtest COMMAND test_xtensor_lib)

# Some files will be compiled twice, however compiling common files in a static
Expand Down
5 changes: 5 additions & 0 deletions test/msan_suppressions.txt
Original file line number Diff line number Diff line change
@@ -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
4 changes: 4 additions & 0 deletions test/test_xadapt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,8 @@ namespace xt
a1(1, 0) = static_cast<int>(i);
EXPECT_EQ(i, data[i * size + st]);
}

delete[] data;
}

TEST(xarray_adaptor, pointer_acquire_ownership)
Expand Down Expand Up @@ -300,6 +302,8 @@ namespace xt
a1(1, 0) = static_cast<int>(i);
EXPECT_EQ(i, data[i * size + st]);
}

delete[] data;
}

TEST(xtensor_adaptor, pointer_const_no_ownership)
Expand Down
Loading
Loading