Skip to content

Commit

Permalink
Improve resiliency in the face of memory corruption (arrays/strings w…
Browse files Browse the repository at this point in the history
…ith invalid length)

Summary: We have a few memory snapshots with unexpected/inconsistent contents in them (possibly due to memory corruption). We can report better warnings for strings/arrays with invalid length, and avoid a crash when dumping a string with an invalid length.

Differential Revision: D56714330

fbshipit-source-id: 2d8c0b27cb0b1681e41bb9cc005b2cb98c912147
  • Loading branch information
elliekorn authored and facebook-github-bot committed Apr 29, 2024
1 parent dbf8e6c commit b3ecb7c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
11 changes: 10 additions & 1 deletion Analysis/TracedHeap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,16 @@ public TracedHeap(IRootSet rootSet, ILogger logger)
m_objectAddressToPostorderIndex[lookupEntryIndex].PostorderIndex = postorderIndex;
lookupEntryIndex++;

m_numberOfLiveBytes += m_traceableHeap.GetObjectSize(m_native.From(address), typeIndex, committedOnly: true);
int objectSize = m_traceableHeap.GetObjectSize(m_native.From(address), typeIndex, committedOnly: true);
if (objectSize < 0)
{
// This indicates a memory corruption (array or string with invalid length field)
LogWarning(m_traceableHeap.TypeSystem.QualifiedName(typeIndex), $"object {postorderIndex} has negative size {objectSize}");
}
else
{
m_numberOfLiveBytes += objectSize;
}
}
}
Array.Sort(m_objectAddressToPostorderIndex, (x, y) => x.Address.CompareTo(y.Address));
Expand Down
8 changes: 7 additions & 1 deletion CommandInfrastructure/Command.cs
Original file line number Diff line number Diff line change
Expand Up @@ -547,7 +547,13 @@ protected void DumpObjectMemory(MemoryView objectView, int typeIndex, int indent
int length = maxLength < stringLength ? maxLength : stringLength;
for (int i = 0; i < length; i++)
{
objectView.Read(CurrentTraceableHeap.TypeSystem.SystemStringFirstCharOffset + i * 2, out char c);
int offset = CurrentTraceableHeap.TypeSystem.SystemStringFirstCharOffset + i * 2;
if (offset + 2 > objectView.Size)
{
// Guard against crashing if there is a memory corruption (string with invalid length)
break;
}
objectView.Read(offset, out char c);
sb.Append(c);
}

Expand Down

0 comments on commit b3ecb7c

Please sign in to comment.