Skip to content

feat: add alloc-tracking#266

Open
Ben Chatelain (phatblat) wants to merge 2 commits into
masterfrom
ben/sdks-1896/alloc-tracking
Open

feat: add alloc-tracking#266
Ben Chatelain (phatblat) wants to merge 2 commits into
masterfrom
ben/sdks-1896/alloc-tracking

Conversation

@phatblat
Copy link
Copy Markdown
Member

@phatblat Ben Chatelain (phatblat) commented Oct 22, 2025

Resolves SDKS-1896
Extracted from getditto/ditto#18937

Summary

Adds an opt-in alloc-tracking Cargo feature that records allocation, deallocation, and Arc clone/drop events with backtraces to help diagnose use-after-free, double-free, and Arc refcount bugs in FFI code.

Changes

  • New alloc-tracking feature (off by default) gated on a new optional backtrace dependency
  • New src/alloc_tracking.rs module exposing AllocationTracker with:
    • track_alloc / track_free for Box/Vec/slice/char_p allocations (double-free and unknown-pointer-free panics)
    • track_arc_new / track_arc_clone / track_arc_drop for Arc refcount events
    • Global ALLOC_TRACKER behind a LazyLock<Mutex<…>>
  • Instrumented the following types with feature-gated hooks on construction, clone, and drop paths:
    • ThinArc (src/arc.rs)
    • ThinBox (src/boxed.rs)
    • char_p::Box / CString conversions (src/char_p.rs)
    • slice_boxed (src/slice.rs), skipping zero-length and ZST allocations
    • Vec FFI wrapper (src/vec.rs)
    • Closure Arc/Box wrappers (src/closure/arc.rs, src/closure/boxed.rs)
  • Added serial_test dev-dependency (tracker state is global, so tests must not run in parallel)

Notes

All instrumentation is behind #[cfg(feature = "alloc-tracking")] and has zero runtime cost when the feature is disabled.

cargo check --all-features does not work on this branch or on master — it activates the nightly feature (which requires a nightly toolchain) and hits a pre-existing serde visit_byte_buf mismatch in src/bytes.rs. Use the targeted commands below instead.

Test plan

  • cargo check --features alloc-tracking
  • cargo test --features alloc-tracking
  • cargo check (default features, verify no regression from feature-gating)

@phatblat Ben Chatelain (phatblat) added the K-feature Kind: proposed new code to implement new behaviour label Oct 22, 2025
@phatblat Ben Chatelain (phatblat) changed the base branch from master to ben/sdks-1053/type-metadata-json-output October 22, 2025 22:47
@hamchapman Hamilton Chapman (hamchapman) marked this pull request as ready for review October 27, 2025 17:27
@hamchapman Hamilton Chapman (hamchapman) removed their request for review February 10, 2026 14:32
@phatblat Ben Chatelain (phatblat) changed the base branch from ben/sdks-1053/type-metadata-json-output to master April 17, 2026 15:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

K-feature Kind: proposed new code to implement new behaviour

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants