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
use super::*;

impl AbstractObject {
    /// Read the value at the given offset of the given size inside the memory region.
    pub fn get_value(&self, offset: Bitvector, bytesize: ByteSize) -> Data {
        self.inner.memory.get(offset, bytesize)
    }

    /// Write a value at the given offset to the memory region.
    ///
    /// If the abstract object is not unique (i.e. may represent more than one actual object),
    /// merge the old value at the given offset with the new value.
    pub fn set_value(&mut self, value: Data, offset: &ValueDomain) -> Result<(), Error> {
        let inner = Arc::make_mut(&mut self.inner);
        inner
            .pointer_targets
            .extend(value.referenced_ids().cloned());
        if let Ok(concrete_offset) = offset.try_to_bitvec() {
            if inner.is_unique {
                inner.memory.add(value, concrete_offset);
            } else {
                let merged_value = inner
                    .memory
                    .get(concrete_offset.clone(), value.bytesize())
                    .merge(&value);
                inner.memory.add(merged_value, concrete_offset);
            };
        } else if let Ok((start, end)) = offset.try_to_offset_interval() {
            inner
                .memory
                .mark_interval_values_as_top(start, end, value.bytesize());
        } else {
            inner.memory.mark_all_values_as_top();
        }
        Ok(())
    }

    /// Merge `value` at position `offset` with the value currently saved at that position.
    pub fn merge_value(&mut self, value: Data, offset: &ValueDomain) {
        let inner = Arc::make_mut(&mut self.inner);
        inner
            .pointer_targets
            .extend(value.referenced_ids().cloned());
        if let Ok(concrete_offset) = offset.try_to_bitvec() {
            let merged_value = inner
                .memory
                .get(concrete_offset.clone(), value.bytesize())
                .merge(&value);
            inner.memory.add(merged_value, concrete_offset);
        } else if let Ok((start, end)) = offset.try_to_offset_interval() {
            inner
                .memory
                .mark_interval_values_as_top(start, end, value.bytesize());
        } else {
            inner.memory.mark_all_values_as_top();
        }
    }

    /// Marks all memory as `Top` and adds the `additional_targets` to the pointer targets.
    /// Represents the effect of unknown write instructions to the object
    /// which may include writing pointers to targets from the `additional_targets` set to the object.
    pub fn assume_arbitrary_writes(&mut self, additional_targets: &BTreeSet<AbstractIdentifier>) {
        let inner = Arc::make_mut(&mut self.inner);
        inner.memory.mark_all_values_as_top();
        inner
            .pointer_targets
            .extend(additional_targets.iter().cloned());
    }
}