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

[release/7.0] Fix RVA field alignment (#60) #61

Merged
merged 1 commit into from Apr 5, 2023

Conversation

sbomer
Copy link
Member

@sbomer sbomer commented Jan 10, 2023

Backporting #60 to the release/7.0 branch (which I created for this purpose, pointing at the commit currently referenced by the dotnet/linker 7.0 branch: https://github.com/dotnet/linker/tree/release/7.0/external).

This implements a cecil fix required to support the alignment requirements of the Roslyn CreateSpan support.

Fixes dotnet/runtime#79477

Customer Impact

Soon (once we ship a Roslyn that supports the CreateSpan APIs introduced in 7.0), trimmed 7.0 apps using the latest Roslyn may start crashing if they use constant arrays of long or ulong that are cast to ReadOnlySpan<T>. The failure mode we have observed is ArgumentException during CreateSpan. The crash happens if the constant data representing the array happens to be misaligned after trimming (which is likely, because the 7.0 trimmer did not respect the new alignment requirements).

Note that this hasn't actually been observed externally yet, but we anticipate that we will once the new Roslyn is shipped.

Testing

We might want to wait to service this until we have released the fix in an 8.0 preview to get extra validation in the wild.

Risk

Medium to high risk. This changes the alignment of the code section for all assemblies written by cecil on x64, and can also change the alignment of later sections, even for code which doesn't use CreateSpan helpers. The change is significant at the binary level, but we have no reason to believe that this will break anything downstream. If the binary change is problematic, we would expect to see catastrophic failures during the testing, but this was not the case. (An earlier incomplete fix did result in catastrophic failures, validating this expectation).

This ensures that section starts are aligned by adjusting the
previous Range's length to ensure the computed start of a new
Range meets the alignment requirements. It was done this way
rather than just computing an aligned start for the new Range,
because the TextMap assumes that the Ranges are contiguous - see
for example GetNextRVA.

GetNextRVA was used to compute the Code RVA, before adding that
segment to the TextMap. This meant that the alignment (only added
later) wasn't taken into account when writing out the code, but
then later the TextMap was modified to include the alignment.

This is fixed by first inserting an aligned zero-length Code
segment before writing the code, then inserting it again once the
length is known. Removing the Code alignment altogether would
work too, but the alignment constants are kept in there, because
it looks like they were added intentionally, even though the
reason is unknown.

Note that before this change, the start of the Code segment (at
least on x64) was not 16-byte aligned in the first place, so this
change will actually move the beginning of the Code segment.
Copy link
Member

@jeffschwMSFT jeffschwMSFT left a comment

Choose a reason for hiding this comment

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

approved. we will take for consideration in 7.0.x

@sbomer
Copy link
Member Author

sbomer commented Mar 30, 2023

@agocke @vitek-karas now that we have over a month of this fix shipping in .NET 8 Preview 1, I think we should take it into the next 7.0 servicing release.

@agocke
Copy link
Member

agocke commented Mar 30, 2023

Sounds good to me. I'll send this to tactics.

@vitek-karas
Copy link
Member

Sounds good!

@agocke
Copy link
Member

agocke commented Apr 5, 2023

Approved.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
4 participants