Skip to content

Commit

Permalink
add Function::instr_highlight method
Browse files Browse the repository at this point in the history
  • Loading branch information
rbran committed May 10, 2024
1 parent 5cdf27d commit 16069d9
Show file tree
Hide file tree
Showing 2 changed files with 169 additions and 11 deletions.
30 changes: 19 additions & 11 deletions rust/src/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,21 @@

use binaryninjacore_sys::*;

use crate::databuffer::DataBuffer;
use crate::disassembly::InstructionTextToken;
use crate::rc::*;
use crate::string::*;
use crate::tags::{Tag, TagType};
use crate::types::IndirectBranchInfo;
use crate::types::{ConstantReference, QualifiedName, RegisterValueType, Variable};
use crate::{
architecture::CoreArchitecture,
basicblock::{BasicBlock, BlockContext},
binaryview::{BinaryView, BinaryViewExt},
hlil, llil, mlil,
platform::Platform,
string::*,
symbol::Symbol,
types::{Conf, NamedTypedVariable, Type},
tags::{Tag, TagType},
types::{
Conf, ConstantReference, HighlightColor, IndirectBranchInfo, NamedTypedVariable,
QualifiedName, RegisterValueType, Type, Variable,
},
};
use crate::{databuffer::DataBuffer, disassembly::InstructionTextToken, rc::*};
pub use binaryninjacore_sys::BNAnalysisSkipReason as AnalysisSkipReason;
pub use binaryninjacore_sys::BNFunctionAnalysisSkipOverride as FunctionAnalysisSkipOverride;
pub use binaryninjacore_sys::BNFunctionUpdateType as FunctionUpdateType;
Expand Down Expand Up @@ -716,9 +715,7 @@ impl Function {
}

/// List of indirect branches
pub fn indirect_branches(
&self,
) -> Array<IndirectBranchInfo> {
pub fn indirect_branches(&self) -> Array<IndirectBranchInfo> {
let mut count = 0;
let branches = unsafe { BNGetIndirectBranches(self.handle, &mut count) };
assert!(!branches.is_null());
Expand All @@ -737,6 +734,17 @@ impl Function {
assert!(!branches.is_null());
unsafe { Array::new(branches, count, ()) }
}

/// # Example
/// ```no_run
/// # let fun: binaryninja::function::Function = todo!();
/// let color = fun.instr_highlight(0x1337, None);
/// ```
pub fn instr_highlight(&self, addr: u64, arch: Option<CoreArchitecture>) -> HighlightColor {
let arch = arch.unwrap_or_else(|| self.arch());
let color = unsafe { BNGetInstructionHighlight(self.handle, arch.0, addr) };
HighlightColor::from_raw(color)
}
}

impl fmt::Debug for Function {
Expand Down
150 changes: 150 additions & 0 deletions rust/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3283,3 +3283,153 @@ unsafe impl CoreArrayWrapper for IndirectBranchInfo {
Self::from_raw(*raw)
}
}

/////////////////////////
// HighlightStandardColor

#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
pub enum HighlightStandardColor {
//NoHighlightColor,
BlueHighlightColor,
GreenHighlightColor,
CyanHighlightColor,
RedHighlightColor,
MagentaHighlightColor,
YellowHighlightColor,
OrangeHighlightColor,
WhiteHighlightColor,
BlackHighlightColor,
}

impl HighlightStandardColor {
pub fn from_raw(value: BNHighlightStandardColor) -> Option<Self> {
Some(match value {
BNHighlightStandardColor::NoHighlightColor => return None,
BNHighlightStandardColor::BlueHighlightColor => Self::BlueHighlightColor,
BNHighlightStandardColor::GreenHighlightColor => Self::GreenHighlightColor,
BNHighlightStandardColor::CyanHighlightColor => Self::CyanHighlightColor,
BNHighlightStandardColor::RedHighlightColor => Self::RedHighlightColor,
BNHighlightStandardColor::MagentaHighlightColor => Self::MagentaHighlightColor,
BNHighlightStandardColor::YellowHighlightColor => Self::YellowHighlightColor,
BNHighlightStandardColor::OrangeHighlightColor => Self::OrangeHighlightColor,
BNHighlightStandardColor::WhiteHighlightColor => Self::WhiteHighlightColor,
BNHighlightStandardColor::BlackHighlightColor => Self::BlackHighlightColor,
})
}
pub fn into_raw(self) -> BNHighlightStandardColor {
match self {
//Self::NoHighlightColor => BNHighlightStandardColor::NoHighlightColor,
Self::BlueHighlightColor => BNHighlightStandardColor::BlueHighlightColor,
Self::GreenHighlightColor => BNHighlightStandardColor::GreenHighlightColor,
Self::CyanHighlightColor => BNHighlightStandardColor::CyanHighlightColor,
Self::RedHighlightColor => BNHighlightStandardColor::RedHighlightColor,
Self::MagentaHighlightColor => BNHighlightStandardColor::MagentaHighlightColor,
Self::YellowHighlightColor => BNHighlightStandardColor::YellowHighlightColor,
Self::OrangeHighlightColor => BNHighlightStandardColor::OrangeHighlightColor,
Self::WhiteHighlightColor => BNHighlightStandardColor::WhiteHighlightColor,
Self::BlackHighlightColor => BNHighlightStandardColor::BlackHighlightColor,
}
}
}

/////////////////////////
// HighlightColor

#[derive(Debug, Copy, Clone)]
pub enum HighlightColor {
NoHighlightColor,
StandardHighlightColor {
color: HighlightStandardColor,
alpha: u8,
},
MixedHighlightColor {
color: HighlightStandardColor,
mix_color: HighlightStandardColor,
mix: u8,
alpha: u8,
},
CustomHighlightColor {
r: u8,
g: u8,
b: u8,
alpha: u8,
},
}

impl HighlightColor {
pub fn from_raw(raw: BNHighlightColor) -> Self {
const HIGHLIGHT_COLOR: u32 = BNHighlightColorStyle::StandardHighlightColor as u32;
const MIXED_HIGHLIGHT_COLOR: u32 = BNHighlightColorStyle::MixedHighlightColor as u32;
const CUSTOM_HIGHLIHGT_COLOR: u32 = BNHighlightColorStyle::CustomHighlightColor as u32;
match raw.style as u32 {
HIGHLIGHT_COLOR => {
let Some(color) = HighlightStandardColor::from_raw(raw.color) else {
// StandardHighlightColor with NoHighlightColor, is no color
return Self::NoHighlightColor;
};
Self::StandardHighlightColor {
color,
alpha: raw.alpha,
}
}
MIXED_HIGHLIGHT_COLOR => {
let Some(color) = HighlightStandardColor::from_raw(raw.color) else {
panic!("Highlight mixed color with no color");
};
let Some(mix_color) = HighlightStandardColor::from_raw(raw.mixColor) else {
panic!("Highlight mixed color with no mix_color");
};
Self::MixedHighlightColor {
color,
mix_color,
mix: raw.mix,
alpha: raw.alpha,
}
}
CUSTOM_HIGHLIHGT_COLOR => Self::CustomHighlightColor {
r: raw.r,
g: raw.g,
b: raw.b,
alpha: raw.alpha,
},
// other color style is just no color
_ => Self::NoHighlightColor,
}
}

pub fn into_raw(self) -> BNHighlightColor {
let zeroed: BNHighlightColor = unsafe { core::mem::zeroed() };
match self {
Self::NoHighlightColor => BNHighlightColor {
style: BNHighlightColorStyle::StandardHighlightColor,
color: BNHighlightStandardColor::NoHighlightColor,
..zeroed
},
Self::StandardHighlightColor { color, alpha } => BNHighlightColor {
style: BNHighlightColorStyle::StandardHighlightColor,
color: color.into_raw(),
alpha: alpha,
..zeroed
},
Self::MixedHighlightColor {
color,
mix_color,
mix,
alpha,
} => BNHighlightColor {
color: color.into_raw(),
mixColor: mix_color.into_raw(),
mix: mix,
alpha: alpha,
..zeroed
},
Self::CustomHighlightColor { r, g, b, alpha } => BNHighlightColor {
r: r,
g: g,
b: b,
alpha: alpha,
..zeroed
},
}
}
}

0 comments on commit 16069d9

Please sign in to comment.