Skip to content

JIT: (bug) can pass an implicit-byref parameter as a struct return buffer, violating the retbuf stack invariant #133961

Closed

Description

@EgorBo

For non-async struct-returning calls, the importer can allow the address of an implicit-byref parameter to be used as the hidden return buffer. Morph then turns that local address into the incoming byref, so the callee may store GC references through a caller-supplied byref while assuming the retbuf points to stack memory.

Minimal Repro

using System;
using System.Runtime.CompilerServices;

public struct Big
{
    public object A, B, C, D;
}

public static class Program
{
    [MethodImpl(MethodImplOptions.NoInlining)]
    public static Big GetBig(object o)
    {
        Big b;
        b.A = o;
        b.B = o;
        b.C = o;
        b.D = o;
        return b;
    }

    [MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.AggressiveOptimization)]
    public static object Test(Big b, object o)
    {
        b = GetBig(o);
        return b.A;
    }

    public static void Main() => Console.WriteLine(Test(default, "hello"));
}

Expected

The JIT should reject an implicit-byref local as a return buffer and use a stack temp, then copy the result into b with the required write barriers.

Actual

The caller passes the incoming implicit byref as the retbuf, and the callee writes object references to it with plain stores:

mov      rcx, rbx                 ; retbuf == incoming implicit byref
call     [Program:GetBig(System.Object):Big]

mov      gword ptr [rcx], rdx
mov      gword ptr [rcx+0x08], rdx
mov      gword ptr [rcx+0x10], rdx
mov      gword ptr [rcx+0x18], rdx

The simple JIT-to-JIT repro prints hello, so the missed-barrier risk is latent unless the implicit byref points at GC heap memory.

Notes

The async path in impIsLegalRetBuf already rejects GT_LCL_ADDR of an implicit-byref local, but the non-async path relies on fgAddrCouldBeHeap, which returns false for GT_LCL_ADDR.
The implicit-byref rejection should apply to non-async retbufs too, or fgAddrCouldBeHeap should treat these local addresses as potentially heap-backed.

Activity

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

    @jakobbotsch
    Member

    Implicit byrefs never point to heap today, #82166

  5. EgorBo commented on Sep 15, 2026

    @EgorBo
    MemberAuthor

    @jakobbotsch are you sure, the ABI doc disagrees: https://github.com/dotnet/runtime/blob/main/docs/design/coreclr/botr/clr-abi.md#amd64-only-by-value-value-types

    However, there are some cases within remoting or reflection where apparently stackalloc is too hard, and so they pass in pointers within the GC heap

  6. EgorBo commented on Sep 15, 2026

    @EgorBo
    MemberAuthor

    #133304 - my pr tried to fix that, apparently, the debugger part could still point it to the heap.

  7. EgorBo commented on Sep 15, 2026

    @EgorBo
    MemberAuthor

    https://github.com/dotnet/runtime/blob/main/src/coreclr/debug/ee/funceval.cpp#L1081-L1134 GetFuncEvalArgValue unboxes using o1->GetData() and, when larger than an ARG_SLOT, puts that heap pointer directly into the argument slot

  8. EgorBo commented on Sep 15, 2026

    @EgorBo
    MemberAuthor

    @jkotas so probably we should re-open #133304 to fix that? (I closed it only because I was not able to detect perf improvements from that, but apparently we can simplify the ABI if that place is the only one that might put a gc pointer there).

  9. jakobbotsch commented on Sep 15, 2026

    @jakobbotsch
    Member

    That same method has implicit byref handling here that looks to put it on stack in private memory:

    #if defined(ENREGISTERED_PARAMTYPE_MAXSIZE)
    if (ArgIterator::IsArgPassedByRef(argTH))
    {
    // On X64, by-value value class arguments which are bigger than 8 bytes are passed by reference
    // according to the native calling convention. The same goes for value class arguments whose size
    // is smaller than 8 bytes but not a power of 2. To avoid side effets, we need to allocate a
    // temporary variable and pass that by reference instead. On ARM64, by-value value class
    // arguments which are bigger than 16 bytes are passed by reference.
    _ASSERTE(ppProtectedValueClasses != NULL);
    BYTE * pTemp = new (interopsafe) BYTE[ALIGN_UP(sizeof(ValueClassInfo), 8) + size];
    ValueClassInfo * pValueClassInfo = (ValueClassInfo *)pTemp;
    LPVOID pData = pTemp + ALIGN_UP(sizeof(ValueClassInfo), 8);
    memcpyNoGCRefs(pData, pAddr, size);
    *pArgument = PtrToArgSlot(pData);
    pValueClassInfo->pData = pData;
    pValueClassInfo->pMT = argTH.GetMethodTable();
    pValueClassInfo->pNext = *ppProtectedValueClasses;
    *ppProtectedValueClasses = pValueClassInfo;
    }
    else

    I cannot quite tell if that other path also gets used for the implicit byref cases. This place looks to be the only one that actually checks the ABI and fNeedBoxOrUnbox seems to be about actual byref typed parameters that are ok to point to heap.

    @jakobbotsch are you sure, the ABI doc disagrees: https://github.com/dotnet/runtime/blob/main/docs/design/coreclr/botr/clr-abi.md#amd64-only-by-value-value-types

    However, there are some cases within remoting or reflection where apparently stackalloc is too hard, and so they pass in pointers within the GC heap

    Yes, it disagrees -- #82166 also points out that it should be updated.

  10. jkotas commented on Sep 15, 2026

    @jkotas
    Member

    @jkotas so probably we should re-open #133304 to fix that?

    Yes, I like that change just for the cleaner contract alone. A few potential perf improvements are a cream on top - if we are not able to detect any, that's fine.

  11. self-assigned this
    on Sep 24, 2026
  12. locked and limited conversation to collaborators on Oct 26, 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