diff --git a/libs/libcommon/include/common/avx2_strstr.h b/libs/libcommon/include/common/avx2_strstr.h index c60efb232c2..f8d44702f93 100644 --- a/libs/libcommon/include/common/avx2_strstr.h +++ b/libs/libcommon/include/common/avx2_strstr.h @@ -16,10 +16,6 @@ #include -#include -#include -#include - namespace mem_utils::details { @@ -135,6 +131,9 @@ ALWAYS_INLINE static inline const char * avx2_strstr_impl(const char * src, cons // align to 32 src = reinterpret_cast(ALIGNED_ADDR(size_t(src), BLOCK32_SIZE)); + // load block 32 from new aligned address may cause false positives when using `AddressSanitizer` because asan will provide a malloc()/free() alternative and detect memory visitation. + // generally it's safe to visit address which won't cross page boundary. + // right shift offset to remove useless mask bit auto mask = get_block32_cmp_eq_mask(src, check_block32) >> offset; @@ -251,6 +250,10 @@ ALWAYS_INLINE static inline const char * avx2_strstr_impl(const char * src, size ALWAYS_INLINE static inline size_t avx2_strstr(const char * src, size_t n, const char * needle, size_t k) { +#if defined(ADDRESS_SANITIZER) + return std::string_view{src, n}.find({needle, k}); // memchr@plt -> bcmp@plt +#endif + const auto * p = avx2_strstr_impl(src, n, needle, k); return p ? p - src : std::string_view::npos; } @@ -260,6 +263,10 @@ ALWAYS_INLINE static inline size_t avx2_strstr(std::string_view src, std::string } ALWAYS_INLINE static inline const char * avx2_memchr(const char * src, size_t n, char target) { +#if defined(ADDRESS_SANITIZER) + return static_cast(std::memchr(src, target, n)); // memchr@plt +#endif + if (unlikely(n < 1)) { return nullptr; diff --git a/libs/libcommon/include/common/mem_utils_opt.h b/libs/libcommon/include/common/mem_utils_opt.h index 281256158e9..66057ccdd51 100644 --- a/libs/libcommon/include/common/mem_utils_opt.h +++ b/libs/libcommon/include/common/mem_utils_opt.h @@ -102,7 +102,7 @@ FLATTEN_INLINE_PURE static inline int CompareStrView(const std::string_view & lh #endif } -// same function like `std::string_view::find` +// same function like `std::string_view::find(std::string_view)` FLATTEN_INLINE_PURE static inline size_t StrFind(std::string_view src, std::string_view needle) { #ifdef TIFLASH_ENABLE_AVX_SUPPORT