Skip to content

Commit f27cf74

Browse files
committed
[libc] Unify and extend no_sanitize attributes for strlen.
Fast strlen implementations (naive wide-reads, SIMD-based, and x86_64/aarch64-optimized versions) all may perform technically-out-of-bound reads, which leads to reports under ASan, HWASan (on ARM machines), and also TSan (which also has the capability to detect heap out-of-bound reads). So, we need to explicitly disable instrumentation in all three cases. Tragically, Clang didn't support `[[gnu::no_sanitize]]` syntax until recently, and since we're supporting both GCC and Clang, we have to revert to `__attribute__` syntax.
1 parent 63e4550 commit f27cf74

File tree

4 files changed

+10
-5
lines changed

4 files changed

+10
-5
lines changed

libc/src/string/memory_utils/aarch64/inline_strlen.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
namespace LIBC_NAMESPACE_DECL {
1818

1919
namespace neon {
20-
[[gnu::no_sanitize_address]] [[maybe_unused]] LIBC_INLINE static size_t
20+
__attribute__((no_sanitize("address", "hwaddress", "thread")))
21+
[[maybe_unused]] LIBC_INLINE static size_t
2122
string_length(const char *src) {
2223
using Vector __attribute__((may_alias)) = uint8x8_t;
2324

libc/src/string/memory_utils/generic/inline_strlen.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ LIBC_INLINE constexpr cpp::simd_mask<char> shift_mask(cpp::simd_mask<char> m,
2424
return cpp::bit_cast<cpp::simd_mask<char>>(r);
2525
}
2626

27-
[[clang::no_sanitize("address")]] LIBC_INLINE size_t
27+
__attribute__((no_sanitize("address", "hwaddress", "thread")))
28+
LIBC_INLINE size_t
2829
string_length(const char *src) {
2930
constexpr cpp::simd<char> null_byte = cpp::splat('\0');
3031

libc/src/string/memory_utils/x86_64/inline_strlen.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,14 @@ namespace LIBC_NAMESPACE_DECL {
1818
namespace string_length_internal {
1919
// Return a bit-mask with the nth bit set if the nth-byte in block_ptr is zero.
2020
template <typename Vector, typename Mask>
21-
[[gnu::no_sanitize_address]] LIBC_INLINE static Mask
21+
__attribute__((no_sanitize("address", "hwaddress", "thread")))
22+
LIBC_INLINE static Mask
2223
compare_and_mask(const Vector *block_ptr);
2324

2425
template <typename Vector, typename Mask,
2526
decltype(compare_and_mask<Vector, Mask>)>
26-
[[gnu::no_sanitize_address]] LIBC_INLINE static size_t
27+
__attribute__((no_sanitize("address", "hwaddress", "thread")))
28+
LIBC_INLINE static size_t
2729
string_length_vector(const char *src) {
2830
uintptr_t misalign_bytes = reinterpret_cast<uintptr_t>(src) % sizeof(Vector);
2931

libc/src/string/string_utils.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,8 @@ template <typename T> LIBC_INLINE size_t string_length(const T *src) {
119119
}
120120

121121
template <typename Word>
122-
[[gnu::no_sanitize_address]] LIBC_INLINE void *
122+
__attribute__((no_sanitize("address", "hwaddress", "thread")))
123+
LIBC_INLINE void *
123124
find_first_character_wide_read(const unsigned char *src, unsigned char ch,
124125
size_t n) {
125126
const unsigned char *char_ptr = src;

0 commit comments

Comments
 (0)