microsoft / microsoft/CsWinRT

Bug: CsWinRT 3 undetermined-interface case relies on undefined COM behavior

Open
#2,483 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C#
Stars
665
Forks
134
Avg merge
1d 3h
Merged PRs (30d)
32

Description

Description

When CsWinRT could not determine which interface to use in the marshaller, it falls back to using IInspectable (see code below). However, performing a QueryInterface to IInspectable is undefined behavior.
https://github.com/microsoft/CsWinRT/blob/228e89e3ffcb4dbaba6af7e0365e3a52966bcd83/src/WinRT.Runtime2/InteropServices/WindowsRuntimeComWrappers.cs#L439-L445

Steps To Reproduce

[This sample assumes that you have OneDrive installed with an account signed in and the machine runs Windows 11 version 24H2 or higher]

  1. Go to HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\SyncRootManager
  2. Find subkeys with the prefix OneDrive!, and copy the subkey name for any one of them.
  3. Create a console project, using CsWinRT 3 and enabling unsafe code
  4. Paste the following code in, replacing the sri variable with the subkey name copied above.
using Microsoft.Win32;
using System.Runtime.InteropServices;
using Windows.Storage.Provider;
using WindowsRuntime.InteropServices;

string sri = "[subkey name]";

using var key = Registry.LocalMachine.OpenSubKey($"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\SyncRootManager\\{sri}")!;
Guid factoryClsid = Guid.Parse((string)key.GetValue("StorageProviderStatusUISourceFactory")!);

IStorageProviderStatusUISourceFactory factory;

unsafe
{
    Marshal.ThrowExceptionForHR(PInvoke.CoCreateInstance(factoryClsid, null, 0x4, typeof(IStorageProviderStatusUISourceFactory).GUID, out void* ppv));
    factory = (IStorageProviderStatusUISourceFactory)WindowsRuntimeMarshal.ConvertToManaged(ppv)!;
    var statusUISource = factory.GetStatusUISource(sri);
    Console.WriteLine(statusUISource.GetStatusUI().QuotaUI.QuotaUsedLabel);
}

static partial class PInvoke
{
    [LibraryImport("combase")]
    public static unsafe partial int CoCreateInstance(
        in Guid rclsid,
        void* pUnkOuter,
        uint dwClsContext,
        in Guid riid,
        out void* ppv);
}
  1. Run the project, observe InvalidCastException at the statusUISource line.
Expected Behavior

The console should print the storage usage.

Version Info

CsWinRT 3.0.0-preview.260319.2 (latest on NuGet)

Additional Context

This is essentially the same issue as https://github.com/microsoft/CsWin32/issues/1716, but for WinRT

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in src/WinRT.Runtime2/InteropServices/WindowsRuntimeComWrappers.cs around lines 439-445, then reproduce the provided OneDrive sample on Windows 11 24H2 or later. Trace the undetermined-interface marshalling path and verify the fix by rerunning the sample; done means it no longer throws InvalidCastException and prints the storage usage.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
desktop, operating-systems
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.