Skip to content
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

[Swift language feature] Implement Swift.Array support #2964

Conversation

kotlarmilos
Copy link
Member

@kotlarmilos kotlarmilos commented Jan 28, 2025

Description

This PR is a follow-up to #2948. It introduces support for Swift.Array.

In Swift, Array is defined as @frozen struct Array<Element>. It is projected into a SwiftArray class with a helper struct ArrayBuffer, which is used at the ABI level to ensure correct lowering and PInvoke signatures.

Fixes #2833

@kotlarmilos kotlarmilos added the area-SwiftBindings Swift bindings for .NET label Jan 28, 2025
@kotlarmilos kotlarmilos self-assigned this Jan 28, 2025
src/Swift.Runtime/src/Swift/SwiftArray.cs Show resolved Hide resolved
src/Swift.Runtime/src/Swift/SwiftArray.cs Outdated Show resolved Hide resolved
src/Swift.Runtime/src/Swift/SwiftArray.cs Outdated Show resolved Hide resolved
src/Swift.Runtime/src/Swift/SwiftArray.cs Show resolved Hide resolved
/// </summary>
unsafe SwiftArray(SwiftHandle handle)
{
this.buffer = *(ArrayBuffer*)(handle);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this just copy the memory? What about arrays of reference types?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would remove this constructor as it goes against the expected behavior on Swift side. Even if payload value is copied, it would point to the same instance.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't be removed, it is used in the ISwiftObject.NewFromPayload. It should perform a shallow copy of the ArrayBuffer struct.

Anyway, it doesn't have the same semantics as in Swift.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, maybe this is a future problem once we start supporting reference types. We might need to call InitializeWithCopy here

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe this is a future problem once we start supporting reference types

Could you provide an example?

We might need to call InitializeWithCopy here

Added to #2930.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't have anything specific in my mind yet. I was think about having an array of reference types. If we just make a shallow copy of the array the ref count of those references will not be updated.

src/Swift.Runtime/src/Swift/SwiftArray.cs Outdated Show resolved Hide resolved
src/Swift.Runtime/src/Swift/SwiftArray.cs Outdated Show resolved Hide resolved
src/Swift.Runtime/src/Swift/SwiftArray.cs Outdated Show resolved Hide resolved
src/Swift.Runtime/src/Swift/SwiftArray.cs Outdated Show resolved Hide resolved
src/Swift.Runtime/src/Swift/SwiftArray.cs Outdated Show resolved Hide resolved
src/Swift.Runtime/src/Swift/SwiftArray.cs Show resolved Hide resolved
static ProtocolConformanceDescriptor ISwiftObject.GetProtocolConformanceDescriptor<TProtocol>()
where TProtocol : class
{
return ProtocolConformanceDescriptor.Zero;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will have to be able to obtain the descriptor for array : Collection

{
if (_buffer.storage != IntPtr.Zero)
{
Arc.Release(*(IntPtr*)_buffer.storage);
Copy link
Member

@jkotas jkotas Jan 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you have any discussions about thread-safety and security of the Swift projections?

For example, with this implementation - user code calling Dispose method on multiple threads can lead to releasing the _buffer.storage twice (double-free security bug). .NET core libraries typically try to avoid turning race conditions in user code into memory safety violations. Are we ok with Swift projections not providing this hardening?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great point - created #2975 to track this to revisit all projections and fix it there (if we have any other really). In any case which is something we might want to have a helper for.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @vitek-karas for creating the tracking issues. We need to revisit this.

On the same note, we've already encountered a multi-threading scenario — in-app purchase request can't be performed on the main thread obviously.

Copy link
Member

@matouskozak matouskozak left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good job Milos!

{
fixed (void* _payloadPtr = &_buffer)
{
metadata.ValueWitnessTable->InitializeWithCopy((void*)swiftDest, (void*)_payloadPtr, metadata);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

General comment about current design of MarshalToSwift - this feels very prone to buffer overruns.
It's a virtual call, so the caller technically doesn't know what the callee will be precisely. But at the same time, the caller MUST now the size of the buffer to allocate for swiftDest. That feels wrong.

What is the scenario where we have a preexisting buffer and need to marshal to it? So far the callers I've seen all need to allocate before calling this method. And they have to get the size right, otherwise we'll get buffer overruns at runtime.

I think the minimum should be that we don't pass a raw pointer, but some representation of pointer + size. Or better yet, this method would be responsible for allocating the buffer and returning it.

I also find it weird that the method takes a destination buffer, but it may choose to return another buffer. What is the memory ownership? We should at least precisely document this in comments, but ideally redesign the API to make it "secure by default".

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we could use MemoryManager<byte> (our own implementation) which would handle the memory ownership. But it's also possible that having a simple struct of our own would be good enough.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree that we should review the MarshalToSwift design. I would not tackle this in this PR though.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Absolutely - created #2974 to track that.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added to #2851

@kotlarmilos kotlarmilos changed the title [Swift language feature] Implement Swift.Array support for primitive types [Swift language feature] Implement Swift.Array support Feb 3, 2025
@kotlarmilos kotlarmilos merged commit 54d46f6 into dotnet:feature/swift-bindings Feb 4, 2025
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-SwiftBindings Swift bindings for .NET
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants