Skip to content

Commit 6a32ee4

Browse files
Skip static fields in FieldLayoutIntervalCalculator (#115987)
It was reported on discord that: ```csharp using System.Runtime.InteropServices; ref struct Inner1 { public const string SomeStr = "AAA"; } ref struct Inner2 { public Inner1 F; } [StructLayout(LayoutKind.Explicit, Size = 100)] ref struct Outer { [FieldOffset(0)] public Inner2 F; } class Program { static void Main(string[] args) { Outer x = new Outer(); x.F.F = new Inner1(); Console.WriteLine(Inner1.SomeStr); } } ``` Will throw a `System.TypeLoadException: Could not load type 'Outer' from assembly 'TestProj, Version=1.0.0.0, Culture=neutral, PublicKey=null' because it contains an object field at offset '2147483647' that is incorrectly aligned or overlapped by a non-object field` at runtime in .NET 9. It does not throw that on .NET 10, presumably thanks to #111584, but psychic debugging tells me we still have an issue here.
1 parent 30160aa commit 6a32ee4

File tree

1 file changed

+3
-0
lines changed

1 file changed

+3
-0
lines changed

src/coreclr/tools/Common/TypeSystem/Common/FieldLayoutIntervalCalculator.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,9 @@ private void AddToFieldLayout(List<FieldLayoutInterval> fieldLayout, int offset,
135135

136136
foreach (FieldDesc field in fieldType.GetFields())
137137
{
138+
if (field.IsStatic)
139+
continue;
140+
138141
int fieldOffset = offset + field.Offset.AsInt;
139142
AddToFieldLayout(fieldLayout, fieldOffset, field.FieldType);
140143
}

0 commit comments

Comments
 (0)