Skip to content

Commit

Permalink
lib/utils/debug: add ToJsonCompact trait
Browse files Browse the repository at this point in the history
  • Loading branch information
Valentin Obst committed Mar 25, 2024
1 parent 6a69d7b commit f751c48
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 1 deletion.
14 changes: 14 additions & 0 deletions src/cwe_checker_lib/src/abstract_domain/mem_region.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use super::{AbstractDomain, HasTop, SizedDomain};
use crate::intermediate_representation::ByteSize;
use crate::prelude::*;
use crate::utils::ToJsonCompact;
use apint::{Int, Width};
use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;
Expand Down Expand Up @@ -66,6 +67,19 @@ impl<T: AbstractDomain + SizedDomain + HasTop + std::fmt::Debug> HasTop for MemR
}
}

impl<T> ToJsonCompact for MemRegion<T>
where
T: ToJsonCompact + AbstractDomain + SizedDomain + HasTop + std::fmt::Debug,
{
fn to_json_compact(&self) -> serde_json::Value {
serde_json::Value::Object(
self.iter()
.map(|(offset, val)| (offset.to_string(), val.to_json_compact()))
.collect(),
)
}
}

impl<T: AbstractDomain + SizedDomain + HasTop + std::fmt::Debug> MemRegion<T> {
/// Create a new, empty memory region.
pub fn new(address_bytesize: ByteSize) -> MemRegion<T> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use crate::prelude::*;
/// The interprocedural_flow value will either be transferred from the end of the called subroutine
/// to the return site in case of a forward analysis or from the beginning of the called subroutine
/// to the callsite in a backward analysis.
#[derive(PartialEq, Eq, Serialize, Deserialize, Clone)]
#[derive(PartialEq, Eq, Serialize, Deserialize, Clone, Debug)]
pub enum NodeValue<T: PartialEq + Eq + Clone> {
/// A single abstract value
Value(T),
Expand Down
7 changes: 7 additions & 0 deletions src/cwe_checker_lib/src/analysis/taint/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use crate::analysis::{
};
use crate::intermediate_representation::*;
use crate::prelude::*;
use crate::utils::ToJsonCompact;
use std::convert::AsRef;
use std::fmt::Display;

Expand Down Expand Up @@ -593,6 +594,12 @@ impl RegisterDomain for Taint {
}
}

impl ToJsonCompact for Taint {
fn to_json_compact(&self) -> serde_json::Value {
serde_json::json!(self.to_string())
}
}

impl Taint {
/// Checks whether the given value is in fact tainted.
pub fn is_tainted(&self) -> bool {
Expand Down
26 changes: 26 additions & 0 deletions src/cwe_checker_lib/src/analysis/taint/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use crate::analysis::pointer_inference::Data as PiData;
use crate::analysis::vsa_results::VsaResult;
use crate::intermediate_representation::*;
use crate::prelude::*;
use crate::utils::ToJsonCompact;

use std::collections::HashMap;

Expand All @@ -30,6 +31,31 @@ pub struct State {
memory_taint: HashMap<AbstractIdentifier, MemRegion<Taint>>,
}

impl ToJsonCompact for State {
fn to_json_compact(&self) -> serde_json::Value {
let mut state_map = serde_json::Map::new();

let register_taint = self
.register_taint
.iter()
.map(|(reg, taint)| (reg.name.clone(), taint.to_json_compact()))
.collect();
let register_taint = serde_json::Value::Object(register_taint);

let memory_taint = self
.memory_taint
.iter()
.map(|(mem_id, mem_region)| (mem_id.to_string(), mem_region.to_json_compact()))
.collect();
let memory_taint = serde_json::Value::Object(memory_taint);

state_map.insert("registers".into(), register_taint);
state_map.insert("memory".into(), memory_taint);

serde_json::Value::Object(state_map)
}
}

impl PartialEq for State {
/// Two states are equal if the same values are tainted in both states.
fn eq(&self, other: &Self) -> bool {
Expand Down
2 changes: 2 additions & 0 deletions src/cwe_checker_lib/src/checkers/cwe_252.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,5 +203,7 @@ pub fn check_cwe(
let config: Config =
serde_json::from_value(cwe_params.clone()).expect("CWE252: invalid configuration");

println!("{}", analysis_results.project.program.term);

CweAnalysis::new(analysis_results, config).run()
}
17 changes: 17 additions & 0 deletions src/cwe_checker_lib/src/checkers/cwe_252/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,23 @@ pub type FpComputation<'a, 'b> = fixpoint::Computation<
forward_interprocedural_fixpoint::GeneralizedContext<'a, TaCompCtx<'a, 'b>>,
>;

impl ToJsonCompact for FpComputation<'_, '_> {
fn to_json_compact(&self) -> serde_json::Value {
let graph = self.get_graph();
let mut json_nodes = serde_json::Map::new();

for (node_index, node_value) in self.node_values().iter() {
let node = graph.node_weight(*node_index).unwrap();

if let NodeValue::Value(value) = node_value {
json_nodes.insert(format!("{node}"), value.to_json_compact());
}
}

serde_json::Value::Object(json_nodes)
}
}

/// Type that represents the definition of the taint analysis.
///
/// Values of this type represent the taint analysis for a particular call to an
Expand Down
11 changes: 11 additions & 0 deletions src/cwe_checker_lib/src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,14 @@ pub fn get_ghidra_plugin_path(plugin_name: &str) -> std::path::PathBuf {
let data_dir = project_dirs.data_dir();
data_dir.join("ghidra").join(plugin_name)
}

/// TODO
pub trait ToJsonCompact {
/// TODO
fn to_json_compact(&self) -> serde_json::Value;

/// TODO
fn print_compact_json(&self) {
println!("{:#}", self.to_json_compact())
}
}

0 comments on commit f751c48

Please sign in to comment.