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

[DllImportGenerator] Initialize by-value out array to default #61431

Merged
merged 2 commits into from
Nov 10, 2021

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

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?

@@ -71,6 +71,8 @@ Only single-dimensional arrays are supported for source generated marshalling.

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