|
| 1 | +use crate::{ |
| 2 | + eip7702::{Eip7702Bytecode, EIP7702_MAGIC_BYTES}, |
| 3 | + BytecodeDecodeError, Eof, JumpTable, LegacyAnalyzedBytecode, LegacyRawBytecode, |
| 4 | + EOF_MAGIC_BYTES, |
| 5 | +}; |
| 6 | +use core::fmt::Debug; |
| 7 | +use primitives::{keccak256, Address, Bytes, B256, KECCAK_EMPTY}; |
| 8 | +use std::sync::Arc; |
| 9 | + |
| 10 | +/// State of the [`Bytecode`] analysis. |
| 11 | +#[derive(Clone, Debug, PartialEq, Eq, Hash)] |
| 12 | +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] |
| 13 | +pub enum Bytecode { |
| 14 | + /// No analysis has been performed. |
| 15 | + LegacyRaw(LegacyRawBytecode), |
| 16 | + /// The bytecode has been analyzed for valid jump destinations. |
| 17 | + LegacyAnalyzed(LegacyAnalyzedBytecode), |
| 18 | + /// Ethereum Object Format |
| 19 | + Eof(Arc<Eof>), |
| 20 | + /// EIP-7702 delegated bytecode |
| 21 | + Eip7702(Eip7702Bytecode), |
| 22 | +} |
| 23 | + |
| 24 | +impl Default for Bytecode { |
| 25 | + #[inline] |
| 26 | + fn default() -> Self { |
| 27 | + // Creates a new legacy analyzed [`Bytecode`] with exactly one STOP opcode. |
| 28 | + Self::new() |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +impl Bytecode { |
| 33 | + // Creates a new legacy analyzed [`Bytecode`] with exactly one STOP opcode. |
| 34 | + #[inline] |
| 35 | + pub fn new() -> Self { |
| 36 | + Self::LegacyAnalyzed(LegacyAnalyzedBytecode::default()) |
| 37 | + } |
| 38 | + |
| 39 | + /// Return jump table if bytecode is analyzed |
| 40 | + #[inline] |
| 41 | + pub fn legacy_jump_table(&self) -> Option<&JumpTable> { |
| 42 | + match &self { |
| 43 | + Self::LegacyAnalyzed(analyzed) => Some(analyzed.jump_table()), |
| 44 | + _ => None, |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + /// Calculate hash of the bytecode. |
| 49 | + pub fn hash_slow(&self) -> B256 { |
| 50 | + if self.is_empty() { |
| 51 | + KECCAK_EMPTY |
| 52 | + } else { |
| 53 | + keccak256(self.original_byte_slice()) |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + /// Return reference to the EOF if bytecode is EOF. |
| 58 | + #[inline] |
| 59 | + pub const fn eof(&self) -> Option<&Arc<Eof>> { |
| 60 | + match self { |
| 61 | + Self::Eof(eof) => Some(eof), |
| 62 | + _ => None, |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + /// Returns true if bytecode is EOF. |
| 67 | + #[inline] |
| 68 | + pub const fn is_eof(&self) -> bool { |
| 69 | + matches!(self, Self::Eof(_)) |
| 70 | + } |
| 71 | + |
| 72 | + /// Returns true if bytecode is EIP-7702. |
| 73 | + pub const fn is_eip7702(&self) -> bool { |
| 74 | + matches!(self, Self::Eip7702(_)) |
| 75 | + } |
| 76 | + |
| 77 | + /// Creates a new legacy [`Bytecode`]. |
| 78 | + #[inline] |
| 79 | + pub fn new_legacy(raw: Bytes) -> Self { |
| 80 | + Self::LegacyRaw(raw.into()) |
| 81 | + } |
| 82 | + |
| 83 | + /// Creates a new raw [`Bytecode`]. |
| 84 | + /// |
| 85 | + /// # Panics |
| 86 | + /// |
| 87 | + /// Panics if bytecode is in incorrect format. |
| 88 | + #[inline] |
| 89 | + pub fn new_raw(bytecode: Bytes) -> Self { |
| 90 | + Self::new_raw_checked(bytecode).expect("Expect correct EOF bytecode") |
| 91 | + } |
| 92 | + |
| 93 | + /// Creates a new EIP-7702 [`Bytecode`] from [`Address`]. |
| 94 | + #[inline] |
| 95 | + pub fn new_eip7702(address: Address) -> Self { |
| 96 | + Self::Eip7702(Eip7702Bytecode::new(address)) |
| 97 | + } |
| 98 | + |
| 99 | + /// Creates a new raw [`Bytecode`]. |
| 100 | + /// |
| 101 | + /// Returns an error on incorrect Bytecode format. |
| 102 | + #[inline] |
| 103 | + pub fn new_raw_checked(bytecode: Bytes) -> Result<Self, BytecodeDecodeError> { |
| 104 | + let prefix = bytecode.get(..2); |
| 105 | + match prefix { |
| 106 | + Some(prefix) if prefix == &EOF_MAGIC_BYTES => { |
| 107 | + let eof = Eof::decode(bytecode)?; |
| 108 | + Ok(Self::Eof(Arc::new(eof))) |
| 109 | + } |
| 110 | + Some(prefix) if prefix == &EIP7702_MAGIC_BYTES => { |
| 111 | + let eip7702 = Eip7702Bytecode::new_raw(bytecode)?; |
| 112 | + Ok(Self::Eip7702(eip7702)) |
| 113 | + } |
| 114 | + _ => Ok(Self::LegacyRaw(bytecode.into())), |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + /// Perform bytecode analysis. |
| 119 | + /// |
| 120 | + /// The analysis finds and caches valid jump destinations for later execution as an optimization step. |
| 121 | + /// |
| 122 | + /// If the bytecode is already analyzed, it is returned as-is. |
| 123 | + #[inline] |
| 124 | + pub fn into_analyzed(self) -> Bytecode { |
| 125 | + let Bytecode::LegacyRaw(bytecode) = self else { |
| 126 | + return self; |
| 127 | + }; |
| 128 | + |
| 129 | + Bytecode::LegacyAnalyzed(bytecode.into_analyzed()) |
| 130 | + } |
| 131 | + |
| 132 | + /// Create new checked bytecode. |
| 133 | + /// |
| 134 | + /// # Safety |
| 135 | + /// |
| 136 | + /// Bytecode needs to end with STOP (0x00) opcode as checked bytecode assumes |
| 137 | + /// that it is safe to iterate over bytecode without checking lengths. |
| 138 | + pub unsafe fn new_analyzed( |
| 139 | + bytecode: Bytes, |
| 140 | + original_len: usize, |
| 141 | + jump_table: JumpTable, |
| 142 | + ) -> Self { |
| 143 | + Self::LegacyAnalyzed(LegacyAnalyzedBytecode::new( |
| 144 | + bytecode, |
| 145 | + original_len, |
| 146 | + jump_table, |
| 147 | + )) |
| 148 | + } |
| 149 | + |
| 150 | + /// Returns a reference to the bytecode. |
| 151 | + /// |
| 152 | + /// In case of EOF this will be the first code section. |
| 153 | + #[inline] |
| 154 | + pub fn bytecode(&self) -> &Bytes { |
| 155 | + match self { |
| 156 | + Self::LegacyRaw(bytes) => bytes, |
| 157 | + Self::LegacyAnalyzed(analyzed) => analyzed.bytecode(), |
| 158 | + Self::Eof(eof) => eof |
| 159 | + .body |
| 160 | + .code(0) |
| 161 | + .expect("Valid EOF has at least one code section"), |
| 162 | + Self::Eip7702(code) => code.raw(), |
| 163 | + } |
| 164 | + } |
| 165 | + |
| 166 | + /// Returns false if bytecode can't be executed in Interpreter. |
| 167 | + pub fn is_execution_ready(&self) -> bool { |
| 168 | + !matches!(self, Self::LegacyRaw(_)) |
| 169 | + } |
| 170 | + |
| 171 | + /// Returns bytes |
| 172 | + #[inline] |
| 173 | + pub fn bytes(&self) -> Bytes { |
| 174 | + match self { |
| 175 | + Self::LegacyAnalyzed(analyzed) => analyzed.bytecode().clone(), |
| 176 | + _ => self.original_bytes(), |
| 177 | + } |
| 178 | + } |
| 179 | + |
| 180 | + /// Returns bytes slice |
| 181 | + #[inline] |
| 182 | + pub fn bytes_slice(&self) -> &[u8] { |
| 183 | + match self { |
| 184 | + Self::LegacyAnalyzed(analyzed) => analyzed.bytecode(), |
| 185 | + _ => self.original_byte_slice(), |
| 186 | + } |
| 187 | + } |
| 188 | + |
| 189 | + /// Returns a reference to the original bytecode. |
| 190 | + #[inline] |
| 191 | + pub fn original_bytes(&self) -> Bytes { |
| 192 | + match self { |
| 193 | + Self::LegacyRaw(bytes) => bytes.0.clone(), |
| 194 | + Self::LegacyAnalyzed(analyzed) => analyzed.original_bytes(), |
| 195 | + Self::Eof(eof) => eof.raw().clone(), |
| 196 | + Self::Eip7702(eip7702) => eip7702.raw().clone(), |
| 197 | + } |
| 198 | + } |
| 199 | + |
| 200 | + /// Returns the original bytecode as a byte slice. |
| 201 | + #[inline] |
| 202 | + pub fn original_byte_slice(&self) -> &[u8] { |
| 203 | + match self { |
| 204 | + Self::LegacyRaw(bytes) => bytes, |
| 205 | + Self::LegacyAnalyzed(analyzed) => analyzed.original_byte_slice(), |
| 206 | + Self::Eof(eof) => eof.raw(), |
| 207 | + Self::Eip7702(eip7702) => eip7702.raw(), |
| 208 | + } |
| 209 | + } |
| 210 | + |
| 211 | + /// Returns the length of the original bytes. |
| 212 | + #[inline] |
| 213 | + pub fn len(&self) -> usize { |
| 214 | + self.original_byte_slice().len() |
| 215 | + } |
| 216 | + |
| 217 | + /// Returns whether the bytecode is empty. |
| 218 | + #[inline] |
| 219 | + pub fn is_empty(&self) -> bool { |
| 220 | + self.len() == 0 |
| 221 | + } |
| 222 | +} |
| 223 | + |
| 224 | +#[cfg(test)] |
| 225 | +mod tests { |
| 226 | + use super::{Bytecode, Eof}; |
| 227 | + use std::sync::Arc; |
| 228 | + |
| 229 | + #[test] |
| 230 | + fn eof_arc_clone() { |
| 231 | + let eof = Arc::new(Eof::default()); |
| 232 | + let bytecode = Bytecode::Eof(Arc::clone(&eof)); |
| 233 | + |
| 234 | + // Cloning the Bytecode should not clone the underlying Eof |
| 235 | + let cloned_bytecode = bytecode.clone(); |
| 236 | + if let Bytecode::Eof(original_arc) = bytecode { |
| 237 | + if let Bytecode::Eof(cloned_arc) = cloned_bytecode { |
| 238 | + assert!(Arc::ptr_eq(&original_arc, &cloned_arc)); |
| 239 | + } else { |
| 240 | + panic!("Cloned bytecode is not Eof"); |
| 241 | + } |
| 242 | + } else { |
| 243 | + panic!("Original bytecode is not Eof"); |
| 244 | + } |
| 245 | + } |
| 246 | +} |
0 commit comments