-
Notifications
You must be signed in to change notification settings - Fork 5k
[ILLink] Mark recursive interface implementations in MarkStep #99922
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
9cad35a
ILVerify after kept member validation
jtschuster 99dabd5
Look at recursive interfaces in interface impl marking
jtschuster e24e7e5
Undo test change, remove commented code
jtschuster f3e5efb
Cache recursive interfaces
jtschuster 2a1b394
Make method internal and fix typo
jtschuster 6129fac
Apply suggestions from code review
jtschuster bca6620
PR feedback: rename vars, make non-null
jtschuster cd9b556
Add Cecil internal Type and Method Reference comparers
jtschuster File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
171 changes: 171 additions & 0 deletions
171
src/tools/illink/src/linker/Linker/MethodReferenceComparer.cs
This file contains hidden or 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,171 @@ | ||
// Copyright (c) .NET Foundation and contributors. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Diagnostics; | ||
using Mono.Cecil; | ||
|
||
namespace Mono.Linker | ||
{ | ||
// Copied from https://github.com/jbevain/cecil/blob/master/Mono.Cecil/MethodReferenceComparer.cs | ||
internal sealed class MethodReferenceComparer : EqualityComparer<MethodReference> | ||
{ | ||
// Initialized lazily for each thread | ||
[ThreadStatic] | ||
static List<MethodReference>? xComparisonStack; | ||
|
||
[ThreadStatic] | ||
static List<MethodReference>? yComparisonStack; | ||
|
||
public readonly ITryResolveMetadata _resolver; | ||
|
||
public MethodReferenceComparer(ITryResolveMetadata resolver) | ||
{ | ||
_resolver = resolver; | ||
} | ||
|
||
public override bool Equals (MethodReference? x, MethodReference? y) | ||
{ | ||
return AreEqual (x, y, _resolver); | ||
} | ||
|
||
public override int GetHashCode (MethodReference obj) | ||
{ | ||
return GetHashCodeFor (obj); | ||
} | ||
|
||
public static bool AreEqual (MethodReference? x, MethodReference? y, ITryResolveMetadata resolver) | ||
{ | ||
if (ReferenceEquals (x, y)) | ||
return true; | ||
|
||
if (x is null ^ y is null) | ||
return false; | ||
|
||
Debug.Assert (x is not null); | ||
Debug.Assert (y is not null); | ||
|
||
if (x.HasThis != y.HasThis) | ||
return false; | ||
|
||
#pragma warning disable RS0030 // MethodReference.HasParameters is banned - this code is copied from Cecil | ||
if (x.HasParameters != y.HasParameters) | ||
return false; | ||
#pragma warning restore RS0030 | ||
|
||
if (x.HasGenericParameters != y.HasGenericParameters) | ||
return false; | ||
|
||
#pragma warning disable RS0030 // MethodReference.HasParameters is banned - this code is copied from Cecil | ||
if (x.Parameters.Count != y.Parameters.Count) | ||
return false; | ||
#pragma warning restore RS0030 | ||
|
||
if (x.Name != y.Name) | ||
return false; | ||
|
||
if (!TypeReferenceEqualityComparer.AreEqual (x.DeclaringType, y.DeclaringType, resolver)) | ||
return false; | ||
|
||
var xGeneric = x as GenericInstanceMethod; | ||
var yGeneric = y as GenericInstanceMethod; | ||
if (xGeneric != null || yGeneric != null) { | ||
if (xGeneric == null || yGeneric == null) | ||
return false; | ||
|
||
if (xGeneric.GenericArguments.Count != yGeneric.GenericArguments.Count) | ||
return false; | ||
|
||
for (int i = 0; i < xGeneric.GenericArguments.Count; i++) | ||
if (!TypeReferenceEqualityComparer.AreEqual (xGeneric.GenericArguments[i], yGeneric.GenericArguments[i], resolver)) | ||
return false; | ||
} | ||
|
||
var xResolved = resolver.TryResolve (x); | ||
var yResolved = resolver.TryResolve (y); | ||
|
||
if (xResolved != yResolved) | ||
return false; | ||
|
||
if (xResolved == null) { | ||
// We couldn't resolve either method. In order for them to be equal, their parameter types _must_ match. But wait, there's a twist! | ||
// There exists a situation where we might get into a recursive state: parameter type comparison might lead to comparing the same | ||
// methods again if the parameter types are generic parameters whose owners are these methods. We guard against these by using a | ||
// thread static list of all our comparisons carried out in the stack so far, and if we're in progress of comparing them already, | ||
// we'll just say that they match. | ||
|
||
xComparisonStack ??= new List<MethodReference> (); | ||
|
||
yComparisonStack ??= new List<MethodReference> (); | ||
|
||
for (int i = 0; i < xComparisonStack.Count; i++) { | ||
if (xComparisonStack[i] == x && yComparisonStack[i] == y) | ||
return true; | ||
} | ||
|
||
xComparisonStack.Add (x); | ||
|
||
try { | ||
yComparisonStack.Add (y); | ||
|
||
try { | ||
#pragma warning disable RS0030 // MethodReference.HasParameters is banned - this code is copied from Cecil | ||
for (int i = 0; i < x.Parameters.Count; i++) { | ||
if (!TypeReferenceEqualityComparer.AreEqual (x.Parameters[i].ParameterType, y.Parameters[i].ParameterType, resolver)) | ||
return false; | ||
} | ||
#pragma warning restore RS0030 | ||
} finally { | ||
yComparisonStack.RemoveAt (yComparisonStack.Count - 1); | ||
} | ||
} finally { | ||
xComparisonStack.RemoveAt (xComparisonStack.Count - 1); | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
|
||
public static bool AreSignaturesEqual (MethodReference x, MethodReference y, ITryResolveMetadata resolver, TypeComparisonMode comparisonMode = TypeComparisonMode.Exact) | ||
{ | ||
if (x.HasThis != y.HasThis) | ||
return false; | ||
|
||
#pragma warning disable RS0030 // MethodReference.HasParameters is banned - this code is copied from Cecil | ||
if (x.Parameters.Count != y.Parameters.Count) | ||
return false; | ||
#pragma warning restore RS0030 | ||
|
||
if (x.GenericParameters.Count != y.GenericParameters.Count) | ||
return false; | ||
|
||
#pragma warning disable RS0030 // MethodReference.HasParameters is banned - this code is copied from Cecil | ||
for (var i = 0; i < x.Parameters.Count; i++) | ||
if (!TypeReferenceEqualityComparer.AreEqual (x.Parameters[i].ParameterType, y.Parameters[i].ParameterType, resolver, comparisonMode)) | ||
return false; | ||
#pragma warning restore RS0030 | ||
|
||
if (!TypeReferenceEqualityComparer.AreEqual (x.ReturnType, y.ReturnType, resolver, comparisonMode)) | ||
return false; | ||
|
||
return true; | ||
} | ||
|
||
public static int GetHashCodeFor (MethodReference obj) | ||
{ | ||
// a very good prime number | ||
const int hashCodeMultiplier = 486187739; | ||
|
||
var genericInstanceMethod = obj as GenericInstanceMethod; | ||
if (genericInstanceMethod != null) { | ||
var hashCode = GetHashCodeFor (genericInstanceMethod.ElementMethod); | ||
for (var i = 0; i < genericInstanceMethod.GenericArguments.Count; i++) | ||
hashCode = hashCode * hashCodeMultiplier + TypeReferenceEqualityComparer.GetHashCodeFor (genericInstanceMethod.GenericArguments[i]); | ||
return hashCode; | ||
} | ||
|
||
return TypeReferenceEqualityComparer.GetHashCodeFor (obj.DeclaringType) * hashCodeMultiplier + obj.Name.GetHashCode (); | ||
} | ||
} | ||
} |
This file contains hidden or 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,17 @@ | ||
// Copyright (c) .NET Foundation and contributors. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
namespace Mono.Linker | ||
{ | ||
// Copied from https://github.com/jbevain/cecil/blob/master/Mono.Cecil/TypeComparisonMode.cs | ||
internal enum TypeComparisonMode | ||
{ | ||
Exact, | ||
SignatureOnly, | ||
|
||
/// <summary> | ||
/// Types can be in different assemblies, as long as the module, assembly, and type names match they will be considered equal | ||
/// </summary> | ||
SignatureOnlyLoose | ||
} | ||
} |
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.