Skip to content

Intercept C23 free_sized() and free_aligned_sized()#912

Open
swaroski wants to merge 2 commits into
bloomberg:mainfrom
swaroski:feat/intercept-c23-free-sized
Open

Intercept C23 free_sized() and free_aligned_sized()#912
swaroski wants to merge 2 commits into
bloomberg:mainfrom
swaroski:feat/intercept-c23-free-sized

Conversation

@swaroski
Copy link
Copy Markdown
Contributor

Closes: #725

Adds hooks for C23 sized deallocators on glibc Linux. Both record as FREE per spec. Weak forward decls + runtime symbol resolution make manylinux
wheels (built on old glibc) work on newer glibc systems.

Test plan

  • New test test_free_sized_extension passes on glibc 2.42+, skips on older
  • Existing extension tests pass (no regression)
  • pre-commit clean

Catch FileNotFoundError/PermissionError in validate_target_file and
convert ImportError raised by runpy.run_module for the script itself
into a MemrayCommandError with a "memray:" prefix, matching the
interpreter's behavior. Suppress the success message when the initial
import fails.

Signed-off-by: Suyog Bhise <bhise.suyog@gmail.com>
@codecov-commenter
Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 66.66667% with 16 lines in your changes missing coverage. Please review.
✅ Project coverage is 92.30%. Comparing base (015e6f0) to head (c759c8e).
⚠️ Report is 3 commits behind head on main.

Files with missing lines Patch % Lines
src/memray/_memray/hooks.cpp 0.00% 8 Missing ⚠️
tests/integration/test_extensions.py 63.15% 7 Missing ⚠️
src/memray/commands/run.py 88.88% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #912      +/-   ##
==========================================
- Coverage   92.46%   92.30%   -0.17%     
==========================================
  Files          99       99              
  Lines       11748    11811      +63     
  Branches      427      429       +2     
==========================================
+ Hits        10863    10902      +39     
- Misses        885      909      +24     
Flag Coverage Δ
cpp 92.30% <66.66%> (-0.17%) ⬇️
python_and_cython 92.30% <66.66%> (-0.17%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Add hooks for the C23 sized deallocators on
   glibc Linux. Both are recorded as FREE allocations, matching plain free() per the C23 spec. Weak forward declarations
   let the build succeed on libc < 2.42; runtime addresses are resolved via dl_iterate_phdr, so a wheel built against
  old glibc still intercepts these calls when deployed against newer glibc.

Signed-off-by: Suyog Bhise <bhise.suyog@gmail.com>
@swaroski swaroski force-pushed the feat/intercept-c23-free-sized branch from c759c8e to a8dbd27 Compare May 1, 2026 22:12
@swaroski
Copy link
Copy Markdown
Contributor Author

swaroski commented May 1, 2026

Fixed preprocessor guard for non-glibc platforms (Alpine/musl/macOS). __GLIBC_PREREQ undefined there caused
tokenization error even with short-circuit.

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.

Intercept new free_sized and free_aligned_sized functions

2 participants