[libc] Maintain proper alignment for the hermetic tests malloc
authorJoseph Huber <jhuber6@vols.utk.edu>
Thu, 4 May 2023 16:22:38 +0000 (11:22 -0500)
committerJoseph Huber <jhuber6@vols.utk.edu>
Thu, 4 May 2023 18:22:20 +0000 (13:22 -0500)
We use a bump pointer to implement malloc for the hermetic tests.
Currently, we bump the pointer up by any amount. This means that calling
`malloc(1)` will misalign the buffer so any following `malloc(8)`
accesses will not be aligned. This causes problems in architectures
which require alignment.

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D149863

libc/test/UnitTest/HermeticTestUtils.cpp
libc/test/src/__support/CMakeLists.txt

index 32e0044..c8279e5 100644 (file)
@@ -61,7 +61,11 @@ 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 __llvm_libc::atexit(func); }
 
+constexpr uint64_t ALIGNMENT = alignof(uintptr_t);
+
 void *malloc(size_t s) {
+  // Keep the bump pointer aligned on an eight byte boundary.
+  s = ((s + ALIGNMENT - 1) / ALIGNMENT) * ALIGNMENT;
   void *mem = ptr;
   ptr += s;
   return mem;
index f0192c2..7390ed2 100644 (file)
@@ -1,17 +1,14 @@
 add_custom_target(libc-support-tests)
 
-# This test fails with a misaigned address on NVPTX.
-if(NOT LIBC_GPU_TARGET_ARCHITECTURE_IS_NVPTX)
-  add_libc_test(
-    blockstore_test
-    SUITE
-      libc-support-tests
-    SRCS
-      blockstore_test.cpp
-    DEPENDS
-      libc.src.__support.blockstore
-  )
-endif()
+add_libc_test(
+  blockstore_test
+  SUITE
+    libc-support-tests
+  SRCS
+    blockstore_test.cpp
+  DEPENDS
+    libc.src.__support.blockstore
+)
 
 add_libc_test(
   endian_test