Skip to content

ifConversion can hoist invalid managed references. #124807

Open

Description

@EgorBo

We shouldn't speculatively hoist potentially invalid byrefs as those may lead to GC crashes.

byte Problem(ref byte x, int len)
{
    ref byte t = ref (len == 0 ? 
        ref Unsafe.NullRef<byte>() : 
        ref Unsafe.Add(ref x, 100));
    return t;
}

Codegen:

; Method Benchmarks:Problem(byref,int):byte:this (FullOpts)
G_M23671_IG01:        ; bbWeight=1, gcrefRegs=0000 {}, byrefRegs=0000 {}, byref, nogc
G_M23671_IG02:        ; bbWeight=1, gcrefRegs=0000 {}, byrefRegs=0004 {rdx}, byref
                            ; byrRegs +[rdx]
       xor      rax, rax
                            ; byrRegs +[rax]
       add      rdx, 100
       test     r8d, r8d
       cmovne   rax, rdx
       movzx    rax, byte  ptr [rax]
                            ; byrRegs -[rax]
G_M23671_IG03:        ; bbWeight=1, epilog, nogc, extend
       ret      
; Total bytes of code: 17

Activity

  1. added
    untriaged
    on Feb 24, 2026
  2. added
    area-CodeGen-coreclr
    on Feb 24, 2026
  3. dotnet-policy-service commented on Feb 24, 2026

    @dotnet-policy-service
    Contributor

    Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch
    See info in area-owners.md if you want to be subscribed.

  4. added this to the 11.0.0 milestone on Feb 24, 2026
  5. removed
    untriaged
    on Feb 24, 2026
  6. jakobbotsch commented on Feb 25, 2026

    @jakobbotsch
    Member

    There are similar problems in other areas. I suspect the real general fix would be to set GTF_ORDER_SIDEEFF on any byref arithmetic or possible unmanaged to managed reinterpretations, but it's a big hammer to use.

    Here is an example where forward sub does similar reordering:

    private static void Foo(ref int x, int len)
    {
        int result = Validate(123, len);
        Bar(ref Unsafe.Add(ref x, 123), result);
    }
    
    [MethodImpl(MethodImplOptions.NoInlining)]
    private static int Validate(int index, int len)
    {
        if (index >= len)
            throw new System.Exception();
    
        return 123;
    }
    
    [MethodImpl(MethodImplOptions.NoInlining)]
    private static void Bar(ref int x, int y)
    {
    }

    (morph ends up reordering things again to turn it back into the original evaluation order, but the IR is still out of order for a while.)

    Also I think gtSetEvalOrder could probably cause similar things to happen.

  7. modified the milestones: 11.0.0, 12.0.0 on Jul 9, 2026
  8. EgorBo commented on Jul 9, 2026

    @EgorBo
    MemberAuthor

    Moving to 12.0 as it has always been like that (at least since ifConversion was introduced)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area-CodeGen-coreclr

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions