Open jtschuster opened 1 year ago
Tagging subscribers to this area: @dotnet/interop-contrib See info in area-owners.md if you want to be subscribed.
Author: | jtschuster |
---|---|
Assignees: | jtschuster |
Labels: | `area-System.Runtime.InteropServices` |
Milestone: | - |
See https://github.com/dotnet/runtime/issues/88111 for how the current generated code doesn't follow the rules.
Motivation:
To follow the ownership and lifetime expectations for COM, the generator pipeline should only assign to parameters and return values at the end of the method after unmarshalling / marshalling all parameters has succeeded. The generally recommended way to achieve this is to avoid modifying any parameters until the last basic block that returns a successful HRESULT. For our generator pipeline, this could look like an additional stage where local variables holding the final values of parameters are assigned to the parameters.
Proposal:
The new list of stages would be:
Issues:
This causes slightly unexpected behavior for arrays of blittable elements that are pinned in the ManagedToUnmanaged stubs. These values could be modified if the pInvoke succeeds, but unmarshalling throws an exception. However, this may be more of a minor issue since this doesn't lead to a memory leak or double free, and can be fixed by allocating and copying all the values to a new temporary array to pass to the Unmanaged COM method.
Examples:
Assign out would look like the following for the following scenarios for ManagedToUnmanaged.
ref StructType parameter
): \<parameter> = \<UnmarshalledValue>ref StructType parameter
:ClassType[] array
:ref ClassType parameter
:And like the following for UnmanagedToManaged:
StructType* parameter
): (*\<parameter>) = \<MarshalledValue>StructType* value
int* array, int length
:int[]* array
/int[]* array
nint* handle
Drawbacks:
This could be a significant amount of work and may only be necessary for a few edge cases that are left.
Benefits:
This would make our lifetime issues much less likely and generated code would follow general COM guidance. This also would allow us to remove ownership tracking marshalling generators like
UnmanagedToManagedOwnershipTrackingStrategy
Todo:
param_native_out
variable to hold the marshalled value.#89462https://github.com/dotnet/runtime/issues/89465