Skip to content

Prevent AssemblyResolve Event from causing infinite recursion on mscorlib resource lookup#573

Merged
zacharycmontoya merged 9 commits into
masterfrom
zach/bug-fix/assembly-resolve-infinite-recursion
Dec 2, 2019
Merged

Prevent AssemblyResolve Event from causing infinite recursion on mscorlib resource lookup#573
zacharycmontoya merged 9 commits into
masterfrom
zach/bug-fix/assembly-resolve-infinite-recursion

Conversation

@zacharycmontoya

Copy link
Copy Markdown
Contributor

Changes proposed in this pull request:
Modifies the AssemblyResolve event handler in the Datadog.Trace.ClrProfiler.Managed.Loader assembly to eagerly exit if the requested assembly is mscorlib.resources or System.Private.CoreLib.resources on .NET Framework and .NET Core, respectively.

This fixes a known issue where on a non-US locale, a lookup of an mscorlib resource invokes the AssemblyResolve event and our event handler causes infinite recursion and the .NET application to crash. This occurred because, in processing the event, the event handler would make a method call to File.Exists, which triggers yet another AssemblyResolve event for mscorlib and the cycle continues. Even though this has only been demonstrated to occur on .NET Framework and should not occur on .NET Core 2.0+ (see https://github.com/dotnet/coreclr/issues/12668), proactively guard against AssemblyResolve events for System.Private.CoreLib.resources as well.

@DataDog/apm-dotnet

@zacharycmontoya zacharycmontoya added area:native-library Automatic instrumentation native C++ code (Datadog.Trace.ClrProfiler.Native) priority:high labels Nov 27, 2019
@zacharycmontoya zacharycmontoya added this to the 1.10.1 milestone Nov 27, 2019
@zacharycmontoya zacharycmontoya requested a review from a team as a code owner November 27, 2019 00:41
@zacharycmontoya zacharycmontoya self-assigned this Nov 27, 2019
Comment thread src/Datadog.Trace.ClrProfiler.Managed.Loader/Startup.NetCore.cs
// to enter the AssemblyResolve event when searching for resources
// in its satellite assemblies. Exit early so we don't cause
// infinite recursion.
if (string.Equals(assemblyName, "mscorlib.resources", StringComparison.OrdinalIgnoreCase))

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the fix for .NET Framework. Since the fix is commented out, we will see a failure in CI, at which point I will re-enable the fix.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be safer to check for any resource assembly (*.resources)?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No other resource assembly will cause this recursion issue so I think it's helpful to single out this particular issue. We could add another check for resources assemblies in general as a small optimization if you'd like, but it wouldn't necessarily provide any more safety.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we not limit our checks to only our dependencies?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In other words, wouldn't this be resolved by short-circuiting when the assembly name is not one of those WE need?

@zacharycmontoya zacharycmontoya merged commit 58f948f into master Dec 2, 2019
@zacharycmontoya zacharycmontoya deleted the zach/bug-fix/assembly-resolve-infinite-recursion branch December 2, 2019 22:41
@lucaspimentel lucaspimentel modified the milestones: 1.10.1, 1.11.0 Dec 10, 2019
MikeGoldsmith pushed a commit to lightstep/ls-trace-dotnet that referenced this pull request Mar 20, 2020
…rlib resource lookup (DataDog#573)

Modifies the AssemblyResolve event handler in the Datadog.Trace.ClrProfiler.Managed.Loader assembly to eagerly exit if the requested assembly is `mscorlib.resources` or `System.Private.CoreLib.resources` on .NET Framework and .NET Core, respectively.

This fixes a known issue where on a non-US locale, a lookup of an `mscorlib` resource invokes the AssemblyResolve event and our event handler causes infinite recursion and the .NET application to crash. This occurred because, in processing the event, the event handler would make a method call to `File.Exists`, which triggers yet another AssemblyResolve event for `mscorlib` and the cycle continues. Even though this has only been demonstrated to occur on .NET Framework and should not occur on .NET Core 2.0+ (see https://github.com/dotnet/coreclr/issues/12668), proactively guard against AssemblyResolve events for `System.Private.CoreLib.resources` as well.
macrogreg pushed a commit that referenced this pull request Aug 20, 2021
…rlib resource lookup (#573)

Modifies the AssemblyResolve event handler in the Datadog.Trace.ClrProfiler.Managed.Loader assembly to eagerly exit if the requested assembly is `mscorlib.resources` or `System.Private.CoreLib.resources` on .NET Framework and .NET Core, respectively.

This fixes a known issue where on a non-US locale, a lookup of an `mscorlib` resource invokes the AssemblyResolve event and our event handler causes infinite recursion and the .NET application to crash. This occurred because, in processing the event, the event handler would make a method call to `File.Exists`, which triggers yet another AssemblyResolve event for `mscorlib` and the cycle continues. Even though this has only been demonstrated to occur on .NET Framework and should not occur on .NET Core 2.0+ (see https://github.com/dotnet/coreclr/issues/12668), proactively guard against AssemblyResolve events for `System.Private.CoreLib.resources` as well.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:native-library Automatic instrumentation native C++ code (Datadog.Trace.ClrProfiler.Native) priority:high type:bug

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants