-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from br0kej/dev
Bumper Feature Addition
- Loading branch information
Showing
7 changed files
with
568 additions
and
169 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
use crate::files::AGCJFile; | ||
use crate::networkx::NetworkxDiGraph; | ||
use petgraph::prelude::Graph; | ||
use serde::{Deserialize, Serialize}; | ||
use std::fs::File; | ||
|
||
#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)] | ||
#[serde(rename_all = "camelCase")] | ||
pub struct AGCJFunctionCallGraphs { | ||
pub name: String, | ||
pub size: i64, | ||
pub imports: Vec<String>, | ||
} | ||
|
||
#[derive(Serialize, Deserialize, Debug)] | ||
pub struct AGCJParsedObjects { | ||
pub edge_property: String, | ||
pub edges: Vec<Vec<i32>>, | ||
pub node_holes: Vec<String>, | ||
pub nodes: Vec<String>, | ||
} | ||
|
||
impl AGCJFunctionCallGraphs { | ||
// Creates a petgraph object of a given function and all functions called as part of it's execution | ||
pub fn to_petgraph(&self, output_path: &String, binary_name: &String) { | ||
let mut graph = Graph::<String, u32>::new(); | ||
|
||
let calling_func = graph.add_node(self.name.clone()); | ||
for ele in self.imports.iter() { | ||
let callee = graph.add_node(ele.clone()); | ||
graph.update_edge(calling_func, callee, 0); | ||
} | ||
let networkx_graph = NetworkxDiGraph::from(graph); | ||
let filename = format!("{}/{}-{}-cg.json", output_path, binary_name, self.name); | ||
serde_json::to_writer( | ||
&File::create(filename).expect("Failed to create writer"), | ||
&networkx_graph, | ||
) | ||
.expect("Unable to write JSON"); | ||
} | ||
|
||
// Creates a petgraph object of a given function, all of the functions called functions and | ||
// then their callees. | ||
pub fn one_hop_to_petgraph(&self, global_cg: &AGCJFile) { | ||
let mut graph = Graph::<String, u32>::new(); | ||
|
||
// Dealing with local call graph | ||
let calling_func = graph.add_node(self.name.clone()); | ||
for ele in self.imports.iter() { | ||
let callee = graph.add_node(ele.clone()); | ||
graph.update_edge(calling_func, callee, 0); | ||
} | ||
|
||
// Getting imports call graphs | ||
for import in self.imports.iter() { | ||
let import_object: &Option<&AGCJFunctionCallGraphs> = &global_cg | ||
.function_call_graphs | ||
.as_ref() | ||
.unwrap() | ||
.iter() | ||
.find(|cg| cg.name == *import); | ||
if import_object.is_some() { | ||
for ele in import_object.unwrap().imports.iter() { | ||
let callee = graph.add_node(ele.clone()); | ||
let import_node_index = | ||
graph.node_indices().find(|i| &graph[*i] == import).unwrap(); | ||
debug!("{} -> {}", import, ele); | ||
graph.update_edge(import_node_index, callee, 0); | ||
} | ||
} | ||
} | ||
|
||
let networkx_graph = NetworkxDiGraph::from(graph); | ||
let filename = format!( | ||
"{}/{}-{}-1hop-cg.json", | ||
global_cg.output_path, global_cg.filename, self.name | ||
); | ||
serde_json::to_writer( | ||
&File::create(filename).expect("Failed to create writer"), | ||
&networkx_graph, | ||
) | ||
.expect("Unable to write JSON"); | ||
} | ||
|
||
pub fn print_callees(&self) { | ||
println!("{:?}", self.imports) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
use std::fmt::Display; | ||
use std::io; | ||
use thiserror::Error; | ||
|
||
#[derive(Error, Debug)] | ||
pub enum FileLoadError { | ||
FileError(io::Error), | ||
DeserializeError(serde_json::Error), | ||
} | ||
|
||
impl From<serde_json::Error> for FileLoadError { | ||
fn from(e: serde_json::Error) -> Self { | ||
Self::DeserializeError(e) | ||
} | ||
} | ||
|
||
impl From<io::Error> for FileLoadError { | ||
fn from(e: io::Error) -> Self { | ||
Self::FileError(e) | ||
} | ||
} | ||
|
||
impl Display for FileLoadError { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
match &self { | ||
FileLoadError::FileError(e) => { | ||
f.write_fmt(format_args!("could not open file due to error {:?}", e)) | ||
} | ||
FileLoadError::DeserializeError(e) => f.write_fmt(format_args!( | ||
"could not deserialize file due to error {:?}", | ||
e | ||
)), | ||
} | ||
} | ||
} |
Oops, something went wrong.