Skip to content

Commit

Permalink
fix bug in param sanitation
Browse files Browse the repository at this point in the history
  • Loading branch information
Enkelmann committed Nov 23, 2023
1 parent 9bea23e commit dcdb7ea
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 9 deletions.
43 changes: 39 additions & 4 deletions src/cwe_checker_lib/src/analysis/function_signature/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,17 +288,21 @@ impl FunctionSignature {
///
/// Only non-nested stack parameters are joined by this function.
fn merge_intersecting_stack_parameters(&mut self, stack_register: &Variable) {
let stack_params: BTreeMap<i64, (AbstractLocation, AccessPattern)> = self
let stack_params: BTreeMap<(i64, ByteSize), (AbstractLocation, AccessPattern)> = self
.parameters
.iter()
.filter_map(|(location, access_pattern)| {
get_offset_if_simple_stack_param(location, stack_register)
.map(|offset| (offset, (location.clone(), *access_pattern)))
get_offset_if_simple_stack_param(location, stack_register).map(|offset| {
(
(offset, location.bytesize()),
(location.clone(), *access_pattern),
)
})
})
.collect();

let mut current_param: Option<(i64, i64, AccessPattern)> = None;
for (offset, (param, access_pattern)) in stack_params.into_iter() {
for ((offset, _), (param, access_pattern)) in stack_params.into_iter() {
self.parameters.remove(&param);
if let Some((cur_offset, cur_size, cur_access_pattern)) = current_param {
if offset < cur_offset + cur_size {
Expand Down Expand Up @@ -343,6 +347,37 @@ impl Default for FunctionSignature {
}
}

impl FunctionSignature {
/// Generate a compact JSON-representation of the function signature for pretty printing.
#[allow(dead_code)]
pub fn to_json_compact(&self) -> serde_json::Value {
let mut json_map = serde_json::Map::new();
let mut param_map = serde_json::Map::new();
for (param, pattern) in self.parameters.iter() {
param_map.insert(
format!("{param}"),
serde_json::Value::String(format!("{pattern}")),
);
}
json_map.insert(
"Parameters".to_string(),
serde_json::Value::Object(param_map),
);
let mut global_param_map = serde_json::Map::new();
for (param, pattern) in self.global_parameters.iter() {
global_param_map.insert(
format!("{param}"),
serde_json::Value::String(format!("{pattern}")),
);
}
json_map.insert(
"Globals".to_string(),
serde_json::Value::Object(global_param_map),
);
serde_json::Value::Object(json_map)
}
}

/// If the abstract location is a location on the stack
/// then return its offset relative to the zero position on the stack.
fn get_offset_if_simple_stack_param(
Expand Down
11 changes: 6 additions & 5 deletions src/cwe_checker_lib/src/analysis/function_signature/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,18 +80,19 @@ fn test_two_parameter_overlapping_merging() {
fn test_merging_multiple_parameters() {
let proj = Project::mock_x64();
let mut func_sig = FunctionSignature::mock_x64();
let stack_parm_1 = mock_stack_arg(0x1000, 8);
let stack_parm_2 = mock_stack_arg(0x1000, 1);
let stack_parm_3 = mock_stack_arg(0x1007, 1);
let stack_parm_4 = mock_stack_arg(0x1008, 8);
let stack_parm_1 = mock_stack_arg(0x8, 8);
let stack_parm_2 = mock_stack_arg(0x8, 1);
let stack_parm_3 = mock_stack_arg(0xf, 1);
let stack_parm_4 = mock_stack_arg(0x10, 8);

func_sig.parameters.extend([
(stack_parm_1.clone(), AccessPattern::new()),
(stack_parm_2, AccessPattern::new()),
(stack_parm_3, AccessPattern::new()),
(stack_parm_4.clone(), AccessPattern::new()),
]);
assert_eq!(Vec::<String>::new(), func_sig.sanitize(&proj));
let logs = func_sig.sanitize(&proj);
assert_eq!(logs, Vec::<String>::new());

let mut expected_function_sig = FunctionSignature::mock_x64();
expected_function_sig.parameters.extend([
Expand Down

0 comments on commit dcdb7ea

Please sign in to comment.