Skip to content

Commit 9a24ec0

Browse files
committed
Don't choke as much on malformed data
1 parent 39bae18 commit 9a24ec0

File tree

2 files changed

+15
-7
lines changed

2 files changed

+15
-7
lines changed

UMS.Analysis/SnapshotFile.cs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -134,15 +134,21 @@ public RawManagedObjectInfo ParseManagedObjectInfo(ulong address)
134134
return new();
135135

136136
info.TypeInfoAddress = ReadPointer(typeInfoSpan);
137-
TypeIndicesByPointer.TryGetValue(info.TypeInfoAddress, out info.TypeDescriptionIndex);
138-
139-
if (!info.IsKnownType)
140-
throw new($"Failed to resolve type for object at {address:X}");
137+
if (!TypeIndicesByPointer.TryGetValue(info.TypeInfoAddress, out info.TypeDescriptionIndex) || !info.IsKnownType)
138+
{
139+
Console.WriteLine($"WARNING: Failed to resolve type for object at {address:X}");
140+
141+
//Cache the failure - let's not waste time.
142+
ret = _managedObjectInfoCache[address] = new();
143+
return ret;
144+
}
141145
}
142146

143147
var typeIndex = info.TypeDescriptionIndex;
144148
info.Flags = GetTypeFlagsByIndex(typeIndex);
145149
info.Size = SizeOfObjectInBytes(info, heap);
150+
if (info.Size == 0)
151+
throw new("Size 0?");
146152
info.Data = heap[..info.Size].ToArray();
147153
info.SelfAddress = address;
148154

@@ -190,8 +196,8 @@ private int GetObjectSizeFromArrayInBytes(RawManagedObjectInfo info, Span<byte>
190196

191197
if (arrayLength > heap.Length)
192198
{
193-
Console.WriteLine($"Warning: Skipping unreasonable array length {arrayLength}");
194-
return VirtualMachineInformation.ArrayHeaderSize; //Sanity bailout
199+
Console.WriteLine($"Warning: Reducing array length {arrayLength} to {heap.Length} because the heap doesn't contain all the data.");
200+
return heap.Length; //Sanity bailout
195201
}
196202

197203
//Need to check if the array element type is a value type

UMS.Analysis/Structures/Objects/ManagedClassInstance.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,8 @@ public ManagedClassInstance(SnapshotFile file, RawManagedObjectInfo info, Manage
100100
var elementTypeSize = (elementFlags & TypeFlags.ValueType) != 0 ? file.GetTypeDescriptionSizeBytes(elementType.TypeIndex) : 8;
101101
var arrayData = info.Data.AsSpan(file.VirtualMachineInformation.ArrayHeaderSize..);
102102

103+
arrayElementCount = Math.Min(arrayElementCount, arrayData.Length / elementTypeSize); //Just in case the array length is wrong
104+
103105
Fields = new IFieldValue[arrayElementCount];
104106
for (var i = 0; i < arrayElementCount; i++)
105107
{
@@ -304,7 +306,7 @@ public bool IsLeakedManagedShell(SnapshotFile file)
304306
var basicFieldInfoCache = fields[fieldNumber];
305307
var name = file.GetFieldName(basicFieldInfoCache.FieldIndex);
306308

307-
if (name == "m_CachedPtr")
309+
if (name == "m_CachedPtr" && Fields.Length > fieldNumber)
308310
{
309311
var value = Fields[fieldNumber];
310312

0 commit comments

Comments
 (0)