🚧 Initial safer & stronger API for buffers#505
Draft
atifaziz wants to merge 9 commits intotonybaloney:mainfrom
Draft
🚧 Initial safer & stronger API for buffers#505atifaziz wants to merge 9 commits intotonybaloney:mainfrom
atifaziz wants to merge 9 commits intotonybaloney:mainfrom
Conversation
Conflicts resolved: - src/CSnakes.Runtime/Python/PyBuffer.cs - src/Integration.Tests/BufferTests.cs
Conflicts resolved: - src/CSnakes.Runtime/PyObjectTypeConverter.cs - src/CSnakes.Runtime/Python/PyBuffer.cs - src/CSnakes.Runtime/Python/PyBufferExtensions.cs
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.
This PR is a proposal to address and close #504. It refactors the buffer API to avoid critical memory safety vulnerabilities that can easily lead to
System.AccessViolationExceptioncrashes when juggling the lifetime management rules of native memory backing Python buffer objects, and spans that may be pointing into them. This is done by making most of the API use safe patterns where spans are provided as input to functions as opposed to being handed out, e.g.:The only unsafe access is provided by
UnsafeMemory, which returns aMemory<T>(as being discussed in #471). Ideally, this would be markedunsafe, but that's ineffective for interface members that don't expose unsafe types in their signatures (like a pointer).I've made the API more type-safe:
PyBufferclass with run-time type checking and unsafe castingIPyBuffer<T>representing a shape-less base for some buffer ofTitemsIPyArrayBuffer<T>for scalars and 1D arraysIPyArray2DBuffer<T>for 2D arraysPyTensorBuffer<T>for tensorsThis eliminates run-time type mismatches and provides compile-time safety. Developers can now rely on standard C# patterns like:
It also improves performance as all checks and creation of the right buffer sub-type is done once. The validation also lies with each sub-type where it makes sense.
Other Notes
PyObject, added buffer-specific disposal queue that safely handles cleanup when the GIL isn't available (see also AddPyBufferfinalizer to prevent potential leaks #544).AsSpan<T>()family of methods to discourage unsafe usage patterns.Pending Decisions & Work
This is a draft PR to summon early feedback.
IPyBuffersub-type/hierarchical APIMap,Do,CopyTo,CopyFrom, etc. (including overloads)buffers.md)