Skip to content

Use CoreCLRs GetCustomAttributes implementation in native AOT - #133342

Open
MichalStrehovsky wants to merge 6 commits into
share-customattribute-datafrom
share-activation
Open

Use CoreCLRs GetCustomAttributes implementation in native AOT#133342
MichalStrehovsky wants to merge 6 commits into
share-customattribute-datafrom
share-activation

Conversation

@MichalStrehovsky

Copy link
Copy Markdown
Member

Stack created with GitHub Stacks CLIGive Feedback 💬

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 3 pipeline(s).
13 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @agocke, @dotnet/ilc-contrib
See info in area-owners.md if you want to be subscribed.

@MichalStrehovsky

Copy link
Copy Markdown
Member Author

/azp run runtime-nativeaot-outerloop

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 1 pipeline(s).

MichalStrehovsky and others added 6 commits September 7, 2026 21:39
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot-Session: 7bea1165-1cff-4510-b92f-6b4f822033ce
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot-Session: 7bea1165-1cff-4510-b92f-6b4f822033ce
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI lite review requested due to automatic review settings September 7, 2026 12:39

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

The NativeAOT RuntimeParameterInfo changes can regress pseudo-attribute behavior when metadata is unavailable, and there’s also a fixable allocation regression in an empty-result hot path.

Once you've addressed the issues Copilot identified, you can request another Copilot review.

Pull request overview

This PR routes NativeAOT reflection attribute queries/instantiation through the shared CoreCLR-style RuntimeCustomAttribute implementation (in RuntimeCustomAttributeData.cs), removing the older NativeAOT-specific RuntimeCustomAttribute/instantiator logic and updating NativeAOT reflection surface area to call the new path.

Changes:

  • Add/expand NativeAOT implementations in RuntimeCustomAttributeData.cs for constructor resolution, argument decoding, and instantiation from NativeFormat metadata.
  • Update NativeAOT Type/MemberInfo implementations to validate attributeType and call RuntimeCustomAttribute.* for IsDefined/GetCustomAttributes.
  • Remove the old NativeAOT-only custom attribute implementation files and their csproj entries; add helper APIs to find constructors by metadata identity.
File summaries
File Description
src/coreclr/System.Private.CoreLib/src/System/Reflection/RuntimeCustomAttributeData.cs Adds NativeAOT custom attribute ctor resolution/instantiation helpers and wires NativeAOT RuntimeCustomAttribute to use them.
src/coreclr/nativeaot/System.Private.CoreLib/src/System/RuntimeType.NativeAot.cs Redirects IsDefined/GetCustomAttributes* to RuntimeCustomAttribute with CoreCLR-like argument validation.
src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/RuntimeMethodInfo.cs Uses RuntimeCustomAttribute for CA queries with UnderlyingSystemType validation.
src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/RuntimeCustomAttribute.NativeAot.cs Removes the previous NativeAOT custom attribute implementation.
src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/Runtime/TypeInfos/RuntimeTypeInfo.GetMember.cs Adds GetConstructorWithSameMetadataDefinitionAs(...) used by NativeAOT ctor resolution.
src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/Runtime/TypeInfos/RuntimeTypeInfo.cs Removes CA query forwarding methods now handled elsewhere.
src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/Runtime/PropertyInfos/RuntimePropertyInfo.cs Updates CA query entrypoints to RuntimeCustomAttribute and validates attributeType.
src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/Runtime/ParameterInfos/RuntimeParameterInfo.cs Updates CA query entrypoints and adds metadata-reader guards (currently problematic).
src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/Runtime/Modules/RuntimeModule.cs Updates CA query entrypoints to RuntimeCustomAttribute and validates attributeType.
src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/Runtime/MethodInfos/RuntimePlainConstructorInfo.cs Adds a helper overload for metadata-identity comparison.
src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/Runtime/MethodInfos/RuntimeConstructorInfo.cs Updates CA query entrypoints to RuntimeCustomAttribute and validates attributeType.
src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/Runtime/FieldInfos/RuntimeFieldInfo.cs Updates CA query entrypoints to RuntimeCustomAttribute and validates attributeType.
src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/Runtime/EventInfos/RuntimeEventInfo.cs Updates CA query entrypoints to RuntimeCustomAttribute and validates attributeType.
src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/Runtime/Assemblies/RuntimeAssemblyInfo.cs Updates CA query entrypoints to RuntimeCustomAttribute and validates attributeType.
src/coreclr/nativeaot/System.Private.CoreLib/src/System.Private.CoreLib.csproj Removes compilation of deleted NativeAOT CA files.
src/coreclr/nativeaot/System.Private.CoreLib/src/Internal/Reflection/Extensions/NonPortable/CustomAttributeInstantiator.cs Removes the previous NativeAOT CustomAttributeDataAttribute instantiation helper.
Review details

Suppressed comments (1)

src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/Runtime/ParameterInfos/RuntimeParameterInfo.cs:66

  • RuntimeParameterInfo.IsDefined returns false when GetMetadataReader() is null, which can incorrectly hide pseudo custom attributes (e.g., In/Out/Optional) that don’t require metadata. RuntimeCustomAttribute.IsDefined already accounts for pseudo attributes and treats a null reader as “no metadata attributes”.
        public sealed override bool IsDefined(Type attributeType, bool inherit)
        {
            ArgumentNullException.ThrowIfNull(attributeType);

            if (GetMetadataReader() is null)
                return false;

            if (attributeType.UnderlyingSystemType is not RuntimeType attributeRuntimeType)
                throw new ArgumentException(SR.Arg_MustBeType, nameof(attributeType));

            return RuntimeCustomAttribute.IsDefined(this, attributeRuntimeType);
  • Files reviewed: 16/16 changed files
  • Comments generated: 2
  • Review effort level: Lite

Comment on lines +29 to +33
public sealed override object[] GetCustomAttributes(bool inherit)
{
if (GetMetadataReader() is null)
return [];

Comment on lines +2698 to +2700
#if NATIVEAOT
return (object[])Array.CreateInstance(caType, elementCount);
#else
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants