1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
use super::context::BoundsMetadata;
use super::Context;
use super::Data;
use crate::abstract_domain::*;
use crate::analysis::function_signature::FunctionSignature;
use crate::intermediate_representation::Project;
use crate::prelude::*;
use std::collections::BTreeMap;

/// The state consists of the abstract identifier for the current stack frame
/// and lists of the lower and upper bounds for all known memory objects.
///
/// The bounds of memory objects are computed the first time an access to it is observed.
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)]
pub struct State {
    /// The abstract identifier of the stack frame of the function that the state belongs to.
    stack_id: AbstractIdentifier,
    /// The lower bounds of all memory objects for which accesses have been observed.
    object_lower_bounds: DomainMap<AbstractIdentifier, BitvectorDomain, UnionMergeStrategy>,
    /// The upper bounds of all memory objects for which accesses have been observed.
    object_upper_bounds: DomainMap<AbstractIdentifier, BitvectorDomain, UnionMergeStrategy>,
}

impl State {
    /// Create a new state representing the state at the start of the function
    /// given by the `function_tid` and corresponding function signature.
    ///
    /// Only the bounds of the current stack frame are known in this state,
    /// since there are no memory accesses to observe prior to the function start.
    pub fn new(function_tid: &Tid, function_sig: &FunctionSignature, project: &Project) -> State {
        let stack_id =
            AbstractIdentifier::from_var(function_tid.clone(), &project.stack_pointer_register);
        let stack_upper_bound: i64 = match project.stack_pointer_register.name.as_str() {
            "ESP" => 4,
            "RSP" => 8,
            _ => 0,
        };
        let stack_upper_bound = std::cmp::max(
            stack_upper_bound,
            function_sig.get_stack_params_total_size(&project.stack_pointer_register),
        );
        let object_lower_bounds = BTreeMap::from([(
            stack_id.clone(),
            BitvectorDomain::new_top(stack_id.bytesize()),
        )]);
        let object_upper_bounds = BTreeMap::from([(
            stack_id.clone(),
            Bitvector::from_i64(stack_upper_bound)
                .into_resize_signed(stack_id.bytesize())
                .into(),
        )]);
        State {
            stack_id,
            object_lower_bounds: object_lower_bounds.into(),
            object_upper_bounds: object_upper_bounds.into(),
        }
    }

    /// Check for the given address whether the access to it would be in the boundaries for all possible target memory objects.
    /// Return a list of logging messages describing those cases
    /// where the access may fall outside of the corresponding memory object boundaries.
    pub fn check_address_access(
        &mut self,
        address: &Data,
        value_size: ByteSize,
        context: &Context,
    ) -> Vec<String> {
        let mut out_of_bounds_access_warnings = Vec::new();
        for (id, offset) in address.get_relative_values() {
            if !self.object_lower_bounds.contains_key(id) {
                self.compute_bounds_of_id(id, context);
            }
            if let Ok((lower_offset, upper_offset)) = offset.try_to_offset_interval() {
                if let Ok(lower_bound) = self.object_lower_bounds.get(id).unwrap().try_to_offset() {
                    if lower_bound > lower_offset {
                        out_of_bounds_access_warnings.push(format!("For the object ID {id} access to the offset {lower_offset} may be smaller than the lower object bound of {lower_bound}."));
                        if let (
                            Some(BoundsMetadata {
                                source: Some(source),
                                ..
                            }),
                            _,
                        ) = context.compute_bounds_of_id(id, &self.stack_id)
                        {
                            out_of_bounds_access_warnings.push(format!("The object bound is based on the possible source value {:#} for the object ID.", source.to_json_compact()));
                            let call_sequence_tids = collect_tids_for_cwe_warning(
                                source.get_if_unique_target().unwrap().0,
                                self,
                                context,
                            );
                            out_of_bounds_access_warnings
                                .push(format!("Relevant callgraph TIDs: [{call_sequence_tids}]"));
                        } else {
                            let mut callgraph_tids = format!("{}", self.stack_id.get_tid());
                            for call_tid in id.get_path_hints() {
                                callgraph_tids += &format!(", {call_tid}");
                            }
                            out_of_bounds_access_warnings
                                .push(format!("Relevant callgraph TIDs: [{callgraph_tids}]",));
                        }
                        // Replace the bound with `Top` to prevent duplicate CWE warnings with the same root cause.
                        self.object_lower_bounds
                            .insert(id.clone(), BitvectorDomain::new_top(address.bytesize()));
                    }
                }
                if let Ok(upper_bound) = self.object_upper_bounds.get(id).unwrap().try_to_offset() {
                    if upper_bound < upper_offset + (u64::from(value_size) as i64) {
                        out_of_bounds_access_warnings.push(format!("For the object ID {} access to the offset {} (size {}) may overflow the upper object bound of {}.",
                            id,
                            upper_offset,
                            u64::from(value_size),
                            upper_bound,
                        ));
                        if let (
                            _,
                            Some(BoundsMetadata {
                                source: Some(source),
                                ..
                            }),
                        ) = context.compute_bounds_of_id(id, &self.stack_id)
                        {
                            out_of_bounds_access_warnings.push(format!("The object bound is based on the possible source value {:#} for the object ID.", source.to_json_compact()));
                            let call_sequence_tids = collect_tids_for_cwe_warning(
                                source.get_if_unique_target().unwrap().0,
                                self,
                                context,
                            );
                            out_of_bounds_access_warnings
                                .push(format!("Relevant callgraph TIDs: [{call_sequence_tids}]"));
                        } else {
                            let mut callgraph_tids = format!("{}", self.stack_id.get_tid());
                            for call_tid in id.get_path_hints() {
                                callgraph_tids += &format!(", {call_tid}");
                            }
                            out_of_bounds_access_warnings
                                .push(format!("Relevant callgraph TIDs: [{callgraph_tids}]",));
                        }
                        // Replace the bound with `Top` to prevent duplicate CWE warnings with the same root cause.
                        self.object_upper_bounds
                            .insert(id.clone(), BitvectorDomain::new_top(address.bytesize()));
                    }
                }
            }
        }

        out_of_bounds_access_warnings
    }

    /// Compute the bounds of a memory object given by the provided `object_id`
    /// and insert the results into `self.object_lower_bounds` and `self.object_upper_bounds`.
    ///
    /// This function assumes that the objects bounds have not been computed prior to this function call.
    /// For bounds that could not be determined (e.g. because the source for the object ID is unknown)
    /// we insert `Top` bounds into the bounds maps.
    fn compute_bounds_of_id(&mut self, object_id: &AbstractIdentifier, context: &Context) {
        let (lower_bound, upper_bound) = context.compute_bounds_of_id(object_id, &self.stack_id);
        let lower_bound = match lower_bound {
            Some(bound_metadata) => Bitvector::from_i64(bound_metadata.resulting_bound)
                .into_resize_signed(object_id.bytesize())
                .into(),
            None => BitvectorDomain::new_top(object_id.bytesize()),
        };
        let upper_bound = match upper_bound {
            Some(bound_metadata) => Bitvector::from_i64(bound_metadata.resulting_bound)
                .into_resize_signed(object_id.bytesize())
                .into(),
            None => BitvectorDomain::new_top(object_id.bytesize()),
        };
        self.object_lower_bounds
            .insert(object_id.clone(), lower_bound);
        self.object_upper_bounds
            .insert(object_id.clone(), upper_bound);
    }
}

impl AbstractDomain for State {
    /// Merge two states by merging the known object bounds of both.
    fn merge(&self, other: &State) -> State {
        State {
            stack_id: self.stack_id.clone(),
            object_lower_bounds: self.object_lower_bounds.merge(&other.object_lower_bounds),
            object_upper_bounds: self.object_upper_bounds.merge(&other.object_upper_bounds),
        }
    }

    /// The state has no logical `Top` element.
    fn is_top(&self) -> bool {
        false
    }
}

impl State {
    /// Get a json-representation of the state.
    /// Intended for pretty printing, not useable for serialization/deserialization.
    #[allow(dead_code)]
    pub fn to_json_compact(&self) -> serde_json::Value {
        use serde_json::*;
        let mut state_map = Map::new();
        state_map.insert(
            "stack_id".to_string(),
            Value::String(self.stack_id.to_string()),
        );

        let lower_bounds: Vec<_> = self
            .object_lower_bounds
            .iter()
            .map(|(id, bound)| Value::String(format!("{id}: {bound}")))
            .collect();
        state_map.insert("lower_bounds".to_string(), Value::Array(lower_bounds));
        let upper_bounds: Vec<_> = self
            .object_upper_bounds
            .iter()
            .map(|(id, bound)| Value::String(format!("{id}: {bound}")))
            .collect();
        state_map.insert("upper_bounds".to_string(), Value::Array(upper_bounds));

        Value::Object(state_map)
    }
}

/// Collect all relevant call sequence TIDs corresponding to a CWE warning.
/// This includes:
/// - The TID of a root function from which both the allocation site and the site of the CWE warning can be reached
/// - All call TID that are relevant for reaching the allocation site from the root function.
/// - All call TIDs that are relevant for reachting the site of the CWE warning.
///   This list is complete in the sense that all possible paths in the call graph from the root function to the CWE warning site
///   are covered by these calls.
///
/// The resulting list is returned as a string,
/// as it is currently only used for human-readable context information in the CWE warnings.
fn collect_tids_for_cwe_warning(
    id: &AbstractIdentifier,
    state: &State,
    context: &Context,
) -> String {
    use crate::analysis::callgraph::find_call_sequences_to_target;
    let caller_tid = if context.project.program.term.subs.contains_key(id.get_tid()) {
        // The ID is the stack ID of some function.
        id.get_tid().clone()
    } else {
        // The ID corresponds to a malloc-like call
        let root_call_tid = if let Some(root_call) = id.get_path_hints().last() {
            root_call
        } else {
            id.get_tid()
        };
        context
            .project
            .program
            .term
            .find_sub_containing_jump(root_call_tid)
            .expect("Caller corresponding to call does not exist.")
    };
    let mut tids = Vec::new();
    tids.push(caller_tid.clone());
    tids.extend(id.get_path_hints().iter().cloned());
    if caller_tid != *state.stack_id.get_tid() {
        // We also need the possible call sequences from the caller to the current function
        let call_sequence_tids = find_call_sequences_to_target(
            &context.callgraph,
            &caller_tid,
            state.stack_id.get_tid(),
        );
        tids.extend(call_sequence_tids);
    }
    // Build a string out of the TID list
    tids.iter()
        .map(|tid| format!("{tid}"))
        .reduce(|accum, elem| format!("{accum}, {elem}"))
        .unwrap()
}

#[cfg(test)]
pub mod tests {
    use super::*;
    use crate::{intermediate_representation::*, variable};

    #[test]
    fn test_new() {
        let context = Context::mock_x64();
        let state = State::new(
            &Tid::new("func"),
            &FunctionSignature::mock_x64(),
            context.project,
        );
        let stack_id = AbstractIdentifier::from_var(Tid::new("func"), &variable!("RSP:8"));

        assert_eq!(state.stack_id, stack_id);
        assert_eq!(state.object_lower_bounds.len(), 1);
        assert_eq!(state.object_upper_bounds.len(), 1);
        assert_eq!(
            *state.object_lower_bounds.get(&stack_id).unwrap(),
            BitvectorDomain::new_top(ByteSize::new(8))
        );
        assert_eq!(
            *state.object_upper_bounds.get(&stack_id).unwrap(),
            Bitvector::from_i64(8).into()
        );
    }

    #[test]
    fn test_check_address_access() {
        let context = Context::mock_x64();
        let mut state = State::new(
            &Tid::new("func"),
            &FunctionSignature::mock_x64(),
            context.project,
        );
        let stack_id = AbstractIdentifier::from_var(Tid::new("func"), &variable!("RSP:8"));
        // access in bounds
        let address = Data::from_target(stack_id.clone(), Bitvector::from_i64(-12).into());
        assert!(state
            .check_address_access(&address, ByteSize::new(8), &context)
            .is_empty());
        // access out of bounds
        let address = Data::from_target(stack_id.clone(), Bitvector::from_i64(4).into());
        assert_eq!(
            state
                .check_address_access(&address, ByteSize::new(8), &context)
                .len(),
            2
        );
        // subsequent errors are suppressed
        let address = Data::from_target(stack_id, Bitvector::from_i64(8).into());
        assert!(state
            .check_address_access(&address, ByteSize::new(8), &context)
            .is_empty());
    }

    #[test]
    fn test_compute_bounds_of_id() {
        let mut context = Context::mock_x64();
        context
            .malloc_tid_to_object_size_map
            .insert(Tid::new("malloc_call"), Data::from(Bitvector::from_i64(42)));
        context
            .call_to_caller_fn_map
            .insert(Tid::new("malloc_call"), Tid::new("main"));
        let mut state = State::new(
            &Tid::new("func"),
            &FunctionSignature::mock_x64(),
            context.project,
        );

        state.compute_bounds_of_id(&AbstractIdentifier::mock("malloc_call", "RAX", 8), &context);
        assert_eq!(state.object_lower_bounds.len(), 2);
        assert_eq!(
            state.object_lower_bounds[&AbstractIdentifier::mock("malloc_call", "RAX", 8)],
            Bitvector::from_i64(0).into()
        );
        assert_eq!(
            state.object_upper_bounds[&AbstractIdentifier::mock("malloc_call", "RAX", 8)],
            Bitvector::from_i64(42).into()
        );
    }
}