Use CoreCLRs GetCustomAttributes implementation in native AOT - #133342
Use CoreCLRs GetCustomAttributes implementation in native AOT#133342MichalStrehovsky wants to merge 6 commits into
Conversation
|
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. |
|
Tagging subscribers to this area: @agocke, @dotnet/ilc-contrib |
|
/azp run runtime-nativeaot-outerloop |
|
Azure Pipelines: Successfully started running 1 pipeline(s). |
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>
8698d5f to
e486a4b
Compare
There was a problem hiding this comment.
🟡 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.csfor constructor resolution, argument decoding, and instantiation from NativeFormat metadata. - Update NativeAOT
Type/MemberInfoimplementations to validateattributeTypeand callRuntimeCustomAttribute.*forIsDefined/GetCustomAttributes. - Remove the old NativeAOT-only custom attribute implementation files and their
csprojentries; 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 CustomAttributeData→Attribute 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
| public sealed override object[] GetCustomAttributes(bool inherit) | ||
| { | ||
| if (GetMetadataReader() is null) | ||
| return []; | ||
|
|
| #if NATIVEAOT | ||
| return (object[])Array.CreateInstance(caType, elementCount); | ||
| #else |
Stack created with GitHub Stacks CLI • Give Feedback 💬