Registry Mirror
Skip to content

Conversation

@uweigand

Copy link
Copy Markdown
Contributor
  • Base64Encoder needs to take byte order into accout
    when packing characters into an int.

  • First three fields of GUID data structure are held in
    native byte order (not always little-endian).

  • UnmanagedMemoryAccessor needs to access internal fields
    of decimal type correctly in native byte order.

  • When accessing a memory-mapped resource file, strings
    may need to be converted to native byte order.

  • The metadata blob writer did not handle writing UTF16
    characters correctly on big-endian systems.

  • Endian bug in PEBuilder checksum computation.

@dnfadmin

dnfadmin commented Feb 17, 2021 •

Copy link
Copy Markdown

CLA assistant check
All CLA requirements met.

@directhex directhex added the arch-s390x Related to s390x architecture (unsupported) label Feb 17, 2021
@steveisok

Copy link
Copy Markdown
Member

@stephentoub Do you know who would be the appropriate reviewer(s) for this PR?

@stephentoub

Copy link
Copy Markdown
Member

cc: @GrabYourPitchforks

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This is allocating an array on every access. If we want to do that for the big endian code path for now, ok, but we can't add such allocation to the little endian code path.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

OK, changed to keep the logic and just handle endianness directly.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

What is the perf impact of this change? Previously it was a memcpy; now it's going through Encoding.Unicode.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

In this case, the change looked like a potential correctness (or at least consistency) fix even on little-endian to me. This routine has two code paths, with a special case where the resource file is provided as an UnmanagedMemoryStream. In all other cases, the existing code already used Encoding.Unicode.GetString, only the UMS case used the direct copy.

These are semantically different e.g. in the case when surrogate characters are present. It seems strange to have this semantic difference triggered by simply whether the input stream is a UMS or not.

If this different behavior is indeed intentional, I guess I can instead add a simple byte-swapping loop in the big-endian case.

(As to perf impact, I don't know what would be a good benchmark for that. Looking at the Encoding.Unicode source code, there is a fast path for the case where no surrogates are present, but that is of course still slower than just a memcpy.)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Anywhere we're adding allocations that we weren't previously allocating is a red flag. We shouldn't be doing this here.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Changed to keep the logic on little-endian and just add extra swaps for the big-endian code path.

@ericstj

ericstj commented Feb 26, 2021

Copy link
Copy Markdown
Member

Added a couple reviewers based on areas touched.

Base automatically changed from master to main March 1, 2021 09:07
* Base64Encoder needs to take byte order into accout
  when packing characters into an int.

* First three fields of GUID data structure are held in
  native byte order (not always little-endian).

* UnmanagedMemoryAccessor needs to access internal fields
  of decimal type correctly in native byte order.

* When accessing a memory-mapped resource file, strings
  may need to be converted to native byte order.

* The metadata blob writer did not handle writing UTF16
  characters correctly on big-endian systems.

* Endian bug in PEBuilder checksum computation.
// we need to fix up the first three fields (int, short, short)
if (!BitConverter.IsLittleEndian)
{
byte tmp = guidPtr[0];

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can we use ReverseEndianness oor Read*LittleEndian to better handle this?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

It appears during CI this code is also built against older .NET versions that do not have the BinaryPrimitives features. This has caused failures with earlier versions of the PR, which is why I went back to open-coding everything ...

{
checksum = AggregateChecksum(checksum, *(ushort*)ptr);
// little-endian encoding:
checksum = AggregateChecksum(checksum, (ushort)(ptr[1] << 8 | ptr[0]));

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Same here, maybe BinaryPrimitives.ReadUInt16LittleEndian?

@uweigand

Copy link
Copy Markdown
Contributor Author

Ping? If there is anything else I can do to make progress here, please let me know.

@akoeplinger

Copy link
Copy Markdown
Member

@uweigand I'd also extract the controversial bit where @stephentoub had perf concerns into a separate PR so that we don't need to block this one over it.

@ericstj

ericstj commented Mar 16, 2021

Copy link
Copy Markdown
Member

Ping? If there is anything else I can do to make progress here, please let me know.

Next steps are for area reviewers to chime in and sign off.

Paging owners:
System.Buffers - @pgovind @tannergooding
System.Memory, System.IO - @adamsitnik @carlossanlop @jozkee
System.Resources - @buyaa-n @joperezr @krwq
System.Reflection.Metadata - @buyaa-n @joperezr @krwq @steveharter

@uweigand

Copy link
Copy Markdown
Contributor Author

To better separate concerns and hopefully simplify the review process, I've now split this up into multiple separate PRs:
#49811 (ResourceReader.AllocateStringForNameIndex)
#49812 (System.Reflection.Metadata BlobBuilder/BlobWriter)
#49813 (GUID handling)
#49814 (Base64Encoder)
#49815 (UnmanagedMemoryAccessor.Write (decimal))
#49816 (PEBuilder.CalculateChecksum)
Please have a look there instead. Closing this PR now.

@uweigand uweigand closed this Mar 18, 2021
@ghost ghost locked as resolved and limited conversation to collaborators Apr 17, 2021
@karelz karelz added this to the 6.0.0 milestone May 20, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

arch-s390x Related to s390x architecture (unsupported) area-System.Runtime

Projects

None yet

Development

Successfully merging this pull request may close these issues.

9 participants