Skip to content

[DAC][DBI] ICorDebugModule::GetMetaDataInterface fails in net6.0 for "Anonymously Hosted DynamicMethods Assembly" in unit test project. #62977

Closed

Description

@chuckries

Description

Wild one here...

When managed assemblies load, the VS Debugger obtains an IMetaDataImport2 for the module via the DBI API ICorDebugModule::GetMetaDataInterface. Our callsite specifically looks like this:

    CComPtr<IMetaDataImport2> pMetaData;
    hr = pCorModule->GetMetaDataInterface(IID_IMetaDataImport2, (IUnknown**)&pMetaData);

When we debug a net6.0 xUnit test project, this call fails for the "Anonymously Hosted DynamicMethods Assembly".

I'm not sure what's special about test projects. This doesn't repro in a console app if I create a DynamicMethod in my user code. In the console app, the "Anonymously Hosted DynamicMethods Assembly" loads and we successfully obtain the IMetaDataImport.

I did a cursory debugging of the runtime around the creation of the Anonymously Hosted DynamicMethods Assembly. The first time this is called by managed code, however, s_anonymouslyHostedDynamicMethodsModule is already non null, as if this is being created in the native portion of the runtime somewhere. The field is volatile, but I couldn't find any other reference to it in managed or native runtime code.

I debugged through the DBI/DAC a bit, and at some point when calling pReflectionModule->GetDynamicMetadataBuffer(), the buffer appears to have address 0 and size 24. Address zero is somehow special and is fastfailed in DacInstantiateTypeByAddressHelper

Reproduction Steps

In Visual Studio:

  1. Create a new net6.0 xUnit Test Project.
  2. Set a bp on the opening brace of the test method
  3. Debug the test (via test explorer or codelens)
  4. When the breakpoint is hit, view the locals window.

Expected behavior

this is sucesfully evaluated.

Actual behavior

this shows Internal error retrieving local value.

It's worth noting that this behavior is just a symptom of this issue. If one were to debug the VS process's dac/dbi during this repro, they would be able to see the failed call to ICorDebugModule::GetMetaDataInterface

Regression?

This does not repro in .NET Framework but does repro in all coreclr's I have tested.

Known Workarounds

No response

Configuration

VS:

Microsoft Visual Studio Enterprise 2022 (64-bit) - main
Version 17.1.0 Preview 3.0 [32017.53.main]

.NET:

mscordbi.dll	mscordbi.dll	C:\Program Files\dotnet\shared\Microsoft.NETCore.App\6.0.1\mscordbi.dll	N/A	Yes	Symbols loaded.	d:\symbols\mscordbi.pdb\4C835056DC1C4F83A5622704170A12C71\mscordbi.pdb	299	6,0,121,56705 @Commit: 3a25a7f1cc446b60678ed25c9d829420d6321eba	11/17/2021 10:00 AM	00007FF9A0530000-00007FF9A0669000	[30448] devenv.exe		
mscordaccore.dll	mscordaccore.dll	C:\Program Files\dotnet\shared\Microsoft.NETCore.App\6.0.1\mscordaccore.dll	N/A	Yes	Symbols loaded.	d:\symbols\mscordaccore.pdb\AE9F0EEE79B14AA9930F738743B71DD91\mscordaccore.pdb	354	6,0,121,56705 @Commit: 3a25a7f1cc446b60678ed25c9d829420d6321eba	11/17/2021 10:01 AM	00007FF9A02D0000-00007FF9A0415000	[30448] devenv.exe		

Other information

No response

Activity

  1. ghost added
    untriaged
    on Dec 17, 2021
  2. chuckries commented on Dec 17, 2021

    @chuckries
    ContributorAuthor
  3. added this to the 7.0.0 milestone on Dec 28, 2021
  4. removed
    untriaged
    on Dec 28, 2021
  5. self-assigned this
    on May 17, 2022
  6. eterekhin commented on May 26, 2022

    @eterekhin
    Contributor

    @tommcdon, Hi! Thanks for your help!
    Could you please tell us if there are any workarounds? Also, I should note that we have seen it only in attach scenario.

  7. eterekhin commented on Jun 1, 2022

    @eterekhin
    Contributor

    @tommcdon, Do you know why we get this exception only in attach scenario? Maybe there are some environment variables we can launch our app with to work around the issue?
    I will be really grateful if you can help

  8. davmason commented on Jul 16, 2022

    @davmason
    Contributor

    This is an unintended side effect of #57069. As it stands now we won't generate metadata for dynamic modules unless a debugger is attached. Before that change we would bail from ReflectionModule::CaptureModuleMetaDataToMemory only if IsMetadataCaptureSuppressed() && !CORDebuggerAttached(), IsMetadataCaptureSuppressed() was only true in one case, when we were importing a COM type lib. But, when the import was done it would turn suppression off and we would generate the metadata then.

    if (IsMetadataCaptureSuppressed() && !CORDebuggerAttached())
    {
    return;
    }

    After the change we always bail unconditionally if no debugger is attached, and there is no code to update it when a debugger attaches later.

    if (!CORDebuggerAttached())
    {
    return;
    }

    The fix here is to delete the early out, since the COM typelib code was removed as part of the same effort. This will revert us to the previous behavior.

  9. ghost added
    in-pr
    on Jul 16, 2022
  10. ghost removed
    in-pr
    on Jul 16, 2022
  11. ghost locked as resolved and limited conversation to collaborators on Aug 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions