Skip to content

buffer bound handling in jemalloc stats callback to avoid unintended growth#19334

Open
jmestwa-coder wants to merge 1 commit into
aptos-labs:mainfrom
jmestwa-coder:malloc-callback-bound-check
Open

buffer bound handling in jemalloc stats callback to avoid unintended growth#19334
jmestwa-coder wants to merge 1 commit into
aptos-labs:mainfrom
jmestwa-coder:malloc-callback-bound-check

Conversation

@jmestwa-coder
Copy link
Copy Markdown
Contributor

@jmestwa-coder jmestwa-coder commented Apr 4, 2026

Description

Fixes incorrect bound handling in the jemalloc stats callback (write_cb).

The callback previously used the buffer’s total capacity instead of the remaining space (capacity - len). This allowed the buffer to grow across multiple invocations, exceeding the intended limit and potentially triggering reallocation inside the callback, which is explicitly undesirable in this context.

This change ensures that each write is bounded by the remaining capacity. Once the buffer is full, the callback returns early and performs no further writes.


How Has This Been Tested?

  • Added unit tests to verify:
    • Buffer length never exceeds its capacity across multiple callback invocations
    • No capacity growth occurs after the buffer is full
    • Incoming data is correctly truncated to remaining space
    • Zero-capacity edge case behaves as expected
  • Verified behavior is deterministic and matches intended bounded-write semantics

Key Areas to Review

  • write_cb logic:
    • Uses capacity - len (via saturating_sub) to compute remaining space
    • Returns early when no space is left
    • Copies only the allowed number of bytes per invocation
  • Safety:
    • No changes to unsafe pointer usage or callback interface
    • No new allocations introduced in the callback path

Type of Change

  • Bug fix

Which Components or Systems Does This Change Impact?

  • Other (Admin Service)

Checklist

  • I have read and followed the CONTRIBUTING doc
  • I have performed a self-review of my own code
  • I tested both happy and unhappy path of the functionality

Note

Low Risk
Low risk bug fix limited to the jemalloc stats output callback; it only changes how bytes are appended to a preallocated buffer to avoid exceeding the intended max length or reallocating in the callback.

Overview
Fixes the jemalloc malloc_stats_print write callback to respect remaining buffer capacity (capacity - len) instead of total capacity.

When the stats buffer is full, the callback now returns early and truncates each write to the remaining space, preventing unintended growth/reallocation during stats collection.

Reviewed by Cursor Bugbot for commit f28eb96. Bugbot is set up for automated code reviews on this repo. Configure here.

@jmestwa-coder jmestwa-coder force-pushed the malloc-callback-bound-check branch from 577930e to 2afb2d9 Compare April 27, 2026 16:26
@jmestwa-coder jmestwa-coder force-pushed the malloc-callback-bound-check branch from 2afb2d9 to 0f22457 Compare May 7, 2026 19:06
@jmestwa-coder
Copy link
Copy Markdown
Contributor Author

kindly review this PR. Thank you!

@jmestwa-coder jmestwa-coder force-pushed the malloc-callback-bound-check branch 3 times, most recently from e23b1fe to 2804036 Compare May 11, 2026 13:50
@jmestwa-coder jmestwa-coder force-pushed the malloc-callback-bound-check branch from 2804036 to f28eb96 Compare May 15, 2026 19:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant