iced icon indicating copy to clipboard operation
iced copied to clipboard

CodeAssembler ignores AsmMemoryOperand size mismatches

Open meowette opened this issue 2 years ago • 1 comments

When trying to encode an invalid instruction the assembler "corrects" it rather than returning an error, which should probably be the defined behaviour given that every instruction already returns a Result by default.

use iced_x86::code_asm::{CodeAssembler, rax, eax, qword_ptr, dword_ptr};

fn main() {
    let mut assembler = CodeAssembler::new(64).unwrap();
    assembler.mov(eax, qword_ptr(rax)).unwrap();
    assembler.mov(rax, dword_ptr(rax)).unwrap();
    assembler.mov(qword_ptr(rax), eax).unwrap();
    assembler.mov(dword_ptr(rax), rax).unwrap();
    println!("{:x?}", assembler.assemble(0).unwrap());
    // assembles to
    // mov eax, dword ptr [rax]
    // mov rax, qword ptr [rax]
    // mov dword ptr [rax], eax
    // mov qword ptr [rax], rax
}

This is not only the case with mov, but probably any instruction that uses MemOp and Regs

meowette avatar Dec 27 '23 10:12 meowette

The memory size fns are just hints and only used when it's not possible to figure out which instruction should be used, eg. inc [rax], is that byte/word/dword/qword? if it's mov rax,[mem] we already know the size of the memory operand.

See https://github.com/icedland/iced/blob/master/src/rust/iced-x86/README.md#assemble-instructions

wtfsck avatar Jan 01 '24 19:01 wtfsck