capstone-engine / capstone

Capstone disassembly/disassembler framework for ARM, ARM64 (ARMv8), Alpha, BPF, Ethereum VM, HPPA, LoongArch, M68K, M680X, Mips, MOS65XX, PPC, RISC-V(rv32G/rv64G), SH, Sparc, SystemZ, TMS320C64X, TriCore, Webassembly, XCore and X86.
http://www.capstone-engine.org
7.51k stars 1.54k forks source link

Sparc incorrect Code Condition due to an unintended trimming #2419

Closed david942j closed 1 month ago

david942j commented 2 months ago

Work environment

Questions Answers
OS/arch/bits Ubuntu x86 64
Architecture sparc
Source of Capstone git clone
Version/git commit (v5.0.1, 1bd2a32f2afc6e08225d7491b82619cf4c8e87d9), (next, 4f964a264ec25eb9d468a5495fbd6142778c3a47)

Instruction bytes giving faulty results

Basically all "jump" with conditions have wrong results on the "cc" field. Below is just one example

0x12,0xbf,0xff,0xff

Expected results

It should be:

Code condition should be SPARC_ICC_NE = 265

Steps to get the wrong result

With cstool:

cstool/cstool -d sparc '12bfffff'
 0  12 bf ff ff  bne    -4
        ID: 16 (b)
        op_count: 1
                operands[0].type: IMM = 0xfffffffffffffffc
        Code condition: 256 // <- here reports 256 instead of 265
        Groups: jump

I have root caused this issue and the bug was introduced by this PR: https://github.com/capstone-engine/capstone/pull/2075

It always removes the last byte in the instruction, hence, for example, 'bne' would be considered as 'bn' when setting the cc field.

This bug exists on both v5 and next branches. I am sending pull requests.