[libc-commits] [libc] [libc] Add aligned_alloc and delete[] to hermetic tests (PR #210693)

Pavel Labath via libc-commits libc-commits at lists.llvm.org
Mon Jul 20 04:41:40 PDT 2026


https://github.com/labath created https://github.com/llvm/llvm-project/pull/210693

This lets us run (at least) <search.h> tests in hermetic mode.

>From 9003216152295d06da4c6bd3a94d293584d33f08 Mon Sep 17 00:00:00 2001
From: Pavel Labath <pavel at labath.sk>
Date: Mon, 20 Jul 2026 11:37:10 +0000
Subject: [PATCH] [libc] Add aligned_alloc and delete[] to hermetic tests

This lets us run (at least) <search.h> tests in hermetic mode.
---
 libc/test/UnitTest/HermeticTestUtils.cpp | 11 ++++++++---
 libc/test/src/search/CMakeLists.txt      | 10 +++++-----
 2 files changed, 13 insertions(+), 8 deletions(-)

diff --git a/libc/test/UnitTest/HermeticTestUtils.cpp b/libc/test/UnitTest/HermeticTestUtils.cpp
index 5ce661b2202c0..d2610cc6027e8 100644
--- a/libc/test/UnitTest/HermeticTestUtils.cpp
+++ b/libc/test/UnitTest/HermeticTestUtils.cpp
@@ -78,14 +78,17 @@ void *memset(void *ptr, int value, size_t count) {
 // This is needed if the test was compiled with '-fno-use-cxa-atexit'.
 int atexit(void (*func)(void)) { return LIBC_NAMESPACE::atexit(func); }
 
-void *malloc(size_t s) {
-  // Keep the bump pointer aligned on an eight byte boundary.
-  s = ((s + ALIGNMENT - 1) / ALIGNMENT) * ALIGNMENT;
+void *aligned_alloc(size_t align, size_t s) {
+  if (align & (align - 1)) // Must be power of 2
+    return nullptr;
+  s = ((s + align - 1) / align) * align;
   void *mem = ptr;
   ptr += s;
   return static_cast<uint64_t>(ptr - memory) >= MEMORY_SIZE ? nullptr : mem;
 }
 
+void *malloc(size_t s) { return aligned_alloc(ALIGNMENT, s); }
+
 void free(void *) {}
 
 void *realloc(void *mem, size_t s) {
@@ -134,6 +137,8 @@ void *operator new[](size_t size) { return malloc(size); }
 
 void operator delete(void *ptr) { free(ptr); }
 
+void operator delete[](void *ptr) { free(ptr); }
+
 void operator delete(void *ptr, [[maybe_unused]] size_t size) { free(ptr); }
 
 // Defining members in the std namespace is not preferred. But, we do it here
diff --git a/libc/test/src/search/CMakeLists.txt b/libc/test/src/search/CMakeLists.txt
index b0eac56ff560a..abcf366bf7f01 100644
--- a/libc/test/src/search/CMakeLists.txt
+++ b/libc/test/src/search/CMakeLists.txt
@@ -1,5 +1,5 @@
 add_custom_target(libc_search_unittests)
-add_libc_unittest(
+add_libc_test(
   hsearch_test
   SUITE
     libc_search_unittests
@@ -15,7 +15,7 @@ add_libc_unittest(
     libc.src.errno.errno
 )
 
-add_libc_unittest(
+add_libc_test(
   insque_test
   SUITE
     libc_search_unittests
@@ -26,7 +26,7 @@ add_libc_unittest(
     libc.src.search.remque
 )
 
-add_libc_unittest(
+add_libc_test(
   lfind_test
   SUITE
     libc_search_unittests
@@ -36,7 +36,7 @@ add_libc_unittest(
     libc.src.search.lfind
 )
 
-add_libc_unittest(
+add_libc_test(
   lsearch_test
   SUITE
     libc_search_unittests
@@ -46,7 +46,7 @@ add_libc_unittest(
     libc.src.search.lsearch
 )
 
-add_libc_unittest(
+add_libc_test(
   tsearch_test
   SUITE
     libc_search_unittests



More information about the libc-commits mailing list