@@ -14,7 +14,7 @@ namespace Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.Execution;
1414
1515internal sealed partial class TypeCache
1616{
17- private static bool TryReadProviderMetadata ( string path , out bool hasMarker , out AssemblyName [ ] references )
17+ private static ProviderMetadataReadResult TryReadProviderMetadata ( string path , AssemblyName expectedIdentity , out bool hasMarker , out AssemblyName [ ] references )
1818 {
1919 hasMarker = false ;
2020 references = [ ] ;
@@ -24,6 +24,14 @@ private static bool TryReadProviderMetadata(string path, out bool hasMarker, out
2424 using var peReader = new PEReader ( stream , PEStreamOptions . PrefetchMetadata ) ;
2525 MetadataReader reader = peReader . GetMetadataReader ( ) ;
2626 AssemblyDefinition definition = reader . GetAssemblyDefinition ( ) ;
27+
28+ // The runtime binds the exact requested identity; any other image at the probed path
29+ // means the runtime would select something else (or raise AssemblyResolve).
30+ if ( ! string . Equals ( expectedIdentity . FullName , definition . GetAssemblyName ( ) . FullName , StringComparison . Ordinal ) )
31+ {
32+ return ProviderMetadataReadResult . NoMatchingImage ;
33+ }
34+
2735 foreach ( CustomAttributeHandle handle in definition . GetCustomAttributes ( ) )
2836 {
2937 EntityHandle constructor = reader . GetCustomAttribute ( handle ) . Constructor ;
@@ -53,9 +61,9 @@ private static bool TryReadProviderMetadata(string path, out bool hasMarker, out
5361 }
5462
5563 references = reader . AssemblyReferences . Select ( handle => reader . GetAssemblyReference ( handle ) . GetAssemblyName ( ) ) . ToArray ( ) ;
56- return true ;
64+ return ProviderMetadataReadResult . Read ;
5765 }
58- catch ( Exception ex ) when ( ex is IOException or BadImageFormatException or UnauthorizedAccessException or SecurityException )
66+ catch ( Exception ex ) when ( ex is IOException or BadImageFormatException or UnauthorizedAccessException or SecurityException or InvalidOperationException or ArgumentException )
5967 {
6068 if ( PlatformServiceProvider . Instance . AdapterTraceLogger . IsWarningEnabled )
6169 {
@@ -65,17 +73,25 @@ private static bool TryReadProviderMetadata(string path, out bool hasMarker, out
6573 ex ) ;
6674 }
6775
68- return false ;
76+ return ProviderMetadataReadResult . Unreadable ;
6977 }
7078 }
7179
80+ private enum ProviderMetadataReadResult
81+ {
82+ Read ,
83+ NoMatchingImage ,
84+ Unreadable ,
85+ }
86+
7287 private sealed class ProviderDiscoveryMetadataResolver
7388 {
7489#if NET && ! WINDOWS_UWP
7590 private Dictionary < string , string ? > ? _trustedAssemblyPaths ;
7691#endif
7792#if NETFRAMEWORK
78- private bool ? _hasCodeBaseOverrides ;
93+ private readonly Dictionary < Assembly , string ? > _loadedAssemblyNames = [ ] ;
94+ private bool ? _hasDomainBindingOverrides ;
7995#endif
8096
8197 [ SuppressMessage ( "Performance" , "CA1822:Mark members as static" , Justification = "The resolver caches runtime-specific metadata paths on other target frameworks." ) ]
@@ -94,37 +110,26 @@ private sealed class ProviderDiscoveryMetadataResolver
94110 }
95111
96112 _trustedAssemblyPaths ??= ReadTrustedAssemblyPaths ( ) ;
97- return _trustedAssemblyPaths . TryGetValue ( referenceName . Name ! , out string ? path )
98- && path is not null
99- && HasMatchingProviderDiscoveryIdentity ( referenceName , path ) ? path : null ;
113+ return _trustedAssemblyPaths . TryGetValue ( referenceName . Name ! , out string ? path ) ? path : null ;
100114#elif NETFRAMEWORK
101115 _ = referrer ;
102116 AppDomain domain = AppDomain . CurrentDomain ;
103- AppDomainSetup setup = domain . SetupInformation ;
104- // Fusion can select a loaded image or the GAC before probing the application base.
105- // Keep those bindings, culture probing, redirects and host overrides authoritative.
106- if ( domain . GetAssemblies ( ) . Any ( assembly =>
107- string . Equals ( assembly . GetName ( ) . Name , referenceName . Name , StringComparison . OrdinalIgnoreCase ) )
108- || referenceName . GetPublicKeyToken ( ) is { Length : > 0 }
117+ // Strong-named binds depend on signature verification, GAC and publisher/machine
118+ // policy. Keep the runtime authoritative even when ApplyPolicy returns the same name.
119+ // The identity read with the metadata rejects any other appbase image.
120+ if ( referenceName . GetPublicKeyToken ( ) is { Length : > 0 }
109121 || ! string . IsNullOrEmpty ( referenceName . CultureName )
110- || setup . DisallowApplicationBaseProbing
111- || setup . PrivateBinPathProbe is not null
112- || setup . GetConfigurationBytes ( ) is { Length : > 0 }
113- || ! string . IsNullOrEmpty ( Environment . GetEnvironmentVariable ( "DEVPATH" ) )
114- || ! string . Equals ( domain . ApplyPolicy ( referenceName . FullName ) , referenceName . FullName , StringComparison . Ordinal )
115- || ( _hasCodeBaseOverrides ??= HasCodeBaseOverrides ( ) ) )
122+ || ( _hasDomainBindingOverrides ??= HasDomainBindingOverrides ( domain ) )
123+ || IsLoaded ( domain , referenceName . Name ! )
124+ || ! string . Equals ( domain . ApplyPolicy ( referenceName . FullName ) , referenceName . FullName , StringComparison . Ordinal ) )
116125 {
117126 return null ;
118127 }
119128
120- foreach ( string extension in new [ ] { ".dll" , ".exe" } )
121- {
122- string path = Path . Combine ( domain . BaseDirectory , referenceName . Name + extension ) ;
123- if ( File . Exists ( path ) )
124- {
125- return HasMatchingProviderDiscoveryIdentity ( referenceName , path ) ? path : null ;
126- }
127- }
129+ // Fusion probes [appbase]/name.dll first; any other hit (name/name.dll, private paths,
130+ // then .exe) depends on probing order we don't replicate, so leave it to the runtime.
131+ string path = Path . Combine ( domain . BaseDirectory , referenceName . Name + ".dll" ) ;
132+ return File . Exists ( path ) ? path : null ;
128133#else
129134 _ = referrer ;
130135 _ = referenceName ;
@@ -169,12 +174,38 @@ private sealed class ProviderDiscoveryMetadataResolver
169174 }
170175#endif
171176
172- #if NETFRAMEWORK || ( NET && ! WINDOWS_UWP )
173- private static bool HasMatchingProviderDiscoveryIdentity ( AssemblyName referenceName , string path )
174- => string . Equals ( referenceName . FullName , AssemblyName . GetAssemblyName ( path ) . FullName , StringComparison . Ordinal ) ;
175- #endif
176-
177177#if NETFRAMEWORK
178+ private static bool HasDomainBindingOverrides ( AppDomain domain )
179+ {
180+ AppDomainSetup setup = domain . SetupInformation ;
181+ return setup . DisallowApplicationBaseProbing
182+ || setup . PrivateBinPathProbe is not null
183+ || setup . GetConfigurationBytes ( ) is { Length : > 0 }
184+ || ! string . IsNullOrEmpty ( Environment . GetEnvironmentVariable ( "DEVPATH" ) )
185+ || HasCodeBaseOverrides ( ) ;
186+ }
187+
188+ private bool IsLoaded ( AppDomain domain , string name )
189+ {
190+ // Query the live list for every reference: discovery itself and user code can load
191+ // assemblies between checks. Only the per-assembly name lookup is cached.
192+ foreach ( Assembly assembly in domain . GetAssemblies ( ) )
193+ {
194+ if ( ! _loadedAssemblyNames . TryGetValue ( assembly , out string ? loadedName ) )
195+ {
196+ loadedName = assembly . GetName ( ) . Name ;
197+ _loadedAssemblyNames [ assembly ] = loadedName ;
198+ }
199+
200+ if ( string . Equals ( loadedName , name , StringComparison . OrdinalIgnoreCase ) )
201+ {
202+ return true ;
203+ }
204+ }
205+
206+ return false ;
207+ }
208+
178209 private static bool HasCodeBaseOverrides ( )
179210 {
180211 string configurationFile = AppDomain . CurrentDomain . SetupInformation . ConfigurationFile ;
0 commit comments