Skip to content

Commit

Permalink
Fix asan false positive case
Browse files Browse the repository at this point in the history
  • Loading branch information
solotzg committed Aug 31, 2022
1 parent 79b36c3 commit 3f52c37
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
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

0 comments on commit 3f52c37

Please sign in to comment.