Skip to content

Remove unsafe code from System.Reflection.Metadata PE/blob writers#129626

Open
EgorBo wants to merge 4 commits into
dotnet:mainfrom
EgorBo:reflection-metadata-reduce-unsafe
Open

Remove unsafe code from System.Reflection.Metadata PE/blob writers#129626
EgorBo wants to merge 4 commits into
dotnet:mainfrom
EgorBo:reflection-metadata-reduce-unsafe

Conversation

@EgorBo

@EgorBo EgorBo commented Jun 19, 2026

Copy link
Copy Markdown
Member

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

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>
Copilot AI review requested due to automatic review settings June 19, 2026 16:18
@EgorBo

EgorBo commented Jun 19, 2026

Copy link
Copy Markdown
Member Author

@MihuBot

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @dotnet/area-system-reflection-metadata
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.

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 use BinaryPrimitives.ReadUInt16LittleEndian instead of pointer arithmetic.
  • Updates BlobHandle.SubstituteTemplateParameters to write the substituted value via BinaryPrimitives.WriteUInt32LittleEndian rather 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>
Copilot AI review requested due to automatic review settings June 19, 2026 16:24

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 2 out of 2 changed files in this pull request and generated no new comments.

@EgorBo

EgorBo commented Jun 22, 2026

Copy link
Copy Markdown
Member Author

PTAL @MihaZupan

@EgorBo EgorBo requested a review from MihaZupan June 22, 2026 11:18
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>
Copilot AI review requested due to automatic review settings June 26, 2026 00:20

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 2 out of 2 changed files in this pull request and generated no new comments.

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.

4 participants