-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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
Compiler should emit localinits
when method body contains stackalloc
#23951
Comments
There are a number of people that would be very happy if there was a way to deterministically indicate we don't want the |
@tannergooding - see dotnet/csharplang#1223 It does not seem to be big enough to be a real C# language feature (as in "required by all conforming C# implementations"), but we can have it a s a compiler feature where compiler may optionally recognize a specific attribute as a directive/hint to omit the flag. |
CC: @agocke |
Today I bumped into this issue in .NET Core 3.0. From #1279:
Is this still the planned case? Or is there a compiler switch or something to enforce the compatible behavior in every version? Zeroing manually would be an unnecessary overhead if the code targets older frameworks and I would like to avoid that. |
@koszeggy from what I can see, the compiler is emitting the |
It seems, using fixed buffers can be a possible workaround for avoiding possible doubled initialization. They seem to be initialized in all cases and since they don't need to be pinned anymore they are just as fast as using However, I'm not sure whether I can generally rely on this behavior. |
Avoiding double-initialization will be achievable with the The feature branch for it is here: https://github.com/dotnet/roslyn/tree/features/localsinit |
Note that the fix and the |
SkipLocalsInit has been merged, so this is now fixed. |
Currently the
localinits
is triggered by the presence of IL locals. However the flag has effect on everything that is allocated from local frame, includingstackalloc
.NOTE: the absence of the flag results in stackallocated data containing nondeterministic garbage and as such emitting the flag is not compat breaking.
While the language spec does not specify or require that, stackallocated memory is nearly always zero-inited and users often take dependency on that. Suddenly not emitting the flag could result in bugs that are very hard to reproduce.
We should make sure that the flag is emitted in all cases and document the default behavior as such.
NOTE: the cases with opposite expectations are also known -
Some users use this bug as a way to avoid costs of zeroing out stack-allocated memory. We should provide a more stable and documented way to do that.
Relying on essentially a bug that is triggered by unstable condition such as absence of IL locals is not a maintainable strategy anyways.
Proposal: https://github.com/dotnet/csharplang/blob/master/proposals/skip-localsinit.md
The text was updated successfully, but these errors were encountered: