@@ -791,6 +791,168 @@ public void DiagnosticHintDoesNotProbeCrashRuntime()
791791 target . Symbols . Verify ( ) ;
792792 }
793793
794+ [ Theory ]
795+ [ InlineData ( "WINDOWS" , RuntimeEnumerationFlags . Default ) ]
796+ [ InlineData ( "WINDOWS" , RuntimeEnumerationFlags . All ) ]
797+ [ InlineData ( "LINUX" , RuntimeEnumerationFlags . Default ) ]
798+ [ InlineData ( "LINUX" , RuntimeEnumerationFlags . All ) ]
799+ [ InlineData ( "OSX" , RuntimeEnumerationFlags . Default ) ]
800+ [ InlineData ( "OSX" , RuntimeEnumerationFlags . All ) ]
801+ public void OnlyDefaultDiscoveryPrioritizesPreferredModule ( string platform , RuntimeEnumerationFlags flags )
802+ {
803+ OSPlatform os = OSPlatform . Create ( platform ) ;
804+ using RuntimeDiscoveryTestTarget target = new ( os , Architecture . X64 ) ;
805+ target . AddModule ( 0x10000 , ApplicationName ( os , "unrelated" ) , needsSymbols : true ) ;
806+ RuntimeDiscoveryTestTarget . ModuleDefinition automatic = target . AddModule ( 0x20000 , ApplicationName ( os , "automatic" ) , entryPoint : true ) ;
807+ RuntimeDiscoveryTestTarget . ModuleDefinition preferred = target . AddModule ( 0x30000 , ApplicationName ( os , "preferred" ) ) ;
808+ target . AddSingleFileRuntime ( automatic ) ;
809+ target . AddSingleFileRuntime ( preferred ) ;
810+ target . AddDiagnosticHeader ( automatic . ImageBase ) ;
811+ target . Runtimes . PreferredRuntimeModule = preferred . ImageBase ;
812+ if ( flags == RuntimeEnumerationFlags . All )
813+ {
814+ target . Symbols . ExpectSymbolFile ( 0x10000 , result : null ) ;
815+ }
816+
817+ Assert . Equal ( flags == RuntimeEnumerationFlags . All ? [ automatic . ImageBase , preferred . ImageBase ] : new [ ] { preferred . ImageBase } ,
818+ target . Enumerate ( flags ) . Select ( runtime => runtime . RuntimeModule . ImageBase ) ) ;
819+ Assert . Equal ( flags == RuntimeEnumerationFlags . All ? [ automatic . ImageBase , 0x10000UL , preferred . ImageBase ] : new [ ] { preferred . ImageBase } ,
820+ target . ExportProbes . Where ( probe => probe . Symbol == "DotNetRuntimeInfo" ) . Select ( probe => probe . ModuleBase ) ) ;
821+ target . Symbols . Verify ( ) ;
822+ }
823+
824+ [ Theory ]
825+ [ InlineData ( RuntimeEnumerationFlags . Default ) ]
826+ [ InlineData ( RuntimeEnumerationFlags . All ) ]
827+ public void OnlyDefaultDiscoveryProbesPreferredModuleBeforeNormalRuntime ( RuntimeEnumerationFlags flags )
828+ {
829+ using RuntimeDiscoveryTestTarget target = new ( OSPlatform . Linux , Architecture . X64 ) ;
830+ RuntimeDiscoveryTestTarget . ModuleDefinition preferred = target . AddModule ( 0x10000 , "/app/preferred" , needsSymbols : true ) ;
831+ target . AddSingleFileRuntime ( preferred , symbolOnly : true ) ;
832+ target . AddCoreRuntime ( 0x20000 ) ;
833+ target . Runtimes . PreferredRuntimeModule = preferred . ImageBase ;
834+ target . Symbols . ExpectSymbolFile ( preferred . ImageBase , result : "mock-symbols" ) ;
835+
836+ IRuntime [ ] runtimes = target . Enumerate ( flags ) ;
837+ Assert . Equal ( preferred . ImageBase , Assert . Single ( runtimes , runtime => runtime . Services . GetService < ClrInfo > ( ) . IsSingleFile ) . RuntimeModule . ImageBase ) ;
838+ Assert . Equal ( flags == RuntimeEnumerationFlags . All ? 2 : 1 , runtimes . Length ) ;
839+ Assert . Equal ( flags == RuntimeEnumerationFlags . All ? 0x20000UL : preferred . ImageBase , target . ExportProbes . First ( ) . ModuleBase ) ;
840+ Assert . Equal ( [ preferred . ImageBase ] , target . Symbols . Requests ) ;
841+ target . Symbols . Verify ( ) ;
842+ }
843+
844+ [ Theory ]
845+ [ InlineData ( RuntimeEnumerationFlags . Default ) ]
846+ [ InlineData ( RuntimeEnumerationFlags . All ) ]
847+ public void PreferredNamedRuntimeIsOnlyPrioritizedInDefaultDiscovery ( RuntimeEnumerationFlags flags )
848+ {
849+ using RuntimeDiscoveryTestTarget target = new ( OSPlatform . Linux , Architecture . X64 ) ;
850+ target . AddCoreRuntime ( 0x20000 , diagnosticHeader : false ) ;
851+ target . AddCoreRuntime ( 0x30000 , diagnosticHeader : false ) ;
852+ target . Runtimes . PreferredRuntimeModule = 0x30000 ;
853+
854+ Assert . Equal ( flags == RuntimeEnumerationFlags . All ? [ 0x20000UL , 0x30000UL ] : new [ ] { 0x30000UL } ,
855+ target . Enumerate ( flags ) . Select ( runtime => runtime . RuntimeModule . ImageBase ) ) ;
856+ Assert . Equal ( flags == RuntimeEnumerationFlags . All ? [ 0x20000UL , 0x30000UL ] : new [ ] { 0x30000UL } ,
857+ target . ExportProbes . Where ( probe => probe . Symbol == "DotNetRuntimeContractDescriptor" ) . Select ( probe => probe . ModuleBase ) ) ;
858+ target . Symbols . Verify ( ) ;
859+ }
860+
861+ [ Theory ]
862+ [ InlineData ( RuntimeEnumerationFlags . Default ) ]
863+ [ InlineData ( RuntimeEnumerationFlags . All ) ]
864+ public void UnrecognizedPreferredModuleFallsBackToNormalDiscovery ( RuntimeEnumerationFlags flags )
865+ {
866+ using RuntimeDiscoveryTestTarget target = new ( OSPlatform . Linux , Architecture . X64 ) ;
867+ target . AddModule ( 0x10000 , "/app/preferred" , needsSymbols : true ) ;
868+ target . AddCoreRuntime ( 0x20000 ) ;
869+ target . Runtimes . PreferredRuntimeModule = 0x10000 ;
870+ target . Symbols . ExpectSymbolFile ( 0x10000 , result : null ) ;
871+
872+ Assert . Equal ( 0x20000UL , Assert . Single ( target . Enumerate ( flags ) ) . RuntimeModule . ImageBase ) ;
873+ Assert . Single ( target . ExportProbes , probe => probe . ModuleBase == 0x10000 && probe . Symbol == "DotNetRuntimeInfo" ) ;
874+ target . Symbols . Verify ( ) ;
875+ }
876+
877+ [ Theory ]
878+ [ InlineData ( RuntimeEnumerationFlags . Default ) ]
879+ [ InlineData ( RuntimeEnumerationFlags . All ) ]
880+ public void UnrecognizedPreferredModuleRetainsUnhintedSingleFileFallback ( RuntimeEnumerationFlags flags )
881+ {
882+ using RuntimeDiscoveryTestTarget target = new ( OSPlatform . Linux , Architecture . X64 ) ;
883+ target . AddModule ( 0x10000 , "/app/preferred" , needsSymbols : true ) ;
884+ RuntimeDiscoveryTestTarget . ModuleDefinition app = target . AddModule ( 0x20000 , "/app/singlefile" , needsSymbols : true ) ;
885+ target . AddSingleFileRuntime ( app , symbolOnly : true ) ;
886+ target . Runtimes . PreferredRuntimeModule = 0x10000 ;
887+ target . Symbols . ExpectSymbolFile ( 0x10000 , result : null ) ;
888+ target . Symbols . ExpectSymbolFile ( app . ImageBase , result : "mock-symbols" ) ;
889+
890+ Assert . Equal ( app . ImageBase , Assert . Single ( target . Enumerate ( flags ) ) . RuntimeModule . ImageBase ) ;
891+ Assert . Equal ( [ 0x10000UL , app . ImageBase ] , target . Symbols . Requests ) ;
892+ target . Symbols . Verify ( ) ;
893+ }
894+
895+ [ Theory ]
896+ [ InlineData ( RuntimeEnumerationFlags . Default ) ]
897+ [ InlineData ( RuntimeEnumerationFlags . All ) ]
898+ public void UnrecognizedPreferredModuleFallsBackWithoutRetrying ( RuntimeEnumerationFlags flags )
899+ {
900+ using RuntimeDiscoveryTestTarget target = new ( OSPlatform . Linux , Architecture . X64 ) ;
901+ target . AddModule ( 0x10000 , "/app/preferred" , needsSymbols : true ) ;
902+ RuntimeDiscoveryTestTarget . ModuleDefinition automatic = target . AddModule ( 0x20000 , "/app/automatic" ) ;
903+ target . AddSingleFileRuntime ( automatic ) ;
904+ target . AddDiagnosticHeader ( automatic . ImageBase ) ;
905+ target . Runtimes . PreferredRuntimeModule = 0x10000 ;
906+ target . Symbols . ExpectSymbolFile ( 0x10000 , result : null ) ;
907+
908+ Assert . Equal ( automatic . ImageBase , Assert . Single ( target . Enumerate ( flags ) ) . RuntimeModule . ImageBase ) ;
909+ Assert . Equal ( flags == RuntimeEnumerationFlags . All ? [ automatic . ImageBase , 0x10000UL ] : new [ ] { 0x10000UL , automatic . ImageBase } , target . ExportProbes
910+ . Where ( probe => probe . Symbol == "DotNetRuntimeInfo" ) . Select ( probe => probe . ModuleBase ) ) ;
911+ target . Symbols . Verify ( ) ;
912+ }
913+
914+ [ Fact ]
915+ public void PreferredModuleTriesAllProvidersBeforeAutomaticHints ( )
916+ {
917+ List < ( int Priority , ulong ModuleBase ) > calls = [ ] ;
918+ using RuntimeDiscoveryTestTarget target = new ( OSPlatform . Linux , Architecture . X64 , manager => {
919+ foreach ( int priority in new [ ] { 100 , 50 } )
920+ {
921+ manager . AddProviderFactory ( typeof ( IRuntimeDescriptionProvider ) , _ =>
922+ new TestRuntimeDiscoveryProvider ( module => {
923+ calls . Add ( ( priority , module . ImageBase ) ) ;
924+ return module . ImageBase == ( priority == 100 ? 0x20000UL : 0x30000UL )
925+ ? new RuntimeDescription ( module , RuntimeType . NativeAOT , new Version ( 1 , 0 ) )
926+ : null ;
927+ } , priority ) ) ;
928+ }
929+ } ) ;
930+ target . AddModule ( 0x20000 , "/app/automatic" ) ;
931+ target . AddModule ( 0x30000 , "/app/preferred" ) ;
932+ target . AddDiagnosticHeader ( 0x20000 ) ;
933+ target . Runtimes . PreferredRuntimeModule = 0x30000 ;
934+
935+ Assert . Equal ( 0x30000UL , Assert . Single ( target . Enumerate ( ) ) . RuntimeModule . ImageBase ) ;
936+ Assert . Equal ( [ ( 100 , 0x30000UL ) , ( 50 , 0x30000UL ) ] , calls ) ;
937+ target . Symbols . Verify ( ) ;
938+ }
939+
940+ [ Theory ]
941+ [ InlineData ( RuntimeEnumerationFlags . Default ) ]
942+ [ InlineData ( RuntimeEnumerationFlags . All ) ]
943+ public void PreferredModuleMatchingAutomaticHintIsOnlyProbedOnce ( RuntimeEnumerationFlags flags )
944+ {
945+ using RuntimeDiscoveryTestTarget target = new ( OSPlatform . Linux , Architecture . X64 ) ;
946+ RuntimeDiscoveryTestTarget . ModuleDefinition app = target . AddModule ( 0x20000 , "/app/preferred" ) ;
947+ target . AddSingleFileRuntime ( app ) ;
948+ target . AddDiagnosticHeader ( app . ImageBase ) ;
949+ target . Runtimes . PreferredRuntimeModule = app . ImageBase ;
950+
951+ Assert . Equal ( app . ImageBase , Assert . Single ( target . Enumerate ( flags ) ) . RuntimeModule . ImageBase ) ;
952+ Assert . Single ( target . ExportProbes , probe => probe . Symbol == "DotNetRuntimeInfo" ) ;
953+ target . Symbols . Verify ( ) ;
954+ }
955+
794956 private sealed class TestRuntimeDiscoveryProvider ( Func < IModule , RuntimeDescription > describe , int priority = 0 ) : IRuntimeDescriptionProvider
795957 {
796958 public int Priority => priority ;
0 commit comments