Skip to content

[flang] Fix excess allocation #96663

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

Merged
merged 1 commit into from
Jun 28, 2024
Merged

[flang] Fix excess allocation #96663

merged 1 commit into from
Jun 28, 2024

Conversation

klausler
Copy link
Contributor

A recent patch introduced an error in an aligned byte size calculation that causes an extra word to be allocated when the original byte size is already aligned (including the case of zero). Fix.

A recent patch introduced an error in an aligned byte size calculation
that causes an extra word to be allocated when the original byte size
is already aligned (including the case of zero).  Fix.
@klausler klausler requested a review from vzakhari June 25, 2024 16:29
@llvmbot llvmbot added flang:runtime flang Flang issues not falling into any other category labels Jun 25, 2024
@llvmbot
Copy link
Member

llvmbot commented Jun 25, 2024

@llvm/pr-subscribers-flang-runtime

Author: Peter Klausler (klausler)

Changes

A recent patch introduced an error in an aligned byte size calculation that causes an extra word to be allocated when the original byte size is already aligned (including the case of zero). Fix.


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

1 Files Affected:

  • (modified) flang/runtime/pointer.cpp (+2-2)
diff --git a/flang/runtime/pointer.cpp b/flang/runtime/pointer.cpp
index aeed879f1a2e2..2979181ddd61b 100644
--- a/flang/runtime/pointer.cpp
+++ b/flang/runtime/pointer.cpp
@@ -127,7 +127,7 @@ void RTDEF(PointerAssociateRemapping)(Descriptor &pointer,
 RT_API_ATTRS void *AllocateValidatedPointerPayload(std::size_t byteSize) {
   // Add space for a footer to validate during deallocation.
   constexpr std::size_t align{sizeof(std::uintptr_t)};
-  byteSize = ((byteSize / align) + 1) * align;
+  byteSize = ((byteSize + align - 1) / align) * align;
   std::size_t total{byteSize + sizeof(std::uintptr_t)};
   void *p{std::malloc(total)};
   if (p) {
@@ -197,7 +197,7 @@ static RT_API_ATTRS std::size_t GetByteSize(
 bool RT_API_ATTRS ValidatePointerPayload(const ISO::CFI_cdesc_t &desc) {
   std::size_t byteSize{GetByteSize(desc)};
   constexpr std::size_t align{sizeof(std::uintptr_t)};
-  byteSize = ((byteSize / align) + 1) * align;
+  byteSize = ((byteSize + align - 1) / align) * align;
   const void *p{desc.base_addr};
   const std::uintptr_t *footer{reinterpret_cast<const std::uintptr_t *>(
       static_cast<const char *>(p) + byteSize)};

Copy link
Contributor

@vzakhari vzakhari left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@klausler klausler merged commit 084d943 into llvm:main Jun 28, 2024
10 checks passed
@klausler klausler deleted the align branch June 28, 2024 18:32
lravenclaw pushed a commit to lravenclaw/llvm-project that referenced this pull request Jul 3, 2024
A recent patch introduced an error in an aligned byte size calculation
that causes an extra word to be allocated when the original byte size is
already aligned (including the case of zero). Fix.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
flang:runtime flang Flang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants