Remove unsafe code from System.Reflection.Metadata PE/blob writers#129626
Open
EgorBo wants to merge 4 commits into
Open
Remove unsafe code from System.Reflection.Metadata PE/blob writers#129626EgorBo wants to merge 4 commits into
EgorBo wants to merge 4 commits into
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>
Member
Author
Contributor
|
Tagging subscribers to this area: @dotnet/area-system-reflection-metadata |
Contributor
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. |
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Member
Author
|
PTAL @MihaZupan |
MihaZupan
approved these changes
Jun 22, 2026
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>
MihaZupan
approved these changes
Jun 26, 2026
This was referenced Jun 26, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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