From fc28ba31566eec34d48019d7df50376a6a387d3f Mon Sep 17 00:00:00 2001 From: Vincent Rabaud Date: Thu, 10 Feb 2022 17:44:14 +0100 Subject: [PATCH] Merge pull request #21594 from vrabaud:3.4_msan * Fix harmless MSAN error. This is similar to https://github.com/opencv/opencv/pull/21527 A macro is also created to simplify the code. * Declare fallback only once. --- modules/core/src/hal_internal.cpp | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/modules/core/src/hal_internal.cpp b/modules/core/src/hal_internal.cpp index 8b74a35361..5239acc585 100644 --- a/modules/core/src/hal_internal.cpp +++ b/modules/core/src/hal_internal.cpp @@ -64,6 +64,16 @@ #define HAL_LU_SMALL_MATRIX_THRESH 100 #define HAL_CHOLESKY_SMALL_MATRIX_THRESH 100 +#if defined(__clang__) && defined(__has_feature) +#if __has_feature(memory_sanitizer) +#define CV_ANNOTATE_MEMORY_IS_INITIALIZED(address, size) \ +__msan_unpoison(adresse, size) +#endif +#endif +#ifndef CV_ANNOTATE_MEMORY_IS_INITIALIZED +#define CV_ANNOTATE_MEMORY_IS_INITIALIZED(address, size) do { } while(0) +#endif + //lapack stores matrices in column-major order so transposing is needed everywhere template static inline void transpose_square_inplace(fptype *src, size_t src_ld, size_t m) @@ -239,20 +249,16 @@ lapack_SVD(fptype* a, size_t a_step, fptype *w, fptype* u, size_t u_step, fptype else if(typeid(fptype) == typeid(double)) OCV_LAPACK_FUNC(dgesdd)(mode, &m, &n, (double*)a, &lda, (double*)w, (double*)u, &ldu, (double*)vt, &ldv, (double*)buffer, &lwork, iworkBuf, info); -#if defined(__clang__) && defined(__has_feature) -#if __has_feature(memory_sanitizer) // Make sure MSAN sees the memory as having been written. // MSAN does not think it has been written because a different language was called. - __msan_unpoison(a, a_step * n); - __msan_unpoison(buffer, sizeof(fptype) * (lwork + 1)); + CV_ANNOTATE_MEMORY_IS_INITIALIZED(a, a_step * n); + CV_ANNOTATE_MEMORY_IS_INITIALIZED(buffer, sizeof(fptype) * (lwork + 1)); if (u) - __msan_unpoison(u, u_step * m); + CV_ANNOTATE_MEMORY_IS_INITIALIZED(u, u_step * m); if (vt) - __msan_unpoison(vt, v_step * n); + CV_ANNOTATE_MEMORY_IS_INITIALIZED(vt, v_step * n); if (w) - __msan_unpoison(w, sizeof(fptype) * std::min(m, n)); -#endif // __has_feature(memory_sanitizer) -#endif // defined(__clang__) && defined(__has_feature) + CV_ANNOTATE_MEMORY_IS_INITIALIZED(w, sizeof(fptype) * std::min(m, n)); if(!(flags & CV_HAL_SVD_NO_UV)) transpose_square_inplace(vt, ldv, n); @@ -357,6 +363,7 @@ lapack_QR(fptype* a, size_t a_step, int m, int n, int k, fptype* b, size_t b_ste dgeqrf_(&m, &n, (double*)tmpA, &ldtmpA, (double*)dst, (double*)buffer, &lwork, info); } + CV_ANNOTATE_MEMORY_IS_INITIALIZED(info, sizeof(int)); if (m == n) transpose_square_inplace(a, lda, m); else