Skip to content

[AUTOMATED] Keep 32-bit-address rep string loops on x86-64 - #1981

Merged
mahaloz merged 5 commits into
Noelo-Lab:mainfrom
sjkim1127:claude/fix-repstring-addr32-4ad622
Nov 26, 2026
Merged

mahaloz merged 5 commits into
Noelo-Lab:mainfrom
sjkim1127:claude/fix-repstring-addr32-4ad622

Conversation

@sjkim1127

@sjkim1127 sjkim1127 commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

On x86-64, a rep stos/rep movs with an address-size override (67) steps 32-bit EDI/ESI, which wrap at 4 GiB, but repstring still turns it into a builtin_memset32/builtin_memcpy, which cannot wrap.

printf '\xf3\x67\xab\xc3' > rep.bin    # rep stosd (addr32); ret
kuna decompile rep.bin --raw-image --target x86:LE:64:default:gcc --base 0x1000 --addr 0x1000
void sub_1000(unsigned int *a0,unsigned long a1,unsigned long a2,unsigned int a3)
{
  unsigned int v1; // eax

  builtin_memset32(a0,v1,a3);
}

With EDI = 0xfffffffc, ECX = 2 the CPU (checked in Unicorn) stores the second dword at 0x0 and leaves RDI = 0x4; the memset writes it at 0x100000000.

Fix

  • repstring declines a copy, fill or compare when a pointer it steps is narrower than an address of the default data space, so the instruction stays a loop over the 32-bit pointer. With --option repstring off the output was already this loop.
  • i386 pointers are as wide as its address space, so its rewrites are unchanged; rep stosw through RDI (f3 48 66 ab) still becomes builtin_memset16.
  • No option: the builtin is false about the program whenever the range wraps.

Tests

tests/stages/kuna-rex-before-prefix.xml now pins rex_stos (f3 48 67 ab), rex_movs (48 67 f3 48 a5) and rex_cmps (f3 48 67 a6) as loops and asserts no builtin_memset32/builtin_memcpy/builtin_memcmp; the new assertions fail on main. kuna-repstring.xml and kuna-repstring-i386.xml are unchanged, as is the full datatest corpus (675/675). No measurable speed change; decompile-all of a 3.9 MB x86-64 Rust binary prints the same code before and after.

Model: anthropic/claude-opus-5-5

🤖 Generated with Claude Code

With an address-size override, rep stos/movs/cmps on x86-64 step EDI/ESI,
which wrap at 4 GiB. repstring turned them into builtin_memset32/memcpy,
which cannot wrap. It now declines when a stepped pointer is narrower than
an address of the default data space, so the loop stays a loop. i386 and
64-bit-address forms are unchanged.

Strict correctness fix, no option. Re-records docs/baseline-stages.json for
the renamed and added REXPREFIX assertions.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@sjkim1127
sjkim1127 force-pushed the claude/fix-repstring-addr32-4ad622 branch from 6fba38a to 6f620fb Compare November 26, 2026 01:20
sjkim1127 and others added 4 commits November 26, 2026 10:34
full_width() now refuses the rewrite when the default data space is
unknown instead of allowing it. kuna-rex-before-prefix.xml adds rex_cmps
(f3 48 67 a6), a 32-bit-address repz cmpsb that used to become
builtin_memcmp, so stos, movs and cmps are all pinned as loops.
Re-records docs/baseline-stages.json for the two new assertions.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…the REX stage

The loop assertions `*a0 = v2;` and `*a0 = *a1;` replaced builtin_memset32 and
builtin_memcpy(..., n * 8), which were what pinned the dword and quadword width
the prefixes decode to. Two assertions on the rex_stos and rex_movs prototypes
pin it again. The wrap example in the comment now uses EDI=0xfffffffc, ECX=2.

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

x86:LE:64:compat32 runs 32-bit code on the 64-bit SLEIGH model, so every
ESI/EDI it steps is narrower than the 8-byte data space and the width check
declined all of its rep strings, although they wrap only where the program's
4 GiB address space does, as on i386. A pointer as wide as the compiler spec's
pointer (4 bytes in the x86 32-bit cspecs) now also counts as full width.
x86-64 cspecs have 8-byte pointers, so an address-size override there still
keeps its loop. The spec also says why 16-bit code is unchanged.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_013BBaWTdjVzTz11WxPZmJ9b
5,503 assertions: main's 5,497, the four 32-bit-address loop assertions and
the two width pins.

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

mahaloz commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

[AUTOMATED] Pushed three commits on top of c449784:

  • 144c4cdd6: the loop assertions no longer pinned the decoded element width (the old builtin_memset32 / a3 * 8 did), so REXPREFIX #1c/#3c pin the rex_stos(unsigned int *a0, and rex_movs(unsigned long *a0,unsigned long *a1, prototypes. The wrap example now uses EDI=0xfffffffc, ECX=2.
  • 292307091: under x86:LE:64:compat32 (32-bit code on the 64-bit model), every ESI/EDI is narrower than the 8-byte data space, so full_width declined all of its rep strings. printf '\xf3\xab\xc3' > s.bin; kuna decompile s.bin --raw-image --target x86:LE:64:compat32:gcc --base 0x1000 --addr 0x1000 printed builtin_memset32 before this PR and a loop after it. A pointer as wide as the compiler spec's pointer now also counts. x86-64 cspecs have 8-byte pointers, so 67 f3 ab there still keeps its loop. Includes a CLI test and spec text, which also says why 16-bit code is unchanged.
  • 5321143ba: re-recorded docs/baseline-stages.json (5,503).

@mahaloz
mahaloz marked this pull request as ready for review November 26, 2026 02:15
@mahaloz
mahaloz merged commit 3b6a688 into Noelo-Lab:main Nov 26, 2026
2 checks passed
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.

2 participants