fix use-after-free of a blackboard entry held by getAnyLocked - #1181
Open
aysha-afrah26 wants to merge 1 commit into
Open
fix use-after-free of a blackboard entry held by getAnyLocked#1181aysha-afrah26 wants to merge 1 commit into
aysha-afrah26 wants to merge 1 commit into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Blackboard::getAnyLockedbuilds the returnedAnyPtrLockedfrom ashared_ptr<Entry>that is a local variable, so the caller ends up holding a rawAny*and a rawstd::mutex*into an entry that nothing keeps alive.unset()andclear()takestorage_mutex_but neverentry_mutex, so erasing the key drops the last reference and destroys the entry while its mutex is still locked, which makes the next read through the pointer a heap-use-after-free and leaves~LockedPtrunlocking a mutex that is already gone. The path I ran into it on is the Groot2 server thread, whereExportBlackboardToJSONcallsgetAnyLockedper key while anUnsetBlackboardaction on the tick thread frees the entry underneath it; the test added here does the same sequence on a single thread and trips ASan on current master.Giving
LockedPtran optional shared owner and passing it theshared_ptr<Entry>keeps the entry alive for exactly as long as the lock is held, which is whatgetInputStampedalready does when it takesentry_mutexdirectly. Keeping it insideLockedPtrrather than at each call site means the guarantee holds for every user of the locked-pointer API instead of depending on each one to remember. One note for review:LockedPtrgrows by oneshared_ptr, and it only ever lives as a local scope guard so no class layout changes, but it is a size change in a public header.