32 return toInt63(_size ?
u512(_offset) + _size :
u512(0));
50 uint64_t VM::decodeJumpDest(
const byte*
const _code, uint64_t& _pc)
53 uint64_t dest = _code[_pc++];
54 dest = (dest << 8) | _code[_pc++];
55 dest = (dest << 8) | _code[_pc++];
56 dest = (dest << 8) | _code[_pc++];
60 uint64_t VM::decodeJumpvDest(
const byte*
const _code, uint64_t& _pc,
u256*& _sp)
67 uint64_t i = uint64_t(*_sp--);
70 if (i >= n) i = n - 1;
73 uint64_t dest = decodeJumpDest(_code, pc);
83 void VM::onOperation()
86 (m_onOp)(++m_nSteps, m_PC, m_OP,
87 m_newMemSize > m_mem.size() ? (m_newMemSize - m_mem.size()) / 32 : uint64_t(0),
88 m_runGas, m_io_gas,
this, m_ext);
91 void VM::checkStack(
unsigned _removed,
unsigned _added)
93 int const size = 1 + m_SP - m_stack;
94 int const usedSize = size - _removed;
95 if (usedSize < 0 || usedSize + _added > 1024)
96 throwBadStack(size, _removed, _added);
99 uint64_t VM::gasForMem(
u512 _size)
102 return toInt63((
u512)m_schedule->memoryGas * s + s * s / m_schedule->quadCoeffDiv);
105 void VM::updateIOGas()
107 if (m_io_gas < m_runGas)
109 m_io_gas -= m_runGas;
114 if (m_newMemSize > m_mem.size())
115 m_runGas += toInt63(gasForMem(m_newMemSize) - gasForMem(m_mem.size()));
116 m_runGas += (m_schedule->copyGas * ((m_copyMemSize + 31) / 32));
117 if (m_io_gas < m_runGas)
123 m_newMemSize = (m_newMemSize + 31) / 32 * 32;
125 if (m_newMemSize > m_mem.size())
126 m_mem.resize(m_newMemSize);
131 unsigned n = (unsigned)m_OP - (
unsigned)Instruction::LOG0;
132 m_runGas = toInt63(m_schedule->logGas + m_schedule->logTopicGas * n +
u512(m_schedule->logDataGas) * *(m_SP - 1));
133 m_newMemSize = memNeed(*m_SP, *(m_SP - 1));
137 void VM::fetchInstruction()
141 checkStack(metric.
args, metric.
ret);
144 m_runGas = toInt63(m_schedule->tierStepGas[static_cast<unsigned>(metric.
gasPriceTier)]);
145 m_newMemSize = m_mem.size();
149 #if EVM_HACK_ON_OPERATION 150 #define onOperation() 152 #if EVM_HACK_UPDATE_IO_GAS 153 #define updateIOGas() 156 #define checkStack(r,a) 166 m_io_gas = uint64_t(_io_gas);
170 m_onFail = &VM::onOperation;
175 m_bounce = &VM::initEntry;
188 return std::move(m_output);
194 void VM::interpretCases()
205 m_bounce = &VM::caseCreate;
212 if (!m_schedule->haveDelegateCall)
213 throwBadInstruction();
218 m_bounce = &VM::caseCall;
224 m_newMemSize = memNeed(*m_SP, *(m_SP - 1));
229 size_t b = (size_t)*m_SP--;
230 size_t s = (size_t)*m_SP--;
238 m_runGas = toInt63(m_schedule->suicideGas);
242 if (m_ext->balance(m_ext->myAddress) > 0 || m_schedule->zeroValueTransferChargesNewAccountGas())
245 if (m_schedule->suicideChargesNewAccountGas() && !m_ext->exists(dest))
246 m_runGas += m_schedule->callNewAccountGas;
250 m_ext->suicide(dest);
270 m_newMemSize = toInt63(*m_SP) + 32;
275 *m_SP = (
u256)*(
h256 const*)(m_mem.data() + (unsigned)*m_SP);
281 m_newMemSize = toInt63(*m_SP) + 32;
286 *(
h256*)&m_mem[(
unsigned)*m_SP] = (
h256)*(m_SP - 1);
293 m_newMemSize = toInt63(*m_SP) + 1;
298 m_mem[(unsigned)*m_SP] = (
byte)(*(m_SP - 1) & 0xff);
305 m_runGas = toInt63(m_schedule->sha3Gas + (
u512(*(m_SP - 1)) + 31) / 32 * m_schedule->sha3WordGas);
306 m_newMemSize = memNeed(*m_SP, *(m_SP - 1));
311 uint64_t inOff = (uint64_t)*m_SP--;
312 uint64_t inSize = (uint64_t)*m_SP--;
323 m_ext->log({},
bytesConstRef(m_mem.data() + (uint64_t)*m_SP, (uint64_t)*(m_SP - 1)));
334 m_ext->log({*(m_SP - 2)},
bytesConstRef(m_mem.data() + (uint64_t)*m_SP, (uint64_t)*(m_SP - 1)));
345 m_ext->log({*(m_SP - 2), *(m_SP-3)},
bytesConstRef(m_mem.data() + (uint64_t)*m_SP, (uint64_t)*(m_SP - 1)));
356 m_ext->log({*(m_SP - 2), *(m_SP-3), *(m_SP-4)},
bytesConstRef(m_mem.data() + (uint64_t)*m_SP, (uint64_t)*(m_SP - 1)));
367 m_ext->log({*(m_SP - 2), *(m_SP-3), *(m_SP-4), *(m_SP-5)},
bytesConstRef(m_mem.data() + (uint64_t)*m_SP, (uint64_t)*(m_SP - 1)));
374 u256 expon = *(m_SP - 1);
375 m_runGas = toInt63(m_schedule->expGas + m_schedule->expByteGas * (32 - (
h256(expon).firstBitSet() / 8)));
380 *m_SP = exp256(base, expon);
394 *(m_SP - 1) += *m_SP;
406 *(uint64_t*)(m_SP - 1) *= *(uint64_t*)m_SP;
408 *(m_SP - 1) *= *m_SP;
419 *(m_SP - 1) = *m_SP - *(m_SP - 1);
429 *(m_SP - 1) = *(m_SP - 1) ?
divWorkaround(*m_SP, *(m_SP - 1)) : 0;
449 *(m_SP - 1) = *(m_SP - 1) ?
modWorkaround(*m_SP, *(m_SP - 1)) : 0;
478 *(m_SP - 1) = *m_SP < *(m_SP - 1) ? 1 : 0;
488 *(m_SP - 1) = *m_SP > *(m_SP - 1) ? 1 : 0;
498 *(m_SP - 1) =
u2s(*m_SP) <
u2s(*(m_SP - 1)) ? 1 : 0;
508 *(m_SP - 1) =
u2s(*m_SP) >
u2s(*(m_SP - 1)) ? 1 : 0;
518 *(m_SP - 1) = *m_SP == *(m_SP - 1) ? 1 : 0;
528 *m_SP = *m_SP ? 0 : 1;
537 *(m_SP - 1) = *m_SP & *(m_SP - 1);
547 *(m_SP - 1) = *m_SP | *(m_SP - 1);
557 *(m_SP - 1) = *m_SP ^ *(m_SP - 1);
567 *(m_SP - 1) = *m_SP < 32 ? (*(m_SP - 1) >> (unsigned)(8 * (31 - *m_SP))) & 0xff : 0;
577 *(m_SP - 2) = *(m_SP - 2) ?
u256((
u512(*m_SP) +
u512(*(m_SP - 1))) % *(m_SP - 2)) : 0;
587 *(m_SP - 2) = *(m_SP - 2) ?
u256((
u512(*m_SP) *
u512(*(m_SP - 1))) % *(m_SP - 2)) : 0;
599 unsigned testBit =
static_cast<unsigned>(*m_SP) * 8 + 7;
600 u256& number = *(m_SP - 1);
601 u256 mask = ((
u256(1) << testBit) - 1);
602 if (boost::multiprecision::bit_test(number, testBit))
631 m_runGas = toInt63(m_schedule->balanceGas);
635 *m_SP = m_ext->balance(
asAddress(*m_SP));
654 *++m_SP = m_ext->value;
664 if (
u512(*m_SP) + 31 < m_ext->data.size())
665 *m_SP = (
u256)*(
h256 const*)(m_ext->data.data() + (size_t)*m_SP);
666 else if (*m_SP >= m_ext->data.size())
671 for (uint64_t i = (uint64_t)*m_SP,
e = (uint64_t)*m_SP + (uint64_t)32, j = 0; i <
e; ++i, ++j)
672 r[j] = i < m_ext->
data.size() ? m_ext->data[i] : 0;
684 *++m_SP = m_ext->data.size();
693 *++m_SP = m_ext->code.size();
699 m_runGas = toInt63(m_schedule->extcodesizeGas);
703 *m_SP = m_ext->codeSizeAt(
asAddress(*m_SP));
709 m_copyMemSize = toInt63(*(m_SP - 2));
710 m_newMemSize = memNeed(*m_SP, *(m_SP - 2));
715 copyDataToMemory(m_ext->data, m_SP);
721 m_copyMemSize = toInt63(*(m_SP - 2));
722 m_newMemSize = memNeed(*m_SP, *(m_SP - 2));
727 copyDataToMemory(&m_ext->code, m_SP);
733 m_runGas = toInt63(m_schedule->extcodecopyGas);
734 m_copyMemSize = toInt63(*(m_SP - 3));
735 m_newMemSize = memNeed(*(m_SP - 1), *(m_SP - 3));
742 copyDataToMemory(&m_ext->codeAt(a), m_SP);
752 *++m_SP = m_ext->gasPrice;
761 *m_SP = (
u256)m_ext->blockHash(*m_SP);
770 *++m_SP = (
u160)m_ext->envInfo().author();
779 *++m_SP = m_ext->envInfo().timestamp();
788 *++m_SP = m_ext->envInfo().number();
797 *++m_SP = m_ext->envInfo().difficulty();
806 *++m_SP = m_ext->envInfo().gasLimit();
821 #ifdef EVM_USE_CONSTANT_POOL 826 *++m_SP = m_pool[m_code[m_PC]];
828 m_PC += m_code[m_PC];
830 throwBadInstruction();
839 *++m_SP = m_code[++m_PC];
879 int numBytes = (int)m_OP - (
int)Instruction::PUSH1 + 1;
884 for (++m_PC; numBytes--; ++m_PC)
885 *m_SP = (*m_SP << 8) | m_code[m_PC];
894 m_PC = verifyJumpDest(*m_SP);
904 m_PC = verifyJumpDest(*m_SP);
911 #if EVM_JUMPS_AND_SUBS 916 m_PC = decodeJumpDest(m_code, m_PC);
925 m_PC = decodeJumpDest(m_code, m_PC);
936 m_PC = decodeJumpvDest(m_code, m_PC, m_SP);
946 m_PC = decodeJumpDest(m_code, m_PC);
957 m_PC = decodeJumpDest(m_code, m_PC);
978 throwBadInstruction();
985 #ifdef EVM_REPLACE_CONST_JUMP 989 m_PC = uint64_t(*m_SP);
992 throwBadInstruction();
999 #ifdef EVM_REPLACE_CONST_JUMP 1004 m_PC = uint64_t(*m_SP);
1009 throwBadInstruction();
1034 unsigned n = 1 + (unsigned)m_OP - (
unsigned)Instruction::DUP1;
1036 *(uint64_t*)(m_SP+1) = *(uint64_t*)&m_stack[(1 + m_SP - m_stack) - n];
1038 *(m_SP+1) = m_stack[(1 + m_SP - m_stack) - n];
1065 unsigned n = (unsigned)m_OP - (
unsigned)Instruction::SWAP1 + 2;
1067 *m_SP = m_stack[(1 + m_SP - m_stack) - n];
1068 m_stack[(1 + m_SP - m_stack) - n] = d;
1075 m_runGas = toInt63(m_schedule->sloadGas);
1079 *m_SP = m_ext->store(*m_SP);
1085 if (!m_ext->store(*m_SP) && *(m_SP - 1))
1086 m_runGas = toInt63(m_schedule->sstoreSetGas);
1087 else if (m_ext->store(*m_SP) && !*(m_SP - 1))
1089 m_runGas = toInt63(m_schedule->sstoreResetGas);
1090 m_ext->sub.refunds += m_schedule->sstoreRefundGas;
1093 m_runGas = toInt63(m_schedule->sstoreResetGas);
1097 m_ext->setStore(*m_SP, *(m_SP - 1));
1116 *++m_SP = m_mem.size();
1137 #if EVM_JUMPS_AND_SUBS 1151 throwBadInstruction();
set a potential jumpsub destination
Adapted from code found on http://stackoverflow.com/questions/180947/base64-decode-snippet-in-c Origi...
return to subroutine jumped from
SHA3 message digest base class.
alter the program counter to a beginsub
conditionally alter the program counter
S divWorkaround(S const &_a, S const &_b)
std::hash for asio::adress
Instruction
Virtual machine bytecode instruction.
u256 fromAddress(Address _a)
boost::multiprecision::number< boost::multiprecision::cpp_int_backend< 160, 160, boost::multiprecision::unsigned_magnitude, boost::multiprecision::unchecked, void >> u160
#define LT(i, a, b, c, d, e)
conditionally alter the program counter - pre-verified
alter the program counter - pre-verified
vector_ref< byte const > bytesConstRef
Fixed-size raw-byte array container type, with an API optimised for storing hashes.
boost::multiprecision::number< boost::multiprecision::cpp_int_backend< 512, 512, boost::multiprecision::unsigned_magnitude, boost::multiprecision::unchecked, void >> u512
std::function< void(uint64_t, uint64_t, Instruction, bigint, bigint, bigint, VM *, ExtVMFace const *)> OnOpFunc
boost::multiprecision::number< boost::multiprecision::cpp_int_backend< 256, 256, boost::multiprecision::unsigned_magnitude, boost::multiprecision::unchecked, void >> u256
alter the program counter to a beginsub
Interface and null implementation of the class for specifying VM externalities.
s256 u2s(u256 _u)
Interprets _u as a two's complement signed number and returns the resulting s256. ...
S modWorkaround(S const &_a, S const &_b)
Reference to a slice of buffer that also owns the buffer.
uint8_t const size_t const size
virtual EVMSchedule const & evmSchedule() const
Return the EVM gas-price schedule for this execution context.
Address asAddress(u256 _item)
bool sha3(bytesConstRef _input, bytesRef o_output)
Calculate SHA3-256 hash of the given input and load it into the given output.
alter the program counter to a jumpdest
placed to force invalid instruction exception
alter the program counter to a jumpdest
push value from constant pool
boost::multiprecision::number< boost::multiprecision::cpp_int_backend< 512, 512, boost::multiprecision::signed_magnitude, boost::multiprecision::unchecked, void >> s512