soundness fix: retain PyBuffer while using the &[u8] that comes from it#89
Merged
oconnor663 merged 1 commit intomasterfrom Oct 14, 2025
Merged
soundness fix: retain PyBuffer while using the &[u8] that comes from it#89oconnor663 merged 1 commit intomasterfrom
oconnor663 merged 1 commit intomasterfrom
Conversation
Previously we were getting a slice from a `PyBuffer` and then letting that `PyBuffer` drop. That released our "lock" on the buffer, which meant other threads could potentially race to resize it and turn our reads into use-after-free bugs. Refactor things so that the slice is lifetime-bound to the `PyBuffer` that protects it. Also, previously I was calling `PyBuffer::as_slice` to take advantage of the internal contiguity check that it does, but that required disassembling and reassembling the raw slice to cast away `ReadOnlyCell`. Now I think it's cleaner to do the contiguity check explicitly and then work directly with the raw buffer pointer. While cleaning up the comments around this, I realized I had previously misunderstood how `Python::detach` works. I thought it didn't allow certain lifetimes to pass into the closure (similar to `std::thread::scope`), but in fact it basically just requires `Send`. Casting away the `PyBuffer::as_slice` lifetime isn't really relevant; what matters is that `&[u8]` is `Send` while `&[ReadOnlyCell<T>]` isn't. That raised some questions about how all this interacts with freethreaded builds, where we never have a GIL protecting us. I've revised some of the big comments and also opened a discussion thread here: PyO3/pyo3#5508
Merged
ddelange
approved these changes
Oct 13, 2025
Collaborator
ddelange
left a comment
There was a problem hiding this comment.
I love the deep dive, and the PR description is next level. LGTM 👍
Collaborator
|
this reminds me of ABI3 (limited API). I think we can't use it here because of the low level buffer stuff, but AB3 wheels would be awesome of course because they're forward compatible (future python minor version bumps, or even major version bumps?) |
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.
Previously we were getting a slice from a
PyBufferand then letting thatPyBufferdrop. That released our "lock" on the buffer, which meant other threads could potentially race to resize it and turn our reads into use-after-free bugs. Refactor things so that the slice is lifetime-bound to thePyBufferthat protects it.Also, previously I was calling
PyBuffer::as_sliceto take advantage of the internal contiguity check that it does, but that required disassembling and reassembling the raw slice to cast awayReadOnlyCell. Now I think it's cleaner to do the contiguity check explicitly and then work directly with the raw buffer pointer.While cleaning up the comments around this, I realized I had previously misunderstood how
Python::detachworks. I thought it didn't allow certain lifetimes to pass into the closure (similar tostd::thread::scope), but in fact it basically just requiresSend. Casting away thePyBuffer::as_slicelifetime isn't really relevant; what matters is that&[u8]isSendwhile&[ReadOnlyCell<T>]isn't. That raised some questions about how all this interacts with freethreaded builds, where we never have a GIL protecting us. I've revised some of the big comments and also opened a discussion thread here: PyO3/pyo3#5508