-
Notifications
You must be signed in to change notification settings - Fork 300
ndb: copy profile buffer to prevent use-after-free #3563
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
Replace force unwraps (`as!`) with safe optional binding (`as?`) in NdbTxn and SafeNdbTxn transaction inheritance logic. This prevents crashes when thread-local storage is in an unexpected state. Changes: - Move `ref_count` check into the `if let` conditional binding - Use already-bound `txn_generation` instead of re-reading with force unwrap - Simplify ref count increment to single expression The fix ensures transaction inheritance gracefully falls back to creating a new transaction if thread-local state is incomplete, rather than crashing on missing values. Changelog-Fixed: Fixed potential crash in NdbTxn when thread-local storage is incomplete Signed-off-by: alltheseas
ByteBuffer(assumingMemoryBound:capacity:) creates an unowned buffer pointing directly to LMDB's memory-mapped region. When the transaction closes, this memory becomes invalid but the ProfileRecord still references it, causing crashes when accessed later. Changed to ByteBuffer(memory:count:) which copies the data into an owned buffer that remains valid after the transaction closes. Closes damus-io#3560 Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> Signed-off-by: alltheseas <alltheseas@users.noreply.github.com>
Verifies that profile data remains valid after the database is closed. The test writes profile data, reopens the database, looks up the profile, closes the database, then accesses profile fields. This would crash with use-after-free if the buffer wasn't properly copied to owned memory. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> Signed-off-by: alltheseas <alltheseas@users.noreply.github.com>
📝 WalkthroughWalkthroughThis pull request addresses memory safety issues in database operations by implementing proper buffer ownership to prevent use-after-free crashes when LMDB transactions close, fixing transaction reference counting patterns, and adding validation tests. Includes a new metadata configuration file. Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~22 minutes Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
|
@coderabbitai review please |
|
🧠 Learnings used✅ Actions performedReview triggered.
|
Summary
Fixes a use-after-free crash affecting 7 devices in TestFlight v1.16 Build 1277.
ByteBuffer(assumingMemoryBound:capacity:)creates an unowned buffer pointing directly to LMDB's memory-mapped region. When the transaction closes, this memory becomes invalid but theProfileRecordstill references it, causing crashes when accessed later.Changed to
ByteBuffer(memory:count:)which copies the data into an owned buffer that remains valid after the transaction closes.Closes #3560
Checklist
Standard PR Checklist
Closes:orFixes:tags in the commit messages wherever applicable, or made sure those are not needed. See Submitting patchesTest report
Device: iPhone 17 Simulator
iOS: 26.2
Damus: fix/yjw-use-after-free branch (d888490)
Setup: Fresh database with test wire events
Steps:
Results:
test_profile_buffer_ownershipTesting directions for reviewers
NdbTeststarget -test_profile_buffer_ownershipspecifically tests that profile data remains valid after database closeprofile_flatbuf_to_recordinNdb.swiftnow usesByteBuffer(memory:count:)instead ofByteBuffer(assumingMemoryBound:capacity:)Other notes
🤖 Generated with Claude Code
Summary by CodeRabbit
Release Notes
Bug Fixes
Tests
✏️ Tip: You can customize this high-level summary in your review settings.