Skip to content

Commit 8d8cc11

Browse files
committed
[libc] Enable wide-read memory operations by default on Linux
Summary: This patch changes the linux build to use the wide reads on the memory operations by default. These memory functions will now potentially read outside of the bounds explicitly allowed by the current function. While technically undefined behavior in the standard, plenty of C library implementations do this. it will not cause a segmentation fault on linux as long as you do not cross a page boundary, and because we are only *reading* memory it should not have atomic effects.
1 parent 8d7b50e commit 8d8cc11

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

libc/config/linux/config.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"string": {
3+
"LIBC_CONF_STRING_UNSAFE_WIDE_READ": {
4+
"value": true
5+
}
6+
}
7+
}

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,15 @@
1717
namespace LIBC_NAMESPACE_DECL {
1818

1919
namespace neon {
20-
[[maybe_unused]] LIBC_INLINE size_t string_length(const char *src) {
20+
[[maybe_unused]] LIBC_INLINE static size_t string_length(const char *src) {
2121
using Vector __attribute__((may_alias)) = uint8x8_t;
2222

2323
uintptr_t misalign_bytes = reinterpret_cast<uintptr_t>(src) % sizeof(Vector);
24-
Vector *block_ptr = reinterpret_cast<Vector *>(src - misalign_bytes);
24+
const Vector *block_ptr =
25+
reinterpret_cast<const Vector *>(src - misalign_bytes);
2526
Vector v = *block_ptr;
2627
Vector vcmp = vceqz_u8(v);
27-
uint64x1_t cmp_mask = vreinterpret_u64_s8(vcmp);
28+
uint64x1_t cmp_mask = vreinterpret_u64_u8(vcmp);
2829
uint64_t cmp = vget_lane_u64(cmp_mask, 0);
2930
cmp = cmp >> (misalign_bytes << 3);
3031
if (cmp)
@@ -34,7 +35,7 @@ namespace neon {
3435
++block_ptr;
3536
v = *block_ptr;
3637
vcmp = vceqz_u8(v);
37-
cmp_mask = vreinterpret_u64_s8(vcmp);
38+
cmp_mask = vreinterpret_u64_u8(vcmp);
3839
cmp = vget_lane_u64(cmp_mask, 0);
3940
if (cmp)
4041
return static_cast<size_t>(reinterpret_cast<uintptr_t>(block_ptr) -

0 commit comments

Comments
 (0)