GregoryComer / rust-x86asm

A Rust library for x86/64 assembly/disassembly.
MIT License
51 stars 11 forks source link

Incorrect encoding of long memory operands. #4

Open purpleposeidon opened 5 years ago

purpleposeidon commented 5 years ago

This fails:

extern crate x86asm;

fn main() {
    use x86asm::*;
    use std::io::{Cursor, Write};
    let mut out = Vec::new();
    let i = Instruction::new2(Mnemonic::MOV, Operand::Memory(0xB0_B1_B2_B3_B4_B5, Some(OperandSize::Qword), None), Operand::Direct(Reg::RAX));
    {
        let mut asm = InstructionWriter::new(&mut out, Mode::Long);
        asm.write(&i).unwrap();
    }
    for x in &out {
        print!("{:02X} ", x);
    }
    println!();
    println!("Expect:");
    println!("{:#?}", i);
    {
        use x86asm::*;
        let mut asm = InstructionReader::new(Cursor::new(&out), Mode::Long);
        let got = asm.read().unwrap();
        println!("Actual:");
        println!("{:#?}", got);
        assert_eq!(i, got);
        assert_eq!(asm.read(), Err(InstructionDecodingError::EndOfStream));
    }
}

Output:

48 89 04 05 B5 B4 B3 B2
Instruction {
    mnemonic: MOV,
    operand1: Some(
        Memory(
            194277253821621,
            Some(
                Qword
            ),
            None
        )
    ),
    operand2: Some(
        Direct(
            RAX
        )
    ),
    operand3: None,
    operand4: None,
    lock: false,
    rounding_mode: None,
    merge_mode: None,
    sae: false,
    mask: None,
    broadcast: None
}
Actual:
Instruction {
    mnemonic: MOV,
    operand1: Some(
        IndirectScaledDisplaced(
            RAX,
            One,
            2998121653,
            Some(
                Qword
            ),
            None
        )
    ),
    operand2: Some(
        Direct(
            RAX
        )
    ),
    operand3: None,
    operand4: None,
    lock: false,
    rounding_mode: None,
    merge_mode: None,
    sae: false,
    mask: None,
    broadcast: None
}

(Well, using my fork https://github.com/purpleposeidon/rust-x86asm, ahem.)