[libc-commits] [libc] [libc] Align hermetic test allocations to max_align_t (PR #239984)

Mike Lothian via libc-commits libc-commits at lists.llvm.org
Sun Nov 8 19:43:53 PST 2026


https://github.com/FireBurn updated https://github.com/llvm/llvm-project/pull/239984

>From 603480851f95484adf1173a587688c7fd43e5bfc Mon Sep 17 00:00:00 2001
From: Mike Lothian <mike at fireburn.co.uk>
Date: Sun, 11 Oct 2026 11:22:27 +0100
Subject: [PATCH] [libc] Align hermetic test allocations to max_align_t

The hermetic allocator handed out blocks aligned to a pointer. The compiler
may assume operator new returns memory aligned for the largest scalar type,
so an object zeroed with aligned vector stores faulted: dir_scan_impl_test
crashed in ReadFailsAtStart.

A new test allocates small blocks one after another and checks each is
aligned to the largest scalar. It fails with the old alignment, reporting
a block 8 bytes off a boundary, and passes with this change.

Assisted-by: Claude
---
 libc/test/UnitTest/HermeticTestUtils.cpp      |  2 +-
 libc/test/src/__support/CMakeLists.txt        | 11 +++++++
 .../test/src/__support/new_alignment_test.cpp | 30 +++++++++++++++++++
 3 files changed, 42 insertions(+), 1 deletion(-)
 create mode 100644 libc/test/src/__support/new_alignment_test.cpp

diff --git a/libc/test/UnitTest/HermeticTestUtils.cpp b/libc/test/UnitTest/HermeticTestUtils.cpp
index 3d9cb6d2cbf1e0..eb447fad13938b 100644
--- a/libc/test/UnitTest/HermeticTestUtils.cpp
+++ b/libc/test/UnitTest/HermeticTestUtils.cpp
@@ -30,7 +30,7 @@ int atexit(void (*func)(void));
 
 } // namespace LIBC_NAMESPACE_DECL
 
-constexpr uint64_t ALIGNMENT = alignof(uintptr_t);
+constexpr uint64_t ALIGNMENT = alignof(max_align_t);
 
 namespace {
 
diff --git a/libc/test/src/__support/CMakeLists.txt b/libc/test/src/__support/CMakeLists.txt
index c39aaf11f1725c..ab8220bc034fe1 100644
--- a/libc/test/src/__support/CMakeLists.txt
+++ b/libc/test/src/__support/CMakeLists.txt
@@ -302,6 +302,17 @@ add_libc_test(
     libc.src.string.memset
 )
 
+add_libc_test(
+  new_alignment_test
+  SUITE
+    libc-support-tests
+  SRCS
+    new_alignment_test.cpp
+  DEPENDS
+    libc.src.__support.CPP.new
+    libc.src.__support.alloc_checker
+)
+
 add_libc_test(
   memory_size_test
   SUITE
diff --git a/libc/test/src/__support/new_alignment_test.cpp b/libc/test/src/__support/new_alignment_test.cpp
new file mode 100644
index 00000000000000..9e380e6e0cb4bb
--- /dev/null
+++ b/libc/test/src/__support/new_alignment_test.cpp
@@ -0,0 +1,30 @@
+//===-- Unittests for the alignment of memory from new --------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/__support/CPP/new.h"
+#include "src/__support/alloc-checker.h"
+#include "test/UnitTest/Test.h"
+
+#include <stdint.h>
+
+using LIBC_NAMESPACE::AllocChecker;
+
+// The compiler may assume that memory from new is aligned for the largest
+// scalar type and zero it with aligned vector stores. The allocator the
+// hermetic tests link, which hands out consecutive blocks, has to honour that,
+// so a block that follows a small one must not land on an 8 byte boundary.
+TEST(LlvmLibcNewAlignmentTest, BlocksFollowingSmallOnesAreAligned) {
+  constexpr uintptr_t ALIGNMENT = __BIGGEST_ALIGNMENT__;
+  AllocChecker ac;
+  for (int i = 0; i < 4; ++i) {
+    uint64_t *block = new (ac) uint64_t;
+    ASSERT_TRUE(static_cast<bool>(ac));
+    EXPECT_EQ(reinterpret_cast<uintptr_t>(block) % ALIGNMENT, uintptr_t(0));
+    delete block;
+  }
+}



More information about the libc-commits mailing list