Skip to content

fix(dotnet): name the truncated instruction when an operand leaves the method body - #2704

Merged
morluto merged 4 commits into
morluto:mainfrom
YunzhouLi-hub:fix/dotnet-2677-operand-diagnostic
Oct 13, 2026
Merged

morluto merged 4 commits into
morluto:mainfrom
YunzhouLi-hub:fix/dotnet-2677-operand-diagnostic

Conversation

@YunzhouLi-hub

@YunzhouLi-hub YunzhouLi-hub commented Oct 13, 2026 •

Copy link
Copy Markdown
Contributor

Summary

decodeInstructions checked the operand extent only after reading the operand value. Every fixed reader and the token path call Node Buffer readers that throw a RangeError once the operand runs past the end of the IL body. That RangeError was caught and its raw runtime message became body.issue, so a truncated method body reported "Attempt to access memory outside buffer bounds" instead of naming the instruction.

This bounds the operand extent before reading, so the existing Instruction <name> at IL offset <n> leaves method body diagnostic wins.

Problem and expected behavior

Reporter's case — buildManagedPeFixture({ ilBody: Buffer.from([0x06, 0x20]) }) (ldloc.0; ldc.i4 with no operand) through inspectManagedMembersBytes / managedPeFixtureTarget:

  • Actual: "issue": "Attempt to access memory outside buffer bounds"
  • Expected: "issue": "Instruction ldc.i4 at IL offset 0 leaves method body"

Verified on the merge base:

  {
    "decoded_instruction_count": 0,
    "header_format": "tiny",
    "il_size": 1,
-   "issue": "Instruction ldc.i4 at IL offset 0 leaves method body",
+   "issue": "Attempt to access memory outside buffer bounds",
    "normalized_il_sha256": null,
    "status": "malformed"
  }

status: "malformed", coverage: "partial" and normalized_il_sha256: null are unchanged — only the diagnostic text improves.

Change and scope

  • src/dotnet/ManagedMemberInstructionDecoder.ts
    • New operandExtent(il, offset, kind) computes the operand width without reading it: fixed widths from the table, 4 + count * 4 for switch (returning null when the count field itself is unreadable), and 4 for token kinds.
    • FIXED_OPERAND_READERS becomes Map<string, FixedOperand> = { width, read }, so each reader's byte width sits beside the read that uses it. The width and the reader can no longer drift apart.
    • decodeInstructions now checks the extent before calling readOperand, and the old post-read operand.next < offset || operand.next > il.length guard is deleted. next < offset was dead — every reader returns offset + non-negative.
    • readOperand's unknown-kind branch returns without reading (it previously read 4 bytes yet reported extent 0). The branch is unreachable from the closed OPCODES table, so behaviour is unchanged; the inconsistency is removed rather than left as a trap.
    • TOKEN_OPERAND_KINDS moved above its consumers to avoid use-before-declaration.
  • src/dotnet/ManagedCilDecoding.test.ts — new describe("managed operand extent diagnostics"), 39 tests.

No change to ManagedMethodBodyReader.ts, the public CLI/MCP contracts, or any output shape.

Contract and boundary impact

The fix is at the producing boundary — the place that decides an operand is out of range — rather than a downstream repair of the message. body.issue remains decoded.issue ?? malformed-regions ?? truncated-fallback at src/dotnet/ManagedMethodBodyReader.ts:175, so status: "malformed", coverage: "partial", decoded_instruction_count and the null normalized_il_sha256 are all preserved exactly as before.

Evidence and regression coverage

Coverage is in both directions, because a width that is wrong in either direction reintroduces the raw buffer error or wrongly rejects a valid operand:

Direction What it catches
Zero operand bytes A width declared as 0
One byte short of the real width A width declared too small
Exactly the declared width, with a known value A width declared too large

Mutation-checked rather than assumed:

Mutation Result
ldc.i4 width 4 → 3 one-byte-short case fails
ldc.i4 width 4 → 5 exact-width case fails
ldc.r4 width 4 → 8 exact-width case fails
TOKEN_OPERAND_WIDTH 4 → 2 8 cases fail (both directions, all 4 token kinds)

With the decoder reverted, 16 of the 19 original zero-byte tests fail and all 27 pre-existing tests pass either way. The 3 that pass both ways are the two controls plus the readable-count switch case, which the old operand.next guard already handled.

The new tests assert the instruction name, offset and decoded value, not just that the message changed, so no generic message would satisfy them.

Validation performed

Command Result
npm run test:local -- src/dotnet/ManagedCilDecoding.test.ts 66/66 pass
npm run test:local -- (all 11 src/dotnet/*.test.ts) 222/222 pass
npm run check:fast 2/2 tasks successful
npm run verify:test-discovery all 1055 test files discovered exactly once
npx oxfmt --check on both changed files correct format

Node 24.15.0 / npm 11.12.1, Windows x64. npm run docs:check passes 4/5 tasks here; artifacts:managed-evidence fails with EPERM ... mkdtemp on this host — I confirmed it fails identically on a clean checkout, so it is environmental, not caused by this change.

Not verified: no real managed .exe/.dll fixture and no real-provider run — this is a diagnostic-wording fix validated entirely on the synthetic ManagedPe.fixture.ts fixtures.

Compatibility, safety, and release

  • Output-shape compatible: only the text of an existing diagnostic changes. Consumers matching on the exact old "Attempt to access memory outside buffer bounds" string would need to relax that match — unlikely, and the new text is the more useful contract.
  • No execution, no filesystem writes, no provider process, no network. Pure in-process decoding of an already-read buffer.
  • Patch-level fix.

Before merge

  • Confirm the diagnostic wording matches the project's preferred phrasing for instruction-extent failures.
  • Decide whether the equivalent unguarded reads elsewhere in the CIL pipeline should be swept in the same change or tracked separately. I found none in decodeInstructions' operand paths, but I have not audited ManagedMemberInstructionDecoder.ts's callers beyond ManagedMethodBodyReader.ts.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 13, 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-13T18:52:35.679392Z 53f7bde 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.

@greptile-apps

greptile-apps Bot commented Oct 13, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerConfidence Score: 5/5

[Medium impact] The PR appears safe to merge; no blocking defects were found.

Summary

The PR checks operand sizes before reading their values and keeps instruction-specific diagnostics for truncated bodies.

  • Truncated CIL operands now name the instruction and its IL offset.

Reviews (5) · Last reviewed commit: "Merge branch 'main' into fix/dotnet-2677..." · Reviewed by Greptile

Comment thread src/dotnet/ManagedCilDecoding.test.ts Resolved
…e method body

The extent guard ran after the operand read, so a truncated operand threw a raw Buffer RangeError that replaced the intended diagnostic with the runtime message and lost the instruction identity.

Bound the operand extent before reading its value and carry the width beside each fixed reader so the two facts cannot drift.

Fixes morluto#2677
@YunzhouLi-hub
YunzhouLi-hub force-pushed the fix/dotnet-2677-operand-diagnostic branch from f0a84dd to 1e9af71 Compare October 13, 2026 10:56

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: a4e87eea00

ℹ️ 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".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/dotnet/ManagedCilDecoding.test.ts Resolved
@morluto morluto added bug Something isn't working area: native Native analysis and decompilation, including Ghidra, IDA, and Hopper analysis behavior P2: medium Medium priority issue labels Oct 13, 2026
@morluto

morluto commented Oct 13, 2026

Copy link
Copy Markdown
Owner

Source fixes and publication evidence for the review findings below. Tests and executable checks were skipped at the maintainer's request. Findings that still require a decision or execution evidence remain open.

review thread

Two tiny-operand truncation tests duplicated the helper and suite. The tests now use one tiny-body builder and one operand-extent suite, retaining distinct il_size and normalized digest expectations. Validation: pinned formatter and git diff --check; tests were not run.

@morluto
morluto merged commit 97268f4 into morluto:main Oct 13, 2026
3 checks passed
vincent-peng added a commit to vincent-peng/rea that referenced this pull request Oct 14, 2026
The describe block merged from main (morluto#2704) runs 162 lines, over the
150-line lint bound, so the operand-width matrix moves into its own
describe.

Generated with [Devin](https://devin.ai)

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: native Native analysis and decompilation, including Ghidra, IDA, and Hopper analysis behavior bug Something isn't working P2: medium Medium priority issue

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants