revm_interpreter/interpreter/stack.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 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447
use crate::{
primitives::{B256, U256},
InstructionResult,
};
use core::{fmt, ptr};
use std::vec::Vec;
/// EVM interpreter stack limit.
pub const STACK_LIMIT: usize = 1024;
/// EVM stack with [STACK_LIMIT] capacity of words.
#[derive(Debug, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct Stack {
/// The underlying data of the stack.
data: Vec<U256>,
}
impl fmt::Display for Stack {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("[")?;
for (i, x) in self.data.iter().enumerate() {
if i > 0 {
f.write_str(", ")?;
}
write!(f, "{x}")?;
}
f.write_str("]")
}
}
impl Default for Stack {
#[inline]
fn default() -> Self {
Self::new()
}
}
impl Stack {
/// Instantiate a new stack with the [default stack limit][STACK_LIMIT].
#[inline]
pub fn new() -> Self {
Self {
// SAFETY: expansion functions assume that capacity is `STACK_LIMIT`.
data: Vec::with_capacity(STACK_LIMIT),
}
}
/// Returns the length of the stack in words.
#[inline]
pub fn len(&self) -> usize {
self.data.len()
}
/// Returns whether the stack is empty.
#[inline]
pub fn is_empty(&self) -> bool {
self.data.is_empty()
}
/// Returns a reference to the underlying data buffer.
#[inline]
pub fn data(&self) -> &Vec<U256> {
&self.data
}
/// Returns a mutable reference to the underlying data buffer.
#[inline]
pub fn data_mut(&mut self) -> &mut Vec<U256> {
&mut self.data
}
/// Consumes the stack and returns the underlying data buffer.
#[inline]
pub fn into_data(self) -> Vec<U256> {
self.data
}
/// Removes the topmost element from the stack and returns it, or `StackUnderflow` if it is
/// empty.
#[inline]
pub fn pop(&mut self) -> Result<U256, InstructionResult> {
self.data.pop().ok_or(InstructionResult::StackUnderflow)
}
/// Removes the topmost element from the stack and returns it.
///
/// # Safety
///
/// The caller is responsible for checking the length of the stack.
#[inline]
pub unsafe fn pop_unsafe(&mut self) -> U256 {
self.data.pop().unwrap_unchecked()
}
/// Peeks the top of the stack.
///
/// # Safety
///
/// The caller is responsible for checking the length of the stack.
#[inline]
pub unsafe fn top_unsafe(&mut self) -> &mut U256 {
let len = self.data.len();
self.data.get_unchecked_mut(len - 1)
}
/// Pop the topmost value, returning the value and the new topmost value.
///
/// # Safety
///
/// The caller is responsible for checking the length of the stack.
#[inline]
pub unsafe fn pop_top_unsafe(&mut self) -> (U256, &mut U256) {
let pop = self.pop_unsafe();
let top = self.top_unsafe();
(pop, top)
}
/// Pops 2 values from the stack.
///
/// # Safety
///
/// The caller is responsible for checking the length of the stack.
#[inline]
pub unsafe fn pop2_unsafe(&mut self) -> (U256, U256) {
let pop1 = self.pop_unsafe();
let pop2 = self.pop_unsafe();
(pop1, pop2)
}
/// Pops 2 values from the stack and returns them, in addition to the new topmost value.
///
/// # Safety
///
/// The caller is responsible for checking the length of the stack.
#[inline]
pub unsafe fn pop2_top_unsafe(&mut self) -> (U256, U256, &mut U256) {
let pop1 = self.pop_unsafe();
let pop2 = self.pop_unsafe();
let top = self.top_unsafe();
(pop1, pop2, top)
}
/// Pops 3 values from the stack.
///
/// # Safety
///
/// The caller is responsible for checking the length of the stack.
#[inline]
pub unsafe fn pop3_unsafe(&mut self) -> (U256, U256, U256) {
let pop1 = self.pop_unsafe();
let pop2 = self.pop_unsafe();
let pop3 = self.pop_unsafe();
(pop1, pop2, pop3)
}
/// Pops 4 values from the stack.
///
/// # Safety
///
/// The caller is responsible for checking the length of the stack.
#[inline]
pub unsafe fn pop4_unsafe(&mut self) -> (U256, U256, U256, U256) {
let pop1 = self.pop_unsafe();
let pop2 = self.pop_unsafe();
let pop3 = self.pop_unsafe();
let pop4 = self.pop_unsafe();
(pop1, pop2, pop3, pop4)
}
/// Pops 5 values from the stack.
///
/// # Safety
///
/// The caller is responsible for checking the length of the stack.
#[inline]
pub unsafe fn pop5_unsafe(&mut self) -> (U256, U256, U256, U256, U256) {
let pop1 = self.pop_unsafe();
let pop2 = self.pop_unsafe();
let pop3 = self.pop_unsafe();
let pop4 = self.pop_unsafe();
let pop5 = self.pop_unsafe();
(pop1, pop2, pop3, pop4, pop5)
}
/// Push a new value into the stack. If it will exceed the stack limit,
/// returns `StackOverflow` error and leaves the stack unchanged.
#[inline]
pub fn push_b256(&mut self, value: B256) -> Result<(), InstructionResult> {
self.push(value.into())
}
/// Push a new value onto the stack.
///
/// If it will exceed the stack limit, returns `StackOverflow` error and leaves the stack
/// unchanged.
#[inline]
pub fn push(&mut self, value: U256) -> Result<(), InstructionResult> {
// Allows the compiler to optimize out the `Vec::push` capacity check.
assume!(self.data.capacity() == STACK_LIMIT);
if self.data.len() == STACK_LIMIT {
return Err(InstructionResult::StackOverflow);
}
self.data.push(value);
Ok(())
}
/// Peek a value at given index for the stack, where the top of
/// the stack is at index `0`. If the index is too large,
/// `StackError::Underflow` is returned.
#[inline]
pub fn peek(&self, no_from_top: usize) -> Result<U256, InstructionResult> {
if self.data.len() > no_from_top {
Ok(self.data[self.data.len() - no_from_top - 1])
} else {
Err(InstructionResult::StackUnderflow)
}
}
/// Duplicates the `N`th value from the top of the stack.
///
/// # Panics
///
/// Panics if `n` is 0.
#[inline]
#[cfg_attr(debug_assertions, track_caller)]
pub fn dup(&mut self, n: usize) -> Result<(), InstructionResult> {
assume!(n > 0, "attempted to dup 0");
let len = self.data.len();
if len < n {
Err(InstructionResult::StackUnderflow)
} else if len + 1 > STACK_LIMIT {
Err(InstructionResult::StackOverflow)
} else {
// SAFETY: check for out of bounds is done above and it makes this safe to do.
unsafe {
let ptr = self.data.as_mut_ptr().add(len);
ptr::copy_nonoverlapping(ptr.sub(n), ptr, 1);
self.data.set_len(len + 1);
}
Ok(())
}
}
/// Swaps the topmost value with the `N`th value from the top.
///
/// # Panics
///
/// Panics if `n` is 0.
#[inline(always)]
#[cfg_attr(debug_assertions, track_caller)]
pub fn swap(&mut self, n: usize) -> Result<(), InstructionResult> {
self.exchange(0, n)
}
/// Exchange two values on the stack.
///
/// `n` is the first index, and the second index is calculated as `n + m`.
///
/// # Panics
///
/// Panics if `m` is zero.
#[inline]
#[cfg_attr(debug_assertions, track_caller)]
pub fn exchange(&mut self, n: usize, m: usize) -> Result<(), InstructionResult> {
assume!(m > 0, "overlapping exchange");
let len = self.data.len();
let n_m_index = n + m;
if n_m_index >= len {
return Err(InstructionResult::StackUnderflow);
}
// SAFETY: `n` and `n_m` are checked to be within bounds, and they don't overlap.
unsafe {
// NOTE: `ptr::swap_nonoverlapping` is more efficient than `slice::swap` or `ptr::swap`
// because it operates under the assumption that the pointers do not overlap,
// eliminating an intemediate copy,
// which is a condition we know to be true in this context.
let top = self.data.as_mut_ptr().add(len - 1);
core::ptr::swap_nonoverlapping(top.sub(n), top.sub(n_m_index), 1);
}
Ok(())
}
/// Pushes an arbitrary length slice of bytes onto the stack, padding the last word with zeros
/// if necessary.
#[inline]
pub fn push_slice(&mut self, slice: &[u8]) -> Result<(), InstructionResult> {
if slice.is_empty() {
return Ok(());
}
let n_words = (slice.len() + 31) / 32;
let new_len = self.data.len() + n_words;
if new_len > STACK_LIMIT {
return Err(InstructionResult::StackOverflow);
}
// SAFETY: length checked above.
unsafe {
let dst = self.data.as_mut_ptr().add(self.data.len()).cast::<u64>();
self.data.set_len(new_len);
let mut i = 0;
// write full words
let words = slice.chunks_exact(32);
let partial_last_word = words.remainder();
for word in words {
// Note: we unroll `U256::from_be_bytes` here to write directly into the buffer,
// instead of creating a 32 byte array on the stack and then copying it over.
for l in word.rchunks_exact(8) {
dst.add(i).write(u64::from_be_bytes(l.try_into().unwrap()));
i += 1;
}
}
if partial_last_word.is_empty() {
return Ok(());
}
// write limbs of partial last word
let limbs = partial_last_word.rchunks_exact(8);
let partial_last_limb = limbs.remainder();
for l in limbs {
dst.add(i).write(u64::from_be_bytes(l.try_into().unwrap()));
i += 1;
}
// write partial last limb by padding with zeros
if !partial_last_limb.is_empty() {
let mut tmp = [0u8; 8];
tmp[8 - partial_last_limb.len()..].copy_from_slice(partial_last_limb);
dst.add(i).write(u64::from_be_bytes(tmp));
i += 1;
}
debug_assert_eq!((i + 3) / 4, n_words, "wrote too much");
// zero out upper bytes of last word
let m = i % 4; // 32 / 8
if m != 0 {
dst.add(i).write_bytes(0, 4 - m);
}
}
Ok(())
}
/// Set a value at given index for the stack, where the top of the
/// stack is at index `0`. If the index is too large,
/// `StackError::Underflow` is returned.
#[inline]
pub fn set(&mut self, no_from_top: usize, val: U256) -> Result<(), InstructionResult> {
if self.data.len() > no_from_top {
let len = self.data.len();
self.data[len - no_from_top - 1] = val;
Ok(())
} else {
Err(InstructionResult::StackUnderflow)
}
}
}
#[cfg(feature = "serde")]
impl<'de> serde::Deserialize<'de> for Stack {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
let mut data = Vec::<U256>::deserialize(deserializer)?;
if data.len() > STACK_LIMIT {
return Err(serde::de::Error::custom(std::format!(
"stack size exceeds limit: {} > {}",
data.len(),
STACK_LIMIT
)));
}
data.reserve(STACK_LIMIT - data.len());
Ok(Self { data })
}
}
#[cfg(test)]
mod tests {
use super::*;
fn run(f: impl FnOnce(&mut Stack)) {
let mut stack = Stack::new();
// fill capacity with non-zero values
unsafe {
stack.data.set_len(STACK_LIMIT);
stack.data.fill(U256::MAX);
stack.data.set_len(0);
}
f(&mut stack);
}
#[test]
fn push_slices() {
// no-op
run(|stack| {
stack.push_slice(b"").unwrap();
assert_eq!(stack.data, []);
});
// one word
run(|stack| {
stack.push_slice(&[42]).unwrap();
assert_eq!(stack.data, [U256::from(42)]);
});
let n = 0x1111_2222_3333_4444_5555_6666_7777_8888_u128;
run(|stack| {
stack.push_slice(&n.to_be_bytes()).unwrap();
assert_eq!(stack.data, [U256::from(n)]);
});
// more than one word
run(|stack| {
let b = [U256::from(n).to_be_bytes::<32>(); 2].concat();
stack.push_slice(&b).unwrap();
assert_eq!(stack.data, [U256::from(n); 2]);
});
run(|stack| {
let b = [&[0; 32][..], &[42u8]].concat();
stack.push_slice(&b).unwrap();
assert_eq!(stack.data, [U256::ZERO, U256::from(42)]);
});
run(|stack| {
let b = [&[0; 32][..], &n.to_be_bytes()].concat();
stack.push_slice(&b).unwrap();
assert_eq!(stack.data, [U256::ZERO, U256::from(n)]);
});
run(|stack| {
let b = [&[0; 64][..], &n.to_be_bytes()].concat();
stack.push_slice(&b).unwrap();
assert_eq!(stack.data, [U256::ZERO, U256::ZERO, U256::from(n)]);
});
}
}