-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtest_buffer.rs
212 lines (183 loc) · 5.42 KB
/
test_buffer.rs
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
// Guillaume Valadon <guillaume@valadon.net>
// binutils - test_buffer.rs
extern crate binutils;
use binutils::bfd;
use binutils::instruction;
use binutils::instruction::Instruction;
use binutils::opcodes::DisassembleInfo;
use binutils::utils;
fn test_buffer_full(arch_name: &str, buffer: Vec<u8>, offset: u64) {
println!("---");
println!("From a buffer (full API) - {}", arch_name);
let mut bfd = bfd::Bfd::empty();
if !bfd::arch_list().iter().any(|arch| arch == arch_name) {
println!("Unsuported architecture ({})!", arch_name);
return;
}
// Retrieve bfd_arch and bfd_mach from the architecture name
let bfd_arch_mach = match bfd.set_arch_mach(arch_name) {
Ok(arch_mach) => arch_mach,
Err(e) => {
println!("Error with set_arch_mach() - {}", e);
return;
}
};
// Construct disassembler_ftype class
let disassemble = match bfd.raw_disassembler(bfd_arch_mach.0, false, bfd_arch_mach.1) {
Ok(d) => d,
Err(e) => {
println!("Error with raw_disassembler() - {}", e);
return;
}
};
// Create a disassemble_info structure
let mut info = match DisassembleInfo::new() {
Ok(i) => i,
Err(e) => {
println!("{}", e);
return;
}
};
// Configure the disassemble_info structure
match info.configure_buffer(bfd_arch_mach.0, bfd_arch_mach.1, &buffer, offset) {
Ok(_) => (),
Err(e) => {
println!("configure_buffer() - {}", e);
return;
}
};
match info.init() {
Ok(_) => (),
Err(e) => {
println!("Error init() - {}", e);
return;
}
};
// Disassemble the buffer
let mut pc = offset;
for _i in 0..3 {
let length = disassemble(pc, &info);
let instruction = match instruction::get_instruction(pc, length) {
Ok(i) => i,
Err(e) => {
println!("{}", e);
return;
}
};
println!("{}", instruction);
pc += length;
}
}
fn test_buffer_compact(arch_name: &str, buffer: Vec<u8>, offset: u64) {
println!("---");
println!("From a buffer (compact API) - {}", arch_name);
let mut bfd = bfd::Bfd::empty();
if !bfd::arch_list().iter().any(|arch| arch == arch_name) {
println!("Unsuported architecture ({})!", arch_name);
return;
}
// Set bfd_arch and bfd_mach from the architecture name
let _ = bfd.set_arch_mach(arch_name);
// Create a disassemble_info structure
let mut info = match DisassembleInfo::new() {
Ok(i) => i,
Err(e) => {
println!("{}", e);
return;
}
};
// Configure the disassemble_info structure
match info.init_buffer(&buffer, bfd, offset) {
Ok(_) => (),
Err(e) => {
println!("init_buffer() - {}", e);
return;
}
};
// Disassemble the buffer
loop {
let instruction = match info.disassemble() {
None => break,
Some(i) => match i {
Ok(i) => i,
Err(e) => {
println!("disassemble() - {}", e);
break;
}
},
};
println!("{}", instruction);
}
}
fn test_buffer_utils(arch_name: &str, buffer: Vec<u8>, offset: u64) {
println!("---");
println!("From a buffer (binutils::utils) - {}", arch_name);
let mut info = match utils::disassemble_buffer(arch_name, &buffer, offset) {
Ok(i) => i,
Err(e) => {
println!("{}", e);
return;
}
};
// Disassemble the buffer
loop {
let instruction = match info.disassemble() {
None => break,
Some(i) => match i {
Ok(i) => i,
Err(e) => {
println!("disassemble() - {}", e);
break;
}
},
};
println!("{}", instruction);
}
}
fn test_buffer_iter(arch_name: &str, buffer: Vec<u8>, offset: u64) {
println!("---");
println!("From a buffer (iter) - {}", arch_name);
let mut bfd = bfd::Bfd::empty();
if !bfd::arch_list().iter().any(|arch| arch == arch_name) {
println!("Unsuported architecture ({})!", arch_name);
return;
}
// Set bfd_arch and bfd_mach from the architecture name
let _ = bfd.set_arch_mach(arch_name);
// Create a disassemble_info structure
let mut info = match DisassembleInfo::new() {
Ok(i) => i,
Err(e) => {
println!("{}", e);
return;
}
};
// Disassemble the buffer using an iterator
for instruction in Instruction::from_buffer(&mut info, bfd, &buffer, offset) {
println!("{}", instruction);
}
}
fn main() {
test_buffer_full("i386:x86-64", vec![0xc3, 0x90, 0x66, 0x90], 0xA00);
test_buffer_compact(
"mep",
vec![
0x53, 0x53, 0x08, 0xd8, 0x01, 0x00, 0x53, 0x53, 0x30, 0xeb, 0x5b, 0x00,
],
0xC00000,
);
test_buffer_utils(
"mep",
vec![
0x53, 0x53, 0x08, 0xd8, 0x01, 0x00, 0x53, 0x53, 0x30, 0xeb, 0x5b, 0x00,
],
0xC00000,
);
test_buffer_iter(
"mep",
vec![
0x53, 0x53, 0x08, 0xd8, 0x01, 0x00, 0x53, 0x53, 0x30, 0xeb, 0x5b, 0x00,
],
0xC00000,
);
}