Add pseudo custom attribute lowering to managed ilasm - #131510
Add pseudo custom attribute lowering to managed ilasm#131510jkoritzinsky wants to merge 3 commits into
Conversation
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: 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. |
|
Tagging subscribers to this area: @JulieLeeMSFT, @dotnet/jit-contrib |
|
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. |
There was a problem hiding this comment.
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 dropCustomAttributerows 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.Testscoverage 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. |
b1089a0 to
1549a04
Compare
There was a problem hiding this comment.
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
1549a04 to
9174dea
Compare
There was a problem hiding this comment.
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>
There was a problem hiding this comment.
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();
}
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 CLI • Give Feedback 💬
Note
This PR description was generated with GitHub Copilot.