Skip to content

sanitizer_allocator.cpp: Ensure at least sizeof(void*) alignment #84440

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

arichardson
Copy link
Member

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.

Created using spr 1.3.6-beta.1
@llvmbot
Copy link
Member

llvmbot commented Mar 8, 2024

@llvm/pr-subscribers-compiler-rt-sanitizer

Author: Alexander Richardson (arichardson)

Changes

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.


Full diff: https://github.com/llvm/llvm-project/pull/84440.diff

1 Files Affected:

  • (modified) compiler-rt/lib/sanitizer_common/sanitizer_allocator.cpp (+3-2)
diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_allocator.cpp b/compiler-rt/lib/sanitizer_common/sanitizer_allocator.cpp
index 0513ae36fbc721..d7f8acd819ade2 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<usize>(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<usize>(8, sizeof(void *));
 constexpr uptr kMinNumPagesRounded = 16;
 constexpr uptr kMinRoundedSize = 65536;
 static uptr low_level_alloc_min_alignment = kLowLevelAllocatorDefaultAlignment;

@arichardson arichardson marked this pull request as draft March 8, 2024 07:38
@cjappl cjappl changed the title sanitizer_allocator.cpp: Ensure at least sizeof(void*) alignment [sanitizer-common] Ensure at least sizeof(void*) alignment in sanitizer allocator Oct 22, 2024
@cjappl
Copy link
Contributor

cjappl commented Oct 22, 2024

This change seems reasonable to me. If you want to rebase on latest and ensure tests are happy I can merge it

@arichardson
Copy link
Member Author

This change seems reasonable to me. If you want to rebase on latest and ensure tests are happy I can merge it

Thanks for the review - I will rebase and merge this tomorrow.

Created using spr 1.3.6-beta.1
@arichardson arichardson changed the title [sanitizer-common] Ensure at least sizeof(void*) alignment in sanitizer allocator sanitizer_allocator.cpp: Ensure at least sizeof(void*) alignment Oct 24, 2024
@arichardson arichardson marked this pull request as ready for review October 24, 2024 19:09
@arichardson arichardson merged commit 4c87793 into main Oct 24, 2024
9 checks passed
@arichardson arichardson deleted the users/arichardson/spr/sanitizer_allocatorcpp-ensure-at-least-sizeofvoid-alignment branch October 24, 2024 21:09
NoumanAmir657 pushed a commit to NoumanAmir657/llvm-project that referenced this pull request Nov 4, 2024
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: llvm#84440
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants