Skip to main content

foundry_evm_symbolic/runtime/
calldata.rs

1use super::*;
2
3#[derive(Clone, Debug)]
4pub(crate) struct SymCalldata {
5    size: usize,
6    size_word: SymExpr,
7    bytes: SymBytes,
8}
9
10impl SymCalldata {
11    pub(crate) fn from_bytes(cx: &mut SymCx, bytes: SymBytes) -> Self {
12        let size = bytes.len();
13        Self { size_word: SymExpr::constant(cx, U256::from(size)), size, bytes }
14    }
15
16    /// Calldata of a symbolic length of at most `bytes.len()`, whose bytes past its end are zero.
17    pub(crate) fn with_size_word(bytes: SymBytes, size_word: SymExpr) -> Self {
18        Self { size: bytes.len(), size_word, bytes }
19    }
20
21    pub(crate) fn size_word(&self) -> SymExpr {
22        self.size_word.clone()
23    }
24
25    pub(crate) fn load_word(&self, cx: &mut SymCx, offset: SymExpr) -> SymExpr {
26        if let Some(offset) = offset.as_const() {
27            let Ok(offset) = usize::try_from(offset) else {
28                return SymExpr::zero(cx);
29            };
30            self.bytes.word_at(cx, offset)
31        } else {
32            self.load_dynamic(cx, &offset)
33        }
34    }
35
36    fn load_dynamic(&self, cx: &mut SymCx, offset: &SymExpr) -> SymExpr {
37        let mut result = SymExpr::zero(cx);
38        for candidate in (0..self.size).rev() {
39            let candidate_expr = SymExpr::constant(cx, U256::from(candidate));
40            let condition = SymBoolExpr::eq(cx, offset.clone(), candidate_expr);
41            let word = self.bytes.word_at(cx, candidate);
42            result = SymExpr::ite(cx, condition, word, result);
43        }
44        result
45    }
46
47    pub(crate) fn read_bytes_offset(
48        &self,
49        cx: &mut SymCx,
50        offset: SymExpr,
51        size: usize,
52    ) -> SymBytes {
53        self.bytes.read_offset(cx, offset, size)
54    }
55}
56
57impl BoundedCopySize {
58    pub(crate) fn expand_memory(&self, cx: &mut SymCx, memory: &mut SymMemory, offset: SymExpr) {
59        let size = self.size_word(cx);
60        memory.expand_range(cx, offset, size);
61    }
62
63    pub(crate) fn read_from_memory(
64        &self,
65        cx: &mut SymCx,
66        memory: &SymMemory,
67        offset: SymExpr,
68    ) -> SymBytes {
69        match self {
70            Self::Concrete(size) => memory.read_bytes_offset(cx, offset, *size),
71            Self::Symbolic { size, max_size } => {
72                memory.read_bytes_symbolic_size(cx, offset, size.clone(), *max_size)
73            }
74        }
75    }
76
77    pub(crate) fn size_word(&self, cx: &mut SymCx) -> SymExpr {
78        match self {
79            Self::Concrete(size) => SymExpr::constant(cx, U256::from(*size)),
80            Self::Symbolic { size, .. } => size.clone(),
81        }
82    }
83
84    pub(crate) fn parts(&self, cx: &mut SymCx) -> (SymExpr, usize, bool) {
85        match self {
86            Self::Concrete(size) => (SymExpr::constant(cx, U256::from(*size)), *size, false),
87            Self::Symbolic { size, max_size } => (size.clone(), *max_size, true),
88        }
89    }
90
91    pub(crate) fn calldata(&self, cx: &mut SymCx, input: SymBytes) -> SymCalldata {
92        match self {
93            Self::Concrete(_) => SymCalldata::from_bytes(cx, input),
94            Self::Symbolic { size, .. } => {
95                SymCalldata { size: input.len(), size_word: size.clone(), bytes: input }
96            }
97        }
98    }
99}