From 3f52c374fbb2ac2f9857af087baf58181a5c9270 Mon Sep 17 00:00:00 2001 From: Zhigao Tong Date: Wed, 31 Aug 2022 12:05:54 +0800 Subject: [PATCH 1/4] Fix asan false positive case --- libs/libcommon/include/common/avx2_strstr.h | 15 +++++++++++---- libs/libcommon/include/common/mem_utils_opt.h | 2 +- 2 files changed, 12 insertions(+), 5 deletions(-) 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 From 3effb8bd2b3802b49e9cb9d2ef8c52e22f69dee3 Mon Sep 17 00:00:00 2001 From: Zhigao Tong Date: Wed, 31 Aug 2022 15:56:15 +0800 Subject: [PATCH 2/4] Update from_unixtime.test --- tests/fullstack-test/expr/from_unixtime.test | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/fullstack-test/expr/from_unixtime.test b/tests/fullstack-test/expr/from_unixtime.test index fdc65a208cd..f0f69133e70 100644 --- a/tests/fullstack-test/expr/from_unixtime.test +++ b/tests/fullstack-test/expr/from_unixtime.test @@ -20,6 +20,6 @@ func> wait_table test t # This will throw error for now, but before https://github.com/pingcap/tics/issues/1433 fixed, it will cause segmentation fault mysql> set session tidb_isolation_read_engines='tiflash'; select * from test.t where FROM_UNIXTIME(1447430881, a) -ERROR 1105 (HY000) at line 1: Argument at index 1 for function fromUnixTime must be constant +{#REGEXP}.*Argument at index 1 for function fromUnixTime must be constant* mysql> drop table if exists test.t From 592ffdb3141de06635b720e96ac772523ce8d5f9 Mon Sep 17 00:00:00 2001 From: Zhigao Tong Date: Wed, 31 Aug 2022 15:57:44 +0800 Subject: [PATCH 3/4] Update from_unixtime.test --- tests/fullstack-test/expr/from_unixtime.test | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/fullstack-test/expr/from_unixtime.test b/tests/fullstack-test/expr/from_unixtime.test index f0f69133e70..468dbd2d578 100644 --- a/tests/fullstack-test/expr/from_unixtime.test +++ b/tests/fullstack-test/expr/from_unixtime.test @@ -20,6 +20,6 @@ func> wait_table test t # This will throw error for now, but before https://github.com/pingcap/tics/issues/1433 fixed, it will cause segmentation fault mysql> set session tidb_isolation_read_engines='tiflash'; select * from test.t where FROM_UNIXTIME(1447430881, a) -{#REGEXP}.*Argument at index 1 for function fromUnixTime must be constant* +{#REGEXP}.*Argument at index 1 for function fromUnixTime must be constant.* mysql> drop table if exists test.t From fb3616e02da4d3ca0ef91b15e61500e4644d4c7f Mon Sep 17 00:00:00 2001 From: Zhigao Tong Date: Wed, 31 Aug 2022 20:54:31 +0800 Subject: [PATCH 4/4] Update tests/fullstack-test/expr/from_unixtime.test --- tests/fullstack-test/expr/from_unixtime.test | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/fullstack-test/expr/from_unixtime.test b/tests/fullstack-test/expr/from_unixtime.test index 468dbd2d578..fdc65a208cd 100644 --- a/tests/fullstack-test/expr/from_unixtime.test +++ b/tests/fullstack-test/expr/from_unixtime.test @@ -20,6 +20,6 @@ func> wait_table test t # This will throw error for now, but before https://github.com/pingcap/tics/issues/1433 fixed, it will cause segmentation fault mysql> set session tidb_isolation_read_engines='tiflash'; select * from test.t where FROM_UNIXTIME(1447430881, a) -{#REGEXP}.*Argument at index 1 for function fromUnixTime must be constant.* +ERROR 1105 (HY000) at line 1: Argument at index 1 for function fromUnixTime must be constant mysql> drop table if exists test.t