-
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 all 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
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
124 changes: 124 additions & 0 deletions
124
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,124 @@ | ||
--- | ||
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 | ||
|
||
The .NET runtime now enforces stricter validation for type layouts. Specifically, the runtime throws a <xref:System.TypeLoadException> if a type with a non-explicit layout (for example, <xref:System.Runtime.InteropServices.LayoutKind.Auto> or <xref:System.Runtime.InteropServices.LayoutKind.Sequential>) specifies explicit field offsets using <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. | ||
|
||
## 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, the following code executed without error, and the `[FieldOffset]` attributes were ignored: | ||
|
||
```csharp | ||
using System; | ||
using System.Runtime.InteropServices; | ||
|
||
[StructLayout(LayoutKind.Sequential)] | ||
public struct MyStruct | ||
{ | ||
[FieldOffset(0)] // Ignored prior to .NET 10. | ||
public int Field1; | ||
|
||
[FieldOffset(4)] // Ignored prior to .NET 10. | ||
gewarren marked this conversation as resolved.
Show resolved
Hide resolved
|
||
public int Field2; | ||
} | ||
|
||
class Program | ||
{ | ||
static void Main() | ||
{ | ||
Console.WriteLine("Struct loaded successfully."); | ||
} | ||
} | ||
``` | ||
|
||
## New behavior | ||
|
||
Starting in .NET 10, 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: | ||
|
||
```csharp | ||
using System; | ||
using System.Runtime.InteropServices; | ||
|
||
[StructLayout(LayoutKind.Sequential)] | ||
public struct MyStruct | ||
{ | ||
[FieldOffset(0)] // TypeLoadException. | ||
public int Field1; | ||
|
||
[FieldOffset(4)] // TypeLoadException. | ||
gewarren marked this conversation as resolved.
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 <xref:System.Runtime.InteropServices.LayoutKind.Explicit?displayProperty=nameWithType> for types that specify explicit field offsets. For example: | ||
|
||
```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 | ||
|
||
This change does not directly affect specific APIs but impacts the run-time behavior of types defined with the following attributes: | ||
gewarren marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
- `[StructLayout(LayoutKind.Sequential)]` | ||
- `[StructLayout(LayoutKind.Auto)]` | ||
- `[FieldOffset]` | ||
gewarren marked this conversation as resolved.
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
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.