uxmal / reko

Reko is a binary decompiler.
https://uxmal.github.io/reko
GNU General Public License v2.0
2.14k stars 253 forks source link

Doesn't correctly update pointer for rep stos for dynamic direction #925

Open rfalke opened 4 years ago

rfalke commented 4 years ago

Subject: https://github.com/rfalke/decompiler-subjects/tree/master/from_holdec/i386_memset_rep_stos/ia32_elf

Output:

void i386_memset_both_b(byte * dwArg04, byte bArg08, word32 dwArg0C, word32 dwArg10)
{
    byte * edi_18 = dwArg04;
    byte al_36 = (byte) (word32) bArg08;
    word32 ecx_20;
    for (ecx_20 = dwArg0C; ecx_20 != 0x00; --ecx_20)
    {
        *edi_18 = al_36;
        ++edi_18;
    }
}

Assembler:

i386_memset_both_b proc
        push    ebx
        push    edi
        mov     eax,[esp+18]
        test    eax,eax
        cld
        jz      0804966C

l0804966B:
        std

l0804966C:
        mov     edi,[esp+0C]
        movzx   eax,byte ptr [esp+10]
        mov     ecx,[esp+14]

l08049679:
        rep stosb

l0804967B:
        pop     edi
        pop     ebx
        xor     eax,eax
        ret

The direction is controlled by the 4th parameter (dwArg10) which reko doesn't use.

The edi_18 update should be something like edi_18 = edi_18 + (dwArg10!=0?-1:1).

rfalke commented 1 year ago

Still an issue in version 0.11.4.0-931ca7d.