Skip to content

fix(memtrack): track successful posix_memalign allocations#470

Open
not-matthias wants to merge 2 commits into
cod-3231-support-disabling-allocator-trackingfrom
cod-3238-memtrack-drops-all-successful-posix_memalign-allocations
Open

fix(memtrack): track successful posix_memalign allocations#470
not-matthias wants to merge 2 commits into
cod-3231-support-disabling-allocator-trackingfrom
cod-3238-memtrack-drops-all-successful-posix_memalign-allocations

Conversation

@not-matthias

Copy link
Copy Markdown
Member

Problem

posix_memalign was attached to the shared memalign uprobe handler (UPROBE_ARG_RET), which assumes the standard allocator ABI. But posix_memalign(void** memptr, size_t alignment, size_t size) is inverted:

  • Returns int 0 on success (nonzero errno on failure), not a pointer.
  • Delivers the allocated pointer through the memptr out-parameter, not the return register.
  • Size lives in PARM3 (PARM2 is the alignment).

The shared handler gated success on a non-NULL return and used the return register as the address. Since successful posix_memalign returns 0, every successful allocation was treated as a failure and silently dropped.

Fix

Dedicated uprobe/uretprobe pair for posix_memalign:

  • uprobe stashes memptr (PARM1) and size (PARM3) keyed by tid.
  • uretprobe gates on RC == 0, reads the address back from *memptr via bpf_probe_read_user, then submits an aligned-alloc event.

Test

Added posix_memalign_test to the existing rstest table in tests/c_tests.rs with a fixture + snapshot confirming the allocation is now tracked.

@not-matthias
not-matthias marked this pull request as ready for review July 24, 2026 11:58
@greptile-apps

greptile-apps Bot commented Jul 24, 2026

Copy link
Copy Markdown

Greptile Summary

Adds dedicated posix_memalign allocation tracking and expands memtrack with per-process RSS lifecycle accounting.

  • Captures successful posix_memalign allocations through its out-parameter while dropping failed calls.
  • Adds RSS, reverse-map, fork, exec, and exit events with userspace parsing and state reconstruction.
  • Introduces optional allocator tracking, kernel compatibility handling, and expanded x86_64/ARM integration coverage.
  • Updates the shared memtrack artifact representation and adds allocation and RSS fixtures and snapshots.

Confidence Score: 5/5

The PR appears safe to merge because no eligible blocking failures remain.

No blocking failure remains.

Important Files Changed

Filename Overview
crates/memtrack/src/ebpf/c/allocator.h Adds dedicated entry and return probes that capture the posix_memalign out-parameter and size according to its ABI.
crates/memtrack/src/ebpf/memtrack/allocator.rs Routes discovered posix_memalign symbols to the dedicated probe pair.
crates/memtrack/src/ebpf/c/rss.bpf.h Adds RSS and reverse-map collection with process ownership and lifecycle tracking.
crates/memtrack/src/ebpf/events.rs Parses the new RSS, reverse-map, fork, exec, and exit event variants.
crates/memtrack/src/ebpf/tracker.rs Supports trackers with optional allocator probes and optional reverse-map hooks.
crates/runner-shared/src/artifacts/memtrack/mod.rs Extends the shared memtrack event model for RSS and process lifecycle data.
crates/memtrack/tests/c_tests.rs Adds successful and failing posix_memalign integration coverage.
crates/memtrack/tests/rss_tests.rs Adds integration coverage for RSS reconstruction across mapping and process lifecycle scenarios.
.github/workflows/ci.yml Runs memtrack test shards, including RSS tests, on x86_64 and ARM Ubuntu runners.

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart LR
    A[Allocator and kernel probes] --> B[eBPF event ring buffer]
    B --> C[Rust event parser]
    C --> D[Memtrack artifact encoder]
    D --> E[Allocation and per-process RSS results]
    F[IPC enable or disable] --> A
    G[Fork, exec, and exit hooks] --> B
Loading

Reviews (2): Last reviewed commit: "test(memtrack): assert failing posix_mem..." | Re-trigger Greptile

@codspeed-hq

codspeed-hq Bot commented Jul 24, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

⚠️ Unknown Walltime execution environment detected

Using the Walltime instrument on standard Hosted Runners will lead to inconsistent data.

For the most accurate results, we recommend using CodSpeed Macro Runners: bare-metal machines fine-tuned for performance measurement consistency.

✅ 17 untouched benchmarks


Comparing cod-3238-memtrack-drops-all-successful-posix_memalign-allocations (b9aeda1) with cod-3231-support-disabling-allocator-tracking (131f8a3)

Open in CodSpeed

posix_memalign was attached to the shared memalign uprobe handler, but
its ABI is inverted relative to memalign/aligned_alloc: it returns int 0
on SUCCESS (nonzero errno on failure) and delivers the pointer through
the memptr out-parameter rather than the return register. The shared
handler skipped any call returning 0 (treating a NULL malloc-style
return as failure), so every successful posix_memalign call was dropped,
undercounting the reported heap footprint. It also read the size from
PARM2 (alignment) instead of PARM3.

Add a dedicated posix_memalign uprobe/uretprobe pair that captures the
memptr out-parameter (PARM1) and size (PARM3) on entry, keeps calls
returning 0, and reads the allocated pointer back from *memptr via
bpf_probe_read_user on return.

Fold the regression case into the existing c_tests rstest table
(posix_memalign_test) instead of a standalone test file; the snapshot
asserts AlignedAlloc { size: 768 } followed by Free.
Add a posix_memalign_einval case to the c_tests rstest table. The
fixture calls posix_memalign with alignment 3 (not a power-of-two
multiple of sizeof(void*)), which returns EINVAL and allocates nothing,
then does a real malloc/free. The snapshot asserts only Malloc/Free,
proving the uretprobe drops the failing call (ret != 0) while tracking
stays live for subsequent allocations.
@not-matthias
not-matthias force-pushed the cod-3238-memtrack-drops-all-successful-posix_memalign-allocations branch from 8071d34 to b9aeda1 Compare July 24, 2026 12:08
@not-matthias
not-matthias changed the base branch from main to cod-3231-support-disabling-allocator-tracking July 24, 2026 12:08
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