Skip to main content

foundry_evm_symbolic/runtime/
address.rs

1use super::*;
2
3impl SymExpr {
4    pub(crate) fn representative_symbolic_address(&self) -> Address {
5        let digest = keccak256(self.symbolic_address_key());
6        let mut bytes = [0u8; 20];
7        bytes[0] = 0xfe;
8        bytes[1..].copy_from_slice(&digest[..19]);
9        Address::from(bytes)
10    }
11
12    pub(crate) fn symbolic_address_key(&self) -> String {
13        if let Some(value) = self.as_const() {
14            format!("concrete-address:{:?}", word_to_address(value))
15        } else {
16            let expr = self.symbolic_address_canonical();
17            let identity = expr.address_byte_terms_for_equivalence().map_or_else(
18                || ExpressionDigests::identity([expr]),
19                |bytes| ExpressionDigests::identity(&bytes),
20            );
21            let bytes = format!("{identity:?}");
22            format!("symbolic-address:{bytes}")
23        }
24    }
25
26    /// Keeps the low 160 bits of a word, as the EVM reads an address operand.
27    pub(crate) fn address_operand(self, cx: &mut SymCx) -> Self {
28        let mask = Self::constant(cx, (U256::ONE << 160) - U256::ONE);
29        Self::binop(cx, SymBinOp::And, self, mask)
30    }
31
32    pub(crate) fn address_match_condition(&self, cx: &mut SymCx, address: Address) -> SymBoolExpr {
33        if let Some(word) = self.as_const() {
34            return SymBoolExpr::constant(cx, word == address_word(address));
35        }
36        let Some(terms) =
37            (12..32).map(|index| self.byte_term(cx, index)).collect::<Option<Vec<_>>>()
38        else {
39            let address = Self::constant(cx, address_word(address));
40            return SymBoolExpr::eq(cx, self.clone(), address);
41        };
42        let bytes = address.as_slice();
43        let conditions = terms
44            .into_iter()
45            .enumerate()
46            .map(|(index, term)| {
47                let byte = Self::constant(cx, U256::from(bytes[index]));
48                SymBoolExpr::eq(cx, term, byte)
49            })
50            .collect();
51        SymBoolExpr::and(cx, conditions)
52    }
53
54    pub(crate) fn symbolic_address_equivalent(&self, alias: &Self) -> bool {
55        match (self.as_const(), alias.as_const()) {
56            (Some(left), Some(right)) => word_to_address(left) == word_to_address(right),
57            (None, None) => self.address_expr_equivalent(alias),
58            _ => false,
59        }
60    }
61
62    fn address_expr_equivalent(&self, alias: &Self) -> bool {
63        let this = self.symbolic_address_canonical();
64        let alias = alias.symbolic_address_canonical();
65
66        if this == alias {
67            return true;
68        }
69
70        if let (Some(candidate), Some(alias)) =
71            (this.address_byte_terms_for_equivalence(), alias.address_byte_terms_for_equivalence())
72        {
73            return candidate == alias;
74        }
75
76        match this.kind() {
77            SymExprKind::BinOp(SymBinOp::And, left, right) if right.is_address_mask() => {
78                left.address_expr_equivalent(alias)
79            }
80            SymExprKind::BinOp(SymBinOp::Shr, value, shift) if shift.is_shift_96() => {
81                match value.kind() {
82                    SymExprKind::BinOp(SymBinOp::Shl, inner, inner_shift)
83                        if inner_shift.is_shift_96() =>
84                    {
85                        inner.address_expr_equivalent(alias)
86                    }
87                    _ => false,
88                }
89            }
90            _ => false,
91        }
92    }
93
94    fn symbolic_address_canonical(&self) -> &Self {
95        match self.kind() {
96            SymExprKind::BinOp(SymBinOp::And, left, right) if right.is_address_mask() => {
97                left.symbolic_address_canonical()
98            }
99            SymExprKind::BinOp(SymBinOp::Shr, value, shift) if shift.is_shift_96() => {
100                match value.kind() {
101                    SymExprKind::BinOp(SymBinOp::Shl, inner, inner_shift)
102                        if inner_shift.is_shift_96() =>
103                    {
104                        inner.symbolic_address_canonical()
105                    }
106                    _ => self,
107                }
108            }
109            _ => self,
110        }
111    }
112
113    fn address_byte_terms_for_equivalence(&self) -> Option<Vec<Self>> {
114        (12..32).map(|index| self.extracted_byte_source(index)).collect()
115    }
116
117    fn is_address_mask(&self) -> bool {
118        self.as_const() == Some((U256::ONE << 160) - U256::ONE)
119    }
120
121    fn is_shift_96(&self) -> bool {
122        self.as_const() == Some(U256::from(96))
123    }
124}
125
126pub(crate) fn mask_bits(value: U256, bits: usize) -> U256 {
127    if bits >= 256 {
128        value
129    } else {
130        let mask = (U256::ONE << bits) - U256::ONE;
131        value & mask
132    }
133}
134
135pub(crate) fn address_word(address: Address) -> U256 {
136    address.into_word().into()
137}
138
139pub(crate) fn word_to_address(value: U256) -> Address {
140    Address::from_word(value.into())
141}
142
143pub(crate) fn stable_symbol(cx: &mut SymCx, prefix: &'static str, input: &[u8]) -> Symbol {
144    let digest = keccak256(input);
145    let mut symbol = String::with_capacity(prefix.len() + 17);
146    symbol.push_str(prefix);
147    symbol.push('_');
148    for byte in &digest[..8] {
149        let _ = write!(symbol, "{byte:02x}");
150    }
151    cx.intern(&symbol)
152}