-
Notifications
You must be signed in to change notification settings - Fork 128
Correctness fixes to interface stripping #1305
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
3 commits
Select commit
Hold shift + click to select a range
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -408,7 +408,7 @@ void ProcessMarkedTypesWithInterfaces () | |
foreach (var type in typesWithInterfaces) { | ||
// Exception, types that have not been flagged as instantiated yet. These types may not need their interfaces even if the | ||
// interface type is marked | ||
if (!Annotations.IsInstantiated (type)) | ||
if (!Annotations.IsInstantiated (type) && !Annotations.IsRelevantToVariantCasting (type)) | ||
continue; | ||
|
||
MarkInterfaceImplementations (type); | ||
|
@@ -1285,6 +1285,16 @@ protected virtual void MarkSerializable (TypeDefinition type) | |
MarkMethodsIf (type.Methods, IsSpecialSerializationConstructor, new DependencyInfo (DependencyKind.SerializationMethodForType, type), type); | ||
} | ||
|
||
internal protected virtual TypeDefinition MarkTypeVisibleToReflection (TypeReference reference, DependencyInfo reason, IMemberDefinition sourceLocationMember) | ||
{ | ||
// If a type is visible to reflection, we need to stop doing optimization that could cause observable difference | ||
// in reflection APIs. This includes APIs like MakeGenericType (where variant castability of the produced type | ||
// could be incorrect) or IsAssignableFrom (where assignability of unconstructed types might change). | ||
Annotations.MarkRelevantToVariantCasting (reference.Resolve ()); | ||
|
||
return MarkType (reference, reason, sourceLocationMember); | ||
} | ||
|
||
/// <summary> | ||
/// Marks the specified <paramref name="reference"/> as referenced. | ||
/// </summary> | ||
|
@@ -2001,9 +2011,12 @@ void MarkModifierType (IModifierType mod, IMemberDefinition sourceLocationMember | |
|
||
void MarkGenericArguments (IGenericInstance instance, IMemberDefinition sourceLocationMember) | ||
{ | ||
foreach (TypeReference argument in instance.GenericArguments) | ||
foreach (TypeReference argument in instance.GenericArguments) { | ||
MarkType (argument, new DependencyInfo (DependencyKind.GenericArgumentType, instance), sourceLocationMember); | ||
|
||
Annotations.MarkRelevantToVariantCasting (argument.Resolve ()); | ||
} | ||
|
||
MarkGenericArgumentConstructors (instance, sourceLocationMember); | ||
} | ||
|
||
|
@@ -2173,8 +2186,14 @@ protected virtual MethodDefinition MarkMethod (MethodReference reference, Depend | |
{ | ||
(reference, reason) = GetOriginalMethod (reference, reason, sourceLocationMember); | ||
|
||
if (reference.DeclaringType is ArrayType) | ||
if (reference.DeclaringType is ArrayType arrayType) { | ||
MarkType (reference.DeclaringType, new DependencyInfo (DependencyKind.DeclaringType, reference), sourceLocationMember); | ||
|
||
if (reference.Name == ".ctor") { | ||
Annotations.MarkRelevantToVariantCasting (arrayType.Resolve ()); | ||
} | ||
return null; | ||
} | ||
|
||
if (reference.DeclaringType is GenericInstanceType) { | ||
// Blame the method reference on the original reason without marking it. | ||
|
@@ -2642,7 +2661,7 @@ protected virtual void MarkInstruction (Instruction instruction, MethodDefinitio | |
Debug.Assert (instruction.OpCode.Code == Code.Ldtoken); | ||
var reason = new DependencyInfo (DependencyKind.Ldtoken, method); | ||
if (token is TypeReference typeReference) { | ||
MarkType (typeReference, reason, method); | ||
MarkTypeVisibleToReflection (typeReference, reason, method); | ||
} else if (token is MethodReference methodReference) { | ||
MarkMethod (methodReference, reason, method); | ||
} else { | ||
|
@@ -2652,6 +2671,9 @@ protected virtual void MarkInstruction (Instruction instruction, MethodDefinitio | |
} | ||
|
||
case OperandType.InlineType: | ||
if (instruction.OpCode.Code == Code.Newarr) { | ||
Annotations.MarkRelevantToVariantCasting (((TypeReference) instruction.Operand).Resolve ()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
} | ||
MarkType ((TypeReference) instruction.Operand, new DependencyInfo (DependencyKind.InstructionTypeRef, method), method); | ||
break; | ||
default: | ||
|
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
44 changes: 44 additions & 0 deletions
44
test/Mono.Linker.Tests.Cases/Generics/ArrayVariantCasting.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,44 @@ | ||
using Mono.Linker.Tests.Cases.Expectations.Assertions; | ||
|
||
namespace Mono.Linker.Tests.Cases.Generics | ||
{ | ||
public class ArrayVariantCasting | ||
{ | ||
[Kept] | ||
interface IFoo { } | ||
|
||
[Kept] | ||
[KeptInterface (typeof (IFoo))] | ||
class Foo : IFoo | ||
{ | ||
// Even though Foo is never allocated (as seen from the removal of the constructor), | ||
// we need to make sure linker keeps its interface list because it's relevant | ||
// for variant casting. | ||
public Foo () { } | ||
} | ||
|
||
[Kept] | ||
class Base | ||
{ | ||
// Removed | ||
public Base () { } | ||
} | ||
|
||
[Kept] | ||
[KeptBaseType (typeof (Base))] | ||
class Derived : Base | ||
{ | ||
// Even though Derived is never allocated (as seen from the removal of the constructor), | ||
// we need to make sure linker keeps its base types because it's relevant | ||
// for variant casting. | ||
public Derived () { } | ||
} | ||
|
||
public static void Main () | ||
{ | ||
// These casts need to succeed after trimming | ||
var foos = (IFoo[]) (object) new Foo[0]; | ||
var bases = (Base[]) (object) new Derived[0]; | ||
} | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
test/Mono.Linker.Tests.Cases/Generics/MdArrayVariantCasting.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,44 @@ | ||
using Mono.Linker.Tests.Cases.Expectations.Assertions; | ||
|
||
namespace Mono.Linker.Tests.Cases.Generics | ||
{ | ||
public class MdArrayVariantCasting | ||
{ | ||
[Kept] | ||
interface IFoo { } | ||
|
||
[Kept] | ||
[KeptInterface (typeof (IFoo))] | ||
class Foo : IFoo | ||
{ | ||
// Even though Foo is never allocated (as seen from the removal of the constructor), | ||
// we need to make sure linker keeps its interface list because it's relevant | ||
// for variant casting. | ||
public Foo () { } | ||
} | ||
|
||
[Kept] | ||
class Base | ||
{ | ||
// Removed | ||
public Base () { } | ||
} | ||
|
||
[Kept] | ||
[KeptBaseType (typeof (Base))] | ||
class Derived : Base | ||
{ | ||
// Even though Derived is never allocated (as seen from the removal of the constructor), | ||
// we need to make sure linker keeps its base types because it's relevant | ||
// for variant casting. | ||
public Derived () { } | ||
} | ||
|
||
public static void Main () | ||
{ | ||
// These casts need to succeed after trimming | ||
var foos = (IFoo[,]) (object) new Foo[0, 0]; | ||
var bases = (Base[,]) (object) new Derived[0, 0]; | ||
} | ||
} | ||
} |
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,63 @@ | ||
using Mono.Linker.Tests.Cases.Expectations.Assertions; | ||
|
||
namespace Mono.Linker.Tests.Cases.Generics | ||
{ | ||
public class VariantCasting | ||
{ | ||
[Kept] | ||
interface IVariant<out T> { } | ||
|
||
[Kept] | ||
interface IFoo { } | ||
|
||
[Kept] | ||
[KeptInterface (typeof (IFoo))] | ||
class Foo : IFoo | ||
{ | ||
// Even though Foo is never allocated (as seen from the removal of the constructor), | ||
// we need to make sure linker keeps its interface list because it's relevant | ||
// for variant casting. | ||
public Foo () { } | ||
} | ||
|
||
[Kept] | ||
class Base | ||
{ | ||
// Removed | ||
public Base () { } | ||
} | ||
|
||
[Kept] | ||
[KeptBaseType (typeof (Base))] | ||
class Derived : Base | ||
{ | ||
// Even though Derived is never allocated (as seen from the removal of the constructor), | ||
// we need to make sure linker keeps its base types because it's relevant | ||
// for variant casting. | ||
public Derived () { } | ||
} | ||
|
||
[Kept] | ||
[KeptInterface (typeof (IVariant<>))] | ||
class Interesting<T> : IVariant<T> | ||
{ | ||
[Kept] | ||
public Interesting () { } | ||
} | ||
|
||
[Kept] | ||
[KeptBaseType (typeof (Interesting<>), "T")] | ||
class Boring<T> : Interesting<T> | ||
{ | ||
[Kept] | ||
public Boring () { } | ||
} | ||
|
||
public static void Main () | ||
{ | ||
// These casts need to succeed after trimming | ||
IVariant<IFoo> foos = (IVariant<IFoo>) (object) new Boring<Foo> (); | ||
IVariant<Base> bases = (IVariant<Base>) (object) new Boring<Derived> (); | ||
} | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
test/Mono.Linker.Tests.Cases/Reflection/IsAssignableFrom.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,21 @@ | ||
| ||
using Mono.Linker.Tests.Cases.Expectations.Assertions; | ||
|
||
namespace Mono.Linker.Tests.Cases.Reflection | ||
{ | ||
class IsAssignableFrom | ||
{ | ||
[Kept] | ||
interface IFoo { } | ||
|
||
[Kept] | ||
[KeptInterface (typeof (IFoo))] | ||
class Foo : IFoo { } | ||
|
||
static void Main () | ||
{ | ||
typeof (IFoo).IsAssignableFrom (typeof (Foo)); | ||
} | ||
|
||
} | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are you sure this is the only relevant opcode? What about code like
var foos = (IFoo[]) Array.CreateInstance (typeof (Foo), 1);
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That pattern should be fine because the
typeof(Foo)
means "visible to reflection" and that's handled elsewhere in this pull request.