From 2460782c8021c0a711bf839278c4145e931a80b2 Mon Sep 17 00:00:00 2001 From: Filip Navara Date: Thu, 22 Sep 2022 19:54:54 +0200 Subject: [PATCH 1/3] Ensure that frozen objects respect the minimum object size --- src/coreclr/tools/Common/TypeSystem/Common/TargetDetails.cs | 2 ++ .../Compiler/DependencyAnalysis/EETypeNode.cs | 2 +- .../tools/aot/ILCompiler.Compiler/Compiler/TypePreinit.cs | 1 + 3 files changed, 4 insertions(+), 1 deletion(-) diff --git a/src/coreclr/tools/Common/TypeSystem/Common/TargetDetails.cs b/src/coreclr/tools/Common/TypeSystem/Common/TargetDetails.cs index 22aa2c0c047fbb..d048c4461a917c 100644 --- a/src/coreclr/tools/Common/TypeSystem/Common/TargetDetails.cs +++ b/src/coreclr/tools/Common/TypeSystem/Common/TargetDetails.cs @@ -185,6 +185,8 @@ public int MinimumCodeAlignment } } + public int MinimumObjectSize => PointerSize * 3; + public TargetDetails(TargetArchitecture architecture, TargetOS targetOS, TargetAbi abi) { Architecture = architecture; diff --git a/src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/DependencyAnalysis/EETypeNode.cs b/src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/DependencyAnalysis/EETypeNode.cs index a74192ec045124..c681a282cc3b05 100644 --- a/src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/DependencyAnalysis/EETypeNode.cs +++ b/src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/DependencyAnalysis/EETypeNode.cs @@ -123,7 +123,7 @@ public override ObjectNodeSection Section } } - public int MinimumObjectSize => _type.Context.Target.PointerSize * 3; + public int MinimumObjectSize => _type.Context.Target.MinimumObjectSize; protected virtual bool EmitVirtualSlotsAndInterfaces => false; diff --git a/src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/TypePreinit.cs b/src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/TypePreinit.cs index 15baf60300503e..6060ee4cc2a723 100644 --- a/src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/TypePreinit.cs +++ b/src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/TypePreinit.cs @@ -2234,6 +2234,7 @@ public ObjectInstance(DefType type, AllocationSite allocationSite) int size = type.InstanceByteCount.AsInt; if (type.IsValueType) size += type.Context.Target.PointerSize; + size = Math.Max(size, type.Context.Target.MinimumObjectSize - type.Context.Target.PointerSize); _data = new byte[size]; } From 7e819976cb91ea0b9cafcd8de3e344065927afef Mon Sep 17 00:00:00 2001 From: Filip Navara Date: Thu, 22 Sep 2022 21:14:35 +0200 Subject: [PATCH 2/3] Apply feedback --- .../tools/Common/TypeSystem/Common/TargetDetails.cs | 2 -- .../Compiler/DependencyAnalysis/EETypeNode.cs | 5 ++++- .../Compiler/DependencyAnalysis/FrozenObjectNode.cs | 9 +++++++++ .../aot/ILCompiler.Compiler/Compiler/TypePreinit.cs | 1 - 4 files changed, 13 insertions(+), 4 deletions(-) diff --git a/src/coreclr/tools/Common/TypeSystem/Common/TargetDetails.cs b/src/coreclr/tools/Common/TypeSystem/Common/TargetDetails.cs index d048c4461a917c..22aa2c0c047fbb 100644 --- a/src/coreclr/tools/Common/TypeSystem/Common/TargetDetails.cs +++ b/src/coreclr/tools/Common/TypeSystem/Common/TargetDetails.cs @@ -185,8 +185,6 @@ public int MinimumCodeAlignment } } - public int MinimumObjectSize => PointerSize * 3; - public TargetDetails(TargetArchitecture architecture, TargetOS targetOS, TargetAbi abi) { Architecture = architecture; diff --git a/src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/DependencyAnalysis/EETypeNode.cs b/src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/DependencyAnalysis/EETypeNode.cs index c681a282cc3b05..913c3e3812661e 100644 --- a/src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/DependencyAnalysis/EETypeNode.cs +++ b/src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/DependencyAnalysis/EETypeNode.cs @@ -123,7 +123,10 @@ public override ObjectNodeSection Section } } - public int MinimumObjectSize => _type.Context.Target.MinimumObjectSize; + public int MinimumObjectSize => GetMinimumObjectSize(_type.Context); + + public static int GetMinimumObjectSize(TypeSystemContext typeSystemContext) + => typeSystemContext.Target.PointerSize * 3; protected virtual bool EmitVirtualSlotsAndInterfaces => false; diff --git a/src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/DependencyAnalysis/FrozenObjectNode.cs b/src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/DependencyAnalysis/FrozenObjectNode.cs index 567ecb7595c513..dbd298b4b801cb 100644 --- a/src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/DependencyAnalysis/FrozenObjectNode.cs +++ b/src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/DependencyAnalysis/FrozenObjectNode.cs @@ -48,11 +48,20 @@ int ISymbolDefinitionNode.Offset public override void EncodeData(ref ObjectDataBuilder dataBuilder, NodeFactory factory, bool relocsOnly) { + int initialOffset = dataBuilder.CountBytes; + // Sync Block dataBuilder.EmitZeroPointer(); // byte contents _data.WriteContent(ref dataBuilder, this, factory); + + int objectSize = dataBuilder.CountBytes - initialOffset; + int minimumObjectSize = EETypeNode.GetMinimumObjectSize(factory.TypeSystemContext); + if (objectSize < minimumObjectSize) + { + dataBuilder.EmitZeros(minimumObjectSize - objectSize); + } } protected override string GetName(NodeFactory factory) => this.GetMangledName(factory.NameMangler); diff --git a/src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/TypePreinit.cs b/src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/TypePreinit.cs index 6060ee4cc2a723..15baf60300503e 100644 --- a/src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/TypePreinit.cs +++ b/src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/TypePreinit.cs @@ -2234,7 +2234,6 @@ public ObjectInstance(DefType type, AllocationSite allocationSite) int size = type.InstanceByteCount.AsInt; if (type.IsValueType) size += type.Context.Target.PointerSize; - size = Math.Max(size, type.Context.Target.MinimumObjectSize - type.Context.Target.PointerSize); _data = new byte[size]; } From 6ac8ebec4301e1b9c31d409b401ae719ee5e12a6 Mon Sep 17 00:00:00 2001 From: Filip Navara Date: Thu, 22 Sep 2022 21:33:38 +0200 Subject: [PATCH 3/3] Handle FrozenStringNode too --- .../Compiler/DependencyAnalysis/FrozenStringNode.cs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/DependencyAnalysis/FrozenStringNode.cs b/src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/DependencyAnalysis/FrozenStringNode.cs index 40765b9b17be8f..d1978383f236b2 100644 --- a/src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/DependencyAnalysis/FrozenStringNode.cs +++ b/src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/DependencyAnalysis/FrozenStringNode.cs @@ -59,6 +59,8 @@ private static IEETypeNode GetEETypeNode(NodeFactory factory) public override void EncodeData(ref ObjectDataBuilder dataBuilder, NodeFactory factory, bool relocsOnly) { + int initialOffset = dataBuilder.CountBytes; + dataBuilder.EmitZeroPointer(); // Sync block dataBuilder.EmitPointerReloc(GetEETypeNode(factory)); @@ -73,6 +75,12 @@ public override void EncodeData(ref ObjectDataBuilder dataBuilder, NodeFactory f // Null-terminate for friendliness with interop dataBuilder.EmitShort(0); + int objectSize = dataBuilder.CountBytes - initialOffset; + int minimumObjectSize = EETypeNode.GetMinimumObjectSize(factory.TypeSystemContext); + if (objectSize < minimumObjectSize) + { + dataBuilder.EmitZeros(minimumObjectSize - objectSize); + } } protected override string GetName(NodeFactory factory) => this.GetMangledName(factory.NameMangler);