Skip to content

x86: use the masked count to decide whether rotates define OF - #1547

Open
bacescuandrei wants to merge 2 commits into
cea-sec:masterfrom
bacescuandrei:fix/x86-rotate-of-masked-count
Open

bacescuandrei wants to merge 2 commits into
cea-sec:masterfrom
bacescuandrei:fix/x86-rotate-of-masked-count

Conversation

@bacescuandrei

Copy link
Copy Markdown
Contributor

ROL, ROR, RCL and RCR compute their result from the masked count (get_shift(): count & 0x1f, or & 0x3f for 64-bit operands), but decided whether OF is defined by comparing the unmasked source with 1. With a count register such as CL = 0x21, the instruction rotates by 1 and the Intel SDM defines OF (IF (COUNT & COUNTMASK) = 1 THEN OF := ...), but miasm left OF at 0:

ROL EAX, CL   EAX=0x7fffffff CL=0x1:  EAX=0xfffffffe OF=1
ROL EAX, CL   EAX=0x7fffffff CL=0x21: EAX=0xfffffffe OF=0   (expected 1)

The same happens for ROR, RCL and RCR, for immediate counts (ROL EAX, 0x21) and for 64-bit operands with counts such as 0x41. The shift template already compares the masked count. This makes _rotate_tpl and rotate_with_carry_tpl use the masked count as well.

Added test_ROTATE_OF_masked_count to test/arch/x86/sem.py, which checks the four rotates with CL = 0x21. It fails on master and passes with this change.

Found by comparing single instructions executed by the python jitter (x86_64 mode, 64/32/16/8-bit operands) with Unicorn 2.1.4; the mismatches were the 32/16/8-bit rotates with CL = 0x21. With this change, 55,552 cases over 118 ALU, shift, rotate, bit-test and extension instructions agree with Unicorn on the general-purpose registers and on the flags the SDM defines for each case.

Also run with this change: the test/arch/x86/unit/mn_*.py scripts with the python and gcc jitters (45 runs, mn_float.py without the python jitter as in test_all.py), test/arch/x86/sem.py and test/arch/x86/arch.py. The llvm jitter was not available here.

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.

1 participant