Skip to content

Add mremap and shmat/shmdt allocation tracking#556

Open
r1viollet wants to merge 1 commit into
mainfrom
r1viollet/add-mremap-shmat-instrumentation
Open

Add mremap and shmat/shmdt allocation tracking#556
r1viollet wants to merge 1 commit into
mainfrom
r1viollet/add-mremap-shmat-instrumentation

Conversation

@r1viollet

Copy link
Copy Markdown
Collaborator

What

Adds allocation tracking hooks for mremap() and System V shared memory (shmat()/shmdt()).

Why

These APIs can allocate or deallocate memory without going through malloc/mmap, leading to under-reporting in allocation profiles. Notable examples:

  • mremap() is used to grow large allocations (e.g., df-executor mmaps)
  • shmat()/shmdt() are used by databases (PostgreSQL, Oracle) for System V shared memory

Changes

Instrumentation added (src/lib/symbol_overrides.cc):

  • mremap() - tracks old region as dealloc + new region as alloc
  • shmat() - queries segment size via shmctl(IPC_STAT) and tracks as alloc
  • shmdt() - tracks as dealloc

Not instrumented (with comment explaining why):

  • brk()/sbrk() - manipulate the program break, not individual allocations. Would double-count if malloc uses them internally, and have wrong semantics (heap boundary vs allocated objects).

Tests added (test/allocation_tracker-ut.cc):

  • mremap test via test_realloc pattern
  • shmat/shmdt test with IPC_PRIVATE segment

All tests gated by weak symbol checks.

Verification

Unit tests pass for the new hooks. Full CI validation pending.

Extends allocation tracking to cover memory allocation APIs that were
previously missing:

1. mremap() - Remap/resize existing mmap regions
   - Tracks old region as deallocation + new region as allocation
   - Commonly used by allocators to grow large allocations (e.g., df-executor)

2. shmat()/shmdt() - System V shared memory attach/detach
   - Queries segment size via shmctl(IPC_STAT) on attach
   - Commonly used by databases (PostgreSQL, Oracle) and legacy IPC

These APIs can bypass malloc/mmap hooks when called directly, leading to
under-reporting of memory usage in allocation profiles.

brk()/sbrk() are explicitly not instrumented (with comment explaining why):
they manipulate the program break (heap boundary), not individual allocations,
and would double-count if malloc uses them internally.

Tests added in allocation_tracker-ut.cc for both mremap and shmat/shmdt.
@r1viollet r1viollet marked this pull request as ready for review June 23, 2026 08:18
@r1viollet r1viollet requested a review from nsavoire as a code owner June 23, 2026 08:18
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.

2 participants