Skip to content

Commit

Permalink
More rust cleanup
Browse files Browse the repository at this point in the history
- Added unit tests for MLIL and HLIL
- "Fixed" MLIL, LLIL, and HLIL having issues regarding Instruction vs Expression indexes
- Renamed CallingConvention to CoreCallingConvention and removed architecture generic
- Renamed CallingConventionBase to CallingConvention
- Simplified calling convention code and fixed some bugs with implicit registers
- Added impl Debug to MLIL and HLIL instructions

Still need to at some point add an Expression to MLIL and HLIL. We also might want to look into having the Instruction kind just return the expression kind.
  • Loading branch information
emesare committed Jan 21, 2025
1 parent da1b04d commit 2ed6a26
Show file tree
Hide file tree
Showing 30 changed files with 862 additions and 704 deletions.
4 changes: 2 additions & 2 deletions arch/riscv/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use binaryninja::{
UnusedRegisterStackInfo,
},
binary_view::{BinaryView, BinaryViewExt},
calling_convention::{register_calling_convention, CallingConventionBase, ConventionBuilder},
calling_convention::{register_calling_convention, CallingConvention, ConventionBuilder},
custom_binary_view::{BinaryViewType, BinaryViewTypeExt},
disassembly::{InstructionTextToken, InstructionTextTokenKind},
function::Function,
Expand Down Expand Up @@ -2670,7 +2670,7 @@ impl<D: 'static + RiscVDisassembler + Send + Sync> RiscVCC<D> {
}
}

impl<D: 'static + RiscVDisassembler + Send + Sync> CallingConventionBase for RiscVCC<D> {
impl<D: 'static + RiscVDisassembler + Send + Sync> CallingConvention for RiscVCC<D> {
type Arch = RiscVArch<D>;

fn caller_saved_registers(&self) -> Vec<Register<D>> {
Expand Down
8 changes: 4 additions & 4 deletions plugins/pdb-ng/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use crate::symbol_parser::{ParsedDataSymbol, ParsedProcedure, ParsedSymbol};
use crate::type_parser::ParsedType;
use binaryninja::architecture::{Architecture, CoreArchitecture};
use binaryninja::binary_view::{BinaryView, BinaryViewExt};
use binaryninja::calling_convention::CallingConvention;
use binaryninja::calling_convention::CoreCallingConvention;
use binaryninja::confidence::{Conf, MIN_CONFIDENCE};
use binaryninja::debuginfo::{DebugFunctionInfo, DebugInfo};
use binaryninja::platform::Platform;
Expand All @@ -48,11 +48,11 @@ pub struct PDBParserInstance<'a, S: Source<'a> + 'a> {
/// Default arch of self.bv
pub(crate) arch: CoreArchitecture,
/// Default calling convention for self.arch
pub(crate) default_cc: Ref<CallingConvention<CoreArchitecture>>,
pub(crate) default_cc: Ref<CoreCallingConvention>,
/// Thiscall calling convention for self.bv, or default_cc if we can't find one
pub(crate) thiscall_cc: Ref<CallingConvention<CoreArchitecture>>,
pub(crate) thiscall_cc: Ref<CoreCallingConvention>,
/// Cdecl calling convention for self.bv, or default_cc if we can't find one
pub(crate) cdecl_cc: Ref<CallingConvention<CoreArchitecture>>,
pub(crate) cdecl_cc: Ref<CoreCallingConvention>,
/// Default platform of self.bv
pub(crate) platform: Ref<Platform>,
/// pdb-rs structure for making lifetime hell a real place
Expand Down
11 changes: 4 additions & 7 deletions plugins/pdb-ng/src/type_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ use std::sync::OnceLock;
use crate::struct_grouper::group_structure;
use crate::PDBParserInstance;
use anyhow::{anyhow, Result};
use binaryninja::architecture::{Architecture, CoreArchitecture};
use binaryninja::architecture::Architecture;
use binaryninja::binary_view::BinaryViewExt;
use binaryninja::calling_convention::CallingConvention;
use binaryninja::calling_convention::CoreCallingConvention;
use binaryninja::confidence::{Conf, MAX_CONFIDENCE};
use binaryninja::platform::Platform;
use binaryninja::rc::Ref;
Expand Down Expand Up @@ -2084,7 +2084,7 @@ impl<'a, S: Source<'a> + 'a> PDBParserInstance<'a, S> {
pub(crate) fn find_calling_convention(
platform: &Platform,
name: &str,
) -> Option<Ref<CallingConvention<CoreArchitecture>>> {
) -> Option<Ref<CoreCallingConvention>> {
platform
.calling_conventions()
.iter()
Expand All @@ -2093,10 +2093,7 @@ impl<'a, S: Source<'a> + 'a> PDBParserInstance<'a, S> {
}

/// Convert pdb calling convention enum to binja
fn cv_call_t_to_calling_convention(
&self,
cv: u8,
) -> Option<Ref<CallingConvention<CoreArchitecture>>> {
fn cv_call_t_to_calling_convention(&self, cv: u8) -> Option<Ref<CoreCallingConvention>> {
match CV_call_t::try_from(cv) {
Ok(CV_call_t::NEAR_FAST) | Ok(CV_call_t::FAR_FAST) => {
self.platform.get_fastcall_calling_convention()
Expand Down
8 changes: 3 additions & 5 deletions plugins/warp/src/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::collections::HashSet;
use binaryninja::architecture::Architecture as BNArchitecture;
use binaryninja::architecture::ArchitectureExt;
use binaryninja::binary_view::{BinaryView, BinaryViewExt};
use binaryninja::calling_convention::CallingConvention as BNCallingConvention;
use binaryninja::calling_convention::CoreCallingConvention as BNCallingConvention;
use binaryninja::confidence::{Conf as BNConf, MAX_CONFIDENCE};
use binaryninja::rc::Ref as BNRef;
use binaryninja::symbol::{Symbol as BNSymbol, SymbolType as BNSymbolType};
Expand Down Expand Up @@ -348,17 +348,15 @@ pub fn from_bn_type_internal(
}
}

pub fn from_bn_calling_convention<A: BNArchitecture>(
raw_cc: BNRef<BNCallingConvention<A>>,
) -> CallingConvention {
pub fn from_bn_calling_convention(raw_cc: BNRef<BNCallingConvention>) -> CallingConvention {
// NOTE: Currently calling convention just stores the name.
CallingConvention::new(raw_cc.name().as_str())
}

pub fn to_bn_calling_convention<A: BNArchitecture>(
arch: &A,
calling_convention: &CallingConvention,
) -> BNRef<BNCallingConvention<A>> {
) -> BNRef<BNCallingConvention> {
for cc in &arch.calling_conventions() {
if cc.name().as_str() == calling_convention.name {
return cc.clone();
Expand Down
File renamed without changes.
6 changes: 5 additions & 1 deletion rust/examples/mlil.rs → rust/examples/medium_level_il.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ mod visitor {
}
Float(float) => {
print_indent(indent);
println!("int {:e}", float);
println!("float {:e}", float);
}
Expr(expr) => print_il_expr(&expr, indent),
Var(var) => {
Expand Down Expand Up @@ -135,6 +135,10 @@ mod visitor {
}
ConstantData(_) => println!("contantdata"),
Intrinsic(intrinsic) => println!("intrinsic {}", intrinsic.name()),
InstructionIndex(idx) => {
print_indent(indent);
println!("index {}", idx);
}
}
}
}
Expand Down
15 changes: 9 additions & 6 deletions rust/src/architecture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use binaryninjacore_sys::*;
use std::fmt::{Debug, Formatter};

use crate::{
calling_convention::CallingConvention,
calling_convention::CoreCallingConvention,
data_buffer::DataBuffer,
disassembly::InstructionTextToken,
low_level_il::{MutableLiftedILExpr, MutableLiftedILFunction},
Expand Down Expand Up @@ -1897,7 +1897,7 @@ impl Architecture for CoreArchitecture {

macro_rules! cc_func {
($get_name:ident, $get_api:ident, $set_name:ident, $set_api:ident) => {
fn $get_name(&self) -> Option<Ref<CallingConvention<Self>>> {
fn $get_name(&self) -> Option<Ref<CoreCallingConvention>> {
let arch = self.as_ref();

unsafe {
Expand All @@ -1906,12 +1906,15 @@ macro_rules! cc_func {
if cc.is_null() {
None
} else {
Some(CallingConvention::ref_from_raw(cc, self.handle()))
Some(CoreCallingConvention::ref_from_raw(
cc,
self.as_ref().handle(),
))
}
}
}

fn $set_name(&self, cc: &CallingConvention<Self>) {
fn $set_name(&self, cc: &CoreCallingConvention) {
let arch = self.as_ref();

assert!(
Expand Down Expand Up @@ -1939,12 +1942,12 @@ pub trait ArchitectureExt: Architecture {
}
}

fn calling_conventions(&self) -> Array<CallingConvention<Self>> {
fn calling_conventions(&self) -> Array<CoreCallingConvention> {
unsafe {
let mut count = 0;
let calling_convs =
BNGetArchitectureCallingConventions(self.as_ref().handle, &mut count);
Array::new(calling_convs, count, self.handle())
Array::new(calling_convs, count, self.as_ref().handle())
}
}

Expand Down
Loading

0 comments on commit 2ed6a26

Please sign in to comment.