-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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
base: main
Are you sure you want to change the base?
Disallow loading non-Explicit types with explicit field offsets #113500
Conversation
Tagging subscribers to this area: @mangod9 |
There was a problem hiding this 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. |
src/tests/Loader/classloader/SequentialLayout/Offsets/SequentialOffsets.cs
Outdated
Show resolved
Hide resolved
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
There was a problem hiding this 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 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 |
7f4f87c
to
c7d3a48
Compare
src/coreclr/tools/Common/TypeSystem/Common/MetadataFieldLayoutAlgorithm.cs
Outdated
Show resolved
Hide resolved
@@ -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." |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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) |
There was a problem hiding this comment.
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.
LGTM other than the existing comments from others |
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.