1#![no_std]
56#![forbid(unsafe_code)]
57
58mod registers;
59
60#[cfg(not(any(feature = "x86", feature = "aarch64")))]
61compile_error!("binbun-disasm needs feature `x86` or `aarch64` (or both)");
62
63use core::fmt;
64use core::iter::FusedIterator;
65
66#[cfg(feature = "aarch64")]
67pub use binbun_aarch64 as aarch64;
68pub use binbun_isa::{Flow, Target};
69#[cfg(feature = "x86")]
70pub use binbun_x86 as x86;
71#[cfg(feature = "aarch64")]
72pub use registers::AArch64Register;
73pub use registers::{Register, Registers};
74
75#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
77pub enum Arch {
78 #[cfg(feature = "x86")]
80 X86(x86::Mode),
81 #[cfg(feature = "aarch64")]
83 AArch64,
84}
85
86impl Arch {
87 pub const fn max_length(self) -> usize {
90 match self {
91 #[cfg(feature = "x86")]
92 Self::X86(_) => x86::MAX_LENGTH,
93 #[cfg(feature = "aarch64")]
94 Self::AArch64 => 4,
95 }
96 }
97
98 pub const fn invalid_step(self) -> usize {
101 match self {
102 #[cfg(feature = "x86")]
103 Self::X86(_) => 1,
104 #[cfg(feature = "aarch64")]
105 Self::AArch64 => 4,
106 }
107 }
108}
109
110#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
112pub enum DecodeError {
113 #[cfg(feature = "x86")]
115 X86(x86::DecodeError),
116 #[cfg(feature = "aarch64")]
118 AArch64(aarch64::Error),
119}
120
121impl DecodeError {
122 pub const fn is_truncated(self) -> bool {
124 match self {
125 #[cfg(feature = "x86")]
126 Self::X86(error) => matches!(error, x86::DecodeError::TooShort),
127 #[cfg(feature = "aarch64")]
128 Self::AArch64(error) => matches!(error, aarch64::Error::TooShort),
129 }
130 }
131}
132
133impl fmt::Display for DecodeError {
134 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
135 match self {
136 #[cfg(feature = "x86")]
137 Self::X86(error) => error.fmt(f),
138 #[cfg(feature = "aarch64")]
139 Self::AArch64(error) => error.fmt(f),
140 }
141 }
142}
143
144impl core::error::Error for DecodeError {}
145
146#[cfg(feature = "x86")]
147impl From<x86::DecodeError> for DecodeError {
148 fn from(error: x86::DecodeError) -> Self {
149 Self::X86(error)
150 }
151}
152
153#[cfg(feature = "aarch64")]
154impl From<aarch64::Error> for DecodeError {
155 fn from(error: aarch64::Error) -> Self {
156 Self::AArch64(error)
157 }
158}
159
160#[derive(Clone, Debug, PartialEq, Eq)]
162enum Native {
163 #[cfg(feature = "x86")]
164 X86(x86::Instruction, x86::Mode),
165 #[cfg(feature = "aarch64")]
166 AArch64(aarch64::Instruction),
167}
168
169#[derive(Clone, Debug, PartialEq, Eq)]
175pub struct Insn<'a> {
176 address: u64,
177 bytes: &'a [u8],
178 native: Native,
179}
180
181impl<'a> Insn<'a> {
182 #[cfg(feature = "x86")]
188 pub fn from_x86(
189 instruction: x86::Instruction,
190 mode: x86::Mode,
191 bytes: &'a [u8],
192 address: u64,
193 ) -> Self {
194 Self {
195 address,
196 bytes: bytes.get(..instruction.len()).unwrap_or(bytes),
197 native: Native::X86(instruction, mode),
198 }
199 }
200
201 #[cfg(feature = "aarch64")]
204 pub fn from_aarch64(instruction: aarch64::Instruction, bytes: &'a [u8], address: u64) -> Self {
205 Self {
206 address,
207 bytes: bytes.get(..4).unwrap_or(bytes),
208 native: Native::AArch64(instruction),
209 }
210 }
211
212 pub const fn arch(&self) -> Arch {
214 match self.native {
215 #[cfg(feature = "x86")]
216 Native::X86(_, mode) => Arch::X86(mode),
217 #[cfg(feature = "aarch64")]
218 Native::AArch64(_) => Arch::AArch64,
219 }
220 }
221
222 pub const fn address(&self) -> u64 {
224 self.address
225 }
226
227 #[allow(clippy::len_without_is_empty, reason = "an instruction is never empty")]
229 pub const fn len(&self) -> usize {
230 self.bytes.len()
231 }
232
233 pub const fn bytes(&self) -> &'a [u8] {
236 self.bytes
237 }
238
239 pub fn mnemonic(&self) -> &'static str {
245 match &self.native {
246 #[cfg(feature = "x86")]
247 Native::X86(instruction, _) => instruction.intel_mnemonic(),
248 #[cfg(feature = "aarch64")]
249 Native::AArch64(instruction) => instruction.mnemonic(),
250 }
251 }
252
253 pub fn flow(&self) -> Flow {
255 match &self.native {
256 #[cfg(feature = "x86")]
257 Native::X86(instruction, _) => binbun_isa::Instruction::flow(instruction, self.address),
258 #[cfg(feature = "aarch64")]
259 Native::AArch64(instruction) => instruction.flow(self.address),
260 }
261 }
262
263 pub fn registers_read(&self) -> Registers {
265 match &self.native {
266 #[cfg(feature = "x86")]
267 Native::X86(instruction, _) => registers::x86(instruction, registers::Side::Read),
268 #[cfg(feature = "aarch64")]
269 Native::AArch64(instruction) => registers::aarch64(*instruction, registers::Side::Read),
270 }
271 }
272
273 pub fn registers_written(&self) -> Registers {
275 match &self.native {
276 #[cfg(feature = "x86")]
277 Native::X86(instruction, _) => registers::x86(instruction, registers::Side::Write),
278 #[cfg(feature = "aarch64")]
279 Native::AArch64(instruction) => {
280 registers::aarch64(*instruction, registers::Side::Write)
281 }
282 }
283 }
284
285 #[cfg(feature = "x86")]
287 pub const fn as_x86(&self) -> Option<&x86::Instruction> {
288 match &self.native {
289 Native::X86(instruction, _) => Some(instruction),
290 #[cfg(feature = "aarch64")]
291 Native::AArch64(_) => None,
292 }
293 }
294
295 #[cfg(feature = "aarch64")]
297 pub const fn as_aarch64(&self) -> Option<&aarch64::Instruction> {
298 match &self.native {
299 #[cfg(feature = "x86")]
300 Native::X86(..) => None,
301 Native::AArch64(instruction) => Some(instruction),
302 }
303 }
304}
305
306impl fmt::Display for Insn<'_> {
307 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
308 match &self.native {
309 #[cfg(feature = "x86")]
310 Native::X86(instruction, _) => instruction.intel(self.address).fmt(f),
311 #[cfg(feature = "aarch64")]
312 Native::AArch64(instruction) => instruction.display(self.address).fmt(f),
313 }
314 }
315}
316
317pub fn decode(arch: Arch, bytes: &[u8], address: u64) -> Result<Insn<'_>, DecodeError> {
325 let native = match arch {
326 #[cfg(feature = "x86")]
327 Arch::X86(mode) => Native::X86(x86::decode(bytes, mode)?, mode),
328 #[cfg(feature = "aarch64")]
329 Arch::AArch64 => Native::AArch64(aarch64::decode_bytes(bytes)?),
330 };
331 let len = match &native {
332 #[cfg(feature = "x86")]
333 Native::X86(instruction, _) => instruction.len(),
334 #[cfg(feature = "aarch64")]
335 Native::AArch64(_) => 4,
336 };
337 Ok(Insn {
338 address,
339 bytes: bytes.get(..len).unwrap_or(bytes),
340 native,
341 })
342}
343
344#[derive(Debug, Clone)]
353pub struct Decoder<'a> {
354 arch: Arch,
355 bytes: &'a [u8],
356 base: u64,
358 offset: usize,
360}
361
362impl<'a> Decoder<'a> {
363 pub const fn new(arch: Arch, bytes: &'a [u8], address: u64) -> Self {
365 Self {
366 arch,
367 bytes,
368 base: address,
369 offset: 0,
370 }
371 }
372
373 pub const fn arch(&self) -> Arch {
375 self.arch
376 }
377
378 pub const fn offset(&self) -> usize {
380 self.offset
381 }
382
383 pub const fn address(&self) -> u64 {
385 self.base.wrapping_add(self.offset as u64)
386 }
387
388 pub const fn set_offset(&mut self, offset: usize) {
390 self.offset = offset;
391 }
392
393 pub fn remaining(&self) -> &'a [u8] {
395 self.bytes.get(self.offset..).unwrap_or_default()
396 }
397}
398
399impl<'a> Iterator for Decoder<'a> {
400 type Item = (u64, Result<Insn<'a>, DecodeError>);
401
402 fn next(&mut self) -> Option<Self::Item> {
403 let rest = self.remaining();
404 if rest.is_empty() {
405 return None;
406 }
407 let address = self.address();
408 let result = decode(self.arch, rest, address);
409 let step = result.as_ref().map_or(self.arch.invalid_step(), Insn::len);
410 self.offset = self.offset.saturating_add(step);
411 Some((address, result))
412 }
413
414 fn size_hint(&self) -> (usize, Option<usize>) {
415 let rest = self.remaining().len();
416 match self.arch {
417 #[cfg(feature = "x86")]
418 Arch::X86(_) => (rest.div_ceil(x86::MAX_LENGTH), Some(rest)),
419 #[cfg(feature = "aarch64")]
420 Arch::AArch64 => (rest.div_ceil(4), Some(rest.div_ceil(4))),
421 }
422 }
423}
424
425impl FusedIterator for Decoder<'_> {}