binaryninja/architecture/
instruction.rs

1use crate::architecture::{BranchInfo, BranchKind, CoreArchitecture};
2use binaryninjacore_sys::*;
3
4/// This is the number of branches that can be specified in an [`InstructionInfo`].
5pub const NUM_BRANCH_INFO: usize = 3;
6
7#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
8pub struct InstructionInfo {
9    pub length: usize,
10    // TODO: This field name is really long...
11    pub arch_transition_by_target_addr: bool,
12    pub delay_slots: u8,
13    pub branches: [Option<BranchInfo>; NUM_BRANCH_INFO],
14}
15
16impl InstructionInfo {
17    // TODO: `new_with_delay_slot`?
18    pub fn new(length: usize, delay_slots: u8) -> Self {
19        Self {
20            length,
21            arch_transition_by_target_addr: false,
22            delay_slots,
23            branches: Default::default(),
24        }
25    }
26
27    /// Add a branch to this [`InstructionInfo`], maximum of 3 branches may be added (as per [`NUM_BRANCH_INFO`]).
28    pub fn add_branch(&mut self, branch_info: impl Into<BranchInfo>) {
29        // Will go through each slot and attempt to add the branch info.
30        // TODO: Return a result with BranchInfoSlotsFilled error.
31        for branch in &mut self.branches {
32            if branch.is_none() {
33                *branch = Some(branch_info.into());
34                return;
35            }
36        }
37    }
38}
39
40impl From<BNInstructionInfo> for InstructionInfo {
41    fn from(value: BNInstructionInfo) -> Self {
42        // TODO: This is quite ugly, but we destructure the branch info so this will have to do.
43        let mut branch_info = [None; NUM_BRANCH_INFO];
44        #[allow(clippy::needless_range_loop)]
45        for i in 0..value.branchCount.min(NUM_BRANCH_INFO) {
46            let branch_target = value.branchTarget[i];
47            branch_info[i] = Some(BranchInfo {
48                kind: match value.branchType[i] {
49                    BNBranchType::UnconditionalBranch => BranchKind::Unconditional(branch_target),
50                    BNBranchType::FalseBranch => BranchKind::False(branch_target),
51                    BNBranchType::TrueBranch => BranchKind::True(branch_target),
52                    BNBranchType::CallDestination => BranchKind::Call(branch_target),
53                    BNBranchType::FunctionReturn => BranchKind::FunctionReturn,
54                    BNBranchType::SystemCall => BranchKind::SystemCall,
55                    BNBranchType::IndirectBranch => BranchKind::Indirect,
56                    BNBranchType::ExceptionBranch => BranchKind::Exception,
57                    BNBranchType::UnresolvedBranch => BranchKind::Unresolved,
58                    BNBranchType::UserDefinedBranch => BranchKind::UserDefined,
59                },
60                arch: if value.branchArch[i].is_null() {
61                    None
62                } else {
63                    Some(unsafe { CoreArchitecture::from_raw(value.branchArch[i]) })
64                },
65            });
66        }
67        Self {
68            length: value.length,
69            arch_transition_by_target_addr: value.archTransitionByTargetAddr,
70            delay_slots: value.delaySlots,
71            branches: branch_info,
72        }
73    }
74}
75
76impl From<InstructionInfo> for BNInstructionInfo {
77    fn from(value: InstructionInfo) -> Self {
78        let branch_count = value.branches.into_iter().filter(Option::is_some).count();
79        // TODO: This is quite ugly, but we destructure the branch info so this will have to do.
80        let branch_info_0 = value.branches[0].unwrap_or_default();
81        let branch_info_1 = value.branches[1].unwrap_or_default();
82        let branch_info_2 = value.branches[2].unwrap_or_default();
83        Self {
84            length: value.length,
85            branchCount: branch_count,
86            archTransitionByTargetAddr: value.arch_transition_by_target_addr,
87            delaySlots: value.delay_slots,
88            branchType: [
89                branch_info_0.into(),
90                branch_info_1.into(),
91                branch_info_2.into(),
92            ],
93            branchTarget: [
94                branch_info_0.target().unwrap_or_default(),
95                branch_info_1.target().unwrap_or_default(),
96                branch_info_2.target().unwrap_or_default(),
97            ],
98            branchArch: [
99                branch_info_0
100                    .arch
101                    .map(|a| a.handle)
102                    .unwrap_or(std::ptr::null_mut()),
103                branch_info_1
104                    .arch
105                    .map(|a| a.handle)
106                    .unwrap_or(std::ptr::null_mut()),
107                branch_info_2
108                    .arch
109                    .map(|a| a.handle)
110                    .unwrap_or(std::ptr::null_mut()),
111            ],
112        }
113    }
114}