Fix uninitialized read when the filler closure panics - #151
Open
tooson9010-spec wants to merge 1 commit into
Open
Fix uninitialized read when the filler closure panics#151tooson9010-spec wants to merge 1 commit into
tooson9010-spec wants to merge 1 commit into
Conversation
AllocRingBuffer::fill_with and ConstGenericRingBuffer::fill_with set the write pointer to the full capacity before running the filler. If the closure panics partway through, the buffer still reports every slot as live while the ones the loop had not reached hold uninitialized memory. A later read - or the buffer's own Drop - then ptr::reads one of them. Commit each slot as it is written instead.
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.
fill_withsets the write pointer to the full capacity before running thefiller. If the closure panics partway through, the buffer reports every slot as
live while the ones the loop hadn't reached hold uninitialized memory. A later
read — or the buffer's own
Drop— thenptr::reads one of them.Reported in #150 for
AllocRingBuffer;ConstGenericRingBufferhas the sameproblem.
GrowableAllocRingBufferis unaffected — it pushes into aVecDeque.Reproducer
The added test fills an 8-slot buffer with a closure that panics on the fourth
call:
Draining it afterwards trips Miri:
The backtrace runs through
Drop for AllocRingBuffer→dequeue, so the callerdoesn't have to touch the buffer for this to fire.
The fix
Advance the write pointer one slot at a time, as each slot is written. A panic
then leaves it covering only what was initialised.
ConstGenericRingBuffer::fill_withdelegated to[MaybeUninit<T>; CAP]::fill_with,which gives no place to commit per slot, so it now uses an explicit loop.
Regression test
src/lib.rsgains a case that runs all three implementations through apanicking filler.
cargo testcatches it;cargo miri testconfirms theuninitialized read is gone.