From 4c8779388fd2f25730a1b044f9f10d6717833fce Mon Sep 17 00:00:00 2001 From: Alexander Richardson Date: Thu, 24 Oct 2024 14:09:05 -0700 Subject: [PATCH] sanitizer_allocator.cpp: Ensure at least sizeof(void*) alignment Some platforms (e.g. 64-bit CHERI) have stronger alignment requirements on values returned from allocators. For all other platforms this does not result in any functional change. Reviewed By: cjappl, vitalybuka Pull Request: https://github.com/llvm/llvm-project/pull/84440 --- compiler-rt/lib/sanitizer_common/sanitizer_allocator.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_allocator.cpp b/compiler-rt/lib/sanitizer_common/sanitizer_allocator.cpp index 1d5058c81acb..9d899371c2dd 100644 --- a/compiler-rt/lib/sanitizer_common/sanitizer_allocator.cpp +++ b/compiler-rt/lib/sanitizer_common/sanitizer_allocator.cpp @@ -59,7 +59,7 @@ static void *RawInternalAlloc(uptr size, InternalAllocatorCache *cache, static void *RawInternalRealloc(void *ptr, uptr size, InternalAllocatorCache *cache) { - uptr alignment = 8; + constexpr usize alignment = Max(8, sizeof(void *)); if (cache == 0) { SpinMutexLock l(&internal_allocator_cache_mu); return internal_allocator()->Reallocate(&internal_allocator_cache, ptr, @@ -137,7 +137,8 @@ void InternalAllocatorUnlock() SANITIZER_NO_THREAD_SAFETY_ANALYSIS { } // LowLevelAllocator -constexpr uptr kLowLevelAllocatorDefaultAlignment = 8; +constexpr usize kLowLevelAllocatorDefaultAlignment = + Max(8, sizeof(void *)); constexpr uptr kMinNumPagesRounded = 16; constexpr uptr kMinRoundedSize = 65536; static uptr low_level_alloc_min_alignment = kLowLevelAllocatorDefaultAlignment;