binaryninja/architecture/
intrinsic.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
use crate::architecture::CoreArchitecture;
use crate::confidence::Conf;
use crate::rc::Ref;
use crate::types::{NameAndType, Type};
use binaryninjacore_sys::{
    BNFreeNameAndTypeList, BNFreeOutputTypeList, BNFreeString, BNGetArchitectureIntrinsicClass,
    BNGetArchitectureIntrinsicInputs, BNGetArchitectureIntrinsicName,
    BNGetArchitectureIntrinsicOutputs, BNIntrinsicClass,
};
use std::borrow::Cow;
use std::ffi::CStr;
use std::fmt::{Debug, Formatter};

crate::new_id_type!(IntrinsicId, u32);

pub trait Intrinsic: Debug + Sized + Clone + Copy {
    fn name(&self) -> Cow<'_, str>;

    /// Unique identifier for this `Intrinsic`.
    fn id(&self) -> IntrinsicId;

    /// The intrinsic class for this `Intrinsic`.
    fn class(&self) -> BNIntrinsicClass {
        BNIntrinsicClass::GeneralIntrinsicClass
    }

    // TODO: Maybe just return `(String, Conf<Ref<Type>>)`?
    /// List of the input names and types for this intrinsic.
    fn inputs(&self) -> Vec<NameAndType>;

    /// List of the output types for this intrinsic.
    fn outputs(&self) -> Vec<Conf<Ref<Type>>>;
}

/// Type for architectures that do not use intrinsics. Will panic if accessed as an intrinsic.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct UnusedIntrinsic;

impl Intrinsic for UnusedIntrinsic {
    fn name(&self) -> Cow<'_, str> {
        unreachable!()
    }
    fn id(&self) -> IntrinsicId {
        unreachable!()
    }
    fn inputs(&self) -> Vec<NameAndType> {
        unreachable!()
    }
    fn outputs(&self) -> Vec<Conf<Ref<Type>>> {
        unreachable!()
    }
}

#[derive(Copy, Clone, Eq, PartialEq)]
pub struct CoreIntrinsic {
    pub arch: CoreArchitecture,
    pub id: IntrinsicId,
}

impl CoreIntrinsic {
    pub fn new(arch: CoreArchitecture, id: IntrinsicId) -> Option<Self> {
        let intrinsic = Self { arch, id };
        intrinsic.is_valid().then_some(intrinsic)
    }

    fn is_valid(&self) -> bool {
        // We check the name to see if the intrinsic is actually valid.
        let name = unsafe { BNGetArchitectureIntrinsicName(self.arch.handle, self.id.into()) };
        match name.is_null() {
            true => false,
            false => {
                unsafe { BNFreeString(name) };
                true
            }
        }
    }
}

impl Intrinsic for CoreIntrinsic {
    fn name(&self) -> Cow<'_, str> {
        unsafe {
            let name = BNGetArchitectureIntrinsicName(self.arch.handle, self.id.into());

            // We need to guarantee ownership, as if we're still
            // a Borrowed variant we're about to free the underlying
            // memory.
            // TODO: ^ the above assertion nullifies any benefit to passing back Cow tho?
            let res = CStr::from_ptr(name);
            let res = res.to_string_lossy().into_owned().into();

            BNFreeString(name);

            res
        }
    }

    fn id(&self) -> IntrinsicId {
        self.id
    }

    fn class(&self) -> BNIntrinsicClass {
        unsafe { BNGetArchitectureIntrinsicClass(self.arch.handle, self.id.into()) }
    }

    fn inputs(&self) -> Vec<NameAndType> {
        let mut count: usize = 0;
        unsafe {
            let inputs =
                BNGetArchitectureIntrinsicInputs(self.arch.handle, self.id.into(), &mut count);

            let ret = std::slice::from_raw_parts_mut(inputs, count)
                .iter()
                .map(NameAndType::from_raw)
                .collect();

            BNFreeNameAndTypeList(inputs, count);

            ret
        }
    }

    fn outputs(&self) -> Vec<Conf<Ref<Type>>> {
        let mut count: usize = 0;
        unsafe {
            let inputs =
                BNGetArchitectureIntrinsicOutputs(self.arch.handle, self.id.into(), &mut count);

            let ret = std::slice::from_raw_parts_mut(inputs, count)
                .iter()
                .map(Conf::<Ref<Type>>::from_raw)
                .collect();

            BNFreeOutputTypeList(inputs, count);

            ret
        }
    }
}

impl Debug for CoreIntrinsic {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("CoreIntrinsic")
            .field("id", &self.id)
            .field("name", &self.name())
            .field("class", &self.class())
            .field("inputs", &self.inputs())
            .field("outputs", &self.outputs())
            .finish()
    }
}