Skip to content
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

Fix preinit of types placing the same value in two fields #73605

Merged
merged 3 commits into from
Aug 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Collections.Generic;

using Internal.Text;
Expand All @@ -14,19 +15,22 @@ namespace ILCompiler.DependencyAnalysis
/// </summary>
public class FrozenObjectNode : EmbeddedObjectNode, ISymbolDefinitionNode
{
private readonly FieldDesc _field;
private readonly MetadataType _owningType;
private readonly TypePreinit.ISerializableReference _data;
private readonly int _allocationSiteId;

public FrozenObjectNode(FieldDesc field, TypePreinit.ISerializableReference data)
public FrozenObjectNode(MetadataType owningType, int allocationSiteId, TypePreinit.ISerializableReference data)
{
_field = field;
_owningType = owningType;
_allocationSiteId = allocationSiteId;
_data = data;
}

public void AppendMangledName(NameMangler nameMangler, Utf8StringBuilder sb)
{
sb.Append(nameMangler.CompilationUnitPrefix).Append("__FrozenObj_")
.Append(nameMangler.GetMangledFieldName(_field));
.Append(nameMangler.GetMangledTypeName(_owningType))
.Append(_allocationSiteId.ToStringInvariant());
}

public override bool StaticDependenciesAreComputed => true;
Expand All @@ -38,7 +42,7 @@ int ISymbolDefinitionNode.Offset
get
{
// The frozen object symbol points at the MethodTable portion of the object, skipping over the sync block
return OffsetFromBeginningOfArray + _field.Context.Target.PointerSize;
return OffsetFromBeginningOfArray + _owningType.Context.Target.PointerSize;
}
}

Expand Down Expand Up @@ -81,7 +85,12 @@ protected override void OnMarked(NodeFactory factory)

public override int CompareToImpl(ISortableNode other, CompilerComparer comparer)
{
return comparer.Compare(((FrozenObjectNode)other)._field, _field);
var otherFrozenObjectNode = (FrozenObjectNode)other;
int result = comparer.Compare(otherFrozenObjectNode._owningType, _owningType);
if (result != 0)
return result;

return _allocationSiteId.CompareTo(otherFrozenObjectNode._allocationSiteId);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public override ObjectData GetData(NodeFactory factory, bool relocsOnly = false)
TypePreinit.ISerializableValue val = _preinitializationInfo.GetFieldValue(field);
int currentOffset = builder.CountBytes;
if (val != null)
val.WriteFieldData(ref builder, field, factory);
val.WriteFieldData(ref builder, factory);
else
builder.EmitZeroPointer();
Debug.Assert(builder.CountBytes - currentOffset == field.FieldType.GetElementSize().AsInt);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ private void CreateNodeCaches()

_frozenObjectNodes = new NodeCache<SerializedFrozenObjectKey, FrozenObjectNode>(key =>
{
return new FrozenObjectNode(key.Owner, key.SerializableObject);
return new FrozenObjectNode(key.OwnerType, key.AllocationSiteId, key.SerializableObject);
});

_interfaceDispatchCells = new NodeCache<DispatchCellKey, InterfaceDispatchCellNode>(callSiteCell =>
Expand Down Expand Up @@ -1063,9 +1063,9 @@ public FrozenStringNode SerializedStringObject(string data)

private NodeCache<SerializedFrozenObjectKey, FrozenObjectNode> _frozenObjectNodes;

public FrozenObjectNode SerializedFrozenObject(FieldDesc owningField, TypePreinit.ISerializableReference data)
public FrozenObjectNode SerializedFrozenObject(MetadataType owningType, int allocationSiteId, TypePreinit.ISerializableReference data)
{
return _frozenObjectNodes.GetOrAdd(new SerializedFrozenObjectKey(owningField, data));
return _frozenObjectNodes.GetOrAdd(new SerializedFrozenObjectKey(owningType, allocationSiteId, data));
}

private NodeCache<MethodDesc, EmbeddedObjectNode> _eagerCctorIndirectionNodes;
Expand Down Expand Up @@ -1306,18 +1306,21 @@ public UninitializedWritableDataBlobKey(Utf8String name, int size, int alignment

protected struct SerializedFrozenObjectKey : IEquatable<SerializedFrozenObjectKey>
{
public readonly FieldDesc Owner;
public readonly MetadataType OwnerType;
public readonly int AllocationSiteId;
public readonly TypePreinit.ISerializableReference SerializableObject;

public SerializedFrozenObjectKey(FieldDesc owner, TypePreinit.ISerializableReference obj)
public SerializedFrozenObjectKey(MetadataType ownerType, int allocationSiteId, TypePreinit.ISerializableReference obj)
{
Owner = owner;
Debug.Assert(ownerType.HasStaticConstructor);
OwnerType = ownerType;
AllocationSiteId = allocationSiteId;
SerializableObject = obj;
}

public override bool Equals(object obj) => obj is SerializedFrozenObjectKey && Equals((SerializedFrozenObjectKey)obj);
public bool Equals(SerializedFrozenObjectKey other) => Owner == other.Owner;
public override int GetHashCode() => Owner.GetHashCode();
public bool Equals(SerializedFrozenObjectKey other) => OwnerType == other.OwnerType && AllocationSiteId == other.AllocationSiteId;
public override int GetHashCode() => HashCode.Combine(OwnerType.GetHashCode(), AllocationSiteId);
}

private struct MethodILKey : IEquatable<MethodILKey>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ public override ObjectData GetData(NodeFactory factory, bool relocsOnly)

TypePreinit.ISerializableValue val = preinitInfo.GetFieldValue(field);
int currentOffset = builder.CountBytes;
val.WriteFieldData(ref builder, field, factory);
val.WriteFieldData(ref builder, factory);
Debug.Assert(builder.CountBytes - currentOffset == field.FieldType.GetElementSize().AsInt);
}

Expand Down
Loading