Skip to content

Analysis-tier: MIPS16 ISA_MODE decode-mode painting - #18

Merged
mahaloz merged 2 commits into
mainfrom
mips16-isamode
Jun 23, 2026
Merged

mahaloz merged 2 commits into
mainfrom
mips16-isamode

Conversation

@mahaloz

@mahaloz mahaloz commented Jun 23, 2026

Copy link
Copy Markdown
Contributor

Ports MIPS16 ISA_MODE decode-mode painting — the MIPS analog of ARM Thumb TMode — closing the last sibling of the Increment 8 arch-markers frontier (Increment 17 deferred it for lack of a MIPS16 fixture; the dev container now ships mips-linux-gnu-gcc 10.3.0 with -mips16).

BEFORE vs AFTER

A MIPS16 function would misdecode as MIPS32 (MIPS16 ISA_MODE was out of scope). With the mips_isa pass painting ISA_MODE=1 at the function entry, the body decodes as MIPS16:

// BEFORE (mips_isa off, MIPS32 misdecode)      // AFTER (mips_isa on, MIPS16-decoded)
void m16_square(void)                           int4 m16_square(int4 a0)
{                                               {
  return;                                         return a0 * a0 + 3;
}                                               }

The MIPS32 misdecode reads the 8 MIPS16 bytes (mult a0,a0; mflo v0; jr ra; addiu v0,3) as 2 garbage 4-byte words and recovers nothing; the painted form cleanly recovers a0 * a0 + 3. The e2e actually decoded MIPS16 — it was not skipped.

What

  • New MipsIsaModePass (pass id mips_isa) in s1_loader/mips_markers.rs, a sibling of the $gp pass. Gated on MIPS; for each defined STT_FUNC marked MIPS16e/microMIPS it emits ContextPaint { addr: entry & !1, var: "ISA_MODE", value: 1 } REUSING the existing context-paint commit arm (the same one the ARM TMode paints use — no new arm), plus a re-home SymFact at the even entry for an LSB-set FUNC (the MIPS analog of Increment 17's Thumb-FUNC re-home).
  • Faithful to upstream MIPS_ElfExtension.applyIsaMode: enables ISA_MODE for EITHER an LSB-set symbol value (re-homed to even) OR st_other & 0xf0 in {STO_MIPS_MIPS16 (0xf0), STO_MIPS_MICROMIPS (0x80)}. Both branches ported (st_other read via the generic ObjectSymbol::flags()).
  • Registered default-on per the mips_gp precedent exactly: stages.toml row + KUNA_OPTION_NAMES + analysis_mips_isa flag (architecture.rs, default-on) + set_kuna_option arm + engine.rs analysis_pass_enabled arm + console kuna_live_value arm. Count tests bumped (settable 33→34) + stage_catalog.json fixture regenerated + docs/assertions.md regenerated.

The ISA-mode context var (the critical finding)

The exact context var the vendored MIPS SLEIGH spec uses for MIPS16 selection is ISA_MODE (specs/Ghidra/Processors/MIPS/data/languages/mips.sinc:411, define context contextreg ... ISA_MODE=(1,1) "=1 Decode using alternate ISA, variable", behind @ifdef ISA_VARIANT). The mips32{le,be}.slaspec both @define ISA_VARIANT and @include "mips16.sinc", so the .sla carries full MIPS16 decode (MIPS16 constructors gate on ISA_MODE=1 & RELP=1; MIPS32 on ISA_MODE=0). RELP=1 is already fixed globally by the default mips32.pspec, so painting ISA_MODE=1 alone flips a function to MIPS16 — exactly the Thumb TMode situation. No spec gap.

Toolchain note

mips-linux-gnu-gcc 10.3.0 marks a __attribute__((mips16)) function via st_other = STO_MIPS_MIPS16 with an even symbol value, NOT an LSB-set odd address — which is precisely the st_other branch of upstream applyIsaMode. Both that and the LSB branch are ported.

Fixture

mips16_le32 (1584-byte freestanding ET_EXEC, big-endian; mips-linux-gnu-gcc -mips16 -O1 -no-pie -nostdlib -ffreestanding, source vendored). Freestanding because the container has the MIPS runtime libc but no libc6-dev (no crt1.o/headers); a decode fixture needs no runtime. m16_square is a genuine MIPS16 leaf at even entry 0x400130 with st_other = STO_MIPS_MIPS16.

Gates

  • make test → 675/675 PARITY OK
  • make test-stages → 158/158 PARITY OK
  • make rust-test → green
  • kuna catalog --check → catalog OK

Additive real-ELF-path change — the XML datatest gates are structurally untouched (the <binaryimage> path never runs analysis passes).

🤖 Generated with Claude Code

https://claude.ai/code/session_01FvRnfTgYKoZvS2TEvdYpWb

mahaloz and others added 2 commits June 23, 2026 16:12
Port the MIPS16 ISA_MODE decode-mode paint — the MIPS analog of ARM Thumb
TMode — closing the last sibling of the Increment 8 arch-markers frontier
(Increment 17 deferred it for lack of a MIPS16 fixture; the dev container now
has mips-linux-gnu-gcc 10.3.0 with -mips16).

New MipsIsaModePass (pass id `mips_isa`) in s1_loader/mips_markers.rs: gated on
MIPS, for each defined STT_FUNC marked MIPS16e/microMIPS it paints
ContextPaint { addr: entry & !1, var: "ISA_MODE", value: 1 } (REUSING the
existing context-paint commit arm — the same one the ARM TMode paints use) plus,
for an LSB-set FUNC, a re-home SymFact at the even entry (the MIPS analog of
Increment 17's Thumb-FUNC re-home). Faithful to MIPS_ElfExtension.applyIsaMode,
which enables ISA_MODE for EITHER an LSB-set symbol value OR
st_other & 0xf0 in {STO_MIPS_MIPS16, STO_MIPS_MICROMIPS} — this toolchain emits
the latter (even entry + st_other marker), read via ObjectSymbol::flags().

The context var is `ISA_MODE` (mips.sinc:411, behind @ifdef ISA_VARIANT which
the mips32{le,be}.slaspec @define + @include mips16.sinc). RELP=1 is already
fixed by the default mips32.pspec, so painting ISA_MODE=1 alone flips a function
to MIPS16 — exactly the Thumb TMode situation. No spec gap.

Registered default-on per the mips_gp precedent: stages.toml row +
KUNA_OPTION_NAMES + analysis_mips_isa flag (architecture.rs) + set_kuna_option
arm + engine.rs analysis_pass_enabled arm + kuna_live_value arm. Count tests
bumped (settable 33->34) + stage_catalog.json regenerated + assertions.md
regenerated. kuna catalog --check: catalog OK.

Fixture mips16_le32 (freestanding, big-endian, -mips16 -nostdlib): m16_square is
a MIPS16 leaf at even entry 0x400130 with st_other = STO_MIPS_MIPS16. e2e
verify_mips16_isa.rs decodes it to `return a0 * a0 + 3;` (MIPS16) vs an empty
`void` body (MIPS32 misdecode) — the e2e actually decoded MIPS16, not skipped.

Gates: make test 675/675 PARITY OK; make test-stages 158/158 PARITY OK;
make rust-test green. Additive real-ELF-path change — XML gates structurally
untouched.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01FvRnfTgYKoZvS2TEvdYpWb
@mahaloz
mahaloz merged commit 5db2da1 into main Jun 23, 2026
@mahaloz
mahaloz deleted the mips16-isamode branch July 29, 2026 08:01
mahaloz added a commit that referenced this pull request Sep 23, 2026
…ending)

Union #26 (union_datatype.xml) pins upstream's `res = (int4)ptr->b->bCharField;`
and opts out with `option castimplied off`. Ten stage assertions of other
options pinned a widening this option leaves out and now match the new form:
undefname #4, charbyte #1 #2 #4 #7 #8, PEBNAMES #18, ptrfromuse #10,
TIEDPHITRIM #1 #2. docs/baseline.json and docs/baseline-stages.json unchanged.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_011Q4qgBGM28rgY6CEh5pird
mahaloz added a commit that referenced this pull request Sep 23, 2026
…ending)

Union #26 (union_datatype.xml) pins upstream's `res = (int4)ptr->b->bCharField;`
and opts out with `option castimplied off`. Ten stage assertions of other
options pinned a widening this option leaves out and now match the new form:
undefname #4, charbyte #1 #2 #4 #7 #8, PEBNAMES #18, ptrfromuse #10,
TIEDPHITRIM #1 #2. docs/baseline.json and docs/baseline-stages.json unchanged.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_011Q4qgBGM28rgY6CEh5pird
mahaloz added a commit that referenced this pull request Oct 1, 2026
…enamed away and given away

Two holes in the batch rules:

- `name v2 tmp`, `name v1 v2`, `type v2 ...`: the printed reading of `v2`
  picked the local already renamed to `tmp`, silently. When the variable
  printed under an identifier was renamed away earlier in the batch and an
  earlier directive gave that identifier to another local, the directive
  is now rejected naming both.
- The vN allocator ignored name-locked Symbols, so after `name v1 v3` +
  `name v3 v1` an untouched local also took `v1` and the printer pushed
  the caller's `v1` to `v1_1`. Every default now skips a name a
  name-locked local Symbol holds; with no such Symbol the numbering is
  unchanged (byte-identical over 2,336 corpus functions).

calleenamedlocals' s4 relied on that collision: it now renames both slots
to `w` for the collision coverage, and asserts that `rename v2 v1` alone
leaves the caller's `v1` on the slot it named (#18, #19; stages baseline
re-recorded, two keys added). The console rename/retype commands get the
current function through one helper instead of a discarded read_symbol.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_017cAUeUBnQ9xRA9oRJNGWjw
mahaloz added a commit that referenced this pull request Oct 1, 2026
…enamed away and given away

Two holes in the batch rules:

- `name v2 tmp`, `name v1 v2`, `type v2 ...`: the printed reading of `v2`
  picked the local already renamed to `tmp`, silently. When the variable
  printed under an identifier was renamed away earlier in the batch and an
  earlier directive gave that identifier to another local, the directive
  is now rejected naming both.
- The vN allocator ignored name-locked Symbols, so after `name v1 v3` +
  `name v3 v1` an untouched local also took `v1` and the printer pushed
  the caller's `v1` to `v1_1`. Every default now skips a name a
  name-locked local Symbol holds; with no such Symbol the numbering is
  unchanged (byte-identical over 2,336 corpus functions).

calleenamedlocals' s4 relied on that collision: it now renames both slots
to `w` for the collision coverage, and asserts that `rename v2 v1` alone
leaves the caller's `v1` on the slot it named (#18, #19; stages baseline
re-recorded, two keys added). The console rename/retype commands get the
current function through one helper instead of a discarded read_symbol.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_017cAUeUBnQ9xRA9oRJNGWjw
mahaloz added a commit that referenced this pull request Oct 1, 2026
…enamed away and given away

Two holes in the batch rules:

- `name v2 tmp`, `name v1 v2`, `type v2 ...`: the printed reading of `v2`
  picked the local already renamed to `tmp`, silently. When the variable
  printed under an identifier was renamed away earlier in the batch and an
  earlier directive gave that identifier to another local, the directive
  is now rejected naming both.
- The vN allocator ignored name-locked Symbols, so after `name v1 v3` +
  `name v3 v1` an untouched local also took `v1` and the printer pushed
  the caller's `v1` to `v1_1`. Every default now skips a name a
  name-locked local Symbol holds; with no such Symbol the numbering is
  unchanged (byte-identical over 2,336 corpus functions).

calleenamedlocals' s4 relied on that collision: it now renames both slots
to `w` for the collision coverage, and asserts that `rename v2 v1` alone
leaves the caller's `v1` on the slot it named (#18, #19; stages baseline
re-recorded, two keys added). The console rename/retype commands get the
current function through one helper instead of a discarded read_symbol.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_017cAUeUBnQ9xRA9oRJNGWjw
mahaloz added a commit that referenced this pull request Oct 1, 2026
…utput it was shown (#789)

## The problem

Two `--assert name` directives on two different register locals each work alone, but in one run the second is rejected with `No symbol named:`, and reversing them reverses which one fails. A swap (`name a b` + `name b a`) was worse: both came back `applied` and nothing changed.

```sh
cat > t.s <<'EOF'
.globl two_locals, observe
.type two_locals,@function
.type observe,@function
two_locals: push %rbx; push %r12; sub $8,%rsp; xor %ebx,%ebx; mov $7,%r12d
1: add $1,%ebx; imul $3,%r12d,%r12d; add %ebx,%r12d; mov %ebx,%edi; mov %r12d,%esi
   call observe; cmp $4,%ebx; jl 1b
   lea (%rbx,%r12),%eax; add $8,%rsp; pop %r12; pop %rbx; ret
observe: ret
EOF
cc -c t.s && ld -e two_locals t.o -o t
kuna decompile t two_locals --assert 'prototype observe void observe(int index, int sum)' \
  --assert 'name index iteration' --assert 'name sum accumulator'
```
```
int two_locals(void)
{
  int iteration; // ebx
  int sum; // r12d
...
warning: --assert "name sum accumulator" rejected: No symbol named: sum
```

## The fix

- Naming a register local no longer clears the analysis, which emptied the variables every later directive looks its identifier up in. Both surfaces rebuild the IR from the scope for the second pass anyway.
- An identifier now means the variable printed under it in the output the run started from, so directives on different locals work in any order and `name a b` + `name b a` swaps. An identifier that output does not contain may name what an earlier directive renamed a local to (`name v1 rc` then `type rc unsigned int`).
- When an identifier is both a printed name and a name an earlier directive gave another local, the directive is rejected as ambiguous and names both locals: `name v5 v4` then `type v4 int` gives `Ambiguous name: v4 is both the local printed as v4 and the local printed as v5 (now v4)`. The exception is a `name` whose new name was also printed, as in a swap or rotation; it renames the local printed under the identifier.
- The `vN` default for an untouched local now skips any name a name-locked Symbol holds. Before, after `name v1 v3` + `name v3 v1` an untouched third local also took `v1` and the printer pushed the caller's `v1` to `v1_1`.
- The console `rename`/`retype` commands and the in-process (`--json`) path share one resolver.
- Two register locals sharing a register at different widths (`char *s // rax`, `uint4 v1 // eax`) cannot both get a symbol: the second pass folds them into one variable (`s._0_4_ = 0`). The later directive is rejected and names the earlier one.

Behaviour changes beyond the fix:

- After `name v5 v4`, main applied a later `type v4 int` or `name v4 x` to the local just renamed `v4` (unless both were stack slots, where it rejected it); that batch is now rejected. `name v1 v2` + `name v2 v3`, with `v3` also printed, renames the local printed as `v2`, where main renamed the first local twice.
- On the stack, `name v2 credbuf` then `type v2 char[8]` now retypes `credbuf` instead of being rejected.
- Without any directive, a DWARF parameter or local named like a default (`v1`) is a name-locked Symbol too, so it keeps its name and the defaults skip it. Main printed `uint1 v1; uint8 v1_1;` for bash's `size_t v1`; now it is `uint8 v1;`. This renames locals in 5 of 3,107 bash O0 functions (names only); `ls` and `sort` at O2 and `sdiff` at O0 are byte-identical.
- The `calleenamedlocals` stage's `rename v2 v1` no longer collides (the caller's `v1` stays on the slot it named); its collision coverage now renames both slots to `w`.

## The tests

- `kuna-cli/tests/local_name_batch.rs` on a new fixture (`localnamebatch_x86_64`, source beside it): the reported pair in both orders, three renames, stack, register and mixed pairs, swaps and a rotation among three locals, rename-then-retype, and the width, ambiguity and overlap rejections, each run through the text and `--json` surfaces. Everything except the stack-pair control fails on main. A DWARF fixture (`dwarfvnames_x86_64`, C source beside it) checks that a source `v1` keeps its name.
- `tests/cli/two-register-local-names-in-one-batch.json`: the reported case; fails on main.
- `calleenamedlocals` #18/#19 (stages baseline re-recorded, two keys added) fail on main.

Fixes #784

🤖 Generated with [Claude Code](https://claude.com/claude-code)

https://claude.ai/code/session_01Ps66ZCFPLywq9AKHBuDXNU
mahaloz added a commit to borancar/kuna that referenced this pull request Nov 14, 2026
Keep Noelo-Lab#1575's calleereginputs header, row and spec text, with the
virtual-dispatch paragraph after them; move the vcall passes to the
file's off/default form as Noelo-Lab#18-Noelo-Lab#22; options.md and phase_catalog.json
from main (regenerated after the build); synthesize baseline-stages.json.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_0179iMKQeLQDThvbHGNoGw8h
mahaloz added a commit to borancar/kuna that referenced this pull request Nov 16, 2026
…code

A callee whose walk cannot settle its stack effect is taken to pop what its
reached returns pop. That was the most any reached return popped, so a
__cdecl callee whose cold path ends in a call that does not return (one the
walk cannot read) gave the next function's `ret 0x20`: the walk runs on
through the padding into it. The pop was stated on the call, the block
expanded into its arguments, and the caller lost its second parameter and
read a slot above its frame instead.

The walk now records the pop only when every reached return pops the same,
it followed every path (no indirect jump, undecodable instruction or budget
overrun), no path ran into a software interrupt (int3 padding, int 0x29),
and no fall-through entered another known function. byvalreuse_pe_i386
gains stray/nf/ng; REPFRAMECOPY Noelo-Lab#18 and Noelo-Lab#8 fail without this.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_0179iMKQeLQDThvbHGNoGw8h
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant