[libc-commits] [libc] [libc] Fix aligned_alloc used in hermetic tests. (PR #213041)

Alexey Samsonov via libc-commits libc-commits at lists.llvm.org
Thu Jul 30 08:19:43 PDT 2026


https://github.com/vonosmas created https://github.com/llvm/llvm-project/pull/213041

Make sure to use provided alignment both for the pointer to the returned memory chunk and the size.

This fixes occasional segfaults in `hsearch` tests after ce62aab97e58c52e6bb0972c49d6fd1aaa6a08a0, since
hash-table bitmask implementation uses SSE2 intrinsics with 16-byte alignment.

>From a86194f669d5c456246956976f3e8f54cf2fac2a Mon Sep 17 00:00:00 2001
From: Alexey Samsonov <vonosmas at gmail.com>
Date: Thu, 30 Jul 2026 15:15:56 +0000
Subject: [PATCH] [libc] Fix aligned_alloc used in hermetic tests.

---
 libc/test/UnitTest/HermeticTestUtils.cpp | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/libc/test/UnitTest/HermeticTestUtils.cpp b/libc/test/UnitTest/HermeticTestUtils.cpp
index bcc42bed98eb5..05e2bbdde2603 100644
--- a/libc/test/UnitTest/HermeticTestUtils.cpp
+++ b/libc/test/UnitTest/HermeticTestUtils.cpp
@@ -73,6 +73,9 @@ int atexit(void (*func)(void)) { return LIBC_NAMESPACE::atexit(func); }
 void *aligned_alloc(size_t align, size_t s) {
   if (align & (align - 1)) // Must be power of 2
     return nullptr;
+  uintptr_t ptr_val = reinterpret_cast<uintptr_t>(ptr);
+  uintptr_t aligned_ptr_val = ((ptr_val + align - 1) / align) * align;
+  ptr = reinterpret_cast<uint8_t *>(aligned_ptr_val);
   s = ((s + align - 1) / align) * align;
   void *mem = ptr;
   ptr += s;



More information about the libc-commits mailing list