[libcxx-commits] [PATCH] D123511: [libc++abi] Fix fallback allocator alignment issue

Mikhail Maltsev via Phabricator via libcxx-commits libcxx-commits at lists.llvm.org
Mon Apr 11 06:57:35 PDT 2022


miyuki created this revision.
miyuki added a reviewer: ldionne.
Herald added a subscriber: kristof.beyls.
Herald added a project: All.
miyuki requested review of this revision.
Herald added a project: libc++abi.
Herald added a subscriber: libcxx-commits.
Herald added a reviewer: libc++abi.

Currently, the libc++abi fallback memory allocator only guarantees
4-byte alignment even on 64-bit systems. This causes failures
(hardware exceptions due to misalignment) on bare-metal AArch64.

      

The patch fixes the issue by increasing the heap block size to 8
bytes on 64-bit systems.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D123511

Files:
  libcxxabi/src/fallback_malloc.cpp
  libcxxabi/test/test_fallback_malloc.pass.cpp


Index: libcxxabi/test/test_fallback_malloc.pass.cpp
===================================================================
--- libcxxabi/test/test_fallback_malloc.pass.cpp
+++ libcxxabi/test/test_fallback_malloc.pass.cpp
@@ -6,6 +6,7 @@
 //
 //===----------------------------------------------------------------------===//
 
+#include <cassert>
 #include <cstdio>
 #include <deque>
 
@@ -179,6 +180,8 @@
     std::printf("fallback_malloc ( 32 ) --> %lu\n", (unsigned long) (p - heap));
     if ( !is_fallback_ptr ( p ))
         std::printf("### p is not a fallback pointer!!\n");
+    assert(((intptr_t)p % sizeof(void *) == 0)
+           && "p must be aligned as sizeof(void*)");
 
     print_free_list ();
     fallback_free ( p );
Index: libcxxabi/src/fallback_malloc.cpp
===================================================================
--- libcxxabi/src/fallback_malloc.cpp
+++ libcxxabi/src/fallback_malloc.cpp
@@ -60,8 +60,21 @@
 static const size_t HEAP_SIZE = 512;
 char heap[HEAP_SIZE] __attribute__((aligned));
 
-typedef unsigned short heap_offset;
-typedef unsigned short heap_size;
+template<size_t S>
+struct half_size_uint;
+
+template<>
+struct half_size_uint<8> {
+  typedef unsigned int type;
+};
+
+template<>
+struct half_size_uint<4> {
+  typedef unsigned short type;
+};
+
+typedef half_size_uint<sizeof(void*)>::type heap_offset;
+typedef half_size_uint<sizeof(void*)>::type heap_size;
 
 struct heap_node {
   heap_offset next_node; // offset into heap


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D123511.421912.patch
Type: text/x-patch
Size: 1479 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libcxx-commits/attachments/20220411/2b39e053/attachment.bin>


More information about the libcxx-commits mailing list