Skip to content

Store NativeFormat signatures as ECMA-shaped blobs - #137003

Open
MichalStrehovsky wants to merge 7 commits into
dotnet:mainfrom
MichalStrehovsky:nativeformat-signature-blobs
Open

MichalStrehovsky wants to merge 7 commits into
dotnet:mainfrom
MichalStrehovsky:nativeformat-signature-blobs

Conversation

@MichalStrehovsky

@MichalStrehovsky MichalStrehovsky commented Nov 5, 2026 •

Copy link
Copy Markdown
Member

Replace linked method, field, property, and type-expression signature records with interned blobs. Preserve relocatable NativeFormat type handles and migrate reflection and stack-trace consumers to lazy blob traversal.

Replace linked method, field, property, and type-expression signature records with interned blobs. Preserve relocatable NativeFormat type handles and migrate reflection and stack-trace consumers to lazy blob traversal.

Co-authored-by: Copilot <[email protected]>
Copilot-Session: b5ce76ee-f426-4bb5-af9e-7009899c443b
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 3 pipeline(s).
12 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.

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 changes allow out-of-bounds memory reads from oversized blobs, decode signed compressed integers incorrectly, and introduce TypeLoadException failures for profiles without TypedReference.

3 open issues
What changed in this PR

This PR replaces NativeFormat’s linked signature records with interned, ECMA-shaped blobs while retaining relocatable type handles. It updates NativeAOT metadata generation, reflection, and stack-trace consumers to use the new representation.

Changes:

  • Replace signature-record schemas and generated readers/writers with blob-backed records.
  • Add compressed-integer encoding and lazy signature traversal.
  • Migrate reflection and stack-trace handling, including generic arguments and custom modifiers.

Builds and tests were not run.

File Description
src/​coreclr/​tools/​Common/​Internal/​Metadata/​NativeFormat/​NativeFormatSignatures.cs Adds blob-backed signature readers and traversal.
src/​coreclr/​tools/​Common/​Internal/​Metadata/​NativeFormat/​NativeFormatReaderGen.cs Replaces linked signature records with blob fields.
src/​coreclr/​tools/​Common/​Internal/​Metadata/​NativeFormat/​NativeFormatReaderCommonGen.cs Removes obsolete signature handle kinds.
src/​coreclr/​tools/​Common/​Internal/​Metadata/​NativeFormat/​MdBinaryReaderGen.cs Removes obsolete handle-reading overloads.
src/​coreclr/​tools/​Common/​Internal/​Metadata/​NativeFormat/​Generator/​SchemaDef.cs Defines blob-backed signature schemas.
src/​coreclr/​tools/​Common/​Internal/​Metadata/​NativeFormat/​BlobReader.cs Adds compressed-integer decoding.
src/​coreclr/​tools/​aot/​ILCompiler.MetadataTransform/​Internal/​Metadata/​NativeFormat/​Writer/​NativeMetadataWriter.cs Updates signature diagnostic formatting.
src/​coreclr/​tools/​aot/​ILCompiler.MetadataTransform/​Internal/​Metadata/​NativeFormat/​Writer/​NativeFormatWriterGen.cs Uses blobs for signature serialization and equality.
src/​coreclr/​tools/​aot/​ILCompiler.MetadataTransform/​Internal/​Metadata/​NativeFormat/​Writer/​MetadataBlob.cs Adds compressed-integer encoding and blob formatting.
src/​coreclr/​tools/​aot/​ILCompiler.MetadataTransform/​Internal/​Metadata/​NativeFormat/​Writer/​MdBinaryWriterGen.cs Removes obsolete signature-writing overloads.
src/​coreclr/​tools/​aot/​ILCompiler.MetadataTransform/​ILCompiler/​Metadata/​Transform.Type.cs Routes type specifications through blob encoding.
src/​coreclr/​tools/​aot/​ILCompiler.MetadataTransform/​ILCompiler/​Metadata/​Transform.Signature.cs Adds shared method and type signature encoders.
src/​coreclr/​tools/​aot/​ILCompiler.MetadataTransform/​ILCompiler/​Metadata/​Transform.Property.cs Emits property signature blobs.
src/​coreclr/​tools/​aot/​ILCompiler.MetadataTransform/​ILCompiler/​Metadata/​Transform.Method.cs Emits method signature blobs.
src/​coreclr/​tools/​aot/​ILCompiler.MetadataTransform/​ILCompiler/​Metadata/​Transform.Field.cs Emits field signature blobs.
src/​coreclr/​tools/​aot/​ILCompiler.MetadataTransform/​ILCompiler.MetadataTransform.csproj Includes new signature implementation files.
src/​coreclr/​tools/​aot/​ILCompiler.Compiler/​Compiler/​MetadataManager.cs Encodes generic stack-trace type signatures as blobs.
src/​coreclr/​nativeaot/​System.Private.StackTraceMetadata/​src/​Internal/​StackTraceMetadata/​StackTraceMetadata.cs Reads assembly names through blob signatures.
src/​coreclr/​nativeaot/​System.Private.StackTraceMetadata/​src/​Internal/​StackTraceMetadata/​MethodNameFormatter.cs Formats types and parameters from blob signatures.
src/​coreclr/​nativeaot/​System.Private.CoreLib/​src/​System/​Reflection/​RuntimeCustomAttributeData.NativeAot.cs Resolves attribute constructor parameters from signatures.
src/​coreclr/​nativeaot/​System.Private.CoreLib/​src/​System/​Reflection/​Runtime/​TypeInfos/​NativeFormat/​NativeFormatRuntimeNamedTypeInfo.cs Updates attribute signature inspection.
src/​coreclr/​nativeaot/​System.Private.CoreLib/​src/​System/​Reflection/​Runtime/​TypeInfos/​NativeFormat/​NativeFormatRuntimeGenericParameterTypeInfo.cs Delegates constraint modifier skipping to resolution.
src/​coreclr/​nativeaot/​System.Private.CoreLib/​src/​System/​Reflection/​Runtime/​PropertyInfos/​NativeFormat/​NativeFormatRuntimePropertyInfo.cs Uses blob-backed property and parameter types.
src/​coreclr/​nativeaot/​System.Private.CoreLib/​src/​System/​Reflection/​Runtime/​MethodInfos/​NativeFormat/​NativeFormatMethodCommon.cs Builds qualified method signatures from blob positions.
src/​coreclr/​nativeaot/​System.Private.CoreLib/​src/​System/​Reflection/​Runtime/​General/​TypeResolver.NativeFormat.cs Resolves inline type expressions from blobs.
src/​coreclr/​nativeaot/​System.Private.CoreLib/​src/​System/​Reflection/​Runtime/​General/​QSignatureTypeHandle.NativeFormat.cs Wraps NativeFormat type signatures.
src/​coreclr/​nativeaot/​System.Private.CoreLib/​src/​System/​Reflection/​Runtime/​General/​QSignatureTypeHandle.cs Stores signatures for resolution and modifier access.
src/​coreclr/​nativeaot/​System.Private.CoreLib/​src/​System/​Reflection/​Runtime/​General/​MetadataReaderExtensions.NativeFormat.cs Reads custom modifiers from signature blobs.
src/​coreclr/​nativeaot/​System.Private.CoreLib/​src/​System/​Reflection/​Runtime/​General/​MetadataReaderExtensions.cs Removes obsolete modified-handle validation.
src/​coreclr/​nativeaot/​System.Private.CoreLib/​src/​System/​Reflection/​Runtime/​FieldInfos/​NativeFormat/​NativeFormatRuntimeFieldInfo.cs Uses blob signatures for field reflection.
src/​coreclr/​nativeaot/​System.Private.CoreLib/​src/​System/​Reflection/​ModifiedType.NativeAot.cs Traverses nested types and modifiers in blobs.
src/​coreclr/​nativeaot/​System.Private.CoreLib/​src/​System.Private.CoreLib.csproj Includes the shared signature reader.
src/​coreclr/​nativeaot/​System.Private.CoreLib/​src/​Internal/​Runtime/​CompilerServices/​MethodNameAndSignature.cs Detects generic return types from blob type codes.
src/​coreclr/​nativeaot/​System.Private.CoreLib/​src/​CompatibilitySuppressions.xml Updates suppressions for replacement metadata types.

Review effort: Balanced

@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 4 commits November 17, 2026 17:55
Co-authored-by: Copilot <[email protected]>
Copilot-Session: 6c0cc098-fbe5-4782-80d8-6122770ff179
Co-authored-by: Copilot <[email protected]>
Copilot-Session: a510b4d1-79f6-42fe-a8e5-c04140e20ea6
Co-authored-by: Copilot <[email protected]>
Copilot-Session: a510b4d1-79f6-42fe-a8e5-c04140e20ea6
Address review feedback on the ECMA-shaped NativeFormat signature blobs so
the parsing code can later be shared with CoreCLR's CoreLib:

- Make TypeSignature a plain blob position (reader, offset, end), like
  CoreCLR's (Signature, offset); remove the unreachable handle mode.
- Make SignatureReader mirror SigParser: element type reads skip vararg
  sentinels and custom modifiers, SkipType mirrors SkipExactlyOne, and all
  reads are bounds-checked against the blob.
- Replace the NativeFormat SignatureTypeCode with the shared CorElementType
  (public in NativeAOT CoreLib and the tools) and move MdSigCallingConvention
  to shared CoreLib, replacing magic header constants in reader and writer.
- Move type-parameter and function-pointer calling convention navigation
  onto TypeSignature so ModifiedType.NativeAot.cs matches the CoreCLR shape.
- Carry a TypeContext in NativeAOT ModifiedType so custom modifiers that
  are instantiated over generic parameters resolve correctly.
- Avoid redundant signature walks (ParameterCount, single-pass stack trace
  formatting) and revert the now-unused BlobReader additions.

Co-authored-by: Copilot <[email protected]>
Copilot-Session: 7a8e8a57-1f8a-4be8-a801-b545c58ba85d

Copilot AI commented Nov 18, 2026

Copy link
Copy Markdown
Contributor

One or more custom setup steps configured for this repository failed during this Copilot code review run:

Install Dependencies

Setup steps run before each review. If the review above is missing context, or no review was posted at all, the failing step above may be the cause. See the workflow run for failure details, fix your setup steps configuration, and re-request a review.

Note

You can configure setup steps for Copilot code review separately from Copilot cloud agent with a copilot-code-review.yml file. Read the docs for details.

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.

🔵 Needs a closer look

The NativeAOT signature wire-format replacement spans compiler, reflection, and stack-trace consumers, but end-to-end compatibility validation and representative metadata-size and performance measurements are unavailable.

3 open issues

Review effort: Balanced

@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).

- Remove the NativeFormat SignatureCallingConvention enum from the schema and
  use the shared MdSigCallingConvention in the reader, CoreLib and the
  metadata writer.
- Rename TypeSignature navigation helpers to match CoreCLR's Signature
  (GetTypeParameterOffset, GetCallingConventionFromFunctionPointerAtOffset,
  GetCustomModifiersAtOffset).
- Make PeekElementType report STRING/OBJECT as CLASS like
  SigParser::PeekElemType, and skip the vararg sentinel once via
  SkipAnyVASentinel instead of before every custom modifier.
- Drop the redundant MetadataReader argument from QSignatureTypeHandle.

Copilot-Session: 5e3b8f87-7add-458a-8132-b6bced8110d5
Co-Authored-By: Copilot <[email protected]>
- Compile CoreCLR's ModifiedType.CoreCLR.cs into NativeAOT instead of an
  identical NativeAot copy.
- Move the identical RuntimeParameterInfo signature members into the
  shared RuntimeParameterInfo.cs.
- Make the NativeFormat signature reader pointer-based like CoreCLR's
  SigPointer/SigParser, so Signature holds void* _sig/int _csig and its
  offset helpers mirror the native Signature FCalls.
- Rename TypeSignature/SignatureReader/TypeSignatureCollection to
  SigPointer/SigParser/SigPointerCollection and adopt SigParser method
  names (GetElemType, PeekElemType, GetData, GetToken, SkipExactlyOne,
  SkipMethodHeaderSignature, GetCallingConvInfo, GetCallingConv).
- Give the NativeAOT Signature CoreCLR's Arguments/ReturnType/FieldType
  members, resolved eagerly in the constructor like Signature_Init,
  instead of GetParameterType, removing the NATIVEAOT ifdef from the
  shared RuntimeParameterInfo.ParameterType.
- Pass the Signature into NativeAOT RuntimeParameterInfo.GetParameters
  like CoreCLR does, using sig.Arguments.Length for the count.
- Cache one Signature per method and constructor, like CoreCLR's
  RuntimeMethodInfo.Signature, and use it for parameters and ToString.
- Move GetModifiedPropertyType to the RuntimePropertyInfo base class.
- Remove SigPointer.GetGenericArguments; StackTraceMetadata walks the
  GENERICINST with SigParser like Signature.GetTypeParameterOffset.
- Only read array sizes and lower bounds for a nonzero rank, like
  CoreCLR's SigParser::SkipExactlyOne.
- Validate function pointer headers like SigPointer::GetTypeHandleThrowing
  and read the calling convention once.

Copilot-Session: 04a790d4-1199-4ff4-bf2e-f77351401f79
Copilot-Session: 8e648700-b3ac-43c2-86c4-64a3671dbcb7
Copilot-Session: a4a8d9e5-22ea-4d40-851d-9f07177494d7
Co-Authored-By: Copilot <[email protected]>
ret
}

.method public static hidebysig void GenericModReqInArrayElement<T>(int32 modreq (class GenericModifier`1<!!0>)[] arg)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Could delete this test. In ECMA augments we now say TypeSpec modifiers are invalid. However, CoreCLR does pass a type context to these. Native AOT didn't; now that we share code we do. That triggered the AI into "we need a regression test". I don't have an opinion.

<Compile Include="$(CoreClrProjectRoot)System.Private.CoreLib\src\System\Runtime\CompilerServices\AsyncProfiler.CoreCLR.cs" />
<Compile Include="$(CoreClrProjectRoot)System.Private.CoreLib\src\System\Runtime\CompilerServices\RuntimeAsyncTaskContinuation.cs" />
<!-- TODO: shared with CoreCLR; move to the libraries partition (excluded from Mono) -->
<Compile Include="$(CoreClrProjectRoot)System.Private.CoreLib\src\System\Reflection\ModifiedType.CoreCLR.cs" />

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Wanted to avoid introducing more diffs in this PR, it needs to be merged into the existing file.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

CoreCLR file: these members are moving to the shared file.

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

Truncated five-byte signature tokens can cause an out-of-bounds unmanaged read before validation, potentially faulting instead of throwing BadImageFormatException.

1 open issue
3 resolved since last review

Review effort: Balanced

@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 added a commit that referenced this pull request Nov 20, 2026
## Summary

The bounded `NativePrimitiveDecoder` methods validate the shorter
integer encodings but read the full-width payloads without checking
`streamEnd`. A truncated five-byte or nine-byte encoding can therefore
read beyond the supplied buffer.

Check the entire payload before advancing the cursor or reading it in
`DecodeUnsigned`, `DecodeSigned`, `DecodeUnsignedLong`, and
`DecodeSignedLong`. This fixes the shared decoder rather than requiring
caller-side validation, as discussed in [this review
comment](#137003 (comment)).

The change is limited to the four missing bounds checks. Existing
assertion behavior is preserved, and no test changes are included.

## Validation

Before removing the test changes, a Release regression run passed all 18
cases, covering truncated and exact-length payloads. Without the bounds
checks, all 12 nonempty truncated-input cases failed because no
exception was thrown.

After removing the test changes, `ILCompiler.TypeSystem.Tests` built
successfully in Release. A Debug build of `ILCompiler.MetadataTransform`
was blocked during restore because the NuGet vulnerability-data endpoint
was unavailable (`NU1900` / `NU1905`).

No full runtime build or full test suite was run.

> [!NOTE]
> This pull request was prepared with GitHub Copilot.

---------

Co-authored-by: MichalStrehovsky <[email protected]>
Co-authored-by: Copilot <[email protected]>
@MichalStrehovsky

Copy link
Copy Markdown
Member Author

@jkotas do you have an opinion on this direction?

This would leave us with a managed SigPointer and SigParser that we can reuse in CoreCLR to replace some internal calls (potentially: GetParameterOffsetInternal/GetTypeParameterOffsetInternal/GetCallingConventionFromFunctionPointerAtOffsetInternal/GetCustomModifiersAtOffset).

In the opposite direction, native AOT should be able to reuse more of MethodBase/PropertyInfo/EventInfo/FieldInfo from CoreCLR (thanks to the Signature class that has similar shape to CoreCLR's Signature class). There is more that is going to have similar shape and we can deduplicate, potentially a lot of the Signature class itself too, but as usual, I don't want to introduce too many CoreCLR diffs in these PRs.

This branch has not been deployed

No deployments
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