Registry Mirror
Skip to content

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 <noreply@anthropic.com>
@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 <noreply@anthropic.com>
@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 <noreply@anthropic.com>
- 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 <noreply@anthropic.com>

@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 <noreply@anthropic.com>
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 <noreply@anthropic.com>
jkoritzinsky

This comment was marked as duplicate.

am11 and others added 2 commits December 1, 2026 02:09
- Share the decoding helpers (compressed integers and tokens via
  BinaryPrimitives, little-endian reads, token kinds, ClassLayout/ImplMap/
  MethodSemantics rows, UnmanagedType extension block) in MetadataHelpers.
- Split Dasm.cs, Dasm.Members.cs and Dasm.Body.cs into topic files; drop
  region comments; XML doc comments on declarations.
- COMMENT takes a CommentFormat; interpolated strings with one Append;
  out_ -> builder, FQN -> FullyQualifiedName, IsPrivateScope.
- Method body: ImmutableArray code and EH clauses, LocalVariablesSignatureToken,
  AlignUp (linked AlignmentHelper), EnumEHInfo takes the EH format, operand
  bytes formatted without the B() helper, no InlinePhi or pre-1.0 ldstr.
- Manifest: SRM types instead of copies, ProcessorArchitecture, flag keywords
  as IEnumerable.
- Add the OpcodeTable generator (file-based app) and mark the table
  linguist-generated; InvariantGlobalization for the ildasm app.

Output is unchanged on the test corpus.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- Enums instead of constants: MetadataDumpOptions, ColumnType, CodedIndex,
  HeapSizes; TableIndex for the tables, public System.Reflection enums for
  flags, SignatureTypeCode/SignatureKind/SignatureAttributes for signatures,
  UnmanagedType for marshaling descriptors.
- Table schema keyed by TableIndex; column sizes computed with the shared
  index-size helpers, tag bits derived from the coded index tables, table
  and coded index names from ToString() with COMPAT overrides.
- Handles and SRM collections instead of hand-built tokens; type names via
  Dasm.GetNameOfType; ClassLayout via GetClassLayoutRows.
- Spans instead of copied arrays; compressed integers via the shared
  DecodeCompressedInteger (zero-extended past the end of a blob, as the
  dump expects); BinaryPrimitives reads.
- No goto in DisplaySignature/GetOneElementType; DisplayMDCore;
  DumpAbortedException; the function pointer printer merged into
  PrettyPrintSignature; INTERNAL/CMOD_INTERNAL dropped.
- Share ClassTypeCode/ValueTypeTypeCode, TypeAttributeForwarder and the
  extra security actions in MetadataHelpers; add a SignatureTypeCode
  extension block for the modifier bit and ZapSig values; drop the unused
  Dasm.X8/x8.

Output is unchanged on the test corpus.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
am11 and others added 3 commits December 1, 2026 03:21
Invalid IL and metadata are disassembled on a best-effort basis with SRM
APIs, with a short diagnostic where something cannot be decoded:

- Method bodies that MethodBodyBlock rejects print
  "// ERROR: bad method body at RVA 0x..." instead of going through a
  hand-written decoder; member dumps catch BadImageFormatException.
- Signatures are read with BlobReader; array shapes are read like
  SignatureDecoder does and fail on invalid encodings instead of being
  special-cased. Image tokens go through MetadataTokens.Handle.
- The size of RVA field data follows IL rewriters: primitive and pointer
  sizes, or the ClassLayout size of a value type, otherwise unknown.
- The headers dump no longer prints the always-zero loader flags.
- /METADATA decodes signatures with SignatureDecoder and custom attribute
  arguments with CustomAttribute.DecodeValue (including named arguments),
  reads marshaling descriptors with BlobReader, and reports errors per
  signature, custom attribute or type instead of reading zeros past the
  end of blobs or silently stopping the dump.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
am11 and others added 2 commits December 2, 2026 10:58
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@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.

This is getting pretty close. Just a few more bits of feedback.

/// Emulates printf("%#.*g"): the "g" format with the given number of significant digits, but which keeps the trailing
/// zeros and the decimal point, so that ilasm does not read an integral value as the bits of the number.
/// </summary>
internal static string FormatAlternateG(double v, int precision)

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.

Can we rename this to better describe what it's doing? (ToStringWithDecimalSeparator)

Furthermore, could we do something like the following to make this a little cleaner (after handling nan and infinity) to better describe what it's actually doing:

  string text = value.ToString($"G{precision}", CultureInfo.InvariantCulture);
  if (!double.IsFinite(value) || text.Contains('.'))
      return text;
  int exponent = text.IndexOf('E');
  return exponent >= 0
      ? text.Insert(exponent, ".0")
      : text + ".0";

I think this may have different precision, but it will at least guarantee the presence of the decimal point

size = (uint)(sectionEnd - ptr);
}

int j = _ptrTags.FindIndex(t => t.Rva == ptr);

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.

nit: Why are we using j here instead of i?

// Sorted by RVA (the RVAs are distinct), in place: each label ends at the next one.
_ptrTags.Sort((a, b) => a.Rva.CompareTo(b.Rva));
List<(uint Rva, uint Size)> tags = _ptrTags;
for (int j = 0; j < tags.Count; j++)

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.

nit: Why are we using j here instead of i?

- Rename FormatAlternateG to ToStringWithDecimalSeparator and build it on
  the G format, adding a decimal separator to integral values.
- Use i for the loop variables in Dasm.Data.cs.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@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.

Code quality looks good now.

I'm going to run this against a test suite I've built and see if I can find any bugs.

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