Skip to content

Complete managed ildasm and round-trip test support - #137927

Open
am11 wants to merge 8 commits into
dotnet:mainfrom
am11:feature/managed-ildasm-p2
Open

am11 wants to merge 8 commits into
dotnet:mainfrom
am11:feature/managed-ildasm-p2

Conversation

@am11

@am11 am11 commented Nov 19, 2026

Copy link
Copy Markdown
Member

Port the native ildasm (src/coreclr/ildasm, formattype.cpp, dres.cpp and the MetaInfo dump from tools/metainfo) to the managed ildasm on top of System.Reflection.Metadata. The output matches the native tool on the assemblies built from src/tests/**/*.il and the shared framework across all options, including /RAWEH, /BYTES, /TOKENS, /CAVERBAL, /TYPELIST, /FORWARD, /HEADERS, /STATS, /CLASSLIST, /METADATA, /QUOTEALLNAMES, /NOCA, /PUBONLY, /VISIBILITY, /NOIL, /UNICODE, /HTML and /RTF, and runs in about the same time.

Native bugs are fixed rather than reproduced:

Add the managed ildasm to ilasm round-trip testing: Core_Root gets managed-ildasm, run.cs/run.cmd get --use_managed_ildasm, and the managedildasmroundtrip and managedilasmildasmroundtrip scenarios run in the ilasm pipeline. The new NativeIldasmRoundTripIncompatible property excludes tests only when the native ildasm is used; ver_fg_13, badcallee and RVAInit/simple and nested round-trip with the managed one.

Let run.cs target $(NetCoreAppToolCurrent) instead of net11.0.

Port the native ildasm (src/coreclr/ildasm, formattype.cpp, dres.cpp and
the MetaInfo dump from tools/metainfo) to the managed ildasm on top of
System.Reflection.Metadata. The output matches the native tool on the
assemblies built from src/tests/**/*.il and the shared framework across
all options, including /RAWEH, /BYTES, /TOKENS, /CAVERBAL, /TYPELIST,
/FORWARD, /HEADERS, /STATS, /CLASSLIST, /METADATA, /QUOTEALLNAMES,
/NOCA, /PUBONLY, /VISIBILITY, /NOIL, /UNICODE, /HTML and /RTF, and runs
in about the same time.

Native bugs are fixed rather than reproduced:
- Structured EH keeps the order of catch clauses; methods whose clauses
  cannot be nested faithfully are written with raw clauses (dotnet#4873).
- Branch, switch and EH targets that are not instruction starts are
  written as offsets instead of undefined labels (dotnet#38506, dotnet#38507).
- RVA field data of structs with padding is not truncated.
- Undefined opcodes are written as .emitbyte; the no. prefix is
  supported, in ilasm too (dotnet#72695).
- /LINENUM writes .line directives from portable PDBs (dotnet#53822).
- The output is the same on Windows and Unix (dotnet#122976), and Win32
  resources are saved on every platform (dotnet#83254).
- More than 65535 generic parameters are handled (dotnet#127576), and
  signatures are not read past their end.

Add the managed ildasm to ilasm round-trip testing: Core_Root gets
managed-ildasm, run.cs/run.cmd get --use_managed_ildasm, and the
managedildasmroundtrip and managedilasmildasmroundtrip scenarios run in
the ilasm pipeline. The new NativeIldasmRoundTripIncompatible property
excludes tests only when the native ildasm is used; ver_fg_13,
badcallee and RVAInit/simple and nested round-trip with the managed one.

Let run.cs target $(NetCoreAppToolCurrent) instead of net11.0.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@am11
am11 requested a review from jkoritzinsky November 19, 2026 15:56
@dotnet-policy-service dotnet-policy-service Bot added the community-contribution Indicates that the PR has been added by a community member label Nov 19, 2026
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 8 pipeline(s).
7 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @JulieLeeMSFT, @dotnet/jit-contrib
See info in area-owners.md if you want to be subscribed.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Unresolved findings include potential out-of-bounds native reads, an undefined test-runner target framework, and disassembly defects that corrupt round-tripped metadata.

9 open issues
What changed in this PR

This PR expands managed ildasm toward native-tool feature parity and integrates it into CoreCLR’s IL round-trip testing, addressing limitations documented in the linked issues.

Changes:

  • Adds metadata, manifest, resource, formatting, and portable-PDB disassembly support, plus managed ilasm support for no..
  • Adds round-trip and regression tests covering disassembly fidelity.
  • Wires managed ildasm into test runners and CI, with native-only exclusions for affected tests.
File Description
src/​tools/​ildasm/​tests/​ILDisassembler.Tests/​RoundTripTests.cs Adds assembly round-trip coverage.
src/​tools/​ildasm/​tests/​ILDisassembler.Tests/​NativeIssueTests.cs Adds native-tool regression cases.
src/​tools/​ildasm/​tests/​ILDisassembler.Tests/​InstructionDisassemblerTests.cs Removes tests for the replaced instruction decoder.
src/​tools/​ildasm/​tests/​ILDisassembler.Tests/​ILDisassembler.Tests.csproj References managed ilasm for round-trip tests.
src/​tools/​ildasm/​tests/​ILDisassembler.Tests/​FormattingTests.cs Tests numeric formatting and name quoting.
src/​tools/​ildasm/​src/​ILDisassembler/​TypeDisassembler.cs Removes the incomplete type disassembler.
src/​tools/​ildasm/​src/​ILDisassembler/​Options.cs Clarifies source options and adjusts UTF-8 defaults.
src/​tools/​ildasm/​src/​ILDisassembler/​OpcodeTable.cs Defines opcode operand kinds and descriptors.
src/​tools/​ildasm/​src/​ILDisassembler/​InstructionDisassembler.cs Removes the previous instruction decoder.
src/​tools/​ildasm/​src/​ILDisassembler/​ILWriter.cs Removes the superseded output helper.
src/​tools/​ildasm/​src/​ILDisassembler/​ILDisassembler.csproj Enables unsafe code and internal test access.
src/​tools/​ildasm/​src/​ILDisassembler/​Disassembler.cs Delegates to Dasm and adds file-output handling.
src/​tools/​ildasm/​src/​ILDisassembler/​Dasm.Tokens.cs Formats metadata tokens and member references.
src/​tools/​ildasm/​src/​ILDisassembler/​Dasm.SourceLines.cs Emits portable-PDB line directives.
src/​tools/​ildasm/​src/​ILDisassembler/​Dasm.Output.cs Adds quoting, formatting, and HTML/RTF helpers.
src/​tools/​ildasm/​src/​ILDisassembler/​Dasm.Metadata.cs Adds metadata access and signature helpers.
src/​tools/​ildasm/​src/​ILDisassembler/​Dasm.Manifest.cs Emits manifests and extracts managed resources.
src/​tools/​ildasm/​src/​ILDisassembler/​Dasm.Headers.cs Adds PE/CLR header and R2R metadata handling.
src/​tools/​ildasm/​src/​ILDisassembler/​Dasm.Extras.cs Adds statistics, class lists, and Win32 resource export.
src/​tools/​ildasm/​src/​ILDisassembler/​Dasm.CustomAttributes.cs Formats custom attributes and permission sets.
src/​tools/​ildasm/​src/​ildasm/​Program.cs Updates CLI output, options, and error handling.
src/​tools/​ilasm/​src/​ILAssembler/​Actions/​GrammarActions.Instructions.cs Recognizes the no. prefix.
src/​tests/​run.cs Adds managed-ildasm selection and changes framework selection.
src/​tests/​run.cmd Forwards managed-ildasm selection to the runner.
src/​tests/​Loader/​classloader/​regressions/​dev10_403582/​genmeth.ilproj Clarifies the existing exclusion comment.
src/​tests/​Loader/​classloader/​regressions/​dev10_403582/​dev10_403582.ilproj Clarifies why round-trip exclusion remains.
src/​tests/​JIT/​opt/​Inline/​regression/​badcallee/​badcallee.ilproj Limits exclusion to native ildasm.
src/​tests/​JIT/​jit64/​verif/​sniff/​fg/​ver_fg_13.ilproj Enables managed-ildasm round trips.
src/​tests/​JIT/​Directed/​RVAInit/​simple.ilproj Limits padded-data exclusion to native ildasm.
src/​tests/​JIT/​Directed/​RVAInit/​nested.ilproj Enables managed round trips for nested RVA data.
src/​tests/​Directory.Build.targets Generates native-ildasm incompatibility markers.
src/​tests/​Common/​testenvironment.proj Defines managed-ildasm round-trip scenarios.
src/​tests/​Common/​CoreRootArtifacts.targets Includes managed ildasm in Core_Root.
src/​tests/​Common/​CLRTest.Jit.targets Selects ildasm and honors native-only markers.
src/​tests/​Common/​CLRTest.Execute.Batch.targets Adds Windows native-only skip checks.
src/​tests/​Common/​CLRTest.Execute.Bash.targets Adds Unix native-only skip checks.
eng/​pipelines/​coreclr/​templates/​jit-outerloop-pipeline.yml Builds managed ildasm for ilasm testing.
eng/​pipelines/​common/​templates/​runtimes/​run-test-job.yml Adds two managed-ildasm scenarios.
docs/​workflow/​testing/​coreclr/​test-configuration.md Documents native-only round-trip exclusions.
docs/​workflow/​testing/​coreclr/​requiresprocessisolation.md Documents isolation requirements for the new exclusion.

Review effort: Balanced

Comment thread src/tests/run.cs Resolved
@jkoritzinsky

Copy link
Copy Markdown
Member

If managed ildasm can handle all of the cases that native ildasm can (and it's only the case that native ildasm can't handle some things that managed ildasm can), please remove NativeIldasmRoundTripIncompatible and keep the tests you've marked as such as the previous "ilasm/ildasm round trip incompatible" and just add unit tests for the cases that have been fixed to work in managed ildasm.

- Remove NativeIldasmRoundTripIncompatible; the tests it applied to keep
  IlasmRoundTripIncompatible and their cases are covered by unit tests.
- run.cs: target $(BundledNETCoreAppTargetFramework), which is defined
  without the repository's Directory.Build.props.
- Validate the R2R manifest metadata size before reading it.
- Use the assembly reference alias for exported types and resources.
- Keep the raw blob for verbal custom-attribute strings that cannot be
  written losslessly (embedded NULs, invalid UTF-8).
- Include the alignment padding in the Win32 resource header size.
- Split long quoted strings on UTF-8 character boundaries.
- Dispose the PDB provider after disassembly.
- Skip escaped characters in quoted names when splitting signatures.
- Support /HEADERS for PE files without metadata.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@am11

am11 commented Nov 19, 2026

Copy link
Copy Markdown
Member Author

@jkoritzinsky, done in 28a66cc: removed NativeIldasmRoundTripIncompatible, and the four tests (ver_fg_13, badcallee, RVAInit simple/nested) are back to IlasmRoundTripIncompatible as before. The cases they covered have unit tests in ILDisassembler.Tests (NativeIssueTests): branch into an instruction (#38506), branch past the end of the code (#38507), and padded struct RVA field data.

@jkoritzinsky jkoritzinsky left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review on this is going to be in phases given the size. Starting with a few comments here

- Walk custom attribute and permission set blobs with BlobReader and the
  SerializationTypeCode/SignatureTypeCode/PrimitiveTypeCode enums instead
  of byte array indexing; BadImageFormatException replaces the custom
  exception type. A blob that cannot be shown losslessly (a bool other
  than 0 or 1, trailing data, a bad prolog) is dumped as bytes.
- Show enum arrays and trailing empty arrays verbally, and print empty
  arrays as "( )", which managed ilasm accepts.
- Iterate MetadataReader.AssemblyReferences with foreach.
- Remove references to the native implementation from comments, and
  rename NativeIssueTests to RegressionTests.
- Add round-trip tests for verbal custom attributes and permission sets.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
- Read metadata for /METADATA through System.Reflection.Metadata instead
  of emulating the read-write metadata engine; metadata that cannot be
  read stops the dump with an error message.
- Managed ilasm: lex "()" as two tokens so empty arrays in verbal custom
  attributes (int32[0]()) parse; ildasm prints them that way again.
- Revert the run.cs TargetFramework change.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

@jkoritzinsky jkoritzinsky left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed a few more files.

There's a lot of direct-port logic here that operates on tokens and metadata table memory directly. These (including in files I haven't reviewed) should be moved over to the System.Reflection.Metadata typed APIs or have explicit justifications of why it must use these other mechanisms.

Also I left some recommendations for refactorings that I think would make the code cleaner (generally leading to more of the options support being handled in the CLI tool).

Comment thread src/tools/ildasm/src/ildasm/Program.cs Resolved
- Disassembler takes only a Stream; the CLI owns file handling.
- Output formatting (text/HTML/RTF), custom attribute and permission
  set disassembly (bytes/verbal), and whole-file dumps (headers, stats,
  class list, metadata, type list, resources) are pluggable and
  selected by the CLI.
- Use System.Reflection.Metadata handles, collections, constants and
  MethodBodyBlock instead of raw tokens and hand-written decoding.
- Remove .typedef support, move .data handling to its own file, and
  always print the exception in the CLI.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
am11 and others added 2 commits November 24, 2026 02:04
- Dump EH clause bytes as stored in the image for /BYTES instead of
  re-encoding them from the fat/small format.
- Name the Win32 resource format constants and use an enum for the
  dump result.
- Make the formatter title prefix/postfix non-nullable.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-ILTools-coreclr community-contribution Indicates that the PR has been added by a community member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants