Repository navigation
Description
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:
- Create a new net6.0 xUnit Test Project.
- Set a bp on the opening brace of the test method
- Debug the test (via test explorer or codelens)
- 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
chuckries commentedon Dec 17, 2021
cc @tommcdon @hoyosjs
lindexi commentedon May 11, 2022
It break the dnSpy in
https://github.com/dnSpy/dnSpy/blob/2b6dcfaf602fb8ca6462b8b6237fdfc0c74ad994/Extensions/dnSpy.Debugger/dnSpy.Debugger.DotNet.CorDebug/Impl/ModuleCreator.cs#L94-L96
eterekhin commentedon May 26, 2022
@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.
eterekhin commentedon Jun 1, 2022
@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
davmason commentedon Jul 16, 2022
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::CaptureModuleMetaDataToMemoryonly ifIsMetadataCaptureSuppressed() && !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.runtime/src/coreclr/vm/ceeload.cpp
Lines 12688 to 12691 in 3dc99e9
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.
runtime/src/coreclr/vm/ceeload.cpp
Lines 4774 to 4777 in 50fa756
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.