Skip to content

Allow IMallocSpy callbacks during LPSTR/LPWSTR array cleanup#126571

Draft
Copilot wants to merge 2 commits intomainfrom
copilot/fix-extensionpoints-cmd-failure
Draft

Allow IMallocSpy callbacks during LPSTR/LPWSTR array cleanup#126571
Copilot wants to merge 2 commits intomainfrom
copilot/fix-extensionpoints-cmd-failure

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Apr 6, 2026

Interop/COM/ExtensionPoints was failing because the marshaling cleanup path freed CoTaskMem buffers under a NOTHROW contract. When a managed IMallocSpy is registered, CoTaskMemFree can re-enter managed code and violate that contract.

  • Cleanup contract fix

    • Update OleVariant::ClearLPSTRArray and OleVariant::ClearLPWSTRArray to use THROWS.
    • This matches the actual behavior of CoTaskMemFree when COM allocation hooks dispatch into managed IMallocSpy implementations.
  • Why this addresses the failure

    • The ExtensionPoints test marshals string[] through an unmanaged function pointer, which allocates and later frees LPSTR* buffers.
    • The failure was not in marshaling itself, but in cleanup running with an invalid contract for this callback path.
  • Relevant change

void OleVariant::ClearLPSTRArray(void *oleArray, SIZE_T cElements, MethodTable *pInterfaceMT)
{
    CONTRACTL
    {
        THROWS; // CoTaskMemFree can invoke a managed IMallocSpy implementation.
        GC_TRIGGERS;
        MODE_ANY;
        PRECONDITION(CheckPointer(oleArray));
    }
    CONTRACTL_END;
    ...
}

Agent-Logs-Url: https://github.com/dotnet/runtime/sessions/68c1bb22-8f29-41a2-86b8-b039f3aa63bc

Co-authored-by: AaronRobinsonMSFT <30635565+AaronRobinsonMSFT@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix Interop/COM/ExtensionPoints.cmd test failure Allow IMallocSpy callbacks during LPSTR/LPWSTR array cleanup Apr 6, 2026
Copilot AI requested a review from AaronRobinsonMSFT April 6, 2026 06:36
@dotnet-policy-service
Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @dotnet/interop-contrib
See info in area-owners.md if you want to be subscribed.

CONTRACTL
{
NOTHROW;
THROWS; // CoTaskMemFree can invoke a managed IMallocSpy implementation.
Copy link
Copy Markdown
Member

@jkotas jkotas Apr 6, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method is not expected to throw. This contract is correct.

I think this should be fixed by fixing the contracts on the new COM interop implementation. The exceptions are not expected to leak from the COM interop HRESULT-returning methods.

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

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

Interop/COM/ExtensionPoints/ExtensionPoints/ExtensionPoints.cmd failing

3 participants