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
use core::cmp::min; use primitive_types::{H256, U256}; use super::Control; use crate::{Machine, ExitError, ExitSucceed, ExitFatal, ExitRevert}; #[inline] pub fn codesize(state: &mut Machine) -> Control { let size = U256::from(state.code.len()); push_u256!(state, size); Control::Continue(1) } #[inline] pub fn codecopy(state: &mut Machine) -> Control { pop_u256!(state, memory_offset, code_offset, len); try_or_fail!(state.memory.resize_offset(memory_offset, len)); match state.memory.copy_large(memory_offset, code_offset, len, &state.code) { Ok(()) => Control::Continue(1), Err(e) => Control::Exit(e.into()), } } #[inline] pub fn calldataload(state: &mut Machine) -> Control { pop_u256!(state, index); let mut load = [0u8; 32]; for i in 0..32 { if let Some(p) = index.checked_add(U256::from(i)) { if p <= U256::from(usize::max_value()) { let p = p.as_usize(); if p < state.data.len() { load[i] = state.data[p]; } } } } push!(state, H256::from(load)); Control::Continue(1) } #[inline] pub fn calldatasize(state: &mut Machine) -> Control { let len = U256::from(state.data.len()); push_u256!(state, len); Control::Continue(1) } #[inline] pub fn calldatacopy(state: &mut Machine) -> Control { pop_u256!(state, memory_offset, data_offset, len); try_or_fail!(state.memory.resize_offset(memory_offset, len)); if len == U256::zero() { return Control::Continue(1) } match state.memory.copy_large(memory_offset, data_offset, len, &state.data) { Ok(()) => Control::Continue(1), Err(e) => Control::Exit(e.into()), } } #[inline] pub fn pop(state: &mut Machine) -> Control { pop!(state, _val); Control::Continue(1) } #[inline] pub fn mload(state: &mut Machine) -> Control { pop_u256!(state, index); try_or_fail!(state.memory.resize_offset(index, U256::from(32))); let index = as_usize_or_fail!(index); let value = H256::from_slice(&state.memory.get(index, 32)[..]); push!(state, value); Control::Continue(1) } #[inline] pub fn mstore(state: &mut Machine) -> Control { pop_u256!(state, index); pop!(state, value); try_or_fail!(state.memory.resize_offset(index, U256::from(32))); let index = as_usize_or_fail!(index); match state.memory.set(index, &value[..], Some(32)) { Ok(()) => Control::Continue(1), Err(e) => Control::Exit(e.into()), } } #[inline] pub fn mstore8(state: &mut Machine) -> Control { pop_u256!(state, index, value); try_or_fail!(state.memory.resize_offset(index, U256::one())); let index = as_usize_or_fail!(index); let value = (value.low_u32() & 0xff) as u8; match state.memory.set(index, &[value], Some(1)) { Ok(()) => Control::Continue(1), Err(e) => Control::Exit(e.into()), } } #[inline] pub fn jump(state: &mut Machine) -> Control { pop_u256!(state, dest); let dest = as_usize_or_fail!(dest, ExitError::InvalidJump); if state.valids.is_valid(dest) { Control::Jump(dest) } else { Control::Exit(ExitError::InvalidJump.into()) } } #[inline] pub fn jumpi(state: &mut Machine) -> Control { pop_u256!(state, dest); pop!(state, value); let dest = as_usize_or_fail!(dest, ExitError::InvalidJump); if value != H256::zero() { if state.valids.is_valid(dest) { Control::Jump(dest) } else { Control::Exit(ExitError::InvalidJump.into()) } } else { Control::Continue(1) } } #[inline] pub fn pc(state: &mut Machine, position: usize) -> Control { push_u256!(state, U256::from(position)); Control::Continue(1) } #[inline] pub fn msize(state: &mut Machine) -> Control { push_u256!(state, U256::from(state.memory.effective_len())); Control::Continue(1) } #[inline] pub fn push(state: &mut Machine, n: usize, position: usize) -> Control { let end = min(position + 1 + n, state.code.len()); let slice = &state.code[(position + 1)..end]; let mut val = [0u8; 32]; val[(32 - slice.len())..32].copy_from_slice(slice); push!(state, H256(val)); Control::Continue(1 + n) } #[inline] pub fn dup(state: &mut Machine, n: usize) -> Control { let value = match state.stack.peek(n - 1) { Ok(value) => value, Err(e) => return Control::Exit(e.into()), }; push!(state, value); Control::Continue(1) } #[inline] pub fn swap(state: &mut Machine, n: usize) -> Control { let val1 = match state.stack.peek(0) { Ok(value) => value, Err(e) => return Control::Exit(e.into()), }; let val2 = match state.stack.peek(n) { Ok(value) => value, Err(e) => return Control::Exit(e.into()), }; match state.stack.set(0, val2) { Ok(()) => (), Err(e) => return Control::Exit(e.into()), } match state.stack.set(n, val1) { Ok(()) => (), Err(e) => return Control::Exit(e.into()), } Control::Continue(1) } #[inline] pub fn ret(state: &mut Machine) -> Control { pop_u256!(state, start, len); try_or_fail!(state.memory.resize_offset(start, len)); state.return_range = start..(start + len); Control::Exit(ExitSucceed::Returned.into()) } #[inline] pub fn revert(state: &mut Machine) -> Control { pop_u256!(state, start, len); try_or_fail!(state.memory.resize_offset(start, len)); state.return_range = start..(start + len); Control::Exit(ExitRevert::Reverted.into()) }