binaryninja/architecture/
branches.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
use crate::architecture::CoreArchitecture;
use crate::function::Location;
use crate::rc::{CoreArrayProvider, CoreArrayProviderInner};
use binaryninjacore_sys::*;

pub use binaryninjacore_sys::BNBranchType as BranchType;

#[derive(Default, Copy, Clone, PartialEq, Eq, Hash, Debug)]
pub enum BranchKind {
    #[default]
    Unresolved,
    Unconditional(u64),
    False(u64),
    True(u64),
    Call(u64),
    FunctionReturn,
    SystemCall,
    Indirect,
    Exception,
    UserDefined,
}

#[derive(Default, Copy, Clone, PartialEq, Eq, Hash, Debug)]
pub struct BranchInfo {
    /// If `None`, the target architecture is the same as the branching instruction.
    pub arch: Option<CoreArchitecture>,
    pub kind: BranchKind,
}

impl BranchInfo {
    /// Branches to an instruction with the current architecture.
    pub fn new(kind: BranchKind) -> Self {
        Self { arch: None, kind }
    }

    /// Branches to an instruction with an explicit architecture.
    ///
    /// Use this if your architecture can transition to another architecture with a branch.
    pub fn new_with_arch(kind: BranchKind, arch: CoreArchitecture) -> Self {
        Self {
            arch: Some(arch),
            kind,
        }
    }

    pub fn target(&self) -> Option<u64> {
        match self.kind {
            BranchKind::Unconditional(target) => Some(target),
            BranchKind::False(target) => Some(target),
            BranchKind::True(target) => Some(target),
            BranchKind::Call(target) => Some(target),
            _ => None,
        }
    }
}

impl From<BranchInfo> for BNBranchType {
    fn from(value: BranchInfo) -> Self {
        match value.kind {
            BranchKind::Unresolved => BNBranchType::UnresolvedBranch,
            BranchKind::Unconditional(_) => BNBranchType::UnconditionalBranch,
            BranchKind::False(_) => BNBranchType::FalseBranch,
            BranchKind::True(_) => BNBranchType::TrueBranch,
            BranchKind::Call(_) => BNBranchType::CallDestination,
            BranchKind::FunctionReturn => BNBranchType::FunctionReturn,
            BranchKind::SystemCall => BNBranchType::SystemCall,
            BranchKind::Indirect => BNBranchType::IndirectBranch,
            BranchKind::Exception => BNBranchType::ExceptionBranch,
            BranchKind::UserDefined => BNBranchType::UserDefinedBranch,
        }
    }
}

impl From<BranchKind> for BranchInfo {
    fn from(value: BranchKind) -> Self {
        Self {
            arch: None,
            kind: value,
        }
    }
}

impl From<BranchKind> for BranchType {
    fn from(value: BranchKind) -> Self {
        match value {
            BranchKind::Unresolved => BranchType::UnresolvedBranch,
            BranchKind::Unconditional(_) => BranchType::UnconditionalBranch,
            BranchKind::True(_) => BranchType::TrueBranch,
            BranchKind::False(_) => BranchType::FalseBranch,
            BranchKind::Call(_) => BranchType::CallDestination,
            BranchKind::FunctionReturn => BranchType::FunctionReturn,
            BranchKind::SystemCall => BranchType::SystemCall,
            BranchKind::Indirect => BranchType::IndirectBranch,
            BranchKind::Exception => BranchType::ExceptionBranch,
            BranchKind::UserDefined => BranchType::UserDefinedBranch,
        }
    }
}

#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
pub struct IndirectBranchInfo {
    pub source: Location,
    pub dest: Location,
    pub auto_defined: bool,
}

impl From<BNIndirectBranchInfo> for IndirectBranchInfo {
    fn from(value: BNIndirectBranchInfo) -> Self {
        Self {
            source: Location::from_raw(value.sourceAddr, value.sourceArch),
            dest: Location::from_raw(value.destAddr, value.destArch),
            auto_defined: value.autoDefined,
        }
    }
}

impl From<IndirectBranchInfo> for BNIndirectBranchInfo {
    fn from(value: IndirectBranchInfo) -> Self {
        let source_arch = value
            .source
            .arch
            .map(|a| a.handle)
            .unwrap_or(std::ptr::null_mut());
        let dest_arch = value
            .source
            .arch
            .map(|a| a.handle)
            .unwrap_or(std::ptr::null_mut());
        Self {
            sourceArch: source_arch,
            sourceAddr: value.source.addr,
            destArch: dest_arch,
            destAddr: value.dest.addr,
            autoDefined: value.auto_defined,
        }
    }
}

impl From<&BNIndirectBranchInfo> for IndirectBranchInfo {
    fn from(value: &BNIndirectBranchInfo) -> Self {
        Self::from(*value)
    }
}

impl CoreArrayProvider for IndirectBranchInfo {
    type Raw = BNIndirectBranchInfo;
    type Context = ();
    type Wrapped<'a> = Self;
}

unsafe impl CoreArrayProviderInner for IndirectBranchInfo {
    unsafe fn free(raw: *mut Self::Raw, _count: usize, _context: &Self::Context) {
        BNFreeIndirectBranchList(raw)
    }

    unsafe fn wrap_raw<'a>(raw: &'a Self::Raw, _context: &'a Self::Context) -> Self::Wrapped<'a> {
        Self::from(*raw)
    }
}