Skip to content

[VL] Fix StdMemoryAllocator::allocateZeroFilled byte accounting#11855

Open
LuciferYang wants to merge 3 commits intoapache:mainfrom
LuciferYang:fix-std-memory-allocator-zerofilled-accounting
Open

[VL] Fix StdMemoryAllocator::allocateZeroFilled byte accounting#11855
LuciferYang wants to merge 3 commits intoapache:mainfrom
LuciferYang:fix-std-memory-allocator-zerofilled-accounting

Conversation

@LuciferYang
Copy link
Copy Markdown

What changes are proposed in this pull request?

Fix incorrect byte accounting in StdMemoryAllocator::allocateZeroFilled. The method calls std::calloc(nmemb, size) which allocates nmemb * size bytes, but bytes_ was only incremented by size instead of nmemb * size.

This is consistent with ListenableMemoryAllocator::allocateZeroFilled, which already correctly tracks size * nmemb.

How was this patch tested?

Added a unit test StdMemoryAllocator.allocateZeroFilledAccounting that verifies allocateZeroFilled(10, 64, &buf) correctly tracks 640 bytes.

Was this patch authored or co-authored using generative AI tooling?

Generated-by: Claude Code

…led fix

Keep only the test that directly validates the nmemb * size accounting bug;
remove three unrelated general-purpose allocator tests.
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Fixes incorrect memory-usage accounting in StdMemoryAllocator::allocateZeroFilled by tracking nmemb * size bytes (matching the actual calloc allocation size), and adds a unit test to prevent regression.

Changes:

  • Correct StdMemoryAllocator::allocateZeroFilled byte tracking from size to nmemb * size.
  • Add StdMemoryAllocator.allocateZeroFilledAccounting unit test validating the accounting behavior.
  • Register the new test in cpp/core/tests/CMakeLists.txt.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
cpp/core/memory/MemoryAllocator.cc Fixes allocateZeroFilled byte accounting to reflect the true allocation size.
cpp/core/tests/MemoryAllocatorTest.cc Adds a regression test for allocateZeroFilled byte accounting.
cpp/core/tests/CMakeLists.txt Adds the new memory allocator test target to the CMake test list.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 136 to +140
*out = std::calloc(nmemb, size);
if (*out == nullptr) {
return false;
}
bytes_ += size;
bytes_ += nmemb * size;
Copy link

Copilot AI Mar 31, 2026

Choose a reason for hiding this comment

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

nmemb * size is computed in int64_t, which can overflow and cause undefined behavior (and incorrect accounting) for large allocations even if calloc succeeds. Consider computing the total bytes using checked multiplication (e.g., via __int128/overflow check) and GLUTEN_CHECK that it fits in int64_t before updating bytes_.

Copilot uses AI. Check for mistakes.
Comment on lines +28 to +34
void* buf = nullptr;
bool ok = allocator.allocateZeroFilled(10, 64, &buf);
ASSERT_TRUE(ok);
ASSERT_NE(buf, nullptr);
ASSERT_EQ(allocator.getBytes(), 640);

allocator.free(buf, 640);
Copy link

Copilot AI Mar 31, 2026

Choose a reason for hiding this comment

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

The test hard-codes 640 in multiple places. To avoid drift if the inputs change, compute an expectedBytes = 10 * 64 once and use it for the accounting assertion and for free(...) (and assert that free returns true to validate the API contract).

Suggested change
void* buf = nullptr;
bool ok = allocator.allocateZeroFilled(10, 64, &buf);
ASSERT_TRUE(ok);
ASSERT_NE(buf, nullptr);
ASSERT_EQ(allocator.getBytes(), 640);
allocator.free(buf, 640);
const size_t expectedBytes = 10 * 64;
void* buf = nullptr;
bool ok = allocator.allocateZeroFilled(10, 64, &buf);
ASSERT_TRUE(ok);
ASSERT_NE(buf, nullptr);
ASSERT_EQ(allocator.getBytes(), expectedBytes);
ASSERT_TRUE(allocator.free(buf, expectedBytes));

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants