Skip to content

[AUTOMATED] End a two-stage jump table where its byte map begins - #1680

Merged
mahaloz merged 21 commits into
Noelo-Lab:mainfrom
coleleavitt:fix/jumptable-bytemap-rows
Nov 19, 2026
Merged

mahaloz merged 21 commits into
Noelo-Lab:mainfrom
coleleavitt:fix/jumptable-bytemap-rows

Conversation

@coleleavitt

Copy link
Copy Markdown
Contributor

[AUTOMATED]

A two-stage jump table (a byte map picks a 4-byte offset from a table the map directly follows) gets rows that are not in its table when the map has more than 65,536 index values or is not read-only. The first map bytes are read as rows and become branch targets.

build() {  # build COUNT READONLY
  gcc -c -DINPUT_COUNT=$1 decompiler/crates/kuna-decomp/tests/fixtures/selector_domain.S -o s.o
  objcopy -O binary -j .text s.o s.bin
  printf '<binaryimage arch="x86:LE:64:default:gcc"><bytechunk space="ram" offset="0x401000" readonly="%s">%s</bytechunk><symbol space="ram" offset="0x401000" name="dispatch"/></binaryimage>' "$2" "$(xxd -p s.bin | tr -d '\n')" > s.xml
}
build 70000 true  && kuna decompile s.xml dispatch | grep flows
build 300   false && kuna decompile s.xml dispatch | grep flows

Both print, before this change:

unsigned long dispatch(int a0) // warn: Function flows into unmapped memory
        halt_missing(); // warn: Function flow reaches unmapped memory: r0x00401029 flows to r0x00400000
        halt_missing(); // warn: Function flow reaches unmapped memory: r0x00401029 flows to r0x00400001
        halt_missing(); // warn: Function flow reaches unmapped memory: r0x00401029 flows to r0x00400002

After it they print nothing, and the function is the three-case switch (11, 22, 33) that the smaller read-only maps already gave.

The fix

  • When the map's values cannot be proven, the selector still keeps its whole range, but the table now ends at the first selector value whose row would be read from the bytes the map occupies.
  • The map's extent is the selector load's address at the two ends of the nearest guarded index range that does not wrap. Only addresses are used, never the map's values, so a writable map needs no read-only proof.
  • A table whose first row already lies in the map is left alone, so a wrong extent cannot remove a whole table.
  • No option: a row read from the map's own bytes is not a destination the code can reach.

Tests

  • tests/stages/kuna-jumptable-bytemap-rows.xml pins the writable 300-value map: three cases, no default:, no unmapped flow.
  • verify_w10_selector_domain gains the 70,000-value read-only map, and its writable-map case now expects the three rows instead of the map bytes as rows. Both fail without the fix.
  • No datatest expectation moved (675/675). Decompile time of the repro is unchanged within run-to-run noise.

Fixes #1669

coleleavitt and others added 9 commits November 18, 2026 15:07
CircleRange::set_nz_mask shifted its 32-bit step by the mask's lowest set
bit. A RISC-V bit test (slli a1,a0,0x23; bltz a1) gives the mask
0xfffffff800000000, so the shift was 35: a debug build panicked in
jump-table guard analysis and a release build used the stride 8. Such a
mask now forms no range and the guard keeps the range it pulled back.

Fixes Noelo-Lab#1661
When decomp_dbg panicked and left no C, kuna decompile printed "no C
output" and the first 2000 characters of the transcript, which are option
echoes. It now prints the panic's location and message.

Refs Noelo-Lab#1661
A Go binary whose pcHeader magic has been overwritten lost every function
name: no magic-based route found the table, so a go1.26 hello-world listed
198 sub_* functions instead of 1,939 and had no main.main.

When no magic matches a table the image names, the pclntab pass now finds
runtime.firstmoduledata by its shape and reads the table from that record
alone (name table, functab, _func base, function count, text base). A record
is accepted only when its slices and PC bounds agree and the functab bears
them out: its first entry is minpc and its closing entry is maxpc. That test
also selects the functab entry shape (go1.18+ offsets, go1.16/1.17 pointers);
the go1.5-go1.15 record is read too. Section names and kinds are not used, so
a renamed .gopclntab and a PE work, and PE Go detection counts such a table.
locate_header still returns the header address, which the Go type harvest
anchors on.

The search runs only after every magic-based route has failed, on Go images
only, so it ships with no option.

Fixes Noelo-Lab#1638
When the values of a selector's byte map cannot be proven (the index
takes more values than are emulated, or the map is not read-only), the
selector kept its whole 256-value range and the table was read through
the map: map bytes became branch targets. The table now ends at the
first selector value whose row would be read from the bytes the map
occupies. Only the map's addresses are used, not its values.

Fixes Noelo-Lab#1669
…r more bytes

Funcdata::find_subpiece_shadow shifted a constant's 64-bit offset right by least_byte * 8. For a 16-byte constant tested at byte offset 8 that is a shift by 64: a panic in a build with overflow checks, a wrapped shift count otherwise. _dl_relocate_object in the mcount_x86_64 fixture failed to decompile in a dev build because of it.

A constant wider than 8 bytes holds zero above bit 63, so the truncation now reads 0 there. Shifts below 64 are unchanged.

Fixes Noelo-Lab#1674
The panic line is `<location> <message>` (the trailing colon is trimmed),
as the unit test and the CLI error show.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_0179iMKQeLQDThvbHGNoGw8h
…d profile

The gh1674 stage test only fails without the fix in a build with overflow
checks: a release build wraps the shift count, and for the constant 0 the
wrapped shift reads the same 0. The unit test calls
varnode_partial_copy_shadow on 16-byte constants directly, so it fails
on the unfixed code under the test profile (the overflow panic) and also
with a wrapping shift (an 8-byte piece at offset 8 of the 16-byte
constant 0x1234 is not 0x1234).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_0179iMKQeLQDThvbHGNoGw8h
gcc does not emit two-stage tables; the 70,000-entry example is the
hand-written selector_domain.S fixture, laid out the way MSVC places its
table before its byte map. Also state what still reads as rows: a map
before the table, another switch's map after it, or padding between.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_0179iMKQeLQDThvbHGNoGw8h
…ts image

The replaced writable-map test only checked that the table ends at the
map, which a value-narrowed writable map would also pass. With the map's
last selector byte changed from 2 to 1, a read-only map narrows to two
rows, while a writable one keeps all three and ends at the map.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_0179iMKQeLQDThvbHGNoGw8h
@mahaloz

mahaloz commented Nov 18, 2026

Copy link
Copy Markdown
Contributor

[AUTOMATED] Pushed two small commits on top of e077653:

  • verify_w10_selector_domain.rs: the replaced writable-map test only checked that the table ends at the map, which a writable map narrowed from its image would also pass. The new writable_byte_map_keeps_a_row_its_image_does_not_select changes the map's last byte from 2 to 1: the read-only map narrows to two rows, the writable one keeps all three and ends at the map. It fails on main and passes here.
  • docs/spec/02-lift-and-flow.md: gcc does not emit two-stage tables. The 70,000-entry example is the hand-written selector_domain.S fixture, which uses MSVC's table-then-map layout. The paragraph also lists what still reads as rows: a map placed before the table, another switch's map after it, and padding between the table and its map.

No change to the engine code.

mahaloz and others added 12 commits November 19, 2026 00:52
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_0179iMKQeLQDThvbHGNoGw8h

# Conflicts:
#	decompiler/crates/kuna-base/src/xml.rs
#	docs/baseline-stages.json
Main (Noelo-Lab#1701) split recover_go_table into recover_go_header, which the pass
now calls directly; the moduledata fallback for a wiped magic moves into
recover_go_header so both callers keep it.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_0179iMKQeLQDThvbHGNoGw8h
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_0179iMKQeLQDThvbHGNoGw8h

# Conflicts:
#	decompiler/crates/kuna-base/src/xml.rs
#	docs/baseline-stages.json
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_0179iMKQeLQDThvbHGNoGw8h

# Conflicts:
#	decompiler/crates/kuna-base/src/xml.rs
#	docs/baseline-stages.json
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_0179iMKQeLQDThvbHGNoGw8h

# Conflicts:
#	decompiler/crates/kuna-base/src/xml.rs
#	docs/baseline-stages.json
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.

[AUTOMATED] Two-stage jump table reads its byte map as table rows when the map has over 65,536 index values or is not read-only

2 participants