Uh oh!
There was an error while loading. Please reload this page.
Remove unsafe code from System.Reflection.Metadata PE/blob writers - #129626
Conversation
Replace fixed-pointer writes with BinaryPrimitives in SubstituteTemplateParameters and the PE checksum walk, and drop the IEnumerable<Blob>/iterator allocation in CalculateChecksum by iterating the struct enumerator directly. No behavior change. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
EgorBo
commented
Jun 19, 2026
Tagging subscribers to this area: @dotnet/area-system-reflection-metadata |
There was a problem hiding this comment.
Pull request overview
This PR removes unsafe/pointer-based code from two System.Reflection.Metadata write paths by switching to BinaryPrimitives and simplifying PE checksum iteration to avoid iterator allocations.
Changes:
- Reworks PE checksum computation to iterate
BlobBuilder.GetBlobs()directly and to useBinaryPrimitives.ReadUInt16LittleEndianinstead of pointer arithmetic. - Updates
BlobHandle.SubstituteTemplateParametersto write the substituted value viaBinaryPrimitives.WriteUInt32LittleEndianrather than a fixed-pointer cast.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| src/libraries/System.Reflection.Metadata/src/System/Reflection/PortableExecutable/PEBuilder.cs | Removes iterator-based checksum walk and unsafe reads; adds span-based checksum aggregation. |
| src/libraries/System.Reflection.Metadata/src/System/Reflection/Metadata/TypeSystem/Handles.TypeSystem.cs | Removes unsafe from template substitution by using BinaryPrimitives for little-endian writes. |
Uh oh!
There was an error while loading. Please reload this page.
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Uh oh!
There was an error while loading. Please reload this page.
EgorBo
commented
Jun 22, 2026
PTAL @MihaZupan |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Move the odd trailing-byte carry below the pair loop per review feedback, so it reduces to a single 'pendingByte = segment.IsEmpty ? -1 : segment[0];' assignment. Behavior is unchanged. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
…129626) Removes `unsafe` from two `System.Reflection.Metadata` write paths: - `BlobHandle.SubstituteTemplateParameters` and the PE checksum walk now use `BinaryPrimitives` little-endian writes/reads instead of fixed pointers. - `PEBuilder.CalculateChecksum` iterates the `BlobBuilder.Blobs` struct enumerator directly, dropping the `IEnumerable<Blob>`/`yield` iterator allocation. No behavior change. > [!NOTE] > This PR was authored with assistance from GitHub Copilot. [Diffs](MihuBot/runtime-utils#2005) --------- Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Removes
unsafefrom twoSystem.Reflection.Metadatawrite paths:BlobHandle.SubstituteTemplateParametersand the PE checksum walk now useBinaryPrimitiveslittle-endian writes/reads instead of fixed pointers.PEBuilder.CalculateChecksumiterates theBlobBuilder.Blobsstruct enumerator directly, dropping theIEnumerable<Blob>/yielditerator allocation.No behavior change.
Note
This PR was authored with assistance from GitHub Copilot.
Diffs