Skip to content

Add pseudo custom attribute lowering to managed ilasm - #131510

Open
jkoritzinsky wants to merge 3 commits into
ilasm-more-testsfrom
ilasm-pseudoattributes
Open

Add pseudo custom attribute lowering to managed ilasm#131510
jkoritzinsky wants to merge 3 commits into
ilasm-more-testsfrom
ilasm-pseudoattributes

Conversation

@jkoritzinsky

@jkoritzinsky jkoritzinsky commented Jul 28, 2026

Copy link
Copy Markdown
Member

Match native ilasm metadata emission for known pseudo custom attributes, including flag lowering, layout, P/Invoke, marshalling, and validation.

Co-authored-by: Copilot 223556219+Copilot@users.noreply.github.com
Copilot-Session: c177aade-9940-4e79-a9df-6747dcc22bb0


Stack created with GitHub Stacks CLIGive Feedback 💬

Note

This PR description was generated with GitHub Copilot.

Match native ilasm metadata emission for known pseudo custom attributes, including flag lowering, layout, P/Invoke, marshalling, and validation.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: c177aade-9940-4e79-a9df-6747dcc22bb0
@azure-pipelines

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

@jkoritzinsky jkoritzinsky changed the title ilasm pseudoattributes Add pseudo custom attribute lowering to managed ilasm Jul 28, 2026
@dotnet-policy-service

Copy link
Copy Markdown
Contributor

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

@jkoritzinsky
jkoritzinsky marked this pull request as ready for review July 28, 2026 23:33
Copilot AI review requested due to automatic review settings July 28, 2026 23:33
@azure-pipelines

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

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.

Pull request overview

Adds a pseudo custom attribute lowering pass to managed ilasm so that well-known attributes (e.g., DllImportAttribute, MarshalAsAttribute, MethodImplAttribute, SpecialNameAttribute, StructLayoutAttribute, etc.) are translated into the corresponding metadata flags / auxiliary tables, matching native ilasm emission behavior and validation.

Changes:

  • Introduces a new PseudoCustomAttributes.Lower(...) pass that runs before metadata emission and can drop CustomAttribute rows after lowering.
  • Adds parsing/validation + lowering implementations for a set of known pseudo custom attributes (interop, layout, security) and new diagnostics for invalid targets/values/blobs.
  • Expands ILAssembler.Tests coverage to assert correct flag/table emission and “attribute dropped vs kept” behavior.

Reviewed changes

Copilot reviewed 18 out of 18 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
src/tools/ilasm/tests/ILAssembler.Tests/PropertyEventTests.cs Adds tests for SpecialNameAttribute lowering on properties/events.
src/tools/ilasm/tests/ILAssembler.Tests/ParameterTests.cs Adds tests for In/Out/Optional parameter attribute lowering.
src/tools/ilasm/tests/ILAssembler.Tests/MethodTests.cs Adds tests for SpecialNameAttribute and MethodImplAttribute lowering + diagnostics.
src/tools/ilasm/tests/ILAssembler.Tests/InteropTests.cs Adds tests for PreserveSig, DllImport, and MarshalAs lowering/validation behaviors.
src/tools/ilasm/tests/ILAssembler.Tests/FieldTests.cs Adds tests for field pseudo attributes (NonSerialized, SpecialName, FieldOffset).
src/tools/ilasm/tests/ILAssembler.Tests/CustomAttributeTests.cs Adds broad pseudo-CA coverage: type flags, StructLayout, security, ordering, malformed blob rules.
src/tools/ilasm/tests/ILAssembler.Tests/AssemblyTests.cs Adds assembly-level pseudo-CA coverage (TypeLibVersion, etc.) and validation.
src/tools/ilasm/src/ILAssembler/PseudoCustomAttributes.Security.cs Implements special handling for the two security-name-based attributes.
src/tools/ilasm/src/ILAssembler/PseudoCustomAttributes.MarshalAs.cs Implements MarshalAs decoding and FieldMarshal descriptor emission (incl. property fan-out).
src/tools/ilasm/src/ILAssembler/PseudoCustomAttributes.KnownAttributes.cs Defines the known pseudo-CA table (targets, fixed args, named arg descriptors).
src/tools/ilasm/src/ILAssembler/PseudoCustomAttributes.Identity.cs Adds logic to identify attribute types and resolve local owners pre-emission.
src/tools/ilasm/src/ILAssembler/PseudoCustomAttributes.Decoding.cs Adds CA blob decoding matching native parsing behavior (incl. Everett quirks).
src/tools/ilasm/src/ILAssembler/PseudoCustomAttributes.cs Adds the lowering entry point, context, and attribute-row removal orchestration.
src/tools/ilasm/src/ILAssembler/PseudoCustomAttributes.Application.cs Applies decoded pseudo-CA effects to entities (flags, layouts, imports) + validation.
src/tools/ilasm/src/ILAssembler/MetadataExtensions.cs Adds helper constants/masks used by lowering (e.g., miUserMask, marshalling constants).
src/tools/ilasm/src/ILAssembler/GrammarVisitor.cs Hooks the lowering pass into compilation and records .custom source locations.
src/tools/ilasm/src/ILAssembler/EntityRegistry.cs Adds pre-emission local type-def lookup and supports removing lowered CA rows.
src/tools/ilasm/src/ILAssembler/Diagnostic.cs Adds new diagnostic IDs/messages for pseudo-CA validation failures.

Comment thread src/tools/ilasm/src/ILAssembler/PseudoCustomAttributes.MarshalAs.cs
Copilot AI review requested due to automatic review settings July 29, 2026 00:04
@jkoritzinsky
jkoritzinsky force-pushed the ilasm-pseudoattributes branch from b1089a0 to 1549a04 Compare July 29, 2026 00:05

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.

Pull request overview

Copilot reviewed 18 out of 18 changed files in this pull request and generated no new comments.

Comments suppressed due to low confidence (2)

src/tools/ilasm/src/ILAssembler/PseudoCustomAttributes.MarshalAs.cs:164

  • TryGetSignatureParameterCount reads the first byte but doesn't account for generic method signatures, where the generic parameter count appears before the parameter count. Handling the generic case avoids misidentifying the setter parameter count for signatures with the generic bit set.
    private static unsafe bool TryGetSignatureParameterCount(BlobBuilder? signature, out int count)
    {
        count = 0;
        if (signature is null)
        {
            return false;
        }

        byte[] signatureBytes = signature.ToArray();
        fixed (byte* signaturePointer = signatureBytes)
        {
            var reader = new BlobReader(signaturePointer, signatureBytes.Length);
            try
            {
                _ = reader.ReadByte();
                return reader.TryReadCompressedInteger(out count);
            }

src/tools/ilasm/src/ILAssembler/PseudoCustomAttributes.Application.cs:290

  • The DllImportAttribute CallingConvention named argument should fall back to WinApi for unrecognized values (including 0/1), matching the default behavior for DllImport and avoiding emitting a P/Invoke map with no calling convention bits set.
        MethodImportAttributes flags = MethodImportAttributes.None;

        if (FindNamedArgument(arguments, DllImportCallingConvention) is { } callingConventionArgument)
        {
            flags = GetUInt32(callingConventionArgument.Value) switch

Use a reference type for LoweringContext and remove unnecessary readonly-reference parameter modifiers.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: c177aade-9940-4e79-a9df-6747dcc22bb0
@jkoritzinsky
jkoritzinsky force-pushed the ilasm-pseudoattributes branch from 1549a04 to 9174dea Compare July 29, 2026 00:34
Copilot AI review requested due to automatic review settings July 29, 2026 00:34

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.

Pull request overview

Copilot reviewed 18 out of 18 changed files in this pull request and generated no new comments.

Comments suppressed due to low confidence (1)

src/tools/ilasm/src/ILAssembler/PseudoCustomAttributes.Application.cs:102

  • In ApplySpecialName, the default branch reports InvalidValue, but reaching the default means the attribute was applied to an unsupported target type (i.e., an invalid target), not an invalid argument value. This would produce the wrong diagnostic if this path is ever hit.
                @event.Attributes |= EventAttributes.SpecialName;
                return true;
            default:
                return context.InvalidValue();
        }

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings July 29, 2026 00:46

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.

Pull request overview

Copilot reviewed 18 out of 18 changed files in this pull request and generated no new comments.

Comments suppressed due to low confidence (2)

src/tools/ilasm/src/ILAssembler/EntityRegistry.cs:844

  • FindLocalTypeDefinition treats both ModuleEntity and ModuleReferenceEntity as local. A TypeRef with ResolutionScope=ModuleRef refers to a different module, so resolving it to a local TypeDef can cause pseudo-attribute lowering to target the wrong entity (and potentially drop the attribute row) when the TypeRef is actually external.
                case ModuleEntity or ModuleReferenceEntity:
                    return FindTypeDefinition(null, typeReference.Namespace, typeReference.Name);

src/tools/ilasm/src/ILAssembler/PseudoCustomAttributes.Application.cs:102

  • ApplySpecialName reports InvalidValue for unsupported owners, but this condition is about an invalid application target rather than an invalid argument value. Using InvalidTarget here keeps diagnostics consistent with the other pseudo-attribute validations.
                return true;
            default:
                return context.InvalidValue();
        }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

2 participants