-
Notifications
You must be signed in to change notification settings - Fork 6k
Document breaking change: Disallow loading non-Explicit types with explicit field offsets #49125
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
Closed
Closed
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
123 changes: 123 additions & 0 deletions
123
docs/core/compatibility/interop/10.0/explicit-field-offset-validation.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,123 @@ | ||
| --- | ||
| title: "Breaking change: Disallow loading non-Explicit types with explicit field offsets" | ||
| description: "Learn about the breaking change in .NET 10 where the runtime enforces stricter validation for type layouts and throws TypeLoadException if non-explicit layout types specify explicit field offsets." | ||
| ms.date: 10/13/2025 | ||
| ai-usage: ai-assisted | ||
| --- | ||
|
|
||
| # Disallow loading non-Explicit types with explicit field offsets | ||
|
|
||
| Starting in .NET 10 Preview 4, the .NET runtime enforces stricter validation for type layouts. Specifically, the runtime now throws a <xref:System.TypeLoadException> if a type with a non-explicit layout (for example, `Auto` or `Sequential`) specifies explicit field offsets using the <xref:System.Runtime.InteropServices.FieldOffsetAttribute>. This change aligns the runtime's behavior with the ECMA-335 specification, which only permits explicit field offsets for types with an `Explicit` layout. | ||
gewarren marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| ## Version introduced | ||
|
|
||
| .NET 10 Preview 4 | ||
|
|
||
| ## Previous behavior | ||
|
|
||
| In earlier versions of .NET, the runtime ignored explicit field offsets on types with `Auto` or `Sequential` layouts. For example: | ||
gewarren marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| ```csharp | ||
| using System; | ||
| using System.Runtime.InteropServices; | ||
|
|
||
| [StructLayout(LayoutKind.Sequential)] | ||
| public struct MyStruct | ||
| { | ||
| [FieldOffset(0)] // Ignored in .NET versions prior to 10 Preview 4 | ||
gewarren marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| public int Field1; | ||
|
|
||
| [FieldOffset(4)] // Ignored in .NET versions prior to 10 Preview 4 | ||
gewarren marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| public int Field2; | ||
| } | ||
|
|
||
| class Program | ||
| { | ||
| static void Main() | ||
| { | ||
| Console.WriteLine("Struct loaded successfully."); | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| The above code would execute without error, and the `[FieldOffset]` attributes would be ignored. | ||
gewarren marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| ## New behavior | ||
|
|
||
| Starting in .NET 10 Preview 4, the runtime enforces the ECMA-335 specification and throws a <xref:System.TypeLoadException> if a type with `Auto` or `Sequential` layout specifies explicit field offsets. The following code now throws an exception: | ||
gewarren marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| ```csharp | ||
| using System; | ||
| using System.Runtime.InteropServices; | ||
|
|
||
| [StructLayout(LayoutKind.Sequential)] | ||
| public struct MyStruct | ||
| { | ||
| [FieldOffset(0)] // Causes a TypeLoadException in .NET 10 Preview 4 and later | ||
gewarren marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| public int Field1; | ||
|
|
||
| [FieldOffset(4)] // Causes a TypeLoadException in .NET 10 Preview 4 and later | ||
gewarren marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| public int Field2; | ||
| } | ||
|
|
||
| class Program | ||
| { | ||
| static void Main() | ||
| { | ||
| Console.WriteLine("Struct loaded successfully."); | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| **Exception thrown:** | ||
|
|
||
| ```output | ||
| System.TypeLoadException: Explicit field offsets are not allowed on types with Sequential or Auto layout. | ||
| ``` | ||
|
|
||
| ## Type of breaking change | ||
|
|
||
| This change can affect [binary compatibility](../../categories.md#binary-compatibility) and is a [behavioral change](../../categories.md#behavioral-change). | ||
|
|
||
| ## Reason for change | ||
|
|
||
| This change was introduced to align the .NET runtime with the ECMA-335 specification, which explicitly disallows explicit field offsets on types with `Auto` or `Sequential` layouts. The previous behavior of ignoring these offsets was non-compliant and could lead to unexpected behavior or incorrect assumptions about memory layout. | ||
|
|
||
| ## Recommended action | ||
|
|
||
| To resolve this issue, update your code to use `LayoutKind.Explicit` for types that specify explicit field offsets. For example: | ||
gewarren marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| ```csharp | ||
| using System; | ||
| using System.Runtime.InteropServices; | ||
|
|
||
| [StructLayout(LayoutKind.Explicit)] // Use Explicit layout | ||
| public struct MyStruct | ||
| { | ||
| [FieldOffset(0)] | ||
| public int Field1; | ||
|
|
||
| [FieldOffset(4)] | ||
| public int Field2; | ||
| } | ||
|
|
||
| class Program | ||
| { | ||
| static void Main() | ||
| { | ||
| Console.WriteLine("Struct loaded successfully."); | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| Alternatively, if explicit field offsets aren't required, remove the <xref:System.Runtime.InteropServices.FieldOffsetAttribute> attributes and rely on the default behavior of `Sequential` or `Auto` layout. | ||
|
|
||
| ## Affected APIs | ||
|
|
||
| - <xref:System.Runtime.InteropServices.StructLayoutAttribute?displayProperty=fullName> | ||
| - <xref:System.Runtime.InteropServices.FieldOffsetAttribute?displayProperty=fullName> | ||
gewarren marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| ## See also | ||
|
|
||
| - [ECMA-335 specification](https://www.ecma-international.org/publications-and-standards/standards/ecma-335/) | ||
| - [Customize structure marshalling](../../../../standard/native-interop/customize-struct-marshalling.md) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.