Skip to content

Conversation

@elinor-fung
Copy link
Member

@elinor-fung elinor-fung commented Nov 10, 2021

When marshalling an array out by value, we were leaving the native storage uninitialized. This clears it out, so that we don't end up with whatever garbage was there in the array (matches built-in system behaviour).

This is the reason behind the failures in #61389.

@AaronRobinsonMSFT @jkoritzinsky

@ghost ghost added the area-Interop-coreclr label Nov 10, 2021
Copy link
Member

@AaronRobinsonMSFT AaronRobinsonMSFT left a comment

Choose a reason for hiding this comment

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

Do we want to mentioned the discovered behavior around implicit out with the built-in in the compat doc?


In the source-generated marshalling, arrays will be allocated on the stack (instead of through [`AllocCoTaskMem`](https://docs.microsoft.com/dotnet/api/system.runtime.interopservices.marshal.alloccotaskmem)) if they are passed by value or by read-only reference if they contain at most 256 bytes of data. The built-in system does not support this optimization for arrays.

In the built-in system, marshalling a `char` array by value with `CharSet.Unicode` would default to also marshalling data out. In the source-generated marshalling, the `char` array must be marked with the `[Out]` attribute for data to be marshalled out by value.
Copy link
Member

Choose a reason for hiding this comment

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

This is due to the fact that a char array with CharSet.Unicode used to be pinnacle as char is blittable with CharSet.Unicode

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, I figured that was it once I saw the behaviour, but it was not something I realized immediately 😕

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

area-System.Runtime.InteropServices source-generator Indicates an issue with a source generator feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants