Repository navigation
Description
Description
On CoreCLR 10 and 11 the type loader can place an instance field of a derived class at offset 0, on top of the first
field of the base class. Writing one field silently changes the other. No unsafe code, explicit layout or interop is
involved.
Reproduction Steps
var d = new Derived { BaseValue = 0x11223344 };
d.Y = 0xFFFF;
Console.WriteLine($"{Environment.Version}: BaseValue = 0x{d.BaseValue:X8}");
struct OneByte
{
public byte Value;
}
class Base
{
public int BaseValue;
}
class Derived : Base
{
public ushort X;
public OneByte S;
public ushort Y;
public object? R;
}Expected behavior
BaseValue = 0x11223344. This is what .NET 9.0.11 prints.
Actual behavior
BaseValue = 0x1122FFFF on 10.0.0, 10.0.15 and 11.0.0: Derived.Y is at offset 0, the offset of Base.BaseValue.
Measured offsets of Derived, in bytes from the first field:
| Field | .NET 9.0.11 | .NET 10.0.15 |
|---|---|---|
Base.BaseValue |
0..3 | 0..3 |
X |
4..5 | 4..5 |
Y |
6..7 | 0..1 |
R |
8..15 | 8..15 |
S |
16 | 16 |
With a third 2-byte field declared after Y, the last 2-byte field is placed over S instead of offset 0.
Regression?
Yes, from .NET 9. Introduced by #113454.
Known Workarounds
Declare the 2-byte fields next to each other, or move the struct field so that it is not between them.
Configuration
Windows 10 x64. Runtimes 10.0.0, 10.0.15 and 11.0.0 show the problem, 9.0.11 does not. Tiered compilation and ReadyToRun settings make no difference.
Other information
Cause, as far as I can tell from src/coreclr/vm/methodtablebuilder.cpp:
MethodTableBuilder::InitializeFieldDescs keeps a temporary size value in FieldDesc::m_pMTOfEnclosingClass. For
primitive fields it is log2 of the field size. For a by-value instance field of an auto layout type it used to be
LOG2_PTRSIZE with the comment "instance field layout ignores this value". Since #113454 it is
GetNumInstanceFieldBytes() of the field type, a size in bytes.
HandleAutoLayout places small fields into the gap left by the parent ("place small fields first if the parent have
a number of field bytes that is not aligned"). After it places the first field of size class i, it looks for the
next one:
// Update FirstInstanceFieldOfSize[i] to point to the next such field
for (j = j+1; j < bmtEnumFields->dwNumInstanceFields; j++)
{
// The log of the field size is stored in the method table
if ((DWORD_PTR&)pFieldDescList[j].m_pMTOfEnclosingClass == (size_t)i)
{
bmtFP->FirstInstanceFieldOfSize[i] = j;
break;
}
}This comparison now also matches a by-value field whose size in bytes equals i: a 1-byte struct looks like a 2-byte
primitive. In the example:
- The gap starts at 4.
Xis placed at 4,FirstInstanceFieldOfSize[1]becomes the index ofS. - The position is 6, still not aligned to 8, so the loop places
FirstInstanceFieldOfSize[1]again:Sgets
offset 6 andNumInstanceFieldsOfSize[1]drops to 0. - "Place fields, largest first" skips size class 1 because the count is 0, so
InstanceFieldStart[1]stays 0. - The final loop places the still unplaced
YatInstanceFieldStart[1] + 0, which is offset 0. Sis placed again with the other by-value fields, at 16.
The conditions that follow from this: the parent's field bytes leave a gap of at least 4 bytes to the 8-byte boundary
and the base offset is not aligned for ReadyToRun; the class has two or more 2-byte fields with a by-value field of
a 1-byte type declared between the first and the second; it has an 8-byte field; it has no 4-byte fields. Thirteen
variants built from these rules behaved as predicted on 10.0.0, 10.0.15 and 11.0.0, including an empty struct in place
of OneByte and long in place of the reference.
A possible fix is to skip by-value fields in that scan, for example by requiring
GetOffset() == FIELD_OFFSET_UNPLACED as the scan for GC fields a few lines above does.
The code is the same in release/10.0 and in main as of 2026-11-20.
Activity
jkotas commentedon Nov 20, 2026
cc @jkoritzinsky