Skip to content

dotnet-pgo dump: JSON serialization crash when CallWeights are present #125935

@benaadams

Description

@benaadams

Description

dotnet-pgo dump crashes with System.InvalidOperationException: Cannot write a JSON property within an array when the .mibc file contains CallWeights data (caller-callee edge weights from CPU sampling).

Root cause

In Program.cs line 311-317, the dump code opens a JSON array for CallWeights but writes named properties directly inside it without wrapping each entry in a JSON object:

jsonWriter.WriteStartArray("CallWeights");
foreach (var callWeight in data.CallWeights)
{
    jsonWriter.WriteString("Method", callWeight.Key.ToString());  // crash: property inside array
    jsonWriter.WriteNumber("Weight", callWeight.Value);
}
jsonWriter.WriteEndArray();

Fix

Add WriteStartObject() / WriteEndObject() around each array entry:

jsonWriter.WriteStartArray("CallWeights");
foreach (var callWeight in data.CallWeights)
{
    jsonWriter.WriteStartObject();
    jsonWriter.WriteString("Method", callWeight.Key.ToString());
    jsonWriter.WriteNumber("Weight", callWeight.Value);
    jsonWriter.WriteEndObject();
}
jsonWriter.WriteEndArray();

Repro

Any .mibc file with CallWeights (produced by create-mibc from a trace with SampledProfileTraceData events or supplementary call graph data). The bug hasn't surfaced in practice because most .mibc files don't contain CallWeights.

dotnet-pgo dump -i profile-with-callweights.mibc -o dump.txt

Stack trace:

System.InvalidOperationException: Cannot write a JSON property within an array or as the first JSON token. Current token type is 'StartArray'.
   at System.Text.Json.Utf8JsonWriter.OnValidateWritingPropertyFailed()
   at Microsoft.Diagnostics.Tools.Pgo.Program.InnerDumpMain() in Program.cs:line 314

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    Status

    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions