Skip to content

Regression in .NET 10: CFG violation (Indirect call guard check) when accessing Clipboard #14308

@rampaa

Description

@rampaa

Description

After upgrading to .NET 10, an application intermittently crashes with an unhandled exception:

Indirect call guard check detected invalid control transfer

The crash occurs inside Clipboard.ContainsText() when it is called in response to WM_CLIPBOARDUPDATE. The same code path worked reliably in previous .NET versions.

Reproduction Steps

  1. Create a WPF application targeting .NET 10.
  2. Register for clipboard notifications using AddClipboardFormatListener.
  3. Handle WM_CLIPBOARDUPDATE in a custom WndProc.
  4. Call Clipboard.ContainsText() asynchronously from the message handler.

Minimal repro code:

private static nint WndProc(nint hwnd, int msg, nint wParam, nint lParam, ref bool handled)
{
    if (msg == WM_CLIPBOARDUPDATE)
    {
                _ = Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Send, () =>
                {
                           _ = CopyFromClipboard();
                });

        handled = true;
    }

    return 0;
}
private async Task<bool> CopyFromClipboard()
{
    while (Clipboard.ContainsText())
    {
        try
        {
            string text = Clipboard.GetText();
            return true;
        }
        catch (ExternalException)
        {
            await Task.Delay(5).ConfigureAwait(true);
        }
    }

    return false;
}

Run the app and frequently change clipboard contents (e.g., copy text rapidly from different applications).

Eventually, the process crashes with the CFG error.

Expected behavior

Clipboard.ContainsText() should either:

  1. Return a boolean result, or
  2. Throw a managed exception (e.g., ExternalException) if the clipboard is temporarily unavailable.

It should never cause a native crash or trigger a Control Flow Guard violation.

Actual behavior

The application crashes with an unhandled native exception:

Unhandled exception at 0x00007FFE23CC728F in JL.exe: 
Indirect call guard check detected invalid control transfer.

A crash dump shows the fault occurring inside Clipboard.ContainsText().

The exception cannot be caught in managed code.

Here's the full dump: https://mega.nz/file/z0FwBTCB#sssXKDAROlucZkEEVLPR5LKwxTfNex30nPqbgUl99mM

Regression?

Yes. This code path worked reliably in previous .NET versions. I never had a problem with .NET 5, .NET 6, .NET 7, .NET 8, .NET 9.

Known Workarounds

No response

Impact

No response

Configuration

No response

Other information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions