frida_gum_sys/
lib.rs

1/*
2 * Copyright © 2020-2021 Keegan Saunders
3 *
4 * Licence: wxWindows Library Licence, Version 3.1
5 */
6#![no_std]
7#![allow(non_upper_case_globals)]
8#![allow(non_camel_case_types)]
9#![allow(non_snake_case)]
10#![allow(improper_ctypes)]
11
12#[allow(clippy::all)]
13mod bindings {
14    include!(concat!(env!("OUT_DIR"), "/bindings.rs"));
15}
16
17pub use bindings::*;
18
19#[cfg(not(any(target_os = "windows", target_vendor = "apple",)))]
20pub use {_frida_g_object_ref as g_object_ref, _frida_g_object_unref as g_object_unref};
21
22/// A single disassembled CPU instruction.
23#[repr(transparent)]
24pub struct Insn {
25    /// Inner `cs_insn`
26    pub(crate) insn: cs_insn,
27}
28
29#[allow(clippy::len_without_is_empty)]
30impl Insn {
31    /// Create an `Insn` from a raw pointer to a [`capstone_sys::cs_insn`].
32    ///
33    /// This function serves to allow integration with libraries which generate `capstone_sys::cs_insn`'s internally.
34    ///
35    /// # Safety
36    ///
37    /// Note that this function is unsafe, and assumes that you know what you are doing. In
38    /// particular, it generates a lifetime for the `Insn` from nothing, and that lifetime is in
39    /// no-way actually tied to the cs_insn itself. It is the responsibility of the caller to
40    /// ensure that the resulting `Insn` lives only as long as the `cs_insn`. This function
41    /// assumes that the pointer passed is non-null and a valid `cs_insn` pointer.
42    ///
43    /// The caller is fully responsible for the backing allocations lifetime, including freeing.
44    pub unsafe fn from_raw(insn: *const cs_insn) -> Self {
45        Self {
46            insn: core::ptr::read(insn),
47        }
48    }
49
50    /// Size of instruction (in bytes)
51    #[inline]
52    #[allow(clippy::unnecessary_cast)]
53    pub fn len(&self) -> usize {
54        self.insn.size as usize
55    }
56
57    /// Instruction address
58    #[inline]
59    #[allow(clippy::unnecessary_cast)]
60    pub fn address(&self) -> u64 {
61        self.insn.address as u64
62    }
63
64    /// Byte-level representation of the instruction
65    #[inline]
66    pub fn bytes(&self) -> &[u8] {
67        &self.insn.bytes[..self.len()]
68    }
69}