Skip to content

Commit

Permalink
x64: fix cmp rax, 0xffffffff_fff00fff
Browse files Browse the repository at this point in the history
  • Loading branch information
jjyg committed Dec 3, 2013
1 parent 716a0ce commit 96ee958
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
2 changes: 1 addition & 1 deletion metasm/cpu/ia32/opcodes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def init_cpu_constants
:vexvxmm, :vexvymm, :vexvreg, :i4xmm, :i4ymm
].each { |a| @valid_args[a] = true }

[:strop, :stropz, :opsz, :argsz, :setip,
[:strop, :stropz, :opsz, :adsz, :argsz, :setip,
:stopexec, :saveip, :unsigned_imm, :random, :needpfx,
:xmmx, :modrmR, :modrmA, :mrmvex
].each { |a| @valid_props[a] = true }
Expand Down
14 changes: 13 additions & 1 deletion metasm/cpu/x86_64/encode.rb
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,19 @@ def encode_instr_op(program, i, op)
when :mrm_imm; ed = ia.imm.encode("a#{op.props[:adsz] || 64}".to_sym, @endianness)
when :i8, :u8, :i16, :u16, :i32, :u32, :i64, :u64; ed = ia.encode(oa, @endianness)
when :i
type = (opsz == 64 ? op.props[:imm64] ? :a64 : :i32 : "#{op.props[:unsigned_imm] ? 'a' : 'i'}#{opsz}".to_sym)
type = if opsz == 64
if op.props[:imm64]
:a64
else
# handle 0xffffffff_ffffffff -> -1, which should fit in i32
ia = Expression[ia, :-, [(1<<64), :*, [[[ia, :>>, 63], :&, 1], :&, [ia, :>, 0]]]]
:i32
end
elsif op.props[:unsigned_imm]
"a#{opsz}".to_sym
else
"i#{opsz}".to_sym
end
ed = ia.encode(type, @endianness)
when :i4xmm, :i4ymm
ed = ia.val << 4 # u8
Expand Down
3 changes: 3 additions & 0 deletions tests/x86_64.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ def test_basic
assert_equal("\x48\xbb\xef\xcd\xab\x89\x67\x45\x23\x01", assemble("mov rbx, 0123456789abcdefh"))
assert_equal("\x8d\x05\x0c\0\0\0", assemble("lea eax, [rip+12]"))
assert_equal("\x8d\x04\x25\x0c\0\0\0", assemble("lea eax, [12]"))
assert_equal("\x48\x81\xE3\xFF\xF0\xFF\xFF", assemble("and rbx, 0xffffffff_fffff0ff"))
end

def test_err
Expand All @@ -35,6 +36,8 @@ def test_err
assert_raise(Metasm::ParseError) { assemble("add [bx]") }
assert_raise(Metasm::ParseError) { assemble("add [eip+4*eax]") }
assert_raise(Metasm::ParseError) { assemble("add ah, r8b") }
assert_raise(Metasm::EncodeError) { assemble("and rbx, 0x1_ffffffff_ffffffff") }
assert_raise(Metasm::EncodeError) { assemble("mov rbx, 011123456789abcdefh") }
end

def disassemble(bin, cpu=@@cpu)
Expand Down

0 comments on commit 96ee958

Please sign in to comment.