From 0ae7d7a36117630f544f80a782f95b9f0a293659 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Antonio=20S=C3=A1nchez?= Date: Tue, 1 Aug 2023 19:39:08 +0000 Subject: [PATCH] Fix unaligned scalar alignment UB. --- Eigen/src/Core/MapBase.h | 4 +++- test/mapped_matrix.cpp | 34 ++++++---------------------------- 2 files changed, 9 insertions(+), 29 deletions(-) diff --git a/Eigen/src/Core/MapBase.h b/Eigen/src/Core/MapBase.h index 4dda24d21..4bd29cf84 100644 --- a/Eigen/src/Core/MapBase.h +++ b/Eigen/src/Core/MapBase.h @@ -193,6 +193,8 @@ template class MapBase EIGEN_DEVICE_FUNC void checkSanity(std::enable_if_t<(internal::traits::Alignment>0),void*> = 0) const { + // Pointer must be aligned to the Scalar type, otherwise we get UB. + eigen_assert((std::uintptr_t(m_data) % alignof(Scalar) == 0) && "data is not scalar-aligned"); #if EIGEN_MAX_ALIGN_BYTES>0 // innerStride() is not set yet when this function is called, so we optimistically assume the lowest plausible value: const Index minInnerStride = InnerStrideAtCompileTime == Dynamic ? 1 : Index(InnerStrideAtCompileTime); @@ -205,7 +207,7 @@ template class MapBase template EIGEN_DEVICE_FUNC void checkSanity(std::enable_if_t::Alignment==0,void*> = 0) const - {} + { eigen_assert((std::uintptr_t(m_data) % alignof(Scalar) == 0) && "data is not scalar-aligned"); } PointerType m_data; const internal::variable_if_dynamic m_rows; diff --git a/test/mapped_matrix.cpp b/test/mapped_matrix.cpp index 7270d030b..f4638f6bc 100644 --- a/test/mapped_matrix.cpp +++ b/test/mapped_matrix.cpp @@ -68,12 +68,12 @@ template void map_class_matrix(const MatrixType& m) Scalar array4[256]; if(size<=256) for(int i = 0; i < size; i++) array4[i] = Scalar(1); - + Map map1(array1, rows, cols); Map map2(array2, rows, cols); Map map3(array3unaligned, rows, cols); Map map4(array4, rows, cols); - + VERIFY_IS_EQUAL(map1, MatrixType::Ones(rows,cols)); VERIFY_IS_EQUAL(map2, MatrixType::Ones(rows,cols)); VERIFY_IS_EQUAL(map3, MatrixType::Ones(rows,cols)); @@ -88,17 +88,17 @@ template void map_class_matrix(const MatrixType& m) VERIFY_IS_EQUAL(ma1, ma2); VERIFY_IS_EQUAL(ma1, ma3); VERIFY_IS_EQUAL(ma1, map3); - + VERIFY_IS_APPROX(s1*map1, s1*map2); VERIFY_IS_APPROX(s1*ma1, s1*ma2); VERIFY_IS_EQUAL(s1*ma1, s1*ma3); VERIFY_IS_APPROX(s1*map1, s1*map3); - + map2 *= s1; map3 *= s1; VERIFY_IS_APPROX(s1*map1, map2); VERIFY_IS_APPROX(s1*map1, map3); - + if(size<=256) { VERIFY_IS_EQUAL(map4, MatrixType::Ones(rows,cols)); @@ -108,7 +108,7 @@ template void map_class_matrix(const MatrixType& m) VERIFY_IS_EQUAL(ma1, map4); VERIFY_IS_EQUAL(ma1, ma4); VERIFY_IS_APPROX(s1*map1, s1*map4); - + map4 *= s1; VERIFY_IS_APPROX(s1*map1, map4); } @@ -159,27 +159,6 @@ template void check_const_correctness(const PlainObjec VERIFY( !(Map::Flags & LvalueBit) ); } -template -void map_not_aligned_on_scalar() -{ - typedef Matrix MatrixType; - Index size = 11; - Scalar* array1 = internal::aligned_new((size+1)*(size+1)+1); - Scalar* array2 = reinterpret_cast(sizeof(Scalar)/2+std::size_t(array1)); - Map > map2(array2, size, size, OuterStride<>(size+1)); - MatrixType m2 = MatrixType::Random(size,size); - map2 = m2; - VERIFY_IS_EQUAL(m2, map2); - - typedef Matrix VectorType; - Map map3(array2, size); - MatrixType v3 = VectorType::Random(size); - map3 = v3; - VERIFY_IS_EQUAL(v3, map3); - - internal::aligned_delete(array1, (size+1)*(size+1)+1); -} - EIGEN_DECLARE_TEST(mapped_matrix) { for(int i = 0; i < g_repeat; i++) { @@ -204,6 +183,5 @@ EIGEN_DECLARE_TEST(mapped_matrix) CALL_SUBTEST_8( map_static_methods(RowVector3d()) ); CALL_SUBTEST_9( map_static_methods(VectorXcd(8)) ); CALL_SUBTEST_10( map_static_methods(VectorXf(12)) ); - CALL_SUBTEST_11( map_not_aligned_on_scalar() ); } }