You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
JIT: boxing shouldn't be removed for escaping managed references #122099
Here in Test JIT optimizes boxing for (I1)x) into:
; Method Class1:Test(Struct1):byref:this (FullOpts)subrsp,40mov dword ptr [rsp+0x20],edxlearcx,[rsp+0x20]call[Struct1:Method1():byref:this]nopaddrsp,40ret; Total bytes of code: 25
Presumably, it's an UB for unmanaged pointers, but for all managed we probably should keep that boxing since it's a safe C# (the reference may also expose through a byref-like return type or a ref ByRefLikeType arg parameter.
Do we have a written statement somewhere that implies that there is no way to get into this situation via valid unsafe code?
I can't think of any valid way other than exposing a pointer outside of the fixed scope (which is defined as a non-valid code by ECMA-334 23.4 "address remains valid only for the duration of that fixed statement." or converting a managed pointer to an unmanaged using some unsafe API such as Unsafe.AsPointer.
Disabling this optimization for unmanaged pointers effectively means removing this optimization completely as they can be passed through any >=pointer-sized return type or out arg (which will be a big perf regressions - huge diffs last I checked).
usingSystem.Runtime.CompilerServices;unsafeclassMy{staticStashedRef*s;staticvoidMain(){StashedRefr=default;s=&r;Test(default);Console.WriteLine(r.p);}[MethodImpl(MethodImplOptions.NoInlining)]staticvoidTest(Struct1x){((I1)x).MethodThatIsExpectedToBeCalledOnABoxedCopy();}publicrefstructStashedRef{publicrefintp;}publicstructStruct1:I1{intfield;[MethodImpl(MethodImplOptions.NoInlining)]publicvoidMethodThatIsExpectedToBeCalledOnABoxedCopy(){// We expect this method to be only called on a boxed copy, so it is safe to store the reference into a static.s->p=reffield;}}publicinterfaceI1{voidMethodThatIsExpectedToBeCalledOnABoxedCopy();}}
Disabling this optimization
I am not asking for this optimization to be disabled. I am asking for having a doc somewhere (RuntimeAugments?) that implies my example is invalid unsafe code.
I think @jakobbotsch mentioned that this problem is related to the boxing Nullable<> problem (a valid IL that is not currently supported by the runtime, probably also can be put into the augments)
I do not think you can hit the boxing Nullable<> problem in vanilla (unsafe) C#. This one is different - one can write vanilla (unsafe) C# that hits it.
It's the same problem as we discussed around #112543, cc @AndyAyersMS.
If we change the spec to allow this optimization then I wonder if it is possible to at the same time allow that one.
Even with UnscopedRef on MethodThatIsExpectedToBeCalledOnABoxedCopy we see a warning emitted by Roslyn:
warning CS9093: This ref-assigns 'field' to 'p' but 'field' can only escape the current method through a return statement.
Seems like some good inspiration for what the spec augment should look like.
Roslyn allows you to do unsafe byref assignments in unsafe code. It is a feature. You have to know what you are doing if you write unsafe code. I do not see why we would want to change that. It would be a breaking change that is hard to justify.
Unsafe code needs to come with rules that need to be followed to make the unsafe code valid (safe).
It's the same problem as we discussed around #112543,
If we change the spec to allow this optimization then I wonder if it is possible to at the same time allow that one.
I do not see any unsafe code in #112543 (comment) . It is not ok to introduce rules that need to be followed to make vanilla safe C# memory safe.
I do not see any unsafe code in #112543 (comment) . It is not ok to introduce rules that need to be followed to make vanilla safe C# memory safe.
The nail in the coffin for #112543 was an example like the one you shared above. Basically escape a span upwards on the stack via unsafe code such that it is impossible to know from the signature whether something may escape.
Activity
jkotas commentedon Dec 2, 2025
Do we have a written statement somewhere that implies that there is no way to get into this situation via valid unsafe code?
EgorBo commentedon Dec 2, 2025
I can't think of any valid way other than exposing a pointer outside of the
fixedscope (which is defined as a non-valid code by ECMA-334 23.4 "address remains valid only for the duration of that fixed statement." or converting a managed pointer to an unmanaged using some unsafe API such as Unsafe.AsPointer.Disabling this optimization for unmanaged pointers effectively means removing this optimization completely as they can be passed through any >=pointer-sized return type or out arg (which will be a big perf regressions - huge diffs last I checked).
jkotas commentedon Dec 2, 2025
How about this?
I am not asking for this optimization to be disabled. I am asking for having a doc somewhere (RuntimeAugments?) that implies my example is invalid unsafe code.
EgorBo commentedon Dec 2, 2025
I think @jakobbotsch mentioned that this problem is related to the boxing
Nullable<>problem (a valid IL that is not currently supported by the runtime, probably also can be put into the augments)jkotas commentedon Dec 3, 2025
I do not think you can hit the boxing
Nullable<>problem in vanilla (unsafe) C#. This one is different - one can write vanilla (unsafe) C# that hits it.jakobbotsch commentedon Dec 3, 2025
It's the same problem as we discussed around #112543, cc @AndyAyersMS.
If we change the spec to allow this optimization then I wonder if it is possible to at the same time allow that one.
Even with
UnscopedRefonMethodThatIsExpectedToBeCalledOnABoxedCopywe see a warning emitted by Roslyn:Seems like some good inspiration for what the spec augment should look like.
EgorBo commentedon Dec 3, 2025
Should it be promoted to an error in Roslyn then 🤔
jkotas commentedon Dec 3, 2025
Roslyn allows you to do unsafe byref assignments in unsafe code. It is a feature. You have to know what you are doing if you write unsafe code. I do not see why we would want to change that. It would be a breaking change that is hard to justify.
Unsafe code needs to come with rules that need to be followed to make the unsafe code valid (safe).
I do not see any unsafe code in #112543 (comment) . It is not ok to introduce rules that need to be followed to make vanilla safe C# memory safe.
jakobbotsch commentedon Dec 3, 2025
The nail in the coffin for #112543 was an example like the one you shared above. Basically escape a span upwards on the stack via unsafe code such that it is impossible to know from the signature whether something may escape.
JIT: don't make local copy when devirtualizing boxed interface call
JIT: don't make local copy when devirtualizing boxed interface call (#…
JIT: don't make local copy when devirtualizing boxed interface call (#…