Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

*: fix asan false positive case #5742

Merged
merged 7 commits into from
Aug 31, 2022
Merged
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
15 changes: 11 additions & 4 deletions libs/libcommon/include/common/avx2_strstr.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@

#include <common/avx2_mem_utils.h>

#include <cassert>
#include <cstddef>
#include <cstdint>

namespace mem_utils::details
{

Expand Down Expand Up @@ -135,6 +131,9 @@ ALWAYS_INLINE static inline const char * avx2_strstr_impl(const char * src, cons
// align to 32
src = reinterpret_cast<decltype(src)>(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;

Expand Down Expand Up @@ -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;
}
Expand All @@ -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<const char *>(std::memchr(src, target, n)); // memchr@plt
#endif

if (unlikely(n < 1))
{
return nullptr;
Expand Down
2 changes: 1 addition & 1 deletion libs/libcommon/include/common/mem_utils_opt.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down