From 2fcacc55d5466a7d17bdc0b3ce988772aa6d0653 Mon Sep 17 00:00:00 2001 From: KyleMiles Date: Thu, 21 Jan 2021 18:35:20 +0000 Subject: cargo fmt and all my changes --- rust/src/callingconvention.rs | 157 ++++++++++++++++++++++++++++++------------ 1 file changed, 113 insertions(+), 44 deletions(-) (limited to 'rust/src/callingconvention.rs') diff --git a/rust/src/callingconvention.rs b/rust/src/callingconvention.rs index a607b67f..51fe412d 100644 --- a/rust/src/callingconvention.rs +++ b/rust/src/callingconvention.rs @@ -1,9 +1,23 @@ +// Copyright 2021 Vector 35 Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +use std::borrow::Borrow; +use std::marker::PhantomData; use std::mem; +use std::os::raw::c_void; use std::ptr; use std::slice; -use std::os::raw::c_void; -use std::borrow::Borrow; -use std::marker::PhantomData; use binaryninjacore_sys::*; @@ -27,6 +41,7 @@ pub trait CallingConventionBase: Sync { fn arg_registers_shared_index(&self) -> bool; fn reserved_stack_space_for_arg_registers(&self) -> bool; fn stack_adjusted_on_return(&self) -> bool; + fn is_eligible_for_heuristics(&self) -> bool; fn return_int_reg(&self) -> Option<::Register>; fn return_hi_int_reg(&self) -> Option<::Register>; @@ -41,7 +56,7 @@ pub fn register_calling_convention(arch: &A, name: N, cc: C) -> Ref, + C: 'static + CallingConventionBase, { struct CustomCallingConventionContext where @@ -60,7 +75,10 @@ where }) } - fn alloc_register_list + ExactSizeIterator>(items: I, count: &mut usize) -> *mut u32 { + fn alloc_register_list + ExactSizeIterator>( + items: I, + count: &mut usize, + ) -> *mut u32 { let len = items.len(); *count = len; @@ -159,11 +177,14 @@ where where C: CallingConventionBase, { - ffi_wrap!("CallingConvention::reserved_stack_space_for_arg_registers", unsafe { - let ctxt = &*(ctxt as *mut CustomCallingConventionContext); + ffi_wrap!( + "CallingConvention::reserved_stack_space_for_arg_registers", + unsafe { + let ctxt = &*(ctxt as *mut CustomCallingConventionContext); - ctxt.cc.reserved_stack_space_for_arg_registers() - }) + ctxt.cc.reserved_stack_space_for_arg_registers() + } + ) } extern "C" fn cb_stack_adjusted_on_return(ctxt: *mut c_void) -> bool @@ -177,6 +198,17 @@ where }) } + extern "C" fn cb_is_eligible_for_heuristics(ctxt: *mut c_void) -> bool + where + C: CallingConventionBase, + { + ffi_wrap!("CallingConvention::is_eligible_for_heuristics", unsafe { + let ctxt = &*(ctxt as *mut CustomCallingConventionContext); + + ctxt.cc.is_eligible_for_heuristics() + }) + } + extern "C" fn cb_return_int_reg(ctxt: *mut c_void) -> u32 where C: CallingConventionBase, @@ -233,7 +265,10 @@ where }) } - extern "C" fn cb_implicitly_defined_registers(ctxt: *mut c_void, count: *mut usize) -> *mut u32 + extern "C" fn cb_implicitly_defined_registers( + ctxt: *mut c_void, + count: *mut usize, + ) -> *mut u32 where C: CallingConventionBase, { @@ -245,8 +280,12 @@ where }) } - extern "C" fn cb_incoming_reg_value(_ctxt: *mut c_void, _reg: u32, _func: *mut BNFunction, val: *mut BNRegisterValue) - where + extern "C" fn cb_incoming_reg_value( + _ctxt: *mut c_void, + _reg: u32, + _func: *mut BNFunction, + val: *mut BNRegisterValue, + ) where C: CallingConventionBase, { ffi_wrap!("CallingConvention::incoming_reg_value", unsafe { @@ -258,8 +297,12 @@ where }) } - extern "C" fn cb_incoming_flag_value(_ctxt: *mut c_void, _flag: u32, _func: *mut BNFunction, val: *mut BNRegisterValue) - where + extern "C" fn cb_incoming_flag_value( + _ctxt: *mut c_void, + _flag: u32, + _func: *mut BNFunction, + val: *mut BNRegisterValue, + ) where C: CallingConventionBase, { ffi_wrap!("CallingConvention::incoming_flag_value", unsafe { @@ -271,23 +314,37 @@ where }) } - extern "C" fn cb_incoming_var_for_param(ctxt: *mut c_void, var: *const BNVariable, _func: *mut BNFunction, param: *mut BNVariable) - where + extern "C" fn cb_incoming_var_for_param( + ctxt: *mut c_void, + var: *const BNVariable, + _func: *mut BNFunction, + param: *mut BNVariable, + ) where C: CallingConventionBase, { ffi_wrap!("CallingConvention::incoming_var_for_param", unsafe { let ctxt = &*(ctxt as *mut CustomCallingConventionContext); - ptr::write(param, BNGetDefaultIncomingVariableForParameterVariable(ctxt.raw_handle, var)); + ptr::write( + param, + BNGetDefaultIncomingVariableForParameterVariable(ctxt.raw_handle, var), + ); }) } - extern "C" fn cb_incoming_param_for_var(ctxt: *mut c_void, var: *const BNVariable, _func: *mut BNFunction, param: *mut BNVariable) - where + extern "C" fn cb_incoming_param_for_var( + ctxt: *mut c_void, + var: *const BNVariable, + _func: *mut BNFunction, + param: *mut BNVariable, + ) where C: CallingConventionBase, { ffi_wrap!("CallingConvention::incoming_var_for_param", unsafe { let ctxt = &*(ctxt as *mut CustomCallingConventionContext); - ptr::write(param, BNGetDefaultParameterVariableForIncomingVariable(ctxt.raw_handle, var)); + ptr::write( + param, + BNGetDefaultParameterVariableForIncomingVariable(ctxt.raw_handle, var), + ); }) } @@ -310,6 +367,7 @@ where areArgumentRegistersSharedIndex: Some(cb_arg_shared_index::), isStackReservedForArgumentRegisters: Some(cb_stack_reserved_arg_regs::), isStackAdjustedOnReturn: Some(cb_stack_adjusted_on_return::), + isEligibleForHeuristics: Some(cb_is_eligible_for_heuristics::), getIntegerReturnValueRegister: Some(cb_return_int_reg::), getHighIntegerReturnValueRegister: Some(cb_return_hi_int_reg::), @@ -374,7 +432,6 @@ impl Hash for CallingConvention { } } - impl CallingConventionBase for CallingConvention { type Arch = A; @@ -384,9 +441,13 @@ impl CallingConventionBase for CallingConvention { let regs = BNGetCallerSavedRegisters(self.handle, &mut count); let arch = self.arch_handle.borrow(); - let res = slice::from_raw_parts(regs, count).iter().map(|&r| { - arch.register_from_id(r).expect("bad reg id from CallingConvention") - }).collect(); + let res = slice::from_raw_parts(regs, count) + .iter() + .map(|&r| { + arch.register_from_id(r) + .expect("bad reg id from CallingConvention") + }) + .collect(); BNFreeRegisterList(regs); @@ -400,9 +461,13 @@ impl CallingConventionBase for CallingConvention { let regs = BNGetCalleeSavedRegisters(self.handle, &mut count); let arch = self.arch_handle.borrow(); - let res = slice::from_raw_parts(regs, count).iter().map(|&r| { - arch.register_from_id(r).expect("bad reg id from CallingConvention") - }).collect(); + let res = slice::from_raw_parts(regs, count) + .iter() + .map(|&r| { + arch.register_from_id(r) + .expect("bad reg id from CallingConvention") + }) + .collect(); BNFreeRegisterList(regs); @@ -430,38 +495,34 @@ impl CallingConventionBase for CallingConvention { unsafe { BNIsStackAdjustedOnReturn(self.handle) } } + fn is_eligible_for_heuristics(&self) -> bool { + false + } + fn return_int_reg(&self) -> Option { match unsafe { BNGetIntegerReturnValueRegister(self.handle) } { - id if id < 0x8000_0000 => { - self.arch_handle.borrow().register_from_id(id) - } + id if id < 0x8000_0000 => self.arch_handle.borrow().register_from_id(id), _ => None, } } fn return_hi_int_reg(&self) -> Option { match unsafe { BNGetHighIntegerReturnValueRegister(self.handle) } { - id if id < 0x8000_0000 => { - self.arch_handle.borrow().register_from_id(id) - } + id if id < 0x8000_0000 => self.arch_handle.borrow().register_from_id(id), _ => None, } } fn return_float_reg(&self) -> Option { match unsafe { BNGetFloatReturnValueRegister(self.handle) } { - id if id < 0x8000_0000 => { - self.arch_handle.borrow().register_from_id(id) - } + id if id < 0x8000_0000 => self.arch_handle.borrow().register_from_id(id), _ => None, } } fn global_pointer_reg(&self) -> Option { match unsafe { BNGetGlobalPointerRegister(self.handle) } { - id if id < 0x8000_0000 => { - self.arch_handle.borrow().register_from_id(id) - } + id if id < 0x8000_0000 => self.arch_handle.borrow().register_from_id(id), _ => None, } } @@ -502,6 +563,7 @@ pub struct ConventionBuilder { arg_registers_shared_index: bool, reserved_stack_space_for_arg_registers: bool, stack_adjusted_on_return: bool, + is_eligible_for_heuristics: bool, return_int_reg: Option, return_hi_int_reg: Option, @@ -524,13 +586,14 @@ macro_rules! bool_arg { self.$name = val; self } - } + }; } macro_rules! reg_list { ($name:ident) => { pub fn $name(mut self, regs: &[&str]) -> Self { - { // FIXME NLL + { + // FIXME NLL let arch = self.arch_handle.borrow(); let arch_regs = regs.iter().filter_map(|&r| arch.register_by_name(r)); @@ -539,20 +602,21 @@ macro_rules! reg_list { self } - } + }; } macro_rules! reg { ($name:ident) => { pub fn $name(mut self, reg: &str) -> Self { - { // FIXME NLL + { + // FIXME NLL let arch = self.arch_handle.borrow(); self.$name = arch.register_by_name(reg); } self } - } + }; } impl ConventionBuilder { @@ -566,6 +630,7 @@ impl ConventionBuilder { arg_registers_shared_index: false, reserved_stack_space_for_arg_registers: false, stack_adjusted_on_return: false, + is_eligible_for_heuristics: false, return_int_reg: None, return_hi_int_reg: None, @@ -635,6 +700,10 @@ impl CallingConventionBase for ConventionBuilder { self.stack_adjusted_on_return } + fn is_eligible_for_heuristics(&self) -> bool { + self.is_eligible_for_heuristics + } + fn return_int_reg(&self) -> Option { self.return_int_reg.clone() } -- cgit v1.3.1