Colton Scottie
Colton Scottie

Reputation: 897

dnSpy showing strange disassembly code for async methods

I am trying to disassemble my .NET core 3.1 binary using the latest build of dnSpy but am getting output like this:

enter image description here

The original method:

public async Task<List<FirmwarePackage>> CalculatePackagesDeltaAsync(List<FirmwarePackage> firmwarePackages) {
  var packagesToUpdate = await GetPackageDifferences(firmwarePackages);

  // sort packages
  // "version-update" packages should be updated last, with "mez" being last
  packagesToUpdate = packagesToUpdate
    .OrderBy(x => x.TargetType)
    .ThenBy(x => x.Name)
    .ToList();

  packagesToUpdate.RemoveAll(x => x.Name == "version-update");

  var versionPkgs = firmwarePackages
    .Where(x => x.Name == "version-update")
    .OrderBy(x => x.TargetType);

  packagesToUpdate.AddRange(versionPkgs);
  return packagesToUpdate;
}

Is this normal? How can I make it into something more understandable. Trying to debug this is a nightmare.

On the github repo the issues is not available which is why I am asking here.

Upvotes: 10

Views: 5997

Answers (3)

yoel halb
yoel halb

Reputation: 12711

dnSpyEx

There is a new library based on dnSpy called dnSpyEx that does it correctly, find it on GitHub here.

This library will decompile the code back in human readable async/await code (provided the View->Options->Decompiler->C#->Decompile async methods (async/await) is checked).

If you do want to see the compiler generated state machines you can either uncheck the above setting, or check the View->Options->Decompiler->C#->Show hidden decompiler generated classes and methods setting.

dnSpy

For the old dnSpy (which is archived as of now) there is no way to see it as normal code, instead you should check the View->Options->Decompiler->C#->Show hidden decompiler generated classes and methods otherwise the code will be missing at all.

Upvotes: 4

RockiesMagicNumber
RockiesMagicNumber

Reputation: 501

For those of you watching at home, echoing @yoelhalb from their comment

Actually it is a setting in Options->Decompiler->C#->Show hidden decompiler generated classes and methods

Upvotes: 10

Peter Csala
Peter Csala

Reputation: 22829

What you can see there is totally normal. What you have cropped is just one part of the generated code.

The highlighted code saves the local variables (as fields of the state machine) to preserve their state.

The code generation is quite complex because there are couple of use cases that are handled in the different way. But the generic ideas are written quite well in the following articles:

Stephen Toub has written a blog post which reveals some of the optimizations that the .NET team has accomplished in .NET 5.

Upvotes: 4

Related Questions