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

Disallow loading non-Explicit types with explicit field offsets #113500

Open
wants to merge 8 commits into
base: main
Choose a base branch
from

Conversation

jkoritzinsky
Copy link
Member

Today, explicit field offsets on Auto-layout types are ignored, as are offsets on Sequential types that don't qualify for ManagedSequential (and as a result end up with Auto layout).

For Sequential layout, CoreCLR allows offsets to be specified. These "offsets" specify the sequencing order of the fields in sequential layout. This behavior is disallowed (you can't specify FieldOffset on a Sequential type) and unused by Roslyn and F# and C++/CLI. It's also illegal per the ECMA-335 spec (it specifically only allows FieldOffset rows for fields in an explicit layout type).

We had one IL regression test that validated that specifying offsets for a sequential type was ignored (so not testing the above feature).

Remove this unused feature and change the type loader to throw if a non-explicit layout type has offset information for its fields.

@jkoritzinsky jkoritzinsky added breaking-change Issue or PR that represents a breaking API or functional change over a prerelease. area-VM-coreclr labels Mar 13, 2025
@dotnet-policy-service dotnet-policy-service bot added the needs-breaking-change-doc-created Breaking changes need an issue opened with https://github.com/dotnet/docs/issues/new?template=dotnet label Mar 13, 2025
Copy link
Contributor

Tagging subscribers to this area: @mangod9
See info in area-owners.md if you want to be subscribed.

Copy link
Contributor

@Copilot Copilot AI left a comment

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 support for explicit field offsets on non-explicit layout types by making the type loader throw a TypeLoadException when such offsets are detected.

  • Added tests in AutoLayout and SequentialOffsets to validate that a TypeLoadException is thrown.
  • Ensured both auto and sequential layout tests check for the disallowed feature.

Reviewed Changes

Copilot reviewed 14 out of 14 changed files in this pull request and generated 2 comments.

File Description
src/tests/Loader/classloader/AutoLayout/AutoLayout.cs Adds a test to confirm that Auto-layout types with field offsets throw a TypeLoadException.
src/tests/Loader/classloader/SequentialLayout/Offsets/SequentialOffsets.cs Adds a similar test for Sequential layout types with field offsets.

jkoritzinsky and others added 2 commits March 13, 2025 16:07
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@jkoritzinsky jkoritzinsky requested a review from Copilot March 13, 2025 23:08

Choose a reason for hiding this comment

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

Pull Request Overview

This pull request introduces tests to verify that attempting to load non-explicit layout types with explicit field offsets now throws a TypeLoadException, enforcing the ECMA-335 specification.

  • Added test in AutoLayout to ensure explicit field offsets are disallowed for auto-layout types.
  • Added test in SequentialOffsets to ensure explicit field offsets are disallowed for sequential layout types.

Reviewed Changes

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

File Description
src/tests/Loader/classloader/AutoLayout/AutoLayout.cs Adds a test for Auto-layout types with explicit field offsets
src/tests/Loader/classloader/SequentialLayout/Offsets/SequentialOffsets.cs Adds a test for Sequential layout types with explicit field offsets
@steveisok steveisok requested a review from a team March 18, 2025 23:06
@@ -394,7 +394,8 @@ BEGIN
IDS_INVALID_REDIM "Illegal attempt to replace or redimension a fixed or locked SafeArray."

IDS_INVALID_PINVOKE_CALLCONV "Unsupported unmanaged calling convention."
IDS_CLASSLOAD_NSTRUCT_EXPLICIT_OFFSET "Could not load type '%1' from assembly '%2' because field '%3' was not given an explicit offset."
IDS_CLASSLOAD_EXPSTRUCT_EXPLICIT_OFFSET "Could not load type '%1' from assembly '%2' because field '%3' was not given an explicit offset."
IDS_CLASSLOAD_STRUCT_EXPLICIT_OFFSET "Could not load type '%1' from assembly '%2' because field '%3' in a non-explicit type was given an explicit offset."
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
IDS_CLASSLOAD_STRUCT_EXPLICIT_OFFSET "Could not load type '%1' from assembly '%2' because field '%3' in a non-explicit type was given an explicit offset."
IDS_CLASSLOAD_STRUCT_EXPLICIT_OFFSET "Could not load type '%1' from assembly '%2' because field '%3' in was given an explicit offset, that is only allowed in types with explicit layout."

"non-explicit type" is not an established term

@@ -293,17 +293,37 @@ static bool CompareTypeLayout(MetadataType type1, MetadataType type2)
(layoutMetadata1.Size != layoutMetadata2.Size))
return false;

if ((explicitLayout) && !(layoutMetadata1.Offsets == null && layoutMetadata2.Offsets == null))
if (explicitLayout)
Copy link
Member

Choose a reason for hiding this comment

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

We walk all fields on both types starting at line 209 in this file already - I would just add the check there so that we don't need to do this ugly compare-two-IEnumerables dance twice.

@kg
Copy link
Member

kg commented Mar 19, 2025

LGTM other than the existing comments from others

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-VM-coreclr breaking-change Issue or PR that represents a breaking API or functional change over a prerelease. needs-breaking-change-doc-created Breaking changes need an issue opened with https://github.com/dotnet/docs/issues/new?template=dotnet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants