-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Add III.1.7 "Managed pointers exposing parameters outside of the method scope" to Ecma-335-Augments.md #122821
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
base: main
Are you sure you want to change the base?
Conversation
…od scope" to Ecma-335-Augments.md
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
This PR adds a new specification section III.1.7.7 "Managed pointers exposing parameters outside of the method scope" to the Ecma-335 augments document. The goal is to formalize rules for when byrefs derived from method parameters can escape a function's scope, enabling JIT compiler optimizations such as boxing optimizations and inter-procedural escape analysis.
Key changes:
- Defines two permitted ways for byrefs from parameters to escape: explicit returns and writes through byrefs to byref-like types
- Declares undefined behavior for any other escape attempts
- Provides examples to clarify when parameters can and cannot expose values
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
|
Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch |
|
|
||
| ### III.1.7.7 | ||
| Add a new paragraph "III.1.7.7 Managed pointers exposing parameters outside of the method scope" under section "III.1.7 Restrictions on CIL code sequences" | ||
| Byrefs derived from method parameters can escape from a function only in the following ways: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What does "derived" mean exactly? The existing uses of "derived" in the ECMA spec are about inheritance.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When we take a byref from a parameter directly or from any of its fields or array elements.
Byrefs obtained from parameters directly or indirectly (byrefs to their fields or array elements)
is it better?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
array elements
Marshal.UnsafeAddrOfPinnedArrayElement does exactly that. I think this needs to be more limited.
Does this warning have false positives - does it fire on correct (unsafe) code? |
Hard to tell, here is the minimal repro for that warning to show up: unsafe void M(ref int x)
{
int y = 0;
x = ref y; // warning CS9085: This ref-assigns 'y' to 'x' but 'y' has a narrower escape scope than 'x'.
}given it already requires |
It shows up for a code like this that is not obviously invalid:
Right, my guess it was the idea for keeping it as a warning - to have an escape hatch for false positives. |
|
runtime/src/libraries/System.Private.CoreLib/src/System/Runtime/CompilerServices/Unsafe.cs Lines 698 to 709 in ccd875b
So now |
Interesting question. I'm not sure why the Perhaps, the UB part needs to be re-phrased. "If you use unsafe and you know what you do - it's fine" 🙂 |
We need better description of what is valid unsafe code vs. invalid unsafe code than this. If we are not able to come up with one, I would rather revert the problematic JIT optimization. |
Motivation:
given the
void Consume(Span<int>)signature, JIT can be sure that the buffer never escapes anywhere without looking into actual implementation. We expect this to unlock the potential of the current Escape Analysis and handle a lot more cases.Another examples:
Based on the discussion with @jakobbotsch @jkotas. Let me know if the wording needs to be improved.
PS: "multiple levels of byref fields" is not something that can be represented today in C#, but we leave a room for it.
Open questions:
warning CS9085: This ref-assigns 'field' to 'p' but 'field' has a narrower escape scope than 'p'.an error? UPD: given it requires unsafe and there are, possibly, valid scenarios it's fine to leave it as is.