From d75066545afdc9b5d0a52bd2518e43f683c671a5 Mon Sep 17 00:00:00 2001 From: Josh Ferrell Date: Thu, 13 Jun 2024 14:13:11 -0400 Subject: Apply stack variables from DWARF --- rust/src/debuginfo.rs | 55 ++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 48 insertions(+), 7 deletions(-) (limited to 'rust/src/debuginfo.rs') diff --git a/rust/src/debuginfo.rs b/rust/src/debuginfo.rs index f29f8ac2..68c666e8 100644 --- a/rust/src/debuginfo.rs +++ b/rust/src/debuginfo.rs @@ -75,7 +75,7 @@ use crate::{ platform::Platform, rc::*, string::{raw_to_string, BnStrCompatible, BnString}, - types::{DataVariableAndName, NameAndType, Type}, + types::{DataVariableAndName, NameAndType, NamedTypedVariable, Type}, }; use std::{hash::Hash, os::raw::c_void, ptr, slice}; @@ -301,6 +301,7 @@ pub struct DebugFunctionInfo { address: u64, platform: Option>, components: Vec, + local_variables: Vec, } impl From<&BNDebugFunctionInfo> for DebugFunctionInfo { @@ -310,6 +311,15 @@ impl From<&BNDebugFunctionInfo> for DebugFunctionInfo { .map(|component| raw_to_string(*component as *const _).unwrap()) .collect(); + let local_variables: Vec = unsafe { slice::from_raw_parts(raw.localVariables, raw.localVariableN) } + .iter() + .map(|local_variable| { + unsafe { + NamedTypedVariable::from_raw(local_variable) + } + }) + .collect(); + Self { short_name: raw_to_string(raw.shortName), full_name: raw_to_string(raw.fullName), @@ -326,6 +336,7 @@ impl From<&BNDebugFunctionInfo> for DebugFunctionInfo { Some(unsafe { Platform::ref_from_raw(raw.platform) }) }, components, + local_variables, } } } @@ -339,6 +350,7 @@ impl DebugFunctionInfo { address: Option, platform: Option>, components: Vec, + local_variables: Vec, ) -> Self { Self { short_name, @@ -351,6 +363,7 @@ impl DebugFunctionInfo { }, platform, components, + local_variables, } } } @@ -776,14 +789,31 @@ impl DebugInfo { .as_ref() .map_or(ptr::null_mut() as *mut _, |name| name.as_ptr() as _); - let mut components_array: Vec<*const ::std::os::raw::c_char> = + let mut components_array: Vec<*mut ::std::os::raw::c_char> = Vec::with_capacity(new_func.components.len()); - for component in &new_func.components { - components_array.push(component.as_ptr() as _); - } + + + let mut local_variables_array: Vec = + Vec::with_capacity(new_func.local_variables.len()); unsafe { - BNAddDebugFunction( + for component in &new_func.components { + components_array.push(BNAllocString(component.clone().into_bytes_with_nul().as_ptr() as _)); + } + + for local_variable in &new_func.local_variables { + local_variables_array.push( + BNVariableNameAndType { + var: local_variable.var.raw(), + autoDefined: local_variable.auto_defined, + typeConfidence: local_variable.ty.confidence, + name: BNAllocString(local_variable.name.clone().into_bytes_with_nul().as_ptr() as _), + type_: local_variable.ty.contents.handle, + } + ); + } + + let result = BNAddDebugFunction( self.handle, &mut BNDebugFunctionInfo { shortName: short_name, @@ -800,8 +830,19 @@ impl DebugInfo { }, components: components_array.as_ptr() as _, componentN: new_func.components.len(), + localVariables: local_variables_array.as_ptr() as _, + localVariableN: local_variables_array.len(), }, - ) + ); + + for i in components_array { + BNFreeString(i); + } + + for i in &local_variables_array { + BNFreeString(i.name); + } + result } } -- cgit v1.3.1