@@ -343,6 +343,7 @@ HRESULT STDMETHODCALLTYPE CorProfiler::Initialize(IUnknown* cor_profiler_info_un
343
343
344
344
//
345
345
managed_profiler_assembly_reference = AssemblyReference::GetFromCache (managed_profiler_full_assembly_version);
346
+ managedInternalModules_.reserve (10 );
346
347
347
348
const auto currentModuleFileName = shared::GetCurrentModuleFileName ();
348
349
if (currentModuleFileName == shared::EmptyWStr)
@@ -504,8 +505,7 @@ HRESULT STDMETHODCALLTYPE CorProfiler::AssemblyLoadFinished(AssemblyID assembly_
504
505
return S_OK;
505
506
}
506
507
507
- void CorProfiler::RewritingPInvokeMaps (const ModuleID module_id,
508
- const ModuleMetadata& module_metadata,
508
+ void CorProfiler::RewritingPInvokeMaps (const ModuleMetadata& module_metadata,
509
509
const shared::WSTRING& rewrite_reason,
510
510
const shared::WSTRING& nativemethods_type_name,
511
511
const shared::WSTRING& library_path)
@@ -570,10 +570,6 @@ void CorProfiler::RewritingPInvokeMaps(const ModuleID module_id,
570
570
shared::WSTRING (importName).c_str (),
571
571
profiler_ref);
572
572
573
- // Store this methodDef token in the internal tokens list
574
- auto intTokens = internal_rewrite_tokens.Get ();
575
- intTokens->insert ({ module_id, methodDef });
576
-
577
573
if (FAILED (hr))
578
574
{
579
575
Logger::Warn (" ModuleLoadFinished: DefinePinvokeMap to the actual profiler file path "
@@ -799,6 +795,7 @@ HRESULT CorProfiler::TryRejitModule(ModuleID module_id, std::vector<ModuleID>& m
799
795
module_info.assembly .name == system_private_corelib_assemblyName))
800
796
{
801
797
corlib_module_loaded = true ;
798
+ corlib_module_id = module_id;
802
799
corlib_app_domain_id = app_domain_id;
803
800
804
801
ComPtr<IUnknown> metadata_interfaces;
@@ -909,9 +906,6 @@ HRESULT CorProfiler::TryRejitModule(ModuleID module_id, std::vector<ModuleID>& m
909
906
}
910
907
}
911
908
912
- // Add the module_id to the module metadata vector (to allow NGEN analysis later)
913
- modules.push_back (module_id);
914
-
915
909
if (module_info.assembly .name == managed_profiler_name)
916
910
{
917
911
// Fix PInvoke Rewriting
@@ -940,17 +934,17 @@ HRESULT CorProfiler::TryRejitModule(ModuleID module_id, std::vector<ModuleID>& m
940
934
const auto & assemblyVersion = assemblyImport.version .str ();
941
935
942
936
Logger::Info (" ModuleLoadFinished: " , managed_profiler_name, " v" , assemblyVersion, " - Fix PInvoke maps" );
943
- managedProfilerModuleId_ = module_id;
937
+ managedInternalModules_. push_back ( module_id) ;
944
938
#ifdef _WIN32
945
- RewritingPInvokeMaps (module_id, module_metadata, WStr (" windows" ), windows_nativemethods_type);
946
- RewritingPInvokeMaps (module_id, module_metadata, WStr (" ASM" ), appsec_windows_nativemethods_type);
947
- RewritingPInvokeMaps (module_id, module_metadata, WStr (" debugger" ), debugger_windows_nativemethods_type);
948
- RewritingPInvokeMaps (module_id, module_metadata, WStr (" fault_tolerant" ), fault_tolerant_windows_nativemethods_type);
939
+ RewritingPInvokeMaps (module_metadata, WStr (" windows" ), windows_nativemethods_type);
940
+ RewritingPInvokeMaps (module_metadata, WStr (" ASM" ), appsec_windows_nativemethods_type);
941
+ RewritingPInvokeMaps (module_metadata, WStr (" debugger" ), debugger_windows_nativemethods_type);
942
+ RewritingPInvokeMaps (module_metadata, WStr (" fault_tolerant" ), fault_tolerant_windows_nativemethods_type);
949
943
#else
950
- RewritingPInvokeMaps (module_id, module_metadata, WStr (" non-windows" ), nonwindows_nativemethods_type);
951
- RewritingPInvokeMaps (module_id, module_metadata, WStr (" ASM" ), appsec_nonwindows_nativemethods_type);
952
- RewritingPInvokeMaps (module_id, module_metadata, WStr (" debugger" ), debugger_nonwindows_nativemethods_type);
953
- RewritingPInvokeMaps (module_id, module_metadata, WStr (" fault_tolerant" ), fault_tolerant_nonwindows_nativemethods_type);
944
+ RewritingPInvokeMaps (module_metadata, WStr (" non-windows" ), nonwindows_nativemethods_type);
945
+ RewritingPInvokeMaps (module_metadata, WStr (" ASM" ), appsec_nonwindows_nativemethods_type);
946
+ RewritingPInvokeMaps (module_metadata, WStr (" debugger" ), debugger_nonwindows_nativemethods_type);
947
+ RewritingPInvokeMaps (module_metadata, WStr (" fault_tolerant" ), fault_tolerant_nonwindows_nativemethods_type);
954
948
#endif // _WIN32
955
949
956
950
mdTypeDef bubbleUpTypeDef;
@@ -964,15 +958,15 @@ HRESULT CorProfiler::TryRejitModule(ModuleID module_id, std::vector<ModuleID>& m
964
958
if (fs::exists (native_loader_library_path))
965
959
{
966
960
auto native_loader_file_path = shared::ToWSTRING (native_loader_library_path);
967
- RewritingPInvokeMaps (module_id, module_metadata, WStr (" native loader" ), native_loader_nativemethods_type, native_loader_file_path);
961
+ RewritingPInvokeMaps (module_metadata, WStr (" native loader" ), native_loader_nativemethods_type, native_loader_file_path);
968
962
}
969
963
970
964
if (ShouldRewriteProfilerMaps ())
971
965
{
972
966
auto profiler_library_path = shared::GetEnvironmentValue (WStr (" DD_INTERNAL_PROFILING_NATIVE_ENGINE_PATH" ));
973
967
if (!profiler_library_path.empty () && fs::exists (profiler_library_path))
974
968
{
975
- RewritingPInvokeMaps (module_id, module_metadata, WStr (" continuous profiler" ), profiler_nativemethods_type, profiler_library_path);
969
+ RewritingPInvokeMaps (module_metadata, WStr (" continuous profiler" ), profiler_nativemethods_type, profiler_library_path);
976
970
}
977
971
}
978
972
@@ -1042,11 +1036,14 @@ HRESULT CorProfiler::TryRejitModule(ModuleID module_id, std::vector<ModuleID>& m
1042
1036
const auto & assemblyVersion = assemblyImport.version .str ();
1043
1037
1044
1038
Logger::Info (" ModuleLoadFinished: " , manual_instrumentation_name, " v" , assemblyVersion, " - RewriteIsManualInstrumentationOnly" );
1039
+ managedInternalModules_.push_back (module_id);
1045
1040
1046
1041
// Rewrite Instrumentation.IsManualInstrumentationOnly()
1047
1042
RewriteIsManualInstrumentationOnly (module_metadata, module_id);
1048
1043
}
1049
1044
1045
+ modules.push_back (module_id);
1046
+
1050
1047
bool searchForTraceAttribute = trace_annotations_enabled;
1051
1048
if (searchForTraceAttribute)
1052
1049
{
@@ -1761,7 +1758,6 @@ HRESULT STDMETHODCALLTYPE CorProfiler::AppDomainShutdownFinished(AppDomainID app
1761
1758
const auto & count = first_jit_compilation_app_domains.erase (appDomainId);
1762
1759
1763
1760
Logger::Debug (" AppDomainShutdownFinished: AppDomain: " , appDomainId, " , removed " , count, " elements" );
1764
-
1765
1761
return S_OK;
1766
1762
}
1767
1763
@@ -2721,10 +2717,8 @@ HRESULT CorProfiler::RewriteForDistributedTracing(const ModuleMetadata& module_m
2721
2717
module_metadata.metadata_import ));
2722
2718
}
2723
2719
2724
- // Store this methodDef token in the internal tokens list
2725
- auto intTokens = internal_rewrite_tokens.Get ();
2726
- intTokens->insert ({ module_id, getDistributedTraceMethodDef });
2727
-
2720
+ Logger::Debug (" MethodDef was added as an internal rewrite: " , getDistributedTraceMethodDef);
2721
+ getDistributedTraceMethodDef_ = getDistributedTraceMethodDef;
2728
2722
return hr;
2729
2723
}
2730
2724
@@ -2799,8 +2793,7 @@ HRESULT CorProfiler::RewriteForTelemetry(const ModuleMetadata& module_metadata,
2799
2793
2800
2794
// Store this methodDef token in the internal tokens list
2801
2795
Logger::Debug (" MethodDef was added as an internal rewrite: " , getNativeTracerVersionMethodDef);
2802
- auto intTokens = internal_rewrite_tokens.Get ();
2803
- intTokens->insert ({ module_id, getNativeTracerVersionMethodDef });
2796
+ getNativeTracerVersionMethodDef_ = getNativeTracerVersionMethodDef;
2804
2797
2805
2798
return hr;
2806
2799
}
@@ -2866,8 +2859,7 @@ HRESULT CorProfiler::RewriteIsManualInstrumentationOnly(const ModuleMetadata& mo
2866
2859
2867
2860
// Store this methodDef token in the internal tokens list
2868
2861
Logger::Debug (" MethodDef was added as an internal rewrite: " , isAutoEnabledMethodDef);
2869
- auto intTokens = internal_rewrite_tokens.Get ();
2870
- intTokens->insert ({ module_id, isAutoEnabledMethodDef });
2862
+ isManualInstrumentationOnlyMethodDef_ = isAutoEnabledMethodDef;
2871
2863
2872
2864
return hr;
2873
2865
}
@@ -3256,10 +3248,6 @@ HRESULT CorProfiler::RunILStartupHook(const ComPtr<IMetaDataEmit2>& metadata_emi
3256
3248
return hr;
3257
3249
}
3258
3250
3259
- // Store this methodDef token in the internal tokens list
3260
- auto intTokens = internal_rewrite_tokens.Get ();
3261
- intTokens->insert ({ module_id, function_token });
3262
-
3263
3251
return S_OK;
3264
3252
}
3265
3253
@@ -4364,11 +4352,31 @@ HRESULT STDMETHODCALLTYPE CorProfiler::JITCachedFunctionSearchStarted(FunctionID
4364
4352
}
4365
4353
4366
4354
auto _ = trace::Stats::Instance ()->JITCachedFunctionSearchStartedMeasure ();
4367
- if (! pbUseCachedFunction)
4355
+ if (pbUseCachedFunction == nullptr || !* pbUseCachedFunction)
4368
4356
{
4369
4357
return S_OK;
4370
4358
}
4371
4359
4360
+ // Extract Module metadata
4361
+ ModuleID module_id;
4362
+ mdToken function_token = mdTokenNil;
4363
+
4364
+ HRESULT hr = this ->info_ ->GetFunctionInfo (functionId, nullptr , &module_id, &function_token);
4365
+ if (FAILED (hr))
4366
+ {
4367
+ Logger::Warn (" JITCachedFunctionSearchStarted: Call to ICorProfilerInfo.GetFunctionInfo() failed for " ,
4368
+ functionId);
4369
+ return S_OK;
4370
+ }
4371
+
4372
+ // Verify if is the COR module
4373
+ if (module_id == corlib_module_id)
4374
+ {
4375
+ // we don't rewrite the COR module, so we accept all the images from there.
4376
+ *pbUseCachedFunction = true ;
4377
+ return S_OK;
4378
+ }
4379
+
4372
4380
// keep this lock until we are done using the module,
4373
4381
// to prevent it from unloading while in use
4374
4382
auto modulesOpt = module_ids.TryGet ();
@@ -4382,18 +4390,6 @@ HRESULT STDMETHODCALLTYPE CorProfiler::JITCachedFunctionSearchStarted(FunctionID
4382
4390
4383
4391
auto & modules = modulesOpt.value ();
4384
4392
4385
- // Extract Module metadata
4386
- ModuleID module_id;
4387
- mdToken function_token = mdTokenNil;
4388
-
4389
- HRESULT hr = this ->info_ ->GetFunctionInfo (functionId, nullptr , &module_id, &function_token);
4390
- if (FAILED (hr))
4391
- {
4392
- Logger::Warn (" JITCachedFunctionSearchStarted: Call to ICorProfilerInfo4.GetFunctionInfo() failed for " ,
4393
- functionId);
4394
- return S_OK;
4395
- }
4396
-
4397
4393
// Call RequestRejitOrRevert for register inliners and current NGEN module.
4398
4394
if (rejit_handler != nullptr )
4399
4395
{
@@ -4410,30 +4406,50 @@ HRESULT STDMETHODCALLTYPE CorProfiler::JITCachedFunctionSearchStarted(FunctionID
4410
4406
return S_OK;
4411
4407
}
4412
4408
4409
+ bool isAnInternalModule = false ;
4410
+
4413
4411
// Verify that we have the metadata for this module
4414
4412
if (!shared::Contains (modules.Ref (), module_id))
4415
4413
{
4416
- // we haven't stored a ModuleMetadata for this module,
4417
- // so there's nothing to do here, we accept the NGEN image.
4418
- *pbUseCachedFunction = true ;
4419
- return S_OK;
4414
+ isAnInternalModule = shared::Contains (managedInternalModules_, module_id);
4415
+ if (!isAnInternalModule)
4416
+ {
4417
+ // we haven't stored a ModuleMetadata for this module,
4418
+ // so there's nothing to do here, we accept the NGEN image.
4419
+ *pbUseCachedFunction = true ;
4420
+ return S_OK;
4421
+ }
4420
4422
}
4421
4423
4422
- const auto & module_info = GetModuleInfo (this ->info_ , module_id);
4423
- if (!module_info.IsValid ())
4424
+ // let's get the AssemblyID
4425
+ DWORD module_path_len = 0 ;
4426
+ AssemblyID assembly_id = 0 ;
4427
+ hr = this ->info_ ->GetModuleInfo (module_id, nullptr , 0 , &module_path_len,
4428
+ nullptr , &assembly_id);
4429
+ if (FAILED (hr) || module_path_len == 0 )
4424
4430
{
4425
- Logger::Debug (" Disabling NGEN. ModuleInfo is not valid. ModuleId=" , module_id);
4431
+ Logger::Warn (" JITCachedFunctionSearchStarted: Call to ICorProfilerInfo.GetModuleInfo() failed for " ,
4432
+ functionId);
4426
4433
return S_OK;
4427
4434
}
4428
4435
4429
- const auto & appDomainId = module_info.assembly .app_domain_id ;
4436
+ // now the assembly info
4437
+ DWORD assembly_name_len = 0 ;
4438
+ AppDomainID app_domain_id;
4439
+ hr = this ->info_ ->GetAssemblyInfo (assembly_id, 0 , &assembly_name_len, nullptr , &app_domain_id, nullptr );
4440
+ if (FAILED (hr) || assembly_name_len == 0 )
4441
+ {
4442
+ Logger::Warn (" JITCachedFunctionSearchStarted: Call to ICorProfilerInfo.GetAssemblyInfo() failed for " ,
4443
+ functionId);
4444
+ return S_OK;
4445
+ }
4430
4446
4431
4447
const bool has_loader_injected_in_appdomain =
4432
- first_jit_compilation_app_domains.find (appDomainId ) != first_jit_compilation_app_domains.end ();
4448
+ first_jit_compilation_app_domains.find (app_domain_id ) != first_jit_compilation_app_domains.end ();
4433
4449
4434
4450
if (!has_loader_injected_in_appdomain)
4435
4451
{
4436
- Logger::Debug (" Disabling NGEN due to missing loader." );
4452
+ Logger::Debug (" JITCachedFunctionSearchStarted: Disabling NGEN due to missing loader." );
4437
4453
// The loader is missing in this AppDomain, we skip the NGEN image to allow the JITCompilationStart inject
4438
4454
// it.
4439
4455
*pbUseCachedFunction = false ;
@@ -4442,37 +4458,32 @@ HRESULT STDMETHODCALLTYPE CorProfiler::JITCachedFunctionSearchStarted(FunctionID
4442
4458
4443
4459
// Let's check if the method has been rewritten internally
4444
4460
// if that's the case we don't accept the image
4445
- auto internalTokensOpt = internal_rewrite_tokens.TryGet ();
4446
- if (!internalTokensOpt.has_value ())
4447
- {
4448
- Logger::Error (
4449
- " JITCachedFunctionSearchStarted: Failed on exception while tried to acquire the lock for the internal_rewrite_tokens collection for functionId " ,
4450
- functionId);
4451
- return S_OK;
4452
- }
4453
-
4454
- auto & internalTokens = internalTokensOpt.value ();
4455
- bool hasBeenRewritten = internalTokens->find ({ module_id, function_token }) != internalTokens->end ();
4461
+ bool isKnownMethodDef =
4462
+ function_token == getDistributedTraceMethodDef_ ||
4463
+ function_token == getNativeTracerVersionMethodDef_ ||
4464
+ function_token == isManualInstrumentationOnlyMethodDef_;
4465
+ bool hasBeenRewritten = isKnownMethodDef && isAnInternalModule;
4456
4466
if (hasBeenRewritten)
4457
4467
{
4458
4468
// If we are in debug mode and the image is rejected because has been rewritten then let's write a couple of logs
4459
4469
if (Logger::IsDebugEnabled ())
4460
4470
{
4471
+ const auto & module_info = GetModuleInfo (this ->info_ , module_id);
4461
4472
ComPtr<IUnknown> metadata_interfaces;
4462
4473
if (this ->info_ ->GetModuleMetaData (module_id, ofRead, IID_IMetaDataImport2,
4463
4474
metadata_interfaces.GetAddressOf ()) == S_OK)
4464
4475
{
4465
4476
const auto & metadata_import = metadata_interfaces.As <IMetaDataImport2>(IID_IMetaDataImport);
4466
4477
auto functionInfo = GetFunctionInfo (metadata_import, function_token);
4467
4478
4468
- Logger::Debug (" NGEN Image : Rejected (because rewritten) for Module: " , module_info.assembly .name ,
4479
+ Logger::Debug (" JITCachedFunctionSearchStarted : Rejected (because rewritten) for Module: " , module_info.assembly .name ,
4469
4480
" , Method:" , functionInfo.type .name , " ." , functionInfo.name ,
4470
4481
" () previous value = " , *pbUseCachedFunction ? " true" : " false" , " [moduleId=" , module_id,
4471
4482
" , methodDef=" , HexStr (function_token), " ]" );
4472
4483
}
4473
4484
else
4474
4485
{
4475
- Logger::Debug (" NGEN Image : Rejected (because rewritten) for Module: " , module_info.assembly .name ,
4486
+ Logger::Debug (" JITCachedFunctionSearchStarted : Rejected (because rewritten) for Module: " , module_info.assembly .name ,
4476
4487
" , Function: " , HexStr (function_token),
4477
4488
" previous value = " , *pbUseCachedFunction ? " true" : " false" );
4478
4489
}
@@ -4482,10 +4493,6 @@ HRESULT STDMETHODCALLTYPE CorProfiler::JITCachedFunctionSearchStarted(FunctionID
4482
4493
*pbUseCachedFunction = false ;
4483
4494
return S_OK;
4484
4495
}
4485
- else
4486
- {
4487
- Logger::Debug (" JITCachedFunctionSearchStarted: non rejected by internal token on token: " , function_token, " Looking into a hashset with " , internalTokens->size (), " elements" );
4488
- }
4489
4496
4490
4497
// JITCachedFunctionSearchStarted has a different behaviour between .NET Framework and .NET Core
4491
4498
// On .NET Framework when we reject bcl images the rejit calls of the integrations for those bcl assemblies
@@ -4504,21 +4511,22 @@ HRESULT STDMETHODCALLTYPE CorProfiler::JITCachedFunctionSearchStarted(FunctionID
4504
4511
// If we are in debug mode and the image is rejected because has been rejitted then let's write a couple of logs
4505
4512
if (Logger::IsDebugEnabled () && hasBeenRejitted)
4506
4513
{
4514
+ const auto & module_info = GetModuleInfo (this ->info_ , module_id);
4507
4515
ComPtr<IUnknown> metadata_interfaces;
4508
4516
if (this ->info_ ->GetModuleMetaData (module_id, ofRead, IID_IMetaDataImport2,
4509
4517
metadata_interfaces.GetAddressOf ()) == S_OK)
4510
4518
{
4511
4519
const auto & metadata_import = metadata_interfaces.As <IMetaDataImport2>(IID_IMetaDataImport);
4512
4520
auto functionInfo = GetFunctionInfo (metadata_import, function_token);
4513
4521
4514
- Logger::Debug (" NGEN Image : Rejected (because rejitted) for Module: " , module_info.assembly .name ,
4522
+ Logger::Debug (" JITCachedFunctionSearchStarted : Rejected (because rejitted) for Module: " , module_info.assembly .name ,
4515
4523
" , Method:" , functionInfo.type .name , " ." , functionInfo.name ,
4516
4524
" () previous value = " , *pbUseCachedFunction ? " true" : " false" , " [moduleId=" , module_id,
4517
4525
" , methodDef=" , HexStr (function_token), " ]" );
4518
4526
}
4519
4527
else
4520
4528
{
4521
- Logger::Debug (" NGEN Image : Rejected (because rejitted) for Module: " , module_info.assembly .name ,
4529
+ Logger::Debug (" JITCachedFunctionSearchStarted : Rejected (because rejitted) for Module: " , module_info.assembly .name ,
4522
4530
" , Function: " , HexStr (function_token),
4523
4531
" previous value = " , *pbUseCachedFunction ? " true" : " false" );
4524
4532
}
0 commit comments