Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SRM] Refactor reading from streams. #111323

Merged
merged 16 commits into from
Jan 28, 2025
Merged

Conversation

teo-tsirpanis
Copy link
Contributor

@teo-tsirpanis teo-tsirpanis commented Jan 13, 2025

This PR refactors and streamlines the code in System.Reflection.Metadata that reads from streams. I originally attempted to implement my suggestion in #17088 (comment), but realized that it is not necessary, and instead of unifying PEBinaryReader and BlobReader, we always use the former, and wrapping AbstractMemoryBlocks in an UnmanagedMemoryStream still resulted in some simplifications. I also took advantage of some newer APIs in certain cases.

Fixes #17088.

@teo-tsirpanis teo-tsirpanis requested a review from Copilot January 13, 2025 01:50
@dotnet-policy-service dotnet-policy-service bot added the community-contribution Indicates that the PR has been added by a community member label Jan 13, 2025

Choose a reason for hiding this comment

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

Copilot reviewed 8 out of 23 changed files in this pull request and generated no comments.

Files not reviewed (15)
  • src/libraries/System.Reflection.Metadata/src/System.Reflection.Metadata.csproj: Language not supported
  • src/libraries/System.Reflection.Metadata/src/System/Reflection/Internal/Utilities/ImmutableMemoryStream.cs: Evaluated as low risk
  • src/libraries/System.Reflection.Metadata/src/System/Reflection/Internal/MemoryBlocks/StreamConstraints.cs: Evaluated as low risk
  • src/libraries/System.Reflection.Metadata/src/System/Reflection/Internal/MemoryBlocks/MemoryBlockProvider.cs: Evaluated as low risk
  • src/libraries/System.Reflection.Metadata/src/System/Reflection/Internal/Utilities/StreamExtensions.cs: Evaluated as low risk
  • src/libraries/System.Reflection.Metadata/src/System/Reflection/Internal/MemoryBlocks/ByteArrayMemoryProvider.cs: Evaluated as low risk
  • src/libraries/System.Reflection.Metadata/src/System/Reflection/Internal/MemoryBlocks/ExternalMemoryBlockProvider.cs: Evaluated as low risk
  • src/libraries/System.Reflection.Metadata/src/System/Reflection/PortableExecutable/PEReader.cs: Evaluated as low risk
  • src/libraries/System.Reflection.Metadata/src/System/Reflection/PortableExecutable/SectionHeader.cs: Evaluated as low risk
  • src/libraries/System.Reflection.Metadata/src/System/Reflection/Internal/Utilities/StreamExtensions.netcoreapp.cs: Evaluated as low risk
  • src/libraries/System.Reflection.Metadata/src/System/Reflection/PortableExecutable/DirectoryEntry.cs: Evaluated as low risk
  • src/libraries/System.Reflection.Metadata/src/System/Reflection/PortableExecutable/PEHeader.cs: Evaluated as low risk
  • src/libraries/System.Reflection.Metadata/src/System/Reflection/PortableExecutable/PEHeaders.cs: Evaluated as low risk
  • src/libraries/System.Reflection.Metadata/src/System/Reflection/PortableExecutable/CoffHeader.cs: Evaluated as low risk
  • src/libraries/System.Reflection.Metadata/src/System/Reflection/PortableExecutable/PEBinaryReader.cs: Evaluated as low risk
@@ -194,7 +195,7 @@ public unsafe PEReader(Stream peStream, PEStreamOptions options, int size)
// if the caller asked for metadata initialize the PE headers (calculates metadata offset):
if ((options & PEStreamOptions.PrefetchMetadata) != 0)
{
InitializePEHeaders();
_lazyPEHeaders = new PEHeaders(imageBlock, IsLoadedImage);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

We save an unnecessary Interlocked.CompareExchange here.

If the memory provider is backed by a stream, we use that, otherwise we get a memory block and wrap it in an `UnmanagedMemoryStream`.
Turns out the unification of `PEBinaryReader` and `BlobReader` can be avoided for now, and refactorings can still be made without it.
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I kept these changes to make the API shape match BlobReader. Let me know if I should revert them.

@teo-tsirpanis
Copy link
Contributor Author

Failures are unrelated, this is ready for review.

@teo-tsirpanis teo-tsirpanis marked this pull request as ready for review January 14, 2025 15:32
@steveharter steveharter self-assigned this Jan 16, 2025
@steveharter
Copy link
Member

/azp run runtime-wasm

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@steveharter
Copy link
Member

/azp run runtime

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@steveharter
Copy link
Member

Attempting to restart some CI due to hang and failures.

@steveharter steveharter added this to the 10.0.0 milestone Jan 28, 2025
Copy link
Member

@steveharter steveharter left a comment

Choose a reason for hiding this comment

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

LGTM thanks @teo-tsirpanis for cleaning up this. We'll keep an eye out for potential regressions here.

@steveharter steveharter merged commit 473cad8 into dotnet:main Jan 28, 2025
92 of 99 checks passed
@teo-tsirpanis teo-tsirpanis deleted the srm-streams branch January 28, 2025 20:23
grendello added a commit to grendello/runtime that referenced this pull request Jan 28, 2025
* main: (31 commits)
  Fix linux-x86 build (dotnet#111861)
  Add FrozenDictionary specialization for integers / enums (dotnet#111886)
  [SRM] Refactor reading from streams. (dotnet#111323)
  Sign the DAC and DBI during the build process instead of in separate steps (dotnet#111416)
  Removing Entry2MethodDesc as it is unnecessary (dotnet#111756)
  Cross Product for Vector2 and Vector4 (dotnet#111265)
  Handle unicode in absolute URI path for combine. (dotnet#111710)
  Drop RequiresProcessIsolation on mcc tests (dotnet#111887)
  [main] Update dependencies from dotnet/roslyn (dotnet#111691)
  new trimmer feature System.TimeZoneInfo.Invariant (dotnet#111215)
  [browser] reduce msbuild memory footprint (dotnet#111751)
  Add debugging checks for stack overflow tests failure (dotnet#111867)
  Localized file check-in by OneLocBuild Task: Build definition ID 679: Build ID 2629821 (dotnet#111884)
  Bump main to preview2 (dotnet#111882)
  Avoid generic virtual dispatch for frozen collections alternate lookup (dotnet#108732)
  Bump main versioning to preview1 (dotnet#111880)
  Switch OneLoc to main (dotnet#111872)
  Improve docs on building ILVerify (dotnet#111851)
  Update Debian version to 13 (dotnet#111768)
  win32: add fallback to environment vars for system folder (dotnet#109673)
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-System.Reflection.Metadata community-contribution Indicates that the PR has been added by a community member
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Unify decoding PE headers and reading other PE sections
2 participants