hakril / PythonForWindows

A codebase aimed to make interaction with Windows and native execution easier
BSD 3-Clause "New" or "Revised" License
573 stars 112 forks source link

x86 Test Instruction encoding does not match specification #19

Closed srounet closed 3 years ago

srounet commented 3 years ago

Hi, I'm not an expert but I think the definition of Test Instruction does not match the x86 documentation. https://en.wikipedia.org/wiki/X86_instruction_listings#Original_8086/8088_instructions

Instruction Meaning Notes Opcode
TEST Logical compare (AND) (1) r/m & r/imm; (2) r & m/imm; 0x84, 0x84, 0xA8, 0xA9, 0xF6/0, 0xF7/0
class Test(Instruction):
    encoding = [(RawBits.from_int(8, 0xf7), Slash(7), Imm32()),
                (RawBits.from_int(8, 0x85), ModRM([ModRM_REG__REG, ModRM_REG__MEM], has_direction_bit=False))]

I think it should be (notice Slash(0))

class Test(Instruction):
    encoding = [(RawBits.from_int(8, 0xf7), Slash(0), Imm32()),
                (RawBits.from_int(8, 0x85), ModRM([ModRM_REG__REG, ModRM_REG__MEM], has_direction_bit=False))]

Snippet: x86.Test("EBX", 0x01) should produce: f7c3 0100 0000

Again as I said I'm not an expert, but if I'm right it should be fixed for x64 too i guess.

hakril commented 3 years ago

Hi, Thank you for the issue and the analysis. I agree with you, this encoding of Test was clearly not tested as adding some related test-case directly failed. I corrected the encoding in both bitness and added some Test to tests/test_simple_x86.py & tests/test_simple_x64.py in commit 542eb33.

I am closing the issue, do not hesitate to reopen it if you feel it has not been correctly fixed. Feel free to open other issues if you find some other encoding error.

Thank you !