Skip to content

Commit 2a7195f

Browse files
committed
More rust cleanup
- Get rid of RawFunctionViewType - Add better Debug impl for Function
1 parent 3f1533e commit 2a7195f

File tree

1 file changed

+70
-32
lines changed

1 file changed

+70
-32
lines changed

rust/src/function.rs

Lines changed: 70 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -173,11 +173,49 @@ pub enum FunctionViewType {
173173
HighLevelLanguageRepresentation(String),
174174
}
175175

176-
pub(crate) struct RawFunctionViewType(pub BNFunctionViewType);
177-
176+
#[allow(unused)]
178177
impl FunctionViewType {
179-
pub(crate) fn as_raw(&self) -> RawFunctionViewType {
180-
let view_type = match self {
178+
pub(crate) fn from_raw(value: &BNFunctionViewType) -> Option<Self> {
179+
match value.type_ {
180+
BNFunctionGraphType::InvalidILViewType => None,
181+
BNFunctionGraphType::NormalFunctionGraph => Some(FunctionViewType::Normal),
182+
BNFunctionGraphType::LowLevelILFunctionGraph => Some(FunctionViewType::LowLevelIL),
183+
BNFunctionGraphType::LiftedILFunctionGraph => Some(FunctionViewType::LiftedIL),
184+
BNFunctionGraphType::LowLevelILSSAFormFunctionGraph => {
185+
Some(FunctionViewType::LowLevelILSSAForm)
186+
}
187+
BNFunctionGraphType::MediumLevelILFunctionGraph => {
188+
Some(FunctionViewType::MediumLevelIL)
189+
}
190+
BNFunctionGraphType::MediumLevelILSSAFormFunctionGraph => {
191+
Some(FunctionViewType::MediumLevelILSSAForm)
192+
}
193+
BNFunctionGraphType::MappedMediumLevelILFunctionGraph => {
194+
Some(FunctionViewType::MappedMediumLevelIL)
195+
}
196+
BNFunctionGraphType::MappedMediumLevelILSSAFormFunctionGraph => {
197+
Some(FunctionViewType::MappedMediumLevelILSSAForm)
198+
}
199+
BNFunctionGraphType::HighLevelILFunctionGraph => Some(FunctionViewType::HighLevelIL),
200+
BNFunctionGraphType::HighLevelILSSAFormFunctionGraph => {
201+
Some(FunctionViewType::HighLevelILSSAForm)
202+
}
203+
BNFunctionGraphType::HighLevelLanguageRepresentationFunctionGraph => {
204+
Some(FunctionViewType::HighLevelLanguageRepresentation(
205+
raw_to_string(value.name).unwrap(),
206+
))
207+
}
208+
}
209+
}
210+
211+
pub(crate) fn from_owned_raw(value: BNFunctionViewType) -> Option<Self> {
212+
let owned = Self::from_raw(&value);
213+
Self::free_raw(value);
214+
owned
215+
}
216+
217+
pub(crate) fn into_raw(value: Self) -> BNFunctionViewType {
218+
let view_type = match value {
181219
FunctionViewType::Normal => BNFunctionGraphType::NormalFunctionGraph,
182220
FunctionViewType::LowLevelIL => BNFunctionGraphType::LowLevelILFunctionGraph,
183221
FunctionViewType::LiftedIL => BNFunctionGraphType::LiftedILFunctionGraph,
@@ -202,14 +240,20 @@ impl FunctionViewType {
202240
BNFunctionGraphType::HighLevelLanguageRepresentationFunctionGraph
203241
}
204242
};
205-
RawFunctionViewType(BNFunctionViewType {
243+
let view_name = match value {
244+
FunctionViewType::HighLevelLanguageRepresentation(name) => Some(BnString::new(name)),
245+
_ => None,
246+
};
247+
BNFunctionViewType {
206248
type_: view_type,
207-
name: if let FunctionViewType::HighLevelLanguageRepresentation(ref name) = self {
208-
std::ffi::CString::new(name.to_string()).unwrap().into_raw()
209-
} else {
210-
std::ptr::null()
211-
},
212-
})
249+
name: view_name
250+
.map(|n| BnString::into_raw(n) as *mut _)
251+
.unwrap_or(std::ptr::null_mut()),
252+
}
253+
}
254+
255+
pub(crate) fn free_raw(value: BNFunctionViewType) {
256+
let _ = unsafe { BnString::from_raw(value.name as *mut _) };
213257
}
214258
}
215259

@@ -246,16 +290,6 @@ impl From<FunctionGraphType> for FunctionViewType {
246290
}
247291
}
248292

249-
impl Drop for RawFunctionViewType {
250-
fn drop(&mut self) {
251-
if !self.0.name.is_null() {
252-
unsafe {
253-
let _ = std::ffi::CString::from_raw(self.0.name as *mut _);
254-
}
255-
}
256-
}
257-
}
258-
259293
#[derive(Eq)]
260294
pub struct Function {
261295
pub(crate) handle: *mut BNFunction,
@@ -2144,7 +2178,7 @@ impl Function {
21442178
/// 'user' functions may or may not have been created by a user through the or API. For instance the entry point
21452179
/// into a function is always created a 'user' function. 'user' functions should be considered the root of auto
21462180
/// analysis.
2147-
pub fn auto(&self) -> bool {
2181+
pub fn is_auto(&self) -> bool {
21482182
unsafe { BNWasFunctionAutomaticallyDiscovered(self.handle) }
21492183
}
21502184

@@ -2318,11 +2352,12 @@ impl Function {
23182352
pub fn create_graph(
23192353
&self,
23202354
view_type: FunctionViewType,
2321-
settings: Option<DisassemblySettings>,
2355+
settings: Option<&DisassemblySettings>,
23222356
) -> Ref<FlowGraph> {
23232357
let settings_raw = settings.map(|s| s.handle).unwrap_or(std::ptr::null_mut());
2324-
let result =
2325-
unsafe { BNCreateFunctionGraph(self.handle, view_type.as_raw().0, settings_raw) };
2358+
let raw_view_type = FunctionViewType::into_raw(view_type);
2359+
let result = unsafe { BNCreateFunctionGraph(self.handle, raw_view_type, settings_raw) };
2360+
FunctionViewType::free_raw(raw_view_type);
23262361
unsafe { Ref::new(FlowGraph::from_raw(result)) }
23272362
}
23282363

@@ -2337,13 +2372,16 @@ impl Function {
23372372

23382373
impl Debug for Function {
23392374
fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
2340-
write!(
2341-
f,
2342-
"<func '{}' ({}) {:x}>",
2343-
self.symbol().full_name(),
2344-
self.platform().name(),
2345-
self.start()
2346-
)
2375+
// TODO: I am sure there is more we should add to this.
2376+
f.debug_struct("Function")
2377+
.field("start", &self.start())
2378+
.field("arch", &self.arch())
2379+
.field("platform", &self.platform())
2380+
.field("symbol", &self.symbol())
2381+
.field("is_auto", &self.is_auto())
2382+
.field("tags", &self.tags().to_vec())
2383+
.field("comments", &self.comments().to_vec())
2384+
.finish()
23472385
}
23482386
}
23492387

0 commit comments

Comments
 (0)