Skip to content

Commit

Permalink
Merge pull request #1 from br0kej/dev
Browse files Browse the repository at this point in the history
Bumper Feature Addition
  • Loading branch information
br0kej authored Aug 25, 2023
2 parents 46d2e20 + c5ab3d3 commit c30cfce
Show file tree
Hide file tree
Showing 7 changed files with 568 additions and 169 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ prettytable = "0.10.0"
serde-aux = "4.2.0"
log = "0.4.19"
env_logger = "0.10.0"
thiserror = "1.0.47"
[dependencies.petgraph]
version = "0.6.2"
features = ["serde-1"]
Expand Down
88 changes: 88 additions & 0 deletions src/agcj.rs
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)
}
}
35 changes: 35 additions & 0 deletions src/errors.rs
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
)),
}
}
}
Loading

0 comments on commit c30cfce

Please sign in to comment.