Skip to content

JIT: (bug) constrained. callvirt boxes the value-type receiver before the arguments are evaluated #133716

Closed

Description

@EgorBo
using System;
using System.Runtime.CompilerServices;

struct S { public int X; }

class P
{
    static S s;

    [MethodImpl(MethodImplOptions.NoInlining)]
    static object Mutate() { s.X = 7; return new S { X = 7 }; }

    // `s` is boxed for the constrained. callvirt to Object.Equals. Per ECMA-335 III.2.1
    // the dereference+box is part of executing the callvirt, i.e. after Mutate() has run.
    [MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.AggressiveOptimization)]
    static bool Test()
    {
        return s.Equals(Mutate());
    }

    [MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.AggressiveOptimization)]
    static bool Control()
    {
        object arg = Mutate();
        return s.Equals(arg);
    }

    static void Main()
    {
        s.X = 1;
        Console.WriteLine("Test    = " + Test());
        s.X = 1;
        Console.WriteLine("Control = " + Control());
    }
}

Expected: Test = True / Control = True (the box of s must observe X == 7 written by the argument).
Actual: Test = False / Control = True — Test boxes the stale X == 1.

Notes:

  • csc -optimize+, then corerun repro.dll (reproduces with tiering on and with DOTNET_TieredCompilation=0). Building the same source with -optimize- (DebuggableAttribute DisableOptimizations) prints True, so the IL is semantically identical and only the JIT path differs.
  • src/coreclr/jit/importercalls.cpp:1036-1050 spills pending side effects only for CORINFO_DEREF_THIS; CORINFO_BOX_THIS is not covered, and impTransformThis/impImportAndPushBox run after impPopCallArgs has detached the (still unevaluated) argument trees.
  • Requires the argument call to be a non-inline candidate ([MethodImpl(MethodImplOptions.NoInlining)]); if it is inlined it is appended as its own statement first and the ordering happens to come out right.

Activity

  1. added this to the 12.0.0 milestone on Sep 11, 2026
  2. added
    area-CodeGen-coreclr
    on Sep 11, 2026
  3. dotnet-policy-service commented on Sep 11, 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. MihuBot commented on Sep 11, 2026

    @MihuBot
  5. self-assigned this
    on Sep 11, 2026
  6. added a commit that references this issue on Sep 14, 2026
    313084c
  7. added a commit that references this issue on Sep 18, 2026
    b4d268e
  8. EgorBo commented on Sep 18, 2026

    @EgorBo
    MemberAuthor

    Affected SDK versions:

    • 8.0.425
    • 9.0.318
    • 10.0.401
    • 11.0.100-rc.2.26467.112
  9. locked and limited conversation to collaborators on Oct 19, 2026
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