aboutsummaryrefslogtreecommitdiff
path: root/test/test.rs
blob: 166faffb57ffe74583cb89bb7a992a6bacb48d20 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
extern crate x86_rs;

use x86_rs::{Instruction, Opcode, decode_one};

fn decode(bytes: &[u8]) -> Option<Instruction> {
    let mut instr = Instruction::invalid();
    match decode_one(bytes, &mut instr) {
        Some(()) => Some(instr),
        None => None
    }
}

#[test]
fn test_mov() {
    assert_eq!(&format!("{}", decode(
        &[0x48, 0xc7, 0x04, 0x24, 0x00, 0x00, 0x00, 0x00]
    ).unwrap()), "mov [rsp], 0x0");
    assert_eq!(&format!("{}", decode(
        &[0x48, 0x89, 0x44, 0x24, 0x08]
    ).unwrap()), "mov [rsp + 0x8], rax");
    assert_eq!(&format!("{}", decode(
        &[0x48, 0x89, 0x43, 0x18]
    ).unwrap()), "mov [rbx + 0x18], rax");
    assert_eq!(&format!("{}", decode(
        &[0x48, 0xc7, 0x43, 0x10, 0x00, 0x00, 0x00, 0x00]
    ).unwrap()), "mov [rbx + 0x10], 0x0");
    assert_eq!(&format!("{}", decode(
        &[0x49, 0x89, 0x4e, 0x08]
    ).unwrap()), "mov [r14 + 0x8], rcx");
    assert_eq!(&format!("{}", decode(
        &[0x48, 0x8b, 0x32]
    ).unwrap()), "mov rsi, [rdx]");
    assert_eq!(&format!("{}", decode(
        &[0x49, 0x89, 0x46, 0x10]
    ).unwrap()), "mov [r14 + 0x10], rax");
    assert_eq!(&format!("{}", decode(
        &[0x4d, 0x0f, 0x43, 0xec, 0x49]
    ).unwrap()), "cmovnb r13, r12");
    assert_eq!(&format!("{}", decode(
        &[0x0f, 0xb6, 0x06]
    ).unwrap()), "movzx eax, byte [rsi]");
    assert_eq!(&format!("{}", decode(
        &[0x0f, 0xb7, 0x06]
    ).unwrap()), "movzx eax, word [rsi]");
}

#[test]
fn test_stack() {
    assert_eq!(&format!("{}", decode(
        &[0x66, 0x41, 0x50]
    ).unwrap()), "push r8w");
}

#[test]
fn test_prefixes() {
    assert_eq!(&format!("{}", decode(
        &[0x66, 0x41, 0x31, 0xc0]
    ).unwrap()), "xor r8w, ax");
    assert_eq!(&format!("{}", decode(
        &[0x66, 0x41, 0x32, 0xc0]
    ).unwrap()), "xor al, r8b");
    assert_eq!(&format!("{}", decode(
        &[0x40, 0x32, 0xc5]
    ).unwrap()), "xor al, bpl");
}

#[test]
fn test_control_flow() {
    assert_eq!(&format!("{}", decode(
        &[0x73, 0x31]
    ).unwrap()), "jnb 0x31");
    assert_eq!(&format!("{}", decode(
        &[0x72, 0x5a]
    ).unwrap()), "jb 0x5a");
    assert_eq!(&format!("{}", decode(
        &[0x0f, 0x86, 0x8b, 0x01, 0x00, 0x00]
    ).unwrap()), "jna 0x18b");
    assert_eq!(&format!("{}", decode(
        &[0x74, 0x47]
    ).unwrap()), "jz 0x47");
    assert_eq!(&format!("{}", decode(
        &[0xff, 0x15, 0x7e, 0x72, 0x24, 0x00]
    ).unwrap()), "call [rip + 0x24727e]");
    assert_eq!(&format!("{}", decode(
        &[0xc3]
    ).unwrap()), "ret");
}

#[test]
fn test_test_cmp() {
    assert_eq!(&format!("{}", decode(
        &[0x48, 0x3d, 0x01, 0xf0, 0xff, 0xff]
    ).unwrap()), "cmp rax, 0xfffffffffffff001");
    assert_eq!(&format!("{}", decode(
        &[0x3d, 0x01, 0xf0, 0xff, 0xff]
    ).unwrap()), "cmp eax, 0xfffff001");
    assert_eq!(&format!("{}", decode(
        &[0x48, 0x83, 0xf8, 0xff]
    ).unwrap()), "cmp rax, 0xffffffffffffffff");
    assert_eq!(&format!("{}", decode(
        &[0x48, 0x39, 0xc6]
    ).unwrap()), "cmp rsi, rax");
}

#[test]
fn test_misc() {
    assert_eq!(&format!("{}", decode(
        &[0x33, 0xc0]
    ).unwrap()), "xor eax, eax");
    assert_eq!(&format!("{}", decode(
        &[0x48, 0x8d, 0x53, 0x08]
    ).unwrap()), "lea rdx, [rbx + 0x8]");
    assert_eq!(&format!("{}", decode(
        &[0x31, 0xc9]
    ).unwrap()), "xor ecx, ecx");
    assert_eq!(&format!("{}", decode(
        &[0x48, 0x29, 0xc8]
    ).unwrap()), "sub rax, rcx");
    assert_eq!(&format!("{}", decode(
        &[0x48, 0x03, 0x0b]
    ).unwrap()), "add rcx, [rbx]");
    assert_eq!(&format!("{}", decode(
        &[0x5b]
    ).unwrap()), "pop rbx");
    assert_eq!(&format!("{}", decode(
        &[0x41, 0x5e]
    ).unwrap()), "pop r14");
    assert_eq!(&format!("{}", decode(
        &[0x48, 0x8d, 0x0c, 0x12]
    ).unwrap()), "lea rcx, [rdx + rdx]");
    assert_eq!(&format!("{}", decode(
        &[0xf6, 0xc2, 0x18]
    ).unwrap()), "test dl, 0x18");
}