-
Notifications
You must be signed in to change notification settings - Fork 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
Suppress emitting of localsinit flag. (VS 16.8, .NET 5) #1738
Comments
CC. @jaredpar. I logged this issue to track: https://github.com/dotnet/csharplang/blob/master/proposals/skip-localsinit.md I believe we need a new proposal champion to finish pushing this through the compiler. |
Will the While reading the proposal, I assumed that there'll be some kind of restriction (because of potentially unverifiable code), but that's not stated explicitly. |
This is linked to the CoreFX API: https://github.com/dotnet/corefx/issues/29026 |
@MrJul, not as proposed. If the user adds the attribute to a method, they are opting into the behavior and into any "unverifiable" functionality. NOTE: For most "safe" code, dropping |
This already has a prototype/implementation: https://github.com/dotnet/roslyn/tree/features/localsinit The remaining work is just the general cleanup required to merge this back into master. We should look at getting this done for 8.0, if possible. CC: @agocke, @jaredpar, @jcouv |
Nit: This is not true for Span |
Ah right, |
To keep safe code safe, it would help if the compiler disallowed Span |
@jkotas and I talked about this at length. The two reasons why I decided against that:
Both of these things together made me think that this wasn't necessarily worth marking every method with stackalloc unsafe, but it was worth at least a small warning, which is why I changed the |
I don't think that is 100% true. There are many unmanaged types that fall over dead in unexpected ways with you try bitblting random data into them: |
Yeah, but all of those APIs should be marked with |
Maybe we could also fix those types to not fall over dead. There are too many ways that simple structs can be otherwise broken. |
I don't think we need to go quite that far. Recall that Certain types ( TBH I'm fine with the issue as proposed here. But I saw the sentiment (paraphrased) "unmanaged structs are assumed to be resilient to any arbitrary backing bit pattern" and wanted to point out that it's not a universal truth. As such I don't think it should serve as sole justification for a feature, either for this or for any other feature that might come in the future. If there's other justification for doing something then great. :) |
I'd go further and suggest that this is an actively dangerous assumption. If you monkey with the innards of a type in this fashion you're firmly in undefined behavior territory and you shouldn't be surprised if bad things happen. This is precisely the kind of manipulation used by malicious actors to bypass type safeguards in attempts to provoke remote code execution. #warrantyvoidifopened |
Apologies in case this shouldn't be the place to ask, but zeroing memory is also done outside the stack. This currently adds unnecessary overhead for arrays that are initialized immediately after declaration, leading to double initialization (first zeroing, then whatever the user wants). While allowing arbitrary arrays to be pointing to arbitrary memory may be considered a security concern, it would greatly improve safe types like Bottom line, this is great for locals, but could we extend it to support heap memory as well? Related: #1037 |
Yes, not the right place. I think this is still on track for .NET 5: dotnet/runtime#27146 - the discussion should be moved there. |
Hi. In the current (5.0.100-preview.7.20366.6) compiler always emits |
My thoughts are that we don't have a concept of source-only attributes today, and we shouldn't try to add them for a single type. There are certainly plenty of cases where they're useful, and I think source generators will increase that to the point we'll need to consider the. But not for a single attribute. #1452 is the source-only attributes proposal. |
@333fred MethodImplAttribute, SerializableAttribute, StructLayoutAttribute, and others that control IL flags are not emitted as custom attributes, so why should SkipLocalsInitAttribute be? Also, IndexerNameAttribute is an example of compiler-recognized attribute that doesn't target IL flags specifically and is not emitted as a custom attribute. |
C# has plenty of "source-only attributes", aka pseudo-attributes that the compiler recognizes and emits as some other form of metadata. I think an attribute like IIRC the motivation behind the "source-only attributes" wasn't to add new attributes that aren't emitted as attributes but instead to allow attributes to be applied to more targets like locals, assignments or expressions. |
Adding
SkipLocalsInitiAttribute
to CoreCLR: dotnet/coreclr#20093The text was updated successfully, but these errors were encountered: