Repository: dotnet/runtime
File: src/coreclr/vm/interpexec.cpp (C++)

Code before:
    {
        THROWS;
        MODE_COOPERATIVE;
        PRECONDITION(CheckPointer(pMDDelegateInvoke));
        PRECONDITION(CheckPointer(pArgs));
        PRECONDITION(CheckPointer(pRet));
    }
    CONTRACTL_END

    CallStubHeader *stubHeaderTemplate = pMDDelegateInvoke->GetCallStub();
    if (stubHeaderTemplate == NULL)
    {
        stubHeaderTemplate = UpdateCallStubForMethod(pMDDelegateInvoke);
    }

    // CallStubHeaders encode their destination addresses in the Routines array, so they need to be
    // copied to a local buffer before we can actually set their target address.
    size_t templateSize = stubHeaderTemplate->GetSize();
    uint8_t* actualCallStub = (uint8_t*)alloca(templateSize);
    memcpy(actualCallStub, stubHeaderTemplate, templateSize);
    CallStubHeader *pHeader = (CallStubHeader*)actualCallStub;
    pHeader->SetTarget(target); // The method to call
    pHeader->Invoke(pHeader->Routines, pArgs, pRet, pHeader->TotalStackSize);
}

void InvokeUnmanagedCalliWithTransition(PCODE ftn, void *cookie, int8_t *stack, InterpMethodContextFrame *pFrame, int8_t *pArgs, int8_t *pRet)
{
    CONTRACTL
    {
        THROWS;
        MODE_ANY;
        PRECONDITION(CheckPointer((void*)ftn));
        PRECONDITION(CheckPointer(cookie));
    }
    CONTRACTL_END

    // CallStubHeaders encode their destination addresses in the Routines array, so they need to be
    // copied to a local buffer before we can actually set their target address.
    CallStubHeader* stubHeaderTemplate = (CallStubHeader*)cookie;
    size_t templateSize = stubHeaderTemplate->GetSize();
    uint8_t* actualCallStub = (uint8_t*)alloca(templateSize);
    memcpy(actualCallStub, stubHeaderTemplate, templateSize);
    CallStubHeader *pHeader = (CallStubHeader*)actualCallStub;
    pHeader->SetTarget(ftn); // The method to call

    InlinedCallFrame inlinedCallFrame;
    inlinedCallFrame.m_pCallerReturnAddress = (TADDR)pFrame->ip;
    inlinedCallFrame.m_pCallSiteSP = pFrame;
    inlinedCallFrame.m_pCalleeSavedFP = (TADDR)stack;
    inlinedCallFrame.m_pThread = GetThread();
    inlinedCallFrame.m_Datum = NULL;

Review comment (suggestion):
This should be marked with `NOINLINE`, and it can be moved next to InvokeUnmanagedMethodWithTransition outside WASM ifdef.

Code after:
        PRECONDITION(CheckPointer(pArgs));
        PRECONDITION(CheckPointer(pRet));
    }
    CONTRACTL_END

    CallStubHeader *pHeader = pMD->GetCallStub();
    if (pHeader == NULL)
    {
        pHeader = UpdateCallStubForMethod(pMD);
    }

    // Interpreter-FIXME: Potential race condition if a single CallStubHeader is reused for multiple targets.
    pHeader->SetTarget(target); // The method to call

    pHeader->Invoke(pHeader->Routines, pArgs, pRet, pHeader->TotalStackSize);
}

void InvokeUnmanagedMethod(MethodDesc *targetMethod, int8_t *pArgs, int8_t *pRet, PCODE callTarget)
{
    InvokeManagedMethod(targetMethod, pArgs, pRet, callTarget);
}

void InvokeDelegateInvokeMethod(MethodDesc *pMDDelegateInvoke, int8_t *pArgs, int8_t *pRet, PCODE target)
{
    CONTRACTL
    {
        THROWS;
        MODE_COOPERATIVE;
        PRECONDITION(CheckPointer(pMDDelegateInvoke));
        PRECONDITION(CheckPointer(pArgs));
        PRECONDITION(CheckPointer(pRet));
    }
    CONTRACTL_END

    CallStubHeader *stubHeaderTemplate = pMDDelegateInvoke->GetCallStub();
    if (stubHeaderTemplate == NULL)
    {
        stubHeaderTemplate = UpdateCallStubForMethod(pMDDelegateInvoke);
    }

    // CallStubHeaders encode their destination addresses in the Routines array, so they need to be
    // copied to a local buffer before we can actually set their target address.
    size_t templateSize = stubHeaderTemplate->GetSize();
    uint8_t* actualCallStub = (uint8_t*)alloca(templateSize);
    memcpy(actualCallStub, stubHeaderTemplate, templateSize);
    CallStubHeader *pHeader = (CallStubHeader*)actualCallStub;
    pHeader->SetTarget(target); // The method to call
    pHeader->Invoke(pHeader->Routines, pArgs, pRet, pHeader->TotalStackSize);
}

void InvokeUnmanagedCalli(PCODE ftn, void *cookie, int8_t *pArgs, int8_t *pRet)