-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Fix System.Object.GetCustomAttributes() returning extra internal attributes on Mono #117864
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
Conversation
5d6e24d
to
c5dc589
Compare
Notice that I would say that it is a bug in CoreCLR that it only returns the serialiable attribute for System.Object. The problem is here. We should rather fix the bug in CoreCLR instead of trying to introduce it in Mono. In any case, this PR needs a test. |
Tagging subscribers to this area: @dotnet/area-system-reflection |
Thanks @jkotas! You're right about the logical inconsistency in CoreCLR's behavior. If we proceed with fixing CoreCLR, it might lead to changes in ASP.NET Core test cases and other areas. I was looking into I've tried this approach and it works, correctly returning 5 attributes for Would you prefer we proceed with the Mono fix, or tackle the CoreCLR inconsistency instead? |
I would prefer to fix the buggy CoreCLR behavior. |
The number of attributes on System.Object is internal implementation detail. If the ASP.NET test depends on the exact number of attributes on System.Object, it is a fragile test. I am not worried about the test requiring changes to reflect the correct reflection behavior. Could you please submit a PR to fix the ASP.NET test together with the fix here? |
ok thanks, we will go forward with it |
9ffea98
to
d97eb38
Compare
…nal attributes on Mono" This reverts commit c5dc589.
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.
Pull Request Overview
This PR fixes an incompatibility between Mono and CoreCLR runtimes where System.Object.GetCustomAttributes()
returns different numbers of attributes, causing ASP.NET Core test failures. The fix ensures both runtimes return consistent results by filtering internal compiler-generated attributes in Mono and correcting inheritance logic in CoreCLR.
- Adds test coverage for
System.Object
custom attribute inheritance consistency - Modifies CoreCLR's inheritance traversal to include
System.Object
in the loop - Addresses compatibility issues that were causing ASP.NET Core MVC test failures
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
File | Description |
---|---|
CustomAttributeTests.cs | Adds test to verify consistent attribute counts between inherit=true/false |
RuntimeCustomAttributeData.cs | Fixes inheritance traversal logic to include System.Object in the loop |
src/coreclr/System.Private.CoreLib/src/System/Reflection/RuntimeCustomAttributeData.cs
Outdated
Show resolved
Hide resolved
src/coreclr/System.Private.CoreLib/src/System/Reflection/RuntimeCustomAttributeData.cs
Outdated
Show resolved
Hide resolved
Signed-off-by: Medha Tiwari <[email protected]>
ff0f889
to
6bf83a1
Compare
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.
Thanks
Could you please submit a PR with the ASP.NET test fixes before this gets merged? |
Hi @jkotas I have raised PR but I see that CoreCLR has a broader inconsistency problem that affects not just the program
output on coreclr inherit=true (3 attributes): output for mono This inconsistency is causing ASP.NET Core test failures on Mono: |
The order of attributes is not significant. For example, from https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/language-specification/attributes#223-attribute-specification: "The order in which attributes are specified in such a list, and the order in which sections attached to the same program entity are arranged, is not significant. " If the test depends on attributes being returned in a specific order, it should be fixed to not do that. |
yeah, in this case we better fix the testcase |
/ba-g timeouts |
Thanks! |
Fix System.Object.GetCustomAttributes() returning extra internal attributes on Mono
Problem Description
On Mono runtime,
System.Object.GetCustomAttributes()
returns 5 attributes instead of 1 like CoreCLR, causing compatibility issues with ASP.NET Core and other frameworks.Mono (before fix):
CoreCLR (expected behavior):
Root Cause
The issue was discovered through multiple ASP.NET Core test failures in the MVC test bucket:
Test Class:
ModelAttributesTest
(insrc/Mvc/Mvc.Core/test/ModelBinding/Metadata/ModelAttributesTest.cs
)GetAttributesForParameter_NoAttributes()
- Expected exactly 1 attribute forSystem.Object
but Mono returned 5 attributes, causingAssert.Single()
to failGetAttributesForParameter_SomeAttributes()
- ExpectedSerializableAttribute
as the first type attribute but receivedIsReadOnlyAttribute
as the first attributeGetAttributesForParameter_IncludesTypeAttributes()
- Affected by extra internal attributes disrupting expected attribute collectionsGetAttributesForParameter_WithModelType_IncludesTypeAttributes()
- Similar issues with unexpected additional framework-specific attributesCoreCLR:
typeof(System.Object).GetCustomAttributes()
returns 1 attribute (SerializableAttribute
)Mono:
typeof(System.Object).GetCustomAttributes()
returns 5 attributes (including internal ones)ASP.NET Core expectation: Tests expect CoreCLR behavior and fail on Mono
Implementation Difference
Investigation revealed that Mono's
mono_custom_attrs_construct_by_type()
function incustom-attrs.c
was exposing internal/compiler-generated attributes that CoreCLR does not expose through reflection APIs. Whether CoreCLR actively filters these attributes or simply does not include them in metadata is implementation-specific, but the result is that Mono exposes more attributes than CoreCLR for the same types.Regardless of the specific mechanism, the fix makes Mono behave consistently with CoreCLR by filtering out these internal attributes from reflection results, resolving compatibility issues with frameworks expecting CoreCLR behavior.
Solution
Added filtering logic to
mono_custom_attrs_construct_by_type()
to exclude these internal attributes from reflection results:System.Runtime.CompilerServices.NullableContextAttribute
System.Runtime.CompilerServices.TypeForwardedFromAttribute
System.Runtime.CompilerServices.IsReadOnlyAttribute
System.Runtime.InteropServices.ClassInterfaceAttribute
System.Runtime.InteropServices.ComVisibleAttribute
The filtering is applied in three phases:
Changes Made
src/mono/mono/metadata/custom-attrs.c
mono_custom_attrs_construct_by_type()
(around line 1817)should_filter_attribute_for_reflection()
helper functionReproduction Test
Before fix (Mono): 5 attributes
After fix (Mono): 1 attribute
CoreCLR: 1 attribute
Testing
GetAttributesForParameter_NoAttributes()
,GetAttributesForParameter_SomeAttributes()
,GetAttributesForParameter_IncludesTypeAttributes()
,GetAttributesForParameter_WithModelType_IncludesTypeAttributes()
tests passRelated Issues
This addresses compatibility issues between Mono and CoreCLR reflection APIs that were causing ASP.NET Core test failures, particularly around custom attribute enumeration.
cc: @uweigand @giritrivedi @saitama951