Skip to content

fix(managed): honor metadata ExtraData before table decoding - #1893

Merged
morluto merged 2 commits into
morluto:mainfrom
chqcjq:fix/managed-metadata-extra-data
Oct 11, 2026
Merged

morluto merged 2 commits into
morluto:mainfrom
chqcjq:fix/managed-metadata-extra-data

Conversation

@chqcjq

@chqcjq chqcjq commented Oct 11, 2026

Copy link
Copy Markdown
Contributor

Summary

Honor the CLR metadata tables ExtraData flag before decoding table rows. The parser now checks and skips the fixed four-byte word after all row counts, rather than interpreting it as the beginning of the first metadata row.

No existing issue is linked: this was found while auditing table-header decoding, and searches for ExtraData, heapSizes and the specific header behavior found no matching issue or PR before submission.

Problem and expected behavior

When HeapSizes & 0x40 is set, Microsoft readers consume an extra four-byte word after the row counts. The compatibility extension is used by some obfuscators; it is not specified by ECMA-335. See System.Reflection.Metadata's reader and the CLR native implementation.

On base 29d2624b, REA begins decoding tables four bytes too early. A 4 KiB source-owned PE fixture with the extension reports invalid heap/blob indexes and loses usable metadata, while the ordinary fixture succeeds. Microsoft PEReader/MetadataReader independently reads both fixtures as the same module (Fixture.dll), assembly (Fixture.Managed), MVID, type and method.

Expected: align the table data correctly regardless of whether the word is zero, nonzero or 0xffffffff. Its value is opaque, not a length. An incomplete word must produce a typed coverage failure at the expected file location without borrowing bytes from adjacent heaps.

Change and scope

  • Add a bounded, fixed-size header step after row counts and before table construction: 12 runtime lines.
  • Preserve heap-index width flags, row counts, row sizes and all existing representation budgets. No input-sized allocation, copy or loop is added.
  • Return invalid-metadata-root / metadata.tables-extra-data for a truncated word, with an absolute file offset.
  • Extend the source-owned fixture, regression tests, public CLI/MCP parity test and owning documentation.

Contract and boundary impact

  • Semantic owner and earliest changed stage: ManagedMetadataLayout tables-header interpretation.
  • CLI/MCP/tool catalog: no schema or tool change; affected inspections now return correctly aligned metadata.
  • Provider/format/platform: portable static PE/CLI parsing; no external analysis engine or target execution.
  • Evidence/provenance: preserve original artifact identity; decoded row locations account for the extension. New truncation diagnostics use the original file coordinate.
  • Process/authorization/cleanup: unchanged.
  • Generated metadata/package/installation: no dependency/version change or committed generated output.

Evidence and regression coverage

  • Executed baseline: 6 failures and 1 passing ordinary-input control. Three extension values caused incorrect decoding; three truncated-word cases produced the wrong failure scope.
  • Ten focused regressions now pass: absent/zero/nonzero/maximum word, actual four-byte string-heap indexes combined with 0x40, a #- stream, row-count truncation, and ExtraData extents of 0/1/3 bytes with absolute-offset assertions.
  • One compiled CLI/real stdio MCP acceptance test compares artifact/member results, metadata identities and source digests.
  • Independent Microsoft metadata-reader check on the same generated bytes agrees with the corrected module, assembly, MVID, type and method identities. The target assembly was never loaded for execution.

For evidence-bearing changes:

  • Observed, derived, and inferred claims remain distinguishable.
  • Artifact identity, row coordinates and malformed-input evidence remain preserved.
  • Unsupported or incomplete outcomes remain visible.

Validation performed

On Windows x64, pinned Node 24.18.0 / npm 11.16.0:

  • npm run test:local -- <all 9 src/dotnet/*.test.ts files> — 147 passed, no failures or skips.
  • npm run test:focused -- tests/acceptance/managedMetadataExtraData.test.ts — 1 passed, compiled CLI/stdio MCP parity.
  • npm run verify:managed — passed, 11 portable conformance scenarios; optional operator-local manifest/ILSpy checks remain unconfigured.
  • npm run check — passed; reported lint warnings remain, no errors.
  • npm run docs:check — passed.
  • npm run verify:test-discovery — 844 test files, each discovered exactly once.
  • git diff --check — passed.
  • Local codex review --base origin/main, with invocation-only compatibility overrides for two legacy CLI feature settings — completed with no actionable regressions reported. It also reran 103 focused source tests and the compiled acceptance test; the full 147-test result above was run separately.

Compatibility, safety, and release

  • Breaking changes/migrations: none.
  • Scope limits: this supports the ExtraData compatibility word; it does not add Edit-and-Continue delta, #JTD, or deleted-token semantics associated with other metadata flags.
  • Fixtures are synthetic PE files cross-checked with an independent Microsoft reader; no arbitrary compiler/obfuscator corpus claim is made.
  • Linux/macOS were not run locally. No Hopper/Ghidra/Android or runtime execution claim is made.
  • No binary fixtures, credentials, local logs or generated artifacts are committed.

Review checklist

  • Concrete defect, smallest reproducible input and expected behavior documented.
  • One focused outcome and Conventional Commit title.
  • Meaningful positive, malformed-input and public-boundary regressions.
  • Owning documentation updated.
  • Final diff checked for unrelated changes and unsupported claims.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 11, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-11T18:29:03.477972Z 8c0e0b4 New commits
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

…extra-data

# Conflicts:
#	docs/managed-code-analysis.md
@morluto
morluto merged commit fb43dd9 into morluto:main Oct 11, 2026
29 checks passed
@github-actions github-actions Bot mentioned this pull request Oct 11, 2026
3 tasks done
@chqcjq
chqcjq deleted the fix/managed-metadata-extra-data branch October 12, 2026 14:07
@morluto morluto added bug Something isn't working area: domain Domain logic, errors, and pure functions labels Oct 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: domain Domain logic, errors, and pure functions bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants