fix(memtrack): track successful posix_memalign allocations#470
Conversation
Greptile SummaryIntroduces ABI-correct tracking for successful
Confidence Score: 5/5The PR appears safe to merge. No blocking failures remain.
|
| Filename | Overview |
|---|---|
| crates/memtrack/src/ebpf/c/allocator.h | Adds dedicated ABI-aware entry and return probes for posix_memalign. |
| crates/memtrack/src/ebpf/memtrack/allocator.rs | Routes discovered posix_memalign symbols to the new dedicated probe pair. |
| crates/memtrack/tests/c_tests.rs | Registers success and invalid-alignment integration cases for allocation tracking. |
| crates/memtrack/testdata/alloc_cpp/src/main.cpp | Extends allocator compatibility coverage with a posix_memalign allocation. |
| crates/memtrack/testdata/posix_memalign_test.c | Exercises successful posix_memalign tracking and cleanup. |
| crates/memtrack/testdata/posix_memalign_einval.c | Verifies failed posix_memalign calls do not emit allocation events. |
Sequence Diagram
sequenceDiagram
participant App as Tracked process
participant Entry as posix_memalign uprobe
participant Allocator as posix_memalign
participant Return as posix_memalign uretprobe
participant Events as Memtrack event stream
App->>Entry: "posix_memalign(&ptr, alignment, size)"
Entry->>Entry: Store memptr and size by TID
Entry->>Allocator: Continue call
Allocator-->>Return: Return status
alt "status == 0"
Return->>App: "Read allocated address from *memptr"
Return->>Events: Submit AlignedAlloc(size, address)
else "status != 0"
Return-->>Return: Drop failed allocation
end
Reviews (5): Last reviewed commit: "test(memtrack): exercise posix_memalign ..." | Re-trigger Greptile
Merging this PR will not alter performance
|
8071d34 to
b9aeda1
Compare
GuillaumeLagrange
left a comment
There was a problem hiding this comment.
LGTM, I'm guessing you've double checked that we do not double dip and cannot count allocations twice because of this change
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.
b9aeda1 to
40b13fb
Compare
The C fixtures only cover glibc, so the posix_memalign path went untested against jemalloc, mimalloc and tcmalloc, which each forward it differently.
b4cb080 to
e56e9ae
Compare
Problem
posix_memalignwas attached to the sharedmemalignuprobe handler (UPROBE_ARG_RET), which assumes the standard allocator ABI. Butposix_memalign(void** memptr, size_t alignment, size_t size)is inverted:int 0on success (nonzero errno on failure), not a pointer.memptrout-parameter, not the return register.PARM3(PARM2is the alignment).The shared handler gated success on a non-NULL return and used the return register as the address. Since successful
posix_memalignreturns0, every successful allocation was treated as a failure and silently dropped.Fix
Dedicated uprobe/uretprobe pair for
posix_memalign:memptr(PARM1) andsize(PARM3) keyed by tid.RC == 0, reads the address back from*memptrviabpf_probe_read_user, then submits an aligned-alloc event.Test
Added
posix_memalign_testto the existing rstest table intests/c_tests.rswith a fixture + snapshot confirming the allocation is now tracked.