revm_bytecode/legacy/
raw.rs

1use super::{JumpTable, LegacyAnalyzedBytecode};
2use crate::opcode;
3use bitvec::{bitvec, order::Lsb0, vec::BitVec};
4use core::ops::Deref;
5use primitives::Bytes;
6use std::{sync::Arc, vec::Vec};
7
8#[derive(Clone, Debug, PartialEq, Eq, Hash, Ord, PartialOrd)]
9#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
10pub struct LegacyRawBytecode(pub Bytes);
11
12impl LegacyRawBytecode {
13    pub fn analysis(&self) -> JumpTable {
14        analyze_legacy(&self.0)
15    }
16
17    pub fn into_analyzed(self) -> LegacyAnalyzedBytecode {
18        let jump_table = self.analysis();
19        let len = self.0.len();
20        let mut padded_bytecode = Vec::with_capacity(len + 33);
21        padded_bytecode.extend_from_slice(&self.0);
22        padded_bytecode.resize(len + 33, 0);
23        LegacyAnalyzedBytecode::new(padded_bytecode.into(), len, jump_table)
24    }
25}
26
27impl From<Bytes> for LegacyRawBytecode {
28    fn from(bytes: Bytes) -> Self {
29        Self(bytes)
30    }
31}
32
33impl<const N: usize> From<[u8; N]> for LegacyRawBytecode {
34    fn from(bytes: [u8; N]) -> Self {
35        Self(bytes.into())
36    }
37}
38
39impl Deref for LegacyRawBytecode {
40    type Target = Bytes;
41
42    fn deref(&self) -> &Self::Target {
43        &self.0
44    }
45}
46
47/// Analyze the bytecode to find the jumpdests
48pub fn analyze_legacy(bytetecode: &[u8]) -> JumpTable {
49    let mut jumps: BitVec<u8> = bitvec![u8, Lsb0; 0; bytetecode.len()];
50
51    let range = bytetecode.as_ptr_range();
52    let start = range.start;
53    let mut iterator = start;
54    let end = range.end;
55    while iterator < end {
56        let opcode = unsafe { *iterator };
57        if opcode::JUMPDEST == opcode {
58            // SAFETY: Jumps are max length of the code
59            unsafe { jumps.set_unchecked(iterator.offset_from(start) as usize, true) }
60            iterator = unsafe { iterator.offset(1) };
61        } else {
62            let push_offset = opcode.wrapping_sub(opcode::PUSH1);
63            if push_offset < 32 {
64                // SAFETY: Iterator access range is checked in the while loop
65                iterator = unsafe { iterator.offset((push_offset + 2) as isize) };
66            } else {
67                // SAFETY: Iterator access range is checked in the while loop
68                iterator = unsafe { iterator.offset(1) };
69            }
70        }
71    }
72
73    JumpTable(Arc::new(jumps))
74}