MIPS: incorrect BZHI - missing qemu patch
[short version]
There is a bug in unicorn's qemu engine in the handling of BZHI (in MIPS). This issue was also met by the qemu repo and was fixed in https://github.com/qemu/qemu/commit/9ad2ba6e8e7f
This bug was found whilst fuzzing Unicorn against SLEIGH's representation of instructions.
Claude generated a patch, but as I'm not sure of understanding it fully, I prefer to leave this to you, maintainers of the repo. Nevertheless I can give it to you if you want.
Thanks for the awesome project !
[ai generated long version]
Describe the bug
bzhi is wrong for N >= operand size, in both the result and the flags.
Unicorn clamps the bit count N to 63 (or 31 for the 32-bit form) where the
SDM's threshold is 64 (32). Two consequences:
- CF is set one N too early: Unicorn gives
CF = (N >= 63), the SDM saysCF = (N > 63). - For every N >= 64, DEST keeps having bit 63 cleared, instead of SRC1 being copied through unchanged. SF is wrong with it.
This is vendored pre-fix QEMU code, not a unicorn-authored bug. Upstream fixed it in qemu 9ad2ba6e8e7f ("target/i386: Fix BZHI instruction: We did not correctly handle N >= operand size"), reported as qemu issue #1374.
Note that the upstream commit edits target/i386/tcg/emit.c.inc, which unicorn
does not have, but the lines it deletes are identical to unicorn's
qemu/target/i386/translate.c:4165
(case 0x0f5: /* bzhi Gy, Ey, By */) modulo the tcg_ctx threading:
tcg_gen_setcond_tl(tcg_ctx, TCG_COND_LT, tcg_ctx->cpu_cc_src, s->T1, bound);
tcg_gen_movcond_tl(tcg_ctx, TCG_COND_GT, s->T1, s->T1, bound, bound, s->T1);The fix keeps bound at 63 and changes the shape: TCG_COND_LEU for the flag,
and rather than clamping N, build the mask -1 << N and movcond LEU it to
zero when N is out of range, so the andc leaves DEST untouched. Bumping
bound to 64 does not work, because a full-width TCG shift is undefined.
Sample Code
from unicorn import Uc, UC_ARCH_X86, UC_MODE_64
import unicorn.x86_const as x
BASE = 0x1000
CODE = bytes.fromhex("c4e2f0f5c3") # bzhi rax, rbx, rcx
def bzhi(src1, n):
uc = Uc(UC_ARCH_X86, UC_MODE_64)
uc.mem_map(BASE, 0x1000)
uc.mem_write(BASE, CODE)
uc.reg_write(x.UC_X86_REG_RBX, src1)
uc.reg_write(x.UC_X86_REG_RCX, n)
uc.reg_write(x.UC_X86_REG_EFLAGS, 0x202)
uc.emu_start(BASE, BASE + len(CODE))
return uc.reg_read(x.UC_X86_REG_RAX), uc.reg_read(x.UC_X86_REG_EFLAGS) & 1
for n in (62, 63, 64, 255):
print(n, [hex(v) for v in bzhi(0xFFFFFFFFFFFFFFFF, n)])Expected behavior
Per the SDM: N := SRC2[7:0]; DEST := SRC1; IF N < 64: DEST[63:N] := 0; CF := (N > 63). With rbx = 0xffffffffffffffff:
| N | expected DEST | CF | unicorn DEST | CF | |
|---|---|---|---|---|---|
| 62 | 0x3fffffffffffffff |
0 | 0x3fffffffffffffff |
0 | ok |
| 63 | 0x7fffffffffffffff |
0 | 0x7fffffffffffffff |
1 | CF wrong |
| 64 | 0xffffffffffffffff |
1 | 0x7fffffffffffffff |
1 | DEST wrong |
| 255 | 0xffffffffffffffff |
1 | 0x7fffffffffffffff |
1 | DEST wrong |
The 32-bit form (bzhi eax, ebx, ecx) fails identically at its own threshold:
CF flips at N=31 instead of 32, and DEST stays 0x7fffffff for all N >= 32.
Additional context
Reproduced on unicorn 2.1.4 (core 2.1) from PyPI; master still carries the buggy lines as of 7c5db94.
Source: unicorn-engine/unicorn