-
Notifications
You must be signed in to change notification settings - Fork 142
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Subscribe to AssemblyLoadContext.Default.Resolving (#6148)
## Summary of changes In the managed loader, subscribe to `AssemblyLoadContext.Default.Resolving` in addition to `AppDomain.CurrentDomain.AssemblyResolve`. ## Reason for change When an assembly is loaded implicitly (because it's referenced in some code), the JIT compiler calls `AssemblyLoadContext.ResolveUsingResolvingEvent`. This method asks the current ALC to resolve the assembly, and it will in turn raise its `Resolving` event if it fails. If the active ALC `Resolving` event fails, then the global `AssemblyLoadContext.AssemblyResolve` event is finally raised. In the managed loader, we subscribe to `AppDomain.Current.AssemblyResolve`, which is a wrapper around `AssemblyLoadContext.AssemblyResolve`. It means that the ALC `Resolving` event has a chance to resolve assemblies before we do. `System.Management.Automation`, a powershell library, subscribes to `AssemblyLoadContext.Default.Resolving`. In its resolution logic, it loads assemblies from the GAC if they're not found, thus loading the .NET Framework version of Datadog.Trace even if the process is running .NET Core. By subscribing to `AssemblyLoadContext.Default.Resolving`, we prevent that from happining. ## Test coverage Added a smoke test. ## Other details Fixes #6135
- Loading branch information
1 parent
5eb0180
commit 34444ea
Showing
5 changed files
with
98 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
...test-applications/regression/AssemblyLoadContextResolve/AssemblyLoadContextResolve.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFrameworks>netcoreapp2.1;netcoreapp3.0;netcoreapp3.1;net5.0;net6.0;net7.0;net8.0</TargetFrameworks> | ||
</PropertyGroup> | ||
|
||
</Project> |
56 changes: 56 additions & 0 deletions
56
tracer/test/test-applications/regression/AssemblyLoadContextResolve/Program.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
using System; | ||
using System.Collections.Concurrent; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Reflection; | ||
using System.Runtime.Loader; | ||
|
||
namespace AssemblyLoadContextResolve; | ||
|
||
internal class Program | ||
{ | ||
private static ConcurrentStack<string> _assemblyResolveCalls = new(); | ||
|
||
private const string TestAssemblyName = "datadog_test_assembly"; | ||
|
||
static void Main(string[] args) | ||
{ | ||
AssemblyLoadContext.Default.Resolving += AssemblyResolving; | ||
|
||
var traceAssembly = Assembly.Load("Datadog.Trace"); | ||
var alc = AssemblyLoadContext.GetLoadContext(traceAssembly); | ||
|
||
if (alc.GetType().FullName != "Datadog.Trace.ClrProfiler.Managed.Loader.ManagedProfilerAssemblyLoadContext") | ||
{ | ||
throw new InvalidOperationException($"Datadog.Trace was loaded in the wrong ALC: {alc.GetType()}"); | ||
} | ||
|
||
try | ||
{ | ||
var testAssembly = Assembly.Load(TestAssemblyName); | ||
throw new InvalidOperationException($"Test assembly was found, this shouldn't happen: {testAssembly}"); | ||
} | ||
catch (FileNotFoundException) | ||
{ | ||
// Expected | ||
} | ||
|
||
var resolvedAssemblies = _assemblyResolveCalls.ToList(); | ||
|
||
if (!resolvedAssemblies.Contains(TestAssemblyName)) | ||
{ | ||
throw new InvalidOperationException($"AssemblyResolving should have been called for {TestAssemblyName}: {string.Join(", ", resolvedAssemblies)}"); | ||
} | ||
|
||
if (resolvedAssemblies.Contains("Datadog.Trace")) | ||
{ | ||
throw new InvalidOperationException($"AssemblyResolving shouldn't have been called for Datadog.Trace: {string.Join(", ", resolvedAssemblies)}"); | ||
} | ||
} | ||
|
||
private static Assembly AssemblyResolving(AssemblyLoadContext alc, AssemblyName assemblyname) | ||
{ | ||
_assemblyResolveCalls.Push(assemblyname?.Name); | ||
return null; | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
...st/test-applications/regression/AssemblyLoadContextResolve/Properties/launchSettings.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
{ | ||
"profiles": { | ||
"AssemblyLoadContextResolve": { | ||
"commandName": "Project", | ||
"environmentVariables": { | ||
"CORECLR_ENABLE_PROFILING": "1", | ||
"CORECLR_PROFILER": "{846F5F1C-F9AE-4B07-969E-05C26BC060D8}", | ||
"CORECLR_PROFILER_PATH": "$(SolutionDir)shared\\bin\\monitoring-home\\tracer\\win-$(Platform)\\Datadog.Trace.ClrProfiler.Native.dll", | ||
|
||
"DD_DOTNET_TRACER_HOME": "$(SolutionDir)shared\\bin\\monitoring-home\\tracer", | ||
"DD_VERSION": "1.0.0" | ||
}, | ||
"nativeDebugging": true | ||
} | ||
} | ||
} |