forked from bluealloy/revm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbytecode.rs
174 lines (155 loc) · 4.79 KB
/
bytecode.rs
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
use crate::{keccak256, B256, KECCAK_EMPTY};
use alloc::{sync::Arc, vec::Vec};
use bitvec::prelude::{bitvec, Lsb0};
use bitvec::vec::BitVec;
use bytes::Bytes;
use core::fmt::Debug;
/// A map of valid `jump` destinations.
#[derive(Clone, Default, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct JumpMap(pub Arc<BitVec<u8>>);
impl Debug for JumpMap {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.debug_struct("JumpMap")
.field("map", &hex::encode(self.0.as_raw_slice()))
.finish()
}
}
impl JumpMap {
/// Get the raw bytes of the jump map
#[inline]
pub fn as_slice(&self) -> &[u8] {
self.0.as_raw_slice()
}
/// Construct a jump map from raw bytes
#[inline]
pub fn from_slice(slice: &[u8]) -> Self {
Self(Arc::new(BitVec::from_slice(slice)))
}
/// Check if `pc` is a valid jump destination.
#[inline]
pub fn is_valid(&self, pc: usize) -> bool {
pc < self.0.len() && self.0[pc]
}
}
/// State of the [`Bytecode`] analysis.
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum BytecodeState {
/// No analysis has been performed.
Raw,
/// The bytecode has been checked for validity.
Checked { len: usize },
/// The bytecode has been analyzed for valid jump destinations.
Analysed { len: usize, jump_map: JumpMap },
}
#[derive(Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Bytecode {
#[cfg_attr(feature = "serde", serde(with = "crate::utilities::serde_hex_bytes"))]
pub bytecode: Bytes,
pub state: BytecodeState,
}
impl Debug for Bytecode {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.debug_struct("Bytecode")
.field("bytecode", &hex::encode(&self.bytecode[..]))
.field("state", &self.state)
.finish()
}
}
impl Default for Bytecode {
#[inline]
fn default() -> Self {
Bytecode::new()
}
}
impl Bytecode {
/// Creates a new [`Bytecode`] with exactly one STOP opcode.
#[inline]
pub fn new() -> Self {
Bytecode {
bytecode: Bytes::from_static(&[0]),
state: BytecodeState::Analysed {
len: 0,
jump_map: JumpMap(Arc::new(bitvec![u8, Lsb0; 0])),
},
}
}
/// Calculate hash of the bytecode.
pub fn hash_slow(&self) -> B256 {
if self.is_empty() {
KECCAK_EMPTY
} else {
keccak256(&self.original_bytes())
}
}
/// Creates a new raw [`Bytecode`].
#[inline]
pub fn new_raw(bytecode: Bytes) -> Self {
Self {
bytecode,
state: BytecodeState::Raw,
}
}
/// Create new checked bytecode
///
/// # Safety
///
/// Bytecode need to end with STOP (0x00) opcode as checked bytecode assumes
/// that it is safe to iterate over bytecode without checking lengths
pub unsafe fn new_checked(bytecode: Bytes, len: usize) -> Self {
Self {
bytecode,
state: BytecodeState::Checked { len },
}
}
/// Returns a reference to the bytecode.
#[inline]
pub fn bytes(&self) -> &Bytes {
&self.bytecode
}
/// Returns a reference to the original bytecode.
#[inline]
pub fn original_bytes(&self) -> Bytes {
match self.state {
BytecodeState::Raw => self.bytecode.clone(),
BytecodeState::Checked { len } | BytecodeState::Analysed { len, .. } => {
self.bytecode.slice(0..len)
}
}
}
/// Returns the length of the bytecode.
#[inline]
pub fn len(&self) -> usize {
match self.state {
BytecodeState::Raw => self.bytecode.len(),
BytecodeState::Checked { len, .. } | BytecodeState::Analysed { len, .. } => len,
}
}
/// Returns whether the bytecode is empty.
#[inline]
pub fn is_empty(&self) -> bool {
self.len() == 0
}
/// Returns the [`BytecodeState`].
#[inline]
pub fn state(&self) -> &BytecodeState {
&self.state
}
pub fn to_checked(self) -> Self {
match self.state {
BytecodeState::Raw => {
let len = self.bytecode.len();
let mut padded_bytecode = Vec::with_capacity(len + 33);
padded_bytecode.extend_from_slice(&self.bytecode);
padded_bytecode.resize(len + 33, 0);
Self {
bytecode: padded_bytecode.into(),
state: BytecodeState::Checked { len },
}
}
_ => self,
}
}
}